]> gitweb.ps.run Git - ps-cgit/blob - ui-stats.c
git: update to v2.46.0
[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 #define USE_THE_REPOSITORY_VARIABLE
10
11 #include "cgit.h"
12 #include "ui-stats.h"
13 #include "html.h"
14 #include "ui-shared.h"
15
16 struct authorstat {
17         long total;
18         struct string_list list;
19 };
20
21 #define DAY_SECS (60 * 60 * 24)
22 #define WEEK_SECS (DAY_SECS * 7)
23
24 static void trunc_week(struct tm *tm)
25 {
26         time_t t = timegm(tm);
27         t -= ((tm->tm_wday + 6) % 7) * DAY_SECS;
28         gmtime_r(&t, tm);
29 }
30
31 static void dec_week(struct tm *tm)
32 {
33         time_t t = timegm(tm);
34         t -= WEEK_SECS;
35         gmtime_r(&t, tm);
36 }
37
38 static void inc_week(struct tm *tm)
39 {
40         time_t t = timegm(tm);
41         t += WEEK_SECS;
42         gmtime_r(&t, tm);
43 }
44
45 static char *pretty_week(struct tm *tm)
46 {
47         static char buf[10];
48
49         strftime(buf, sizeof(buf), "W%V %G", tm);
50         return buf;
51 }
52
53 static void trunc_month(struct tm *tm)
54 {
55         tm->tm_mday = 1;
56 }
57
58 static void dec_month(struct tm *tm)
59 {
60         tm->tm_mon--;
61         if (tm->tm_mon < 0) {
62                 tm->tm_year--;
63                 tm->tm_mon = 11;
64         }
65 }
66
67 static void inc_month(struct tm *tm)
68 {
69         tm->tm_mon++;
70         if (tm->tm_mon > 11) {
71                 tm->tm_year++;
72                 tm->tm_mon = 0;
73         }
74 }
75
76 static char *pretty_month(struct tm *tm)
77 {
78         static const char *months[] = {
79                 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
80                 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
81         };
82         return fmt("%s %d", months[tm->tm_mon], tm->tm_year + 1900);
83 }
84
85 static void trunc_quarter(struct tm *tm)
86 {
87         trunc_month(tm);
88         while (tm->tm_mon % 3 != 0)
89                 dec_month(tm);
90 }
91
92 static void dec_quarter(struct tm *tm)
93 {
94         dec_month(tm);
95         dec_month(tm);
96         dec_month(tm);
97 }
98
99 static void inc_quarter(struct tm *tm)
100 {
101         inc_month(tm);
102         inc_month(tm);
103         inc_month(tm);
104 }
105
106 static char *pretty_quarter(struct tm *tm)
107 {
108         return fmt("Q%d %d", tm->tm_mon / 3 + 1, tm->tm_year + 1900);
109 }
110
111 static void trunc_year(struct tm *tm)
112 {
113         trunc_month(tm);
114         tm->tm_mon = 0;
115 }
116
117 static void dec_year(struct tm *tm)
118 {
119         tm->tm_year--;
120 }
121
122 static void inc_year(struct tm *tm)
123 {
124         tm->tm_year++;
125 }
126
127 static char *pretty_year(struct tm *tm)
128 {
129         return fmt("%d", tm->tm_year + 1900);
130 }
131
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},
137 };
138
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.
142  */
143 int cgit_find_stats_period(const char *expr, const struct cgit_period **period)
144 {
145         int i;
146         char code = '\0';
147
148         if (!expr)
149                 return 0;
150
151         if (strlen(expr) == 1)
152                 code = expr[0];
153
154         for (i = 0; i < sizeof(periods) / sizeof(periods[0]); i++)
155                 if (periods[i].code == code || !strcmp(periods[i].name, expr)) {
156                         if (period)
157                                 *period = &periods[i];
158                         return i + 1;
159                 }
160         return 0;
161 }
162
163 const char *cgit_find_stats_periodname(int idx)
164 {
165         if (idx > 0 && idx < 4)
166                 return periods[idx - 1].name;
167         else
168                 return "";
169 }
170
171 static void add_commit(struct string_list *authors, struct commit *commit,
172         const struct cgit_period *period)
173 {
174         struct commitinfo *info;
175         struct string_list_item *author, *item;
176         struct authorstat *authorstat;
177         struct string_list *items;
178         char *tmp;
179         struct tm date;
180         time_t t;
181         uintptr_t *counter;
182
183         info = cgit_parse_commit(commit);
184         tmp = xstrdup(info->author);
185         author = string_list_insert(authors, tmp);
186         if (!author->util)
187                 author->util = xcalloc(1, sizeof(struct authorstat));
188         else
189                 free(tmp);
190         authorstat = author->util;
191         items = &authorstat->list;
192         t = info->committer_date;
193         gmtime_r(&t, &date);
194         period->trunc(&date);
195         tmp = xstrdup(period->pretty(&date));
196         item = string_list_insert(items, tmp);
197         counter = (uintptr_t *)&item->util;
198         if (*counter)
199                 free(tmp);
200         (*counter)++;
201
202         authorstat->total++;
203         cgit_free_commitinfo(info);
204 }
205
206 static int cmp_total_commits(const void *a1, const void *a2)
207 {
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;
212
213         return auth2->total - auth1->total;
214 }
215
216 /* Walk the commit DAG and collect number of commits per author per
217  * timeperiod into a nested string_list collection.
218  */
219 static struct string_list collect_stats(const struct cgit_period *period)
220 {
221         struct string_list authors;
222         struct rev_info rev;
223         struct commit *commit;
224         const char *argv[] = {NULL, ctx.qry.head, NULL, NULL, NULL, NULL};
225         int argc = 3;
226         time_t now;
227         long i;
228         struct tm tm;
229         char tmp[11];
230
231         time(&now);
232         gmtime_r(&now, &tm);
233         period->trunc(&tm);
234         for (i = 1; i < period->count; i++)
235                 period->dec(&tm);
236         strftime(tmp, sizeof(tmp), "%Y-%m-%d", &tm);
237         argv[2] = xstrdup(fmt("--since=%s", tmp));
238         if (ctx.qry.path) {
239                 argv[3] = "--";
240                 argv[4] = ctx.qry.path;
241                 argc += 2;
242         }
243         repo_init_revisions(the_repository, &rev, NULL);
244         rev.abbrev = DEFAULT_ABBREV;
245         rev.commit_format = CMIT_FMT_DEFAULT;
246         rev.max_parents = 1;
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;
256         }
257         return authors;
258 }
259
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)
266 {
267         struct string_list_item *author;
268         struct authorstat *authorstat;
269         struct string_list *items;
270         struct string_list_item *date;
271         time_t now;
272         long i, j, total, subtotal;
273         struct tm tm;
274         char *tmp;
275
276         time(&now);
277         gmtime_r(&now, &tm);
278         period->trunc(&tm);
279         for (i = 1; i < period->count; i++)
280                 period->dec(&tm);
281
282         total = 0;
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);
287                 period->inc(&tm);
288                 subtotal = 0;
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);
294                         if (date)
295                                 subtotal += (uintptr_t)date->util;
296                 }
297                 htmlf("<td class='%s'>%ld</td>", centerclass, subtotal);
298                 total += subtotal;
299         }
300         htmlf("<td class='%s'>%ld</td></tr>", rightclass, total);
301 }
302
303 static void print_authors(struct string_list *authors, int top,
304                           const struct cgit_period *period)
305 {
306         struct string_list_item *author;
307         struct authorstat *authorstat;
308         struct string_list *items;
309         struct string_list_item *date;
310         time_t now;
311         long i, j, total;
312         struct tm tm;
313         char *tmp;
314
315         time(&now);
316         gmtime_r(&now, &tm);
317         period->trunc(&tm);
318         for (i = 1; i < period->count; i++)
319                 period->dec(&tm);
320
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);
325                 period->inc(&tm);
326         }
327         html("<th>Total</th></tr>\n");
328
329         if (top <= 0 || top > authors->nr)
330                 top = authors->nr;
331
332         for (i = 0; i < top; i++) {
333                 author = &authors->items[i];
334                 html("<tr><td class='left'>");
335                 html_txt(author->string);
336                 html("</td>");
337                 authorstat = author->util;
338                 items = &authorstat->list;
339                 total = 0;
340                 for (j = 0; j < period->count; j++)
341                         period->dec(&tm);
342                 for (j = 0; j < period->count; j++) {
343                         tmp = period->pretty(&tm);
344                         period->inc(&tm);
345                         date = string_list_lookup(items, tmp);
346                         if (!date)
347                                 html("<td>0</td>");
348                         else {
349                                 htmlf("<td>%lu</td>", (uintptr_t)date->util);
350                                 total += (uintptr_t)date->util;
351                         }
352                 }
353                 htmlf("<td class='sum'>%ld</td></tr>", total);
354         }
355
356         if (top < authors->nr)
357                 print_combined_authorrow(authors, top, authors->nr - 1,
358                         "Others (%ld)", "left", "", "sum", period);
359
360         print_combined_authorrow(authors, 0, authors->nr - 1, "Total",
361                 "total", "sum", "sum", period);
362         html("</table>");
363 }
364
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.
368  */
369 void cgit_show_stats(void)
370 {
371         struct string_list authors;
372         const struct cgit_period *period;
373         int top, i;
374         const char *code = "w";
375
376         if (ctx.qry.period)
377                 code = ctx.qry.period;
378
379         i = cgit_find_stats_period(code, &period);
380         if (!i) {
381                 cgit_print_error_page(404, "Not found",
382                         "Unknown statistics type: %c", code[0]);
383                 return;
384         }
385         if (i > ctx.repo->max_stats) {
386                 cgit_print_error_page(400, "Bad request",
387                         "Statistics type disabled: %s", period->name);
388                 return;
389         }
390         authors = collect_stats(period);
391         qsort(authors.items, authors.nr, sizeof(struct string_list_item),
392                 cmp_total_commits);
393
394         top = ctx.qry.ofs;
395         if (!top)
396                 top = 10;
397
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>");
411         }
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>");
423         html("</form>");
424         html("</div>");
425         htmlf("<h2>Commits per author per %s", period->name);
426         if (ctx.qry.path) {
427                 html(" (path '");
428                 html_txt(ctx.qry.path);
429                 html("')");
430         }
431         html("</h2>");
432         print_authors(&authors, top, period);
433         cgit_print_layout_end();
434 }
435