1 #include <string-list.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 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, 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 struct cgit_period *period)
174 struct commitinfo *info;
175 struct string_list_item *author, *item;
176 struct authorstat *authorstat;
177 struct string_list *items;
182 info = cgit_parse_commit(commit);
183 tmp = xstrdup(info->author);
184 author = string_list_insert(authors, tmp);
186 author->util = xcalloc(1, sizeof(struct authorstat));
189 authorstat = author->util;
190 items = &authorstat->list;
191 t = info->committer_date;
194 tmp = xstrdup(period->pretty(date));
195 item = string_list_insert(items, tmp);
200 cgit_free_commitinfo(info);
203 static int cmp_total_commits(const void *a1, const void *a2)
205 const struct string_list_item *i1 = a1;
206 const struct string_list_item *i2 = a2;
207 const struct authorstat *auth1 = i1->util;
208 const struct authorstat *auth2 = i2->util;
210 return auth2->total - auth1->total;
213 /* Walk the commit DAG and collect number of commits per author per
214 * timeperiod into a nested string_list collection.
216 struct string_list collect_stats(struct cgit_context *ctx,
217 struct cgit_period *period)
219 struct string_list authors;
221 struct commit *commit;
222 const char *argv[] = {NULL, ctx->qry.head, NULL, NULL, NULL, NULL};
232 for (i = 1; i < period->count; i++)
234 strftime(tmp, sizeof(tmp), "%Y-%m-%d", tm);
235 argv[2] = xstrdup(fmt("--since=%s", tmp));
238 argv[4] = ctx->qry.path;
241 init_revisions(&rev, NULL);
242 rev.abbrev = DEFAULT_ABBREV;
243 rev.commit_format = CMIT_FMT_DEFAULT;
245 rev.verbose_header = 1;
246 rev.show_root_diff = 0;
247 setup_revisions(argc, argv, &rev, NULL);
248 prepare_revision_walk(&rev);
249 memset(&authors, 0, sizeof(authors));
250 while ((commit = get_revision(&rev)) != NULL) {
251 add_commit(&authors, commit, period);
252 free(commit->buffer);
253 free_commit_list(commit->parents);
258 void print_combined_authorrow(struct string_list *authors, int from, int to,
259 const char *name, const char *leftclass, const char *centerclass,
260 const char *rightclass, struct cgit_period *period)
262 struct string_list_item *author;
263 struct authorstat *authorstat;
264 struct string_list *items;
265 struct string_list_item *date;
267 long i, j, total, subtotal;
274 for (i = 1; i < period->count; i++)
278 htmlf("<tr><td class='%s'>%s</td>", leftclass,
279 fmt(name, to - from + 1));
280 for (j = 0; j < period->count; j++) {
281 tmp = period->pretty(tm);
284 for (i = from; i <= to; i++) {
285 author = &authors->items[i];
286 authorstat = author->util;
287 items = &authorstat->list;
288 date = string_list_lookup(items, tmp);
290 subtotal += (size_t)date->util;
292 htmlf("<td class='%s'>%ld</td>", centerclass, subtotal);
295 htmlf("<td class='%s'>%ld</td></tr>", rightclass, total);
298 void print_authors(struct string_list *authors, int top,
299 struct cgit_period *period)
301 struct string_list_item *author;
302 struct authorstat *authorstat;
303 struct string_list *items;
304 struct string_list_item *date;
313 for (i = 1; i < period->count; i++)
316 html("<table class='stats'><tr><th>Author</th>");
317 for (j = 0; j < period->count; j++) {
318 tmp = period->pretty(tm);
319 htmlf("<th>%s</th>", tmp);
322 html("<th>Total</th></tr>\n");
324 if (top <= 0 || top > authors->nr)
327 for (i = 0; i < top; i++) {
328 author = &authors->items[i];
329 html("<tr><td class='left'>");
330 html_txt(author->string);
332 authorstat = author->util;
333 items = &authorstat->list;
335 for (j = 0; j < period->count; j++)
337 for (j = 0; j < period->count; j++) {
338 tmp = period->pretty(tm);
340 date = string_list_lookup(items, tmp);
344 htmlf("<td>"SZ_FMT"</td>", (size_t)date->util);
345 total += (size_t)date->util;
348 htmlf("<td class='sum'>%ld</td></tr>", total);
351 if (top < authors->nr)
352 print_combined_authorrow(authors, top, authors->nr - 1,
353 "Others (%ld)", "left", "", "sum", period);
355 print_combined_authorrow(authors, 0, authors->nr - 1, "Total",
356 "total", "sum", "sum", period);
360 /* Create a sorted string_list with one entry per author. The util-field
361 * for each author is another string_list which is used to calculate the
362 * number of commits per time-interval.
364 void cgit_show_stats(struct cgit_context *ctx)
366 struct string_list authors;
367 struct cgit_period *period;
369 const char *code = "w";
372 code = ctx->qry.period;
374 i = cgit_find_stats_period(code, &period);
376 cgit_print_error(fmt("Unknown statistics type: %c", code[0]));
379 if (i > ctx->repo->max_stats) {
380 cgit_print_error(fmt("Statistics type disabled: %s",
384 authors = collect_stats(ctx, period);
385 qsort(authors.items, authors.nr, sizeof(struct string_list_item),
391 htmlf("<h2>Commits per author per %s", period->name);
394 html_txt(ctx->qry.path);
399 html("<form method='get' action='' style='float: right; text-align: right;'>");
400 cgit_add_hidden_formfields(1, 0, "stats");
401 if (ctx->repo->max_stats > 1) {
403 html("<select name='period' onchange='this.form.submit();'>");
404 for (i = 0; i < ctx->repo->max_stats; i++)
405 htmlf("<option value='%c'%s>%s</option>",
407 period == &periods[i] ? " selected" : "",
409 html("</select><br/><br/>");
413 html("<select name='ofs' onchange='this.form.submit();'>");
414 htmlf("<option value='10'%s>10</option>", top == 10 ? " selected" : "");
415 htmlf("<option value='25'%s>25</option>", top == 25 ? " selected" : "");
416 htmlf("<option value='50'%s>50</option>", top == 50 ? " selected" : "");
417 htmlf("<option value='100'%s>100</option>", top == 100 ? " selected" : "");
418 htmlf("<option value='-1'%s>All</option>", top == -1 ? " selected" : "");
420 html("<noscript> <input type='submit' value='Reload'/></noscript>");
422 print_authors(&authors, top, period);