1 /* ui-stats.c: generate stats view
3 * Copyright (C) 2006-2014 cgit Development Team <cgit@lists.zx2c4.com>
5 * Licensed under GNU General Public License v2
6 * (see COPYING for full license text)
9 #define USE_THE_REPOSITORY_VARIABLE
14 #include "ui-shared.h"
18 struct string_list list;
21 #define DAY_SECS (60 * 60 * 24)
22 #define WEEK_SECS (DAY_SECS * 7)
24 static void trunc_week(struct tm *tm)
26 time_t t = timegm(tm);
27 t -= ((tm->tm_wday + 6) % 7) * DAY_SECS;
31 static void dec_week(struct tm *tm)
33 time_t t = timegm(tm);
38 static void inc_week(struct tm *tm)
40 time_t t = timegm(tm);
45 static char *pretty_week(struct tm *tm)
49 strftime(buf, sizeof(buf), "W%V %G", tm);
53 static void trunc_month(struct tm *tm)
58 static void dec_month(struct tm *tm)
67 static void inc_month(struct tm *tm)
70 if (tm->tm_mon > 11) {
76 static char *pretty_month(struct tm *tm)
78 static const char *months[] = {
79 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
80 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
82 return fmt("%s %d", months[tm->tm_mon], tm->tm_year + 1900);
85 static void trunc_quarter(struct tm *tm)
88 while (tm->tm_mon % 3 != 0)
92 static void dec_quarter(struct tm *tm)
99 static void inc_quarter(struct tm *tm)
106 static char *pretty_quarter(struct tm *tm)
108 return fmt("Q%d %d", tm->tm_mon / 3 + 1, tm->tm_year + 1900);
111 static void trunc_year(struct tm *tm)
117 static void dec_year(struct tm *tm)
122 static void inc_year(struct tm *tm)
127 static char *pretty_year(struct tm *tm)
129 return fmt("%d", tm->tm_year + 1900);
132 static const struct cgit_period periods[] = {
133 {'w', "week", 12, 4, trunc_week, dec_week, inc_week, pretty_week},
134 {'m', "month", 12, 4, trunc_month, dec_month, inc_month, pretty_month},
135 {'q', "quarter", 12, 4, trunc_quarter, dec_quarter, inc_quarter, pretty_quarter},
136 {'y', "year", 12, 4, trunc_year, dec_year, inc_year, pretty_year},
139 /* Given a period code or name, return a period index (1, 2, 3 or 4)
140 * and update the period pointer to the correcsponding struct.
141 * If no matching code is found, return 0.
143 int cgit_find_stats_period(const char *expr, const struct cgit_period **period)
151 if (strlen(expr) == 1)
154 for (i = 0; i < sizeof(periods) / sizeof(periods[0]); i++)
155 if (periods[i].code == code || !strcmp(periods[i].name, expr)) {
157 *period = &periods[i];
163 const char *cgit_find_stats_periodname(int idx)
165 if (idx > 0 && idx < 4)
166 return periods[idx - 1].name;
171 static void add_commit(struct string_list *authors, struct commit *commit,
172 const struct cgit_period *period)
174 struct commitinfo *info;
175 struct string_list_item *author, *item;
176 struct authorstat *authorstat;
177 struct string_list *items;
183 info = cgit_parse_commit(commit);
184 tmp = xstrdup(info->author);
185 author = string_list_insert(authors, tmp);
187 author->util = xcalloc(1, sizeof(struct authorstat));
190 authorstat = author->util;
191 items = &authorstat->list;
192 t = info->committer_date;
194 period->trunc(&date);
195 tmp = xstrdup(period->pretty(&date));
196 item = string_list_insert(items, tmp);
197 counter = (uintptr_t *)&item->util;
203 cgit_free_commitinfo(info);
206 static int cmp_total_commits(const void *a1, const void *a2)
208 const struct string_list_item *i1 = a1;
209 const struct string_list_item *i2 = a2;
210 const struct authorstat *auth1 = i1->util;
211 const struct authorstat *auth2 = i2->util;
213 return auth2->total - auth1->total;
216 /* Walk the commit DAG and collect number of commits per author per
217 * timeperiod into a nested string_list collection.
219 static struct string_list collect_stats(const struct cgit_period *period)
221 struct string_list authors;
223 struct commit *commit;
224 const char *argv[] = {NULL, ctx.qry.head, NULL, NULL, NULL, NULL};
234 for (i = 1; i < period->count; i++)
236 strftime(tmp, sizeof(tmp), "%Y-%m-%d", &tm);
237 argv[2] = xstrdup(fmt("--since=%s", tmp));
240 argv[4] = ctx.qry.path;
243 repo_init_revisions(the_repository, &rev, NULL);
244 rev.abbrev = DEFAULT_ABBREV;
245 rev.commit_format = CMIT_FMT_DEFAULT;
247 rev.verbose_header = 1;
248 rev.show_root_diff = 0;
249 setup_revisions(argc, argv, &rev, NULL);
250 prepare_revision_walk(&rev);
251 memset(&authors, 0, sizeof(authors));
252 while ((commit = get_revision(&rev)) != NULL) {
253 add_commit(&authors, commit, period);
254 release_commit_memory(the_repository->parsed_objects, commit);
255 commit->parents = NULL;
260 static void print_combined_authorrow(struct string_list *authors, int from,
261 int to, const char *name,
262 const char *leftclass,
263 const char *centerclass,
264 const char *rightclass,
265 const struct cgit_period *period)
267 struct string_list_item *author;
268 struct authorstat *authorstat;
269 struct string_list *items;
270 struct string_list_item *date;
272 long i, j, total, subtotal;
279 for (i = 1; i < period->count; i++)
283 htmlf("<tr><td class='%s'>%s</td>", leftclass,
284 fmt(name, to - from + 1));
285 for (j = 0; j < period->count; j++) {
286 tmp = period->pretty(&tm);
289 for (i = from; i <= to; i++) {
290 author = &authors->items[i];
291 authorstat = author->util;
292 items = &authorstat->list;
293 date = string_list_lookup(items, tmp);
295 subtotal += (uintptr_t)date->util;
297 htmlf("<td class='%s'>%ld</td>", centerclass, subtotal);
300 htmlf("<td class='%s'>%ld</td></tr>", rightclass, total);
303 static void print_authors(struct string_list *authors, int top,
304 const struct cgit_period *period)
306 struct string_list_item *author;
307 struct authorstat *authorstat;
308 struct string_list *items;
309 struct string_list_item *date;
318 for (i = 1; i < period->count; i++)
321 html("<table class='stats'><tr><th>Author</th>");
322 for (j = 0; j < period->count; j++) {
323 tmp = period->pretty(&tm);
324 htmlf("<th>%s</th>", tmp);
327 html("<th>Total</th></tr>\n");
329 if (top <= 0 || top > authors->nr)
332 for (i = 0; i < top; i++) {
333 author = &authors->items[i];
334 html("<tr><td class='left'>");
335 html_txt(author->string);
337 authorstat = author->util;
338 items = &authorstat->list;
340 for (j = 0; j < period->count; j++)
342 for (j = 0; j < period->count; j++) {
343 tmp = period->pretty(&tm);
345 date = string_list_lookup(items, tmp);
349 htmlf("<td>%lu</td>", (uintptr_t)date->util);
350 total += (uintptr_t)date->util;
353 htmlf("<td class='sum'>%ld</td></tr>", total);
356 if (top < authors->nr)
357 print_combined_authorrow(authors, top, authors->nr - 1,
358 "Others (%ld)", "left", "", "sum", period);
360 print_combined_authorrow(authors, 0, authors->nr - 1, "Total",
361 "total", "sum", "sum", period);
365 /* Create a sorted string_list with one entry per author. The util-field
366 * for each author is another string_list which is used to calculate the
367 * number of commits per time-interval.
369 void cgit_show_stats(void)
371 struct string_list authors;
372 const struct cgit_period *period;
374 const char *code = "w";
377 code = ctx.qry.period;
379 i = cgit_find_stats_period(code, &period);
381 cgit_print_error_page(404, "Not found",
382 "Unknown statistics type: %c", code[0]);
385 if (i > ctx.repo->max_stats) {
386 cgit_print_error_page(400, "Bad request",
387 "Statistics type disabled: %s", period->name);
390 authors = collect_stats(period);
391 qsort(authors.items, authors.nr, sizeof(struct string_list_item),
398 cgit_print_layout_start();
399 html("<div class='cgit-panel'>");
400 html("<b>stat options</b>");
401 html("<form method='get'>");
402 cgit_add_hidden_formfields(1, 0, "stats");
403 html("<table><tr><td colspan='2'/></tr>");
404 if (ctx.repo->max_stats > 1) {
405 html("<tr><td class='label'>Period:</td>");
406 html("<td class='ctrl'><select name='period' onchange='this.form.submit();'>");
407 for (i = 0; i < ctx.repo->max_stats; i++)
408 html_option(fmt("%c", periods[i].code),
409 periods[i].name, fmt("%c", period->code));
410 html("</select></td></tr>");
412 html("<tr><td class='label'>Authors:</td>");
413 html("<td class='ctrl'><select name='ofs' onchange='this.form.submit();'>");
414 html_intoption(10, "10", top);
415 html_intoption(25, "25", top);
416 html_intoption(50, "50", top);
417 html_intoption(100, "100", top);
418 html_intoption(-1, "all", top);
419 html("</select></td></tr>");
420 html("<tr><td/><td class='ctrl'>");
421 html("<noscript><input type='submit' value='Reload'/></noscript>");
422 html("</td></tr></table>");
425 htmlf("<h2>Commits per author per %s", period->name);
428 html_txt(ctx.qry.path);
432 print_authors(&authors, top, period);
433 cgit_print_layout_end();