forked from allinurl/goaccess
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsort.c
387 lines (334 loc) · 9.98 KB
/
sort.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
/**
* sort.c -- functions related to sort functionality
* Copyright (C) 2009-2014 by Gerardo Orellana <[email protected]>
* GoAccess - An Ncurses apache weblog analyzer & interactive viewer
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of
* the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* A copy of the GNU General Public License is attached to this
* source distribution for its full text.
*
* Visit http://goaccess.prosoftcorp.com for new releases.
*/
#if HAVE_CONFIG_H
#include <config.h>
#endif
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <getopt.h>
#include <errno.h>
#include "settings.h"
#include "util.h"
#include "sort.h"
/* *INDENT-OFF* */
const int sort_choices[][SORT_MAX_OPTS] = {
{SORT_BY_HITS, SORT_BY_VISITORS, SORT_BY_DATA, SORT_BY_BW, SORT_BY_USEC, -1},
{SORT_BY_HITS, SORT_BY_VISITORS, SORT_BY_DATA, SORT_BY_BW, SORT_BY_USEC, SORT_BY_PROT, SORT_BY_MTHD, -1},
{SORT_BY_HITS, SORT_BY_VISITORS, SORT_BY_DATA, SORT_BY_BW, SORT_BY_USEC, SORT_BY_PROT, SORT_BY_MTHD, -1},
{SORT_BY_HITS, SORT_BY_VISITORS, SORT_BY_DATA, SORT_BY_BW, SORT_BY_USEC, SORT_BY_PROT, SORT_BY_MTHD, -1},
{SORT_BY_HITS, SORT_BY_VISITORS, SORT_BY_DATA, SORT_BY_BW, SORT_BY_USEC, -1},
{SORT_BY_HITS, SORT_BY_VISITORS, SORT_BY_DATA, SORT_BY_BW, SORT_BY_USEC, -1},
{SORT_BY_HITS, SORT_BY_VISITORS, SORT_BY_DATA, SORT_BY_BW, SORT_BY_USEC, -1},
{SORT_BY_HITS, SORT_BY_VISITORS, SORT_BY_DATA, SORT_BY_BW, SORT_BY_USEC, -1},
{SORT_BY_HITS, SORT_BY_VISITORS, SORT_BY_DATA, SORT_BY_BW, SORT_BY_USEC, -1},
{SORT_BY_HITS, SORT_BY_VISITORS, SORT_BY_DATA, SORT_BY_BW, SORT_BY_USEC, -1},
{SORT_BY_HITS, SORT_BY_VISITORS, SORT_BY_DATA, SORT_BY_BW, SORT_BY_USEC, -1},
#ifdef HAVE_LIBGEOIP
{SORT_BY_HITS, SORT_BY_VISITORS, SORT_BY_DATA, SORT_BY_BW, SORT_BY_USEC, -1},
#endif
{SORT_BY_HITS, SORT_BY_VISITORS, SORT_BY_DATA, SORT_BY_BW, SORT_BY_USEC, -1},
};
static GEnum FIELD[] = {
{"BY_HITS" , SORT_BY_HITS } ,
{"BY_VISITORS" , SORT_BY_VISITORS } ,
{"BY_DATA" , SORT_BY_DATA } ,
{"BY_BW" , SORT_BY_BW } ,
{"BY_USEC" , SORT_BY_USEC } ,
{"BY_PROT" , SORT_BY_PROT } ,
{"BY_MTHD" , SORT_BY_MTHD } ,
};
static GEnum ORDER[] = {
{"ASC" , SORT_ASC } ,
{"DESC" , SORT_DESC } ,
};
GSort module_sort[TOTAL_MODULES] = {
{VISITORS , SORT_BY_DATA , SORT_DESC } ,
{REQUESTS , SORT_BY_HITS , SORT_DESC } ,
{REQUESTS_STATIC , SORT_BY_HITS , SORT_DESC } ,
{NOT_FOUND , SORT_BY_HITS , SORT_DESC } ,
{HOSTS , SORT_BY_HITS , SORT_DESC } ,
{OS , SORT_BY_HITS , SORT_DESC } ,
{BROWSERS , SORT_BY_HITS , SORT_DESC } ,
{VISIT_TIMES , SORT_BY_DATA , SORT_ASC } ,
{REFERRERS , SORT_BY_HITS , SORT_DESC } ,
{REFERRING_SITES , SORT_BY_HITS , SORT_DESC } ,
{KEYPHRASES , SORT_BY_HITS , SORT_DESC } ,
#ifdef HAVE_LIBGEOIP
{GEO_LOCATION , SORT_BY_HITS , SORT_DESC } ,
#endif
{STATUS_CODES , SORT_BY_HITS , SORT_DESC } ,
};
/* *INDENT-ON* */
/* sort data ascending */
static int
cmp_data_asc (const void *a, const void *b)
{
const GHolderItem *ia = a;
const GHolderItem *ib = b;
return strcmp (ia->metrics->data, ib->metrics->data);
}
/* sort data descending */
static int
cmp_data_desc (const void *a, const void *b)
{
const GHolderItem *ia = a;
const GHolderItem *ib = b;
return strcmp (ib->metrics->data, ia->metrics->data);
}
/* sort numeric descending */
static int
cmp_num_desc (const void *a, const void *b)
{
const GHolderItem *ia = a;
const GHolderItem *ib = b;
int va = ia->metrics->hits;
int vb = ib->metrics->hits;
return (va < vb) - (va > vb);
}
/* sort numeric ascending */
static int
cmp_num_asc (const void *a, const void *b)
{
const GHolderItem *ia = a;
const GHolderItem *ib = b;
int va = ia->metrics->hits;
int vb = ib->metrics->hits;
return (va > vb) - (va < vb);
}
/* sort numeric descending */
static int
cmp_vis_desc (const void *a, const void *b)
{
const GHolderItem *ia = a;
const GHolderItem *ib = b;
int va = ia->metrics->visitors;
int vb = ib->metrics->visitors;
return (va < vb) - (va > vb);
}
/* sort numeric ascending */
static int
cmp_vis_asc (const void *a, const void *b)
{
const GHolderItem *ia = a;
const GHolderItem *ib = b;
int va = ia->metrics->visitors;
int vb = ib->metrics->visitors;
return (va > vb) - (va < vb);
}
/* sort raw numeric descending */
static int
cmp_raw_num_desc (const void *a, const void *b)
{
const GRawDataItem *ia = a;
const GRawDataItem *ib = b;
GDataMap *amap = ia->value;
GDataMap *bmap = ib->value;
int va = amap->data;
int vb = bmap->data;
return (va < vb) - (va > vb);
}
/* sort bandwidth descending */
static int
cmp_bw_desc (const void *a, const void *b)
{
const GHolderItem *ia = a;
const GHolderItem *ib = b;
uint64_t va = ia->metrics->bw.nbw;
uint64_t vb = ib->metrics->bw.nbw;
return (va < vb) - (va > vb);
}
/* sort bandwidth ascending */
static int
cmp_bw_asc (const void *a, const void *b)
{
const GHolderItem *ia = a;
const GHolderItem *ib = b;
uint64_t va = ia->metrics->bw.nbw;
uint64_t vb = ib->metrics->bw.nbw;
return (va > vb) - (va < vb);
}
/* sort usec descending */
static int
cmp_usec_desc (const void *a, const void *b)
{
const GHolderItem *ia = a;
const GHolderItem *ib = b;
uint64_t va = ia->metrics->avgts.nts;
uint64_t vb = ib->metrics->avgts.nts;
return (va < vb) - (va > vb);
}
/* sort usec ascending */
static int
cmp_usec_asc (const void *a, const void *b)
{
const GHolderItem *ia = a;
const GHolderItem *ib = b;
uint64_t va = ia->metrics->avgts.nts;
uint64_t vb = ib->metrics->avgts.nts;
return (va > vb) - (va < vb);
}
/* sort protocol ascending */
static int
cmp_proto_asc (const void *a, const void *b)
{
const GHolderItem *ia = a;
const GHolderItem *ib = b;
return strcmp (ia->metrics->protocol, ib->metrics->protocol);
}
/* sort protocol descending */
static int
cmp_proto_desc (const void *a, const void *b)
{
const GHolderItem *ia = a;
const GHolderItem *ib = b;
return strcmp (ib->metrics->protocol, ia->metrics->protocol);
}
/* sort method ascending */
static int
cmp_mthd_asc (const void *a, const void *b)
{
const GHolderItem *ia = a;
const GHolderItem *ib = b;
return strcmp (ia->metrics->method, ib->metrics->method);
}
/* sort method descending */
static int
cmp_mthd_desc (const void *a, const void *b)
{
const GHolderItem *ia = a;
const GHolderItem *ib = b;
return strcmp (ib->metrics->method, ia->metrics->method);
}
int
get_sort_field_enum (const char *str)
{
return str2enum (FIELD, ARRAY_SIZE (FIELD), str);
}
int
get_sort_order_enum (const char *str)
{
return str2enum (ORDER, ARRAY_SIZE (ORDER), str);
}
void
set_initial_sort (const char *smod, const char *sfield, const char *ssort)
{
int module, field, order;
if ((module = get_module_enum (smod)) == -1)
return;
if ((field = get_sort_field_enum (sfield)) == -1)
return;
if ((order = get_sort_order_enum (ssort)) == -1)
return;
if (!can_sort_module (module, field))
return;
module_sort[module].field = field;
module_sort[module].sort = order;
}
int
can_sort_module (GModule module, int field)
{
int i, can_sort = 0;
for (i = 0; -1 != sort_choices[module][i]; i++) {
if (sort_choices[module][i] != field)
continue;
if (SORT_BY_USEC == field && !conf.serve_usecs)
continue;
else if (SORT_BY_BW == field && !conf.bandwidth)
continue;
else if (SORT_BY_PROT == field && !conf.append_protocol)
continue;
else if (SORT_BY_MTHD == field && !conf.append_method)
continue;
can_sort = 1;
break;
}
return can_sort;
}
void
parse_initial_sort (void)
{
int i;
char *view;
char module[SORT_MODULE_LEN], field[SORT_FIELD_LEN], order[SORT_ORDER_LEN];
for (i = 0; i < conf.sort_panel_idx; ++i) {
view = conf.sort_panels[i];
if (sscanf (view, "%8[^','],%7[^','],%4s", module, field, order) != 3)
continue;
set_initial_sort (module, field, order);
}
}
/* apply user defined sort */
void
sort_holder_items (GHolderItem * items, int size, GSort sort)
{
switch (sort.field) {
case SORT_BY_HITS:
if (sort.sort == SORT_DESC)
qsort (items, size, sizeof (GHolderItem), cmp_num_desc);
else
qsort (items, size, sizeof (GHolderItem), cmp_num_asc);
break;
case SORT_BY_VISITORS:
if (sort.sort == SORT_DESC)
qsort (items, size, sizeof (GHolderItem), cmp_vis_desc);
else
qsort (items, size, sizeof (GHolderItem), cmp_vis_asc);
break;
case SORT_BY_DATA:
if (sort.sort == SORT_DESC)
qsort (items, size, sizeof (GHolderItem), cmp_data_desc);
else
qsort (items, size, sizeof (GHolderItem), cmp_data_asc);
break;
case SORT_BY_BW:
if (sort.sort == SORT_DESC)
qsort (items, size, sizeof (GHolderItem), cmp_bw_desc);
else
qsort (items, size, sizeof (GHolderItem), cmp_bw_asc);
break;
case SORT_BY_USEC:
if (sort.sort == SORT_DESC)
qsort (items, size, sizeof (GHolderItem), cmp_usec_desc);
else
qsort (items, size, sizeof (GHolderItem), cmp_usec_asc);
break;
case SORT_BY_PROT:
if (sort.sort == SORT_DESC)
qsort (items, size, sizeof (GHolderItem), cmp_proto_desc);
else
qsort (items, size, sizeof (GHolderItem), cmp_proto_asc);
break;
case SORT_BY_MTHD:
if (sort.sort == SORT_DESC)
qsort (items, size, sizeof (GHolderItem), cmp_mthd_desc);
else
qsort (items, size, sizeof (GHolderItem), cmp_mthd_asc);
break;
}
}
/* sort raw data for the first run (default sort) */
GRawData *
sort_raw_data (GRawData * raw, int ht_size)
{
qsort (raw->items, ht_size, sizeof (GRawDataItem), cmp_raw_num_desc);
return raw;
}