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