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