]> git.cameronkatri.com Git - cgit.git/blob - ui-stats.c
filter: don't forget to reap the auth filter
[cgit.git] / ui-stats.c
1 #include "cgit.h"
2 #include "ui-stats.h"
3 #include "html.h"
4 #include "ui-shared.h"
5
6 #ifdef NO_C99_FORMAT
7 #define SZ_FMT "%u"
8 #else
9 #define SZ_FMT "%zu"
10 #endif
11
12 struct authorstat {
13 long total;
14 struct string_list list;
15 };
16
17 #define DAY_SECS (60 * 60 * 24)
18 #define WEEK_SECS (DAY_SECS * 7)
19
20 static void trunc_week(struct tm *tm)
21 {
22 time_t t = timegm(tm);
23 t -= ((tm->tm_wday + 6) % 7) * DAY_SECS;
24 gmtime_r(&t, tm);
25 }
26
27 static void dec_week(struct tm *tm)
28 {
29 time_t t = timegm(tm);
30 t -= WEEK_SECS;
31 gmtime_r(&t, tm);
32 }
33
34 static void inc_week(struct tm *tm)
35 {
36 time_t t = timegm(tm);
37 t += WEEK_SECS;
38 gmtime_r(&t, tm);
39 }
40
41 static char *pretty_week(struct tm *tm)
42 {
43 static char buf[10];
44
45 strftime(buf, sizeof(buf), "W%V %G", tm);
46 return buf;
47 }
48
49 static void trunc_month(struct tm *tm)
50 {
51 tm->tm_mday = 1;
52 }
53
54 static void dec_month(struct tm *tm)
55 {
56 tm->tm_mon--;
57 if (tm->tm_mon < 0) {
58 tm->tm_year--;
59 tm->tm_mon = 11;
60 }
61 }
62
63 static void inc_month(struct tm *tm)
64 {
65 tm->tm_mon++;
66 if (tm->tm_mon > 11) {
67 tm->tm_year++;
68 tm->tm_mon = 0;
69 }
70 }
71
72 static char *pretty_month(struct tm *tm)
73 {
74 static const char *months[] = {
75 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
76 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
77 };
78 return fmt("%s %d", months[tm->tm_mon], tm->tm_year + 1900);
79 }
80
81 static void trunc_quarter(struct tm *tm)
82 {
83 trunc_month(tm);
84 while (tm->tm_mon % 3 != 0)
85 dec_month(tm);
86 }
87
88 static void dec_quarter(struct tm *tm)
89 {
90 dec_month(tm);
91 dec_month(tm);
92 dec_month(tm);
93 }
94
95 static void inc_quarter(struct tm *tm)
96 {
97 inc_month(tm);
98 inc_month(tm);
99 inc_month(tm);
100 }
101
102 static char *pretty_quarter(struct tm *tm)
103 {
104 return fmt("Q%d %d", tm->tm_mon / 3 + 1, tm->tm_year + 1900);
105 }
106
107 static void trunc_year(struct tm *tm)
108 {
109 trunc_month(tm);
110 tm->tm_mon = 0;
111 }
112
113 static void dec_year(struct tm *tm)
114 {
115 tm->tm_year--;
116 }
117
118 static void inc_year(struct tm *tm)
119 {
120 tm->tm_year++;
121 }
122
123 static char *pretty_year(struct tm *tm)
124 {
125 return fmt("%d", tm->tm_year + 1900);
126 }
127
128 struct cgit_period periods[] = {
129 {'w', "week", 12, 4, trunc_week, dec_week, inc_week, pretty_week},
130 {'m', "month", 12, 4, trunc_month, dec_month, inc_month, pretty_month},
131 {'q', "quarter", 12, 4, trunc_quarter, dec_quarter, inc_quarter, pretty_quarter},
132 {'y', "year", 12, 4, trunc_year, dec_year, inc_year, pretty_year},
133 };
134
135 /* Given a period code or name, return a period index (1, 2, 3 or 4)
136 * and update the period pointer to the correcsponding struct.
137 * If no matching code is found, return 0.
138 */
139 int cgit_find_stats_period(const char *expr, struct cgit_period **period)
140 {
141 int i;
142 char code = '\0';
143
144 if (!expr)
145 return 0;
146
147 if (strlen(expr) == 1)
148 code = expr[0];
149
150 for (i = 0; i < sizeof(periods) / sizeof(periods[0]); i++)
151 if (periods[i].code == code || !strcmp(periods[i].name, expr)) {
152 if (period)
153 *period = &periods[i];
154 return i + 1;
155 }
156 return 0;
157 }
158
159 const char *cgit_find_stats_periodname(int idx)
160 {
161 if (idx > 0 && idx < 4)
162 return periods[idx - 1].name;
163 else
164 return "";
165 }
166
167 static void add_commit(struct string_list *authors, struct commit *commit,
168 struct cgit_period *period)
169 {
170 struct commitinfo *info;
171 struct string_list_item *author, *item;
172 struct authorstat *authorstat;
173 struct string_list *items;
174 char *tmp;
175 struct tm *date;
176 time_t t;
177
178 info = cgit_parse_commit(commit);
179 tmp = xstrdup(info->author);
180 author = string_list_insert(authors, tmp);
181 if (!author->util)
182 author->util = xcalloc(1, sizeof(struct authorstat));
183 else
184 free(tmp);
185 authorstat = author->util;
186 items = &authorstat->list;
187 t = info->committer_date;
188 date = gmtime(&t);
189 period->trunc(date);
190 tmp = xstrdup(period->pretty(date));
191 item = string_list_insert(items, tmp);
192 if (item->util)
193 free(tmp);
194 item->util++;
195 authorstat->total++;
196 cgit_free_commitinfo(info);
197 }
198
199 static int cmp_total_commits(const void *a1, const void *a2)
200 {
201 const struct string_list_item *i1 = a1;
202 const struct string_list_item *i2 = a2;
203 const struct authorstat *auth1 = i1->util;
204 const struct authorstat *auth2 = i2->util;
205
206 return auth2->total - auth1->total;
207 }
208
209 /* Walk the commit DAG and collect number of commits per author per
210 * timeperiod into a nested string_list collection.
211 */
212 static struct string_list collect_stats(struct cgit_period *period)
213 {
214 struct string_list authors;
215 struct rev_info rev;
216 struct commit *commit;
217 const char *argv[] = {NULL, ctx.qry.head, NULL, NULL, NULL, NULL};
218 int argc = 3;
219 time_t now;
220 long i;
221 struct tm *tm;
222 char tmp[11];
223
224 time(&now);
225 tm = gmtime(&now);
226 period->trunc(tm);
227 for (i = 1; i < period->count; i++)
228 period->dec(tm);
229 strftime(tmp, sizeof(tmp), "%Y-%m-%d", tm);
230 argv[2] = xstrdup(fmt("--since=%s", tmp));
231 if (ctx.qry.path) {
232 argv[3] = "--";
233 argv[4] = ctx.qry.path;
234 argc += 2;
235 }
236 init_revisions(&rev, NULL);
237 rev.abbrev = DEFAULT_ABBREV;
238 rev.commit_format = CMIT_FMT_DEFAULT;
239 rev.max_parents = 1;
240 rev.verbose_header = 1;
241 rev.show_root_diff = 0;
242 setup_revisions(argc, argv, &rev, NULL);
243 prepare_revision_walk(&rev);
244 memset(&authors, 0, sizeof(authors));
245 while ((commit = get_revision(&rev)) != NULL) {
246 add_commit(&authors, commit, period);
247 free(commit->buffer);
248 free_commit_list(commit->parents);
249 }
250 return authors;
251 }
252
253 static void print_combined_authorrow(struct string_list *authors, int from,
254 int to, const char *name,
255 const char *leftclass,
256 const char *centerclass,
257 const char *rightclass,
258 struct cgit_period *period)
259 {
260 struct string_list_item *author;
261 struct authorstat *authorstat;
262 struct string_list *items;
263 struct string_list_item *date;
264 time_t now;
265 long i, j, total, subtotal;
266 struct tm *tm;
267 char *tmp;
268
269 time(&now);
270 tm = gmtime(&now);
271 period->trunc(tm);
272 for (i = 1; i < period->count; i++)
273 period->dec(tm);
274
275 total = 0;
276 htmlf("<tr><td class='%s'>%s</td>", leftclass,
277 fmt(name, to - from + 1));
278 for (j = 0; j < period->count; j++) {
279 tmp = period->pretty(tm);
280 period->inc(tm);
281 subtotal = 0;
282 for (i = from; i <= to; i++) {
283 author = &authors->items[i];
284 authorstat = author->util;
285 items = &authorstat->list;
286 date = string_list_lookup(items, tmp);
287 if (date)
288 subtotal += (size_t)date->util;
289 }
290 htmlf("<td class='%s'>%ld</td>", centerclass, subtotal);
291 total += subtotal;
292 }
293 htmlf("<td class='%s'>%ld</td></tr>", rightclass, total);
294 }
295
296 static void print_authors(struct string_list *authors, int top,
297 struct cgit_period *period)
298 {
299 struct string_list_item *author;
300 struct authorstat *authorstat;
301 struct string_list *items;
302 struct string_list_item *date;
303 time_t now;
304 long i, j, total;
305 struct tm *tm;
306 char *tmp;
307
308 time(&now);
309 tm = gmtime(&now);
310 period->trunc(tm);
311 for (i = 1; i < period->count; i++)
312 period->dec(tm);
313
314 html("<table class='stats'><tr><th>Author</th>");
315 for (j = 0; j < period->count; j++) {
316 tmp = period->pretty(tm);
317 htmlf("<th>%s</th>", tmp);
318 period->inc(tm);
319 }
320 html("<th>Total</th></tr>\n");
321
322 if (top <= 0 || top > authors->nr)
323 top = authors->nr;
324
325 for (i = 0; i < top; i++) {
326 author = &authors->items[i];
327 html("<tr><td class='left'>");
328 html_txt(author->string);
329 html("</td>");
330 authorstat = author->util;
331 items = &authorstat->list;
332 total = 0;
333 for (j = 0; j < period->count; j++)
334 period->dec(tm);
335 for (j = 0; j < period->count; j++) {
336 tmp = period->pretty(tm);
337 period->inc(tm);
338 date = string_list_lookup(items, tmp);
339 if (!date)
340 html("<td>0</td>");
341 else {
342 htmlf("<td>"SZ_FMT"</td>", (size_t)date->util);
343 total += (size_t)date->util;
344 }
345 }
346 htmlf("<td class='sum'>%ld</td></tr>", total);
347 }
348
349 if (top < authors->nr)
350 print_combined_authorrow(authors, top, authors->nr - 1,
351 "Others (%ld)", "left", "", "sum", period);
352
353 print_combined_authorrow(authors, 0, authors->nr - 1, "Total",
354 "total", "sum", "sum", period);
355 html("</table>");
356 }
357
358 /* Create a sorted string_list with one entry per author. The util-field
359 * for each author is another string_list which is used to calculate the
360 * number of commits per time-interval.
361 */
362 void cgit_show_stats(void)
363 {
364 struct string_list authors;
365 struct cgit_period *period;
366 int top, i;
367 const char *code = "w";
368
369 if (ctx.qry.period)
370 code = ctx.qry.period;
371
372 i = cgit_find_stats_period(code, &period);
373 if (!i) {
374 cgit_print_error("Unknown statistics type: %c", code[0]);
375 return;
376 }
377 if (i > ctx.repo->max_stats) {
378 cgit_print_error("Statistics type disabled: %s", period->name);
379 return;
380 }
381 authors = collect_stats(period);
382 qsort(authors.items, authors.nr, sizeof(struct string_list_item),
383 cmp_total_commits);
384
385 top = ctx.qry.ofs;
386 if (!top)
387 top = 10;
388
389 html("<div class='cgit-panel'>");
390 html("<b>stat options</b>");
391 html("<form method='get' action=''>");
392 cgit_add_hidden_formfields(1, 0, "stats");
393 html("<table><tr><td colspan='2'/></tr>");
394 if (ctx.repo->max_stats > 1) {
395 html("<tr><td class='label'>Period:</td>");
396 html("<td class='ctrl'><select name='period' onchange='this.form.submit();'>");
397 for (i = 0; i < ctx.repo->max_stats; i++)
398 html_option(fmt("%c", periods[i].code),
399 periods[i].name, fmt("%c", period->code));
400 html("</select></td></tr>");
401 }
402 html("<tr><td class='label'>Authors:</td>");
403 html("<td class='ctrl'><select name='ofs' onchange='this.form.submit();'>");
404 html_intoption(10, "10", top);
405 html_intoption(25, "25", top);
406 html_intoption(50, "50", top);
407 html_intoption(100, "100", top);
408 html_intoption(-1, "all", top);
409 html("</select></td></tr>");
410 html("<tr><td/><td class='ctrl'>");
411 html("<noscript><input type='submit' value='Reload'/></noscript>");
412 html("</td></tr></table>");
413 html("</form>");
414 html("</div>");
415 htmlf("<h2>Commits per author per %s", period->name);
416 if (ctx.qry.path) {
417 html(" (path '");
418 html_txt(ctx.qry.path);
419 html("')");
420 }
421 html("</h2>");
422 print_authors(&authors, top, period);
423 }
424