blob: 74ce0f7197a6597667995be3d7096848c9a68174 [file] [log] [blame]
Lars Hjemlifb2f3f62008-12-07 13:17:21 +01001#include "cgit.h"
John Keeping8f208792013-04-06 11:37:59 +01002#include "ui-stats.h"
Lars Hjemlifb2f3f62008-12-07 13:17:21 +01003#include "html.h"
4#include "ui-shared.h"
Lars Hjemlifb2f3f62008-12-07 13:17:21 +01005
Mark Lodatoe4ddc8f2010-09-04 11:30:18 -04006#ifdef NO_C99_FORMAT
7#define SZ_FMT "%u"
8#else
9#define SZ_FMT "%zu"
10#endif
11
Lars Hjemlif86a23f2008-12-06 17:38:19 +010012struct authorstat {
13 long total;
14 struct string_list list;
15};
16
17#define DAY_SECS (60 * 60 * 24)
18#define WEEK_SECS (DAY_SECS * 7)
19
20static void trunc_week(struct tm *tm)
21{
22 time_t t = timegm(tm);
23 t -= ((tm->tm_wday + 6) % 7) * DAY_SECS;
Lukas Fleischer53bc7472013-03-03 16:04:29 +010024 gmtime_r(&t, tm);
Lars Hjemlif86a23f2008-12-06 17:38:19 +010025}
26
27static void dec_week(struct tm *tm)
28{
29 time_t t = timegm(tm);
30 t -= WEEK_SECS;
Lukas Fleischer53bc7472013-03-03 16:04:29 +010031 gmtime_r(&t, tm);
Lars Hjemlif86a23f2008-12-06 17:38:19 +010032}
33
34static void inc_week(struct tm *tm)
35{
36 time_t t = timegm(tm);
37 t += WEEK_SECS;
Lukas Fleischer53bc7472013-03-03 16:04:29 +010038 gmtime_r(&t, tm);
Lars Hjemlif86a23f2008-12-06 17:38:19 +010039}
40
41static char *pretty_week(struct tm *tm)
42{
43 static char buf[10];
44
45 strftime(buf, sizeof(buf), "W%V %G", tm);
46 return buf;
47}
48
49static void trunc_month(struct tm *tm)
50{
51 tm->tm_mday = 1;
52}
53
54static void dec_month(struct tm *tm)
55{
56 tm->tm_mon--;
57 if (tm->tm_mon < 0) {
58 tm->tm_year--;
59 tm->tm_mon = 11;
60 }
61}
62
63static void inc_month(struct tm *tm)
64{
65 tm->tm_mon++;
66 if (tm->tm_mon > 11) {
67 tm->tm_year++;
68 tm->tm_mon = 0;
69 }
70}
71
72static char *pretty_month(struct tm *tm)
73{
74 static const char *months[] = {
75 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
76 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
77 };
78 return fmt("%s %d", months[tm->tm_mon], tm->tm_year + 1900);
79}
80
81static void trunc_quarter(struct tm *tm)
82{
83 trunc_month(tm);
Jason A. Donenfeldbdae1d82013-03-03 23:21:33 -050084 while (tm->tm_mon % 3 != 0)
Lars Hjemlif86a23f2008-12-06 17:38:19 +010085 dec_month(tm);
86}
87
88static void dec_quarter(struct tm *tm)
89{
90 dec_month(tm);
91 dec_month(tm);
92 dec_month(tm);
93}
94
95static void inc_quarter(struct tm *tm)
96{
97 inc_month(tm);
98 inc_month(tm);
99 inc_month(tm);
100}
101
102static char *pretty_quarter(struct tm *tm)
103{
104 return fmt("Q%d %d", tm->tm_mon / 3 + 1, tm->tm_year + 1900);
105}
106
107static void trunc_year(struct tm *tm)
108{
109 trunc_month(tm);
110 tm->tm_mon = 0;
111}
112
113static void dec_year(struct tm *tm)
114{
115 tm->tm_year--;
116}
117
118static void inc_year(struct tm *tm)
119{
120 tm->tm_year++;
121}
122
123static char *pretty_year(struct tm *tm)
124{
125 return fmt("%d", tm->tm_year + 1900);
126}
127
John Keepingbd9fb032015-03-08 16:32:23 +0000128static const struct cgit_period periods[] = {
Lars Hjemlif86a23f2008-12-06 17:38:19 +0100129 {'w', "week", 12, 4, trunc_week, dec_week, inc_week, pretty_week},
130 {'m', "month", 12, 4, trunc_month, dec_month, inc_month, pretty_month},
131 {'q', "quarter", 12, 4, trunc_quarter, dec_quarter, inc_quarter, pretty_quarter},
132 {'y', "year", 12, 4, trunc_year, dec_year, inc_year, pretty_year},
133};
134
Lars Hjemlifb2f3f62008-12-07 13:17:21 +0100135/* Given a period code or name, return a period index (1, 2, 3 or 4)
136 * and update the period pointer to the correcsponding struct.
137 * If no matching code is found, return 0.
138 */
John Keepingbd9fb032015-03-08 16:32:23 +0000139int cgit_find_stats_period(const char *expr, const struct cgit_period **period)
Lars Hjemlifb2f3f62008-12-07 13:17:21 +0100140{
141 int i;
142 char code = '\0';
143
144 if (!expr)
145 return 0;
146
147 if (strlen(expr) == 1)
148 code = expr[0];
149
150 for (i = 0; i < sizeof(periods) / sizeof(periods[0]); i++)
151 if (periods[i].code == code || !strcmp(periods[i].name, expr)) {
152 if (period)
153 *period = &periods[i];
Lukas Fleischer53bc7472013-03-03 16:04:29 +0100154 return i + 1;
Lars Hjemlifb2f3f62008-12-07 13:17:21 +0100155 }
156 return 0;
157}
158
Lars Hjemlib47b7bd2009-08-24 11:02:48 +0200159const char *cgit_find_stats_periodname(int idx)
160{
161 if (idx > 0 && idx < 4)
162 return periods[idx - 1].name;
163 else
164 return "";
165}
166
Lars Hjemlif86a23f2008-12-06 17:38:19 +0100167static void add_commit(struct string_list *authors, struct commit *commit,
John Keepingbd9fb032015-03-08 16:32:23 +0000168 const struct cgit_period *period)
Lars Hjemlif86a23f2008-12-06 17:38:19 +0100169{
170 struct commitinfo *info;
171 struct string_list_item *author, *item;
172 struct authorstat *authorstat;
173 struct string_list *items;
174 char *tmp;
175 struct tm *date;
176 time_t t;
177
178 info = cgit_parse_commit(commit);
179 tmp = xstrdup(info->author);
Lars Hjemli6d7552b2010-08-22 13:29:57 +0200180 author = string_list_insert(authors, tmp);
Lars Hjemlif86a23f2008-12-06 17:38:19 +0100181 if (!author->util)
182 author->util = xcalloc(1, sizeof(struct authorstat));
183 else
184 free(tmp);
185 authorstat = author->util;
186 items = &authorstat->list;
187 t = info->committer_date;
188 date = gmtime(&t);
189 period->trunc(date);
190 tmp = xstrdup(period->pretty(date));
Lars Hjemli6d7552b2010-08-22 13:29:57 +0200191 item = string_list_insert(items, tmp);
Lars Hjemlif86a23f2008-12-06 17:38:19 +0100192 if (item->util)
193 free(tmp);
194 item->util++;
195 authorstat->total++;
196 cgit_free_commitinfo(info);
197}
198
199static int cmp_total_commits(const void *a1, const void *a2)
200{
201 const struct string_list_item *i1 = a1;
202 const struct string_list_item *i2 = a2;
203 const struct authorstat *auth1 = i1->util;
204 const struct authorstat *auth2 = i2->util;
205
206 return auth2->total - auth1->total;
207}
208
209/* Walk the commit DAG and collect number of commits per author per
210 * timeperiod into a nested string_list collection.
211 */
John Keepingbd9fb032015-03-08 16:32:23 +0000212static struct string_list collect_stats(const struct cgit_period *period)
Lars Hjemlif86a23f2008-12-06 17:38:19 +0100213{
214 struct string_list authors;
215 struct rev_info rev;
216 struct commit *commit;
Lukas Fleischerf60ffa12014-01-15 21:53:15 +0100217 const char *argv[] = {NULL, ctx.qry.head, NULL, NULL, NULL, NULL};
Lars Hjemlic6a6aa22008-12-07 11:45:28 +0100218 int argc = 3;
Lars Hjemlif86a23f2008-12-06 17:38:19 +0100219 time_t now;
220 long i;
221 struct tm *tm;
222 char tmp[11];
223
224 time(&now);
225 tm = gmtime(&now);
226 period->trunc(tm);
227 for (i = 1; i < period->count; i++)
228 period->dec(tm);
229 strftime(tmp, sizeof(tmp), "%Y-%m-%d", tm);
230 argv[2] = xstrdup(fmt("--since=%s", tmp));
Lukas Fleischerf60ffa12014-01-15 21:53:15 +0100231 if (ctx.qry.path) {
Lars Hjemlic6a6aa22008-12-07 11:45:28 +0100232 argv[3] = "--";
Lukas Fleischerf60ffa12014-01-15 21:53:15 +0100233 argv[4] = ctx.qry.path;
Lars Hjemlic6a6aa22008-12-07 11:45:28 +0100234 argc += 2;
235 }
Lars Hjemlif86a23f2008-12-06 17:38:19 +0100236 init_revisions(&rev, NULL);
237 rev.abbrev = DEFAULT_ABBREV;
238 rev.commit_format = CMIT_FMT_DEFAULT;
John Keepingbfc14d02013-03-02 12:32:10 +0000239 rev.max_parents = 1;
Lars Hjemlif86a23f2008-12-06 17:38:19 +0100240 rev.verbose_header = 1;
241 rev.show_root_diff = 0;
Lars Hjemlic6a6aa22008-12-07 11:45:28 +0100242 setup_revisions(argc, argv, &rev, NULL);
Lars Hjemlif86a23f2008-12-06 17:38:19 +0100243 prepare_revision_walk(&rev);
244 memset(&authors, 0, sizeof(authors));
245 while ((commit = get_revision(&rev)) != NULL) {
246 add_commit(&authors, commit, period);
John Keeping865afe02014-07-27 11:56:19 +0100247 free_commit_buffer(commit);
Lars Hjemlif86a23f2008-12-06 17:38:19 +0100248 free_commit_list(commit->parents);
John Keeping2eea4712014-07-27 11:56:20 +0100249 commit->parents = NULL;
Lars Hjemlif86a23f2008-12-06 17:38:19 +0100250 }
251 return authors;
252}
253
Lukas Fleischerbafab422013-03-04 08:52:33 +0100254static void print_combined_authorrow(struct string_list *authors, int from,
255 int to, const char *name,
256 const char *leftclass,
257 const char *centerclass,
258 const char *rightclass,
John Keepingbd9fb032015-03-08 16:32:23 +0000259 const struct cgit_period *period)
Lars Hjemlif86a23f2008-12-06 17:38:19 +0100260{
261 struct string_list_item *author;
262 struct authorstat *authorstat;
263 struct string_list *items;
264 struct string_list_item *date;
265 time_t now;
266 long i, j, total, subtotal;
267 struct tm *tm;
268 char *tmp;
269
270 time(&now);
271 tm = gmtime(&now);
272 period->trunc(tm);
273 for (i = 1; i < period->count; i++)
274 period->dec(tm);
275
276 total = 0;
277 htmlf("<tr><td class='%s'>%s</td>", leftclass,
278 fmt(name, to - from + 1));
279 for (j = 0; j < period->count; j++) {
280 tmp = period->pretty(tm);
281 period->inc(tm);
282 subtotal = 0;
283 for (i = from; i <= to; i++) {
284 author = &authors->items[i];
285 authorstat = author->util;
286 items = &authorstat->list;
Lars Hjemli6d7552b2010-08-22 13:29:57 +0200287 date = string_list_lookup(items, tmp);
Lars Hjemlif86a23f2008-12-06 17:38:19 +0100288 if (date)
289 subtotal += (size_t)date->util;
290 }
Mark Lodatoe4ddc8f2010-09-04 11:30:18 -0400291 htmlf("<td class='%s'>%ld</td>", centerclass, subtotal);
Lars Hjemlif86a23f2008-12-06 17:38:19 +0100292 total += subtotal;
293 }
Mark Lodatoe4ddc8f2010-09-04 11:30:18 -0400294 htmlf("<td class='%s'>%ld</td></tr>", rightclass, total);
Lars Hjemlif86a23f2008-12-06 17:38:19 +0100295}
296
Lukas Fleischerbafab422013-03-04 08:52:33 +0100297static void print_authors(struct string_list *authors, int top,
John Keepingbd9fb032015-03-08 16:32:23 +0000298 const struct cgit_period *period)
Lars Hjemlif86a23f2008-12-06 17:38:19 +0100299{
300 struct string_list_item *author;
301 struct authorstat *authorstat;
302 struct string_list *items;
303 struct string_list_item *date;
304 time_t now;
305 long i, j, total;
306 struct tm *tm;
307 char *tmp;
308
309 time(&now);
310 tm = gmtime(&now);
311 period->trunc(tm);
312 for (i = 1; i < period->count; i++)
313 period->dec(tm);
314
315 html("<table class='stats'><tr><th>Author</th>");
316 for (j = 0; j < period->count; j++) {
317 tmp = period->pretty(tm);
318 htmlf("<th>%s</th>", tmp);
319 period->inc(tm);
320 }
321 html("<th>Total</th></tr>\n");
322
323 if (top <= 0 || top > authors->nr)
324 top = authors->nr;
325
326 for (i = 0; i < top; i++) {
327 author = &authors->items[i];
328 html("<tr><td class='left'>");
329 html_txt(author->string);
330 html("</td>");
331 authorstat = author->util;
332 items = &authorstat->list;
333 total = 0;
334 for (j = 0; j < period->count; j++)
335 period->dec(tm);
336 for (j = 0; j < period->count; j++) {
337 tmp = period->pretty(tm);
338 period->inc(tm);
Lars Hjemli6d7552b2010-08-22 13:29:57 +0200339 date = string_list_lookup(items, tmp);
Lars Hjemlif86a23f2008-12-06 17:38:19 +0100340 if (!date)
341 html("<td>0</td>");
342 else {
Mark Lodatoe4ddc8f2010-09-04 11:30:18 -0400343 htmlf("<td>"SZ_FMT"</td>", (size_t)date->util);
Lars Hjemlif86a23f2008-12-06 17:38:19 +0100344 total += (size_t)date->util;
345 }
346 }
Mark Lodatoe4ddc8f2010-09-04 11:30:18 -0400347 htmlf("<td class='sum'>%ld</td></tr>", total);
Lars Hjemlif86a23f2008-12-06 17:38:19 +0100348 }
349
350 if (top < authors->nr)
351 print_combined_authorrow(authors, top, authors->nr - 1,
Mark Lodatoe4ddc8f2010-09-04 11:30:18 -0400352 "Others (%ld)", "left", "", "sum", period);
Lars Hjemlif86a23f2008-12-06 17:38:19 +0100353
354 print_combined_authorrow(authors, 0, authors->nr - 1, "Total",
355 "total", "sum", "sum", period);
356 html("</table>");
357}
358
359/* Create a sorted string_list with one entry per author. The util-field
360 * for each author is another string_list which is used to calculate the
361 * number of commits per time-interval.
362 */
Lukas Fleischerf60ffa12014-01-15 21:53:15 +0100363void cgit_show_stats(void)
Lars Hjemlif86a23f2008-12-06 17:38:19 +0100364{
365 struct string_list authors;
John Keepingbd9fb032015-03-08 16:32:23 +0000366 const struct cgit_period *period;
Lars Hjemlif86a23f2008-12-06 17:38:19 +0100367 int top, i;
Lars Hjemlifb2f3f62008-12-07 13:17:21 +0100368 const char *code = "w";
Lars Hjemlif86a23f2008-12-06 17:38:19 +0100369
Lukas Fleischerf60ffa12014-01-15 21:53:15 +0100370 if (ctx.qry.period)
371 code = ctx.qry.period;
Lars Hjemlifb2f3f62008-12-07 13:17:21 +0100372
373 i = cgit_find_stats_period(code, &period);
374 if (!i) {
John Keeping892c5442015-08-14 12:47:17 +0100375 cgit_print_error_page(404, "Not found",
376 "Unknown statistics type: %c", code[0]);
Lars Hjemlifb2f3f62008-12-07 13:17:21 +0100377 return;
378 }
Lukas Fleischerf60ffa12014-01-15 21:53:15 +0100379 if (i > ctx.repo->max_stats) {
John Keeping892c5442015-08-14 12:47:17 +0100380 cgit_print_error_page(400, "Bad request",
381 "Statistics type disabled: %s", period->name);
Lars Hjemlifb2f3f62008-12-07 13:17:21 +0100382 return;
Lars Hjemlif86a23f2008-12-06 17:38:19 +0100383 }
Lukas Fleischerf60ffa12014-01-15 21:53:15 +0100384 authors = collect_stats(period);
Lars Hjemlif86a23f2008-12-06 17:38:19 +0100385 qsort(authors.items, authors.nr, sizeof(struct string_list_item),
386 cmp_total_commits);
387
Lukas Fleischerf60ffa12014-01-15 21:53:15 +0100388 top = ctx.qry.ofs;
Lars Hjemlif86a23f2008-12-06 17:38:19 +0100389 if (!top)
390 top = 10;
Lars Hjemli1b5c3362011-03-07 00:00:24 +0100391
John Keeping892c5442015-08-14 12:47:17 +0100392 cgit_print_layout_start();
Lars Hjemli1b5c3362011-03-07 00:00:24 +0100393 html("<div class='cgit-panel'>");
394 html("<b>stat options</b>");
395 html("<form method='get' action=''>");
396 cgit_add_hidden_formfields(1, 0, "stats");
397 html("<table><tr><td colspan='2'/></tr>");
Lukas Fleischerf60ffa12014-01-15 21:53:15 +0100398 if (ctx.repo->max_stats > 1) {
Lars Hjemli1b5c3362011-03-07 00:00:24 +0100399 html("<tr><td class='label'>Period:</td>");
400 html("<td class='ctrl'><select name='period' onchange='this.form.submit();'>");
Lukas Fleischerf60ffa12014-01-15 21:53:15 +0100401 for (i = 0; i < ctx.repo->max_stats; i++)
Lars Hjemli1b5c3362011-03-07 00:00:24 +0100402 html_option(fmt("%c", periods[i].code),
403 periods[i].name, fmt("%c", period->code));
404 html("</select></td></tr>");
405 }
406 html("<tr><td class='label'>Authors:</td>");
407 html("<td class='ctrl'><select name='ofs' onchange='this.form.submit();'>");
408 html_intoption(10, "10", top);
409 html_intoption(25, "25", top);
410 html_intoption(50, "50", top);
411 html_intoption(100, "100", top);
412 html_intoption(-1, "all", top);
Lars Hjemli9acd1cf2011-05-30 22:21:22 +0000413 html("</select></td></tr>");
Lars Hjemli1b5c3362011-03-07 00:00:24 +0100414 html("<tr><td/><td class='ctrl'>");
415 html("<noscript><input type='submit' value='Reload'/></noscript>");
416 html("</td></tr></table>");
417 html("</form>");
418 html("</div>");
Lars Hjemlic6a6aa22008-12-07 11:45:28 +0100419 htmlf("<h2>Commits per author per %s", period->name);
Lukas Fleischerf60ffa12014-01-15 21:53:15 +0100420 if (ctx.qry.path) {
Lars Hjemlic6a6aa22008-12-07 11:45:28 +0100421 html(" (path '");
Lukas Fleischerf60ffa12014-01-15 21:53:15 +0100422 html_txt(ctx.qry.path);
Lars Hjemlic6a6aa22008-12-07 11:45:28 +0100423 html("')");
424 }
425 html("</h2>");
Lars Hjemlif86a23f2008-12-06 17:38:19 +0100426 print_authors(&authors, top, period);
John Keeping892c5442015-08-14 12:47:17 +0100427 cgit_print_layout_end();
Lars Hjemlif86a23f2008-12-06 17:38:19 +0100428}
429