filter: add gravatar scripts
[cgit.git] / ui-stats.c
blob84b247c9a9c94a518483d5f0a66bdf114d8f7e85
1 #include "cgit.h"
2 #include "ui-stats.h"
3 #include "html.h"
4 #include "ui-shared.h"
6 #ifdef NO_C99_FORMAT
7 #define SZ_FMT "%u"
8 #else
9 #define SZ_FMT "%zu"
10 #endif
12 struct authorstat {
13 long total;
14 struct string_list list;
17 #define DAY_SECS (60 * 60 * 24)
18 #define WEEK_SECS (DAY_SECS * 7)
20 static void trunc_week(struct tm *tm)
22 time_t t = timegm(tm);
23 t -= ((tm->tm_wday + 6) % 7) * DAY_SECS;
24 gmtime_r(&t, tm);
27 static void dec_week(struct tm *tm)
29 time_t t = timegm(tm);
30 t -= WEEK_SECS;
31 gmtime_r(&t, tm);
34 static void inc_week(struct tm *tm)
36 time_t t = timegm(tm);
37 t += WEEK_SECS;
38 gmtime_r(&t, tm);
41 static char *pretty_week(struct tm *tm)
43 static char buf[10];
45 strftime(buf, sizeof(buf), "W%V %G", tm);
46 return buf;
49 static void trunc_month(struct tm *tm)
51 tm->tm_mday = 1;
54 static void dec_month(struct tm *tm)
56 tm->tm_mon--;
57 if (tm->tm_mon < 0) {
58 tm->tm_year--;
59 tm->tm_mon = 11;
63 static void inc_month(struct tm *tm)
65 tm->tm_mon++;
66 if (tm->tm_mon > 11) {
67 tm->tm_year++;
68 tm->tm_mon = 0;
72 static char *pretty_month(struct tm *tm)
74 static const char *months[] = {
75 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
76 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
78 return fmt("%s %d", months[tm->tm_mon], tm->tm_year + 1900);
81 static void trunc_quarter(struct tm *tm)
83 trunc_month(tm);
84 while (tm->tm_mon % 3 != 0)
85 dec_month(tm);
88 static void dec_quarter(struct tm *tm)
90 dec_month(tm);
91 dec_month(tm);
92 dec_month(tm);
95 static void inc_quarter(struct tm *tm)
97 inc_month(tm);
98 inc_month(tm);
99 inc_month(tm);
102 static char *pretty_quarter(struct tm *tm)
104 return fmt("Q%d %d", tm->tm_mon / 3 + 1, tm->tm_year + 1900);
107 static void trunc_year(struct tm *tm)
109 trunc_month(tm);
110 tm->tm_mon = 0;
113 static void dec_year(struct tm *tm)
115 tm->tm_year--;
118 static void inc_year(struct tm *tm)
120 tm->tm_year++;
123 static char *pretty_year(struct tm *tm)
125 return fmt("%d", tm->tm_year + 1900);
128 struct cgit_period periods[] = {
129 {'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},
135 /* 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.
139 int cgit_find_stats_period(const char *expr, struct cgit_period **period)
141 int i;
142 char code = '\0';
144 if (!expr)
145 return 0;
147 if (strlen(expr) == 1)
148 code = expr[0];
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];
154 return i + 1;
156 return 0;
159 const char *cgit_find_stats_periodname(int idx)
161 if (idx > 0 && idx < 4)
162 return periods[idx - 1].name;
163 else
164 return "";
167 static void add_commit(struct string_list *authors, struct commit *commit,
168 struct cgit_period *period)
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;
178 info = cgit_parse_commit(commit);
179 tmp = xstrdup(info->author);
180 author = string_list_insert(authors, tmp);
181 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));
191 item = string_list_insert(items, tmp);
192 if (item->util)
193 free(tmp);
194 item->util++;
195 authorstat->total++;
196 cgit_free_commitinfo(info);
199 static int cmp_total_commits(const void *a1, const void *a2)
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;
206 return auth2->total - auth1->total;
209 /* Walk the commit DAG and collect number of commits per author per
210 * timeperiod into a nested string_list collection.
212 static struct string_list collect_stats(struct cgit_context *ctx,
213 struct cgit_period *period)
215 struct string_list authors;
216 struct rev_info rev;
217 struct commit *commit;
218 const char *argv[] = {NULL, ctx->qry.head, NULL, NULL, NULL, NULL};
219 int argc = 3;
220 time_t now;
221 long i;
222 struct tm *tm;
223 char tmp[11];
225 time(&now);
226 tm = gmtime(&now);
227 period->trunc(tm);
228 for (i = 1; i < period->count; i++)
229 period->dec(tm);
230 strftime(tmp, sizeof(tmp), "%Y-%m-%d", tm);
231 argv[2] = xstrdup(fmt("--since=%s", tmp));
232 if (ctx->qry.path) {
233 argv[3] = "--";
234 argv[4] = ctx->qry.path;
235 argc += 2;
237 init_revisions(&rev, NULL);
238 rev.abbrev = DEFAULT_ABBREV;
239 rev.commit_format = CMIT_FMT_DEFAULT;
240 rev.max_parents = 1;
241 rev.verbose_header = 1;
242 rev.show_root_diff = 0;
243 setup_revisions(argc, argv, &rev, NULL);
244 prepare_revision_walk(&rev);
245 memset(&authors, 0, sizeof(authors));
246 while ((commit = get_revision(&rev)) != NULL) {
247 add_commit(&authors, commit, period);
248 free(commit->buffer);
249 free_commit_list(commit->parents);
251 return authors;
254 static 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,
259 struct cgit_period *period)
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;
270 time(&now);
271 tm = gmtime(&now);
272 period->trunc(tm);
273 for (i = 1; i < period->count; i++)
274 period->dec(tm);
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;
287 date = string_list_lookup(items, tmp);
288 if (date)
289 subtotal += (size_t)date->util;
291 htmlf("<td class='%s'>%ld</td>", centerclass, subtotal);
292 total += subtotal;
294 htmlf("<td class='%s'>%ld</td></tr>", rightclass, total);
297 static void print_authors(struct string_list *authors, int top,
298 struct cgit_period *period)
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;
309 time(&now);
310 tm = gmtime(&now);
311 period->trunc(tm);
312 for (i = 1; i < period->count; i++)
313 period->dec(tm);
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);
321 html("<th>Total</th></tr>\n");
323 if (top <= 0 || top > authors->nr)
324 top = authors->nr;
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);
339 date = string_list_lookup(items, tmp);
340 if (!date)
341 html("<td>0</td>");
342 else {
343 htmlf("<td>"SZ_FMT"</td>", (size_t)date->util);
344 total += (size_t)date->util;
347 htmlf("<td class='sum'>%ld</td></tr>", total);
350 if (top < authors->nr)
351 print_combined_authorrow(authors, top, authors->nr - 1,
352 "Others (%ld)", "left", "", "sum", period);
354 print_combined_authorrow(authors, 0, authors->nr - 1, "Total",
355 "total", "sum", "sum", period);
356 html("</table>");
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.
363 void cgit_show_stats(struct cgit_context *ctx)
365 struct string_list authors;
366 struct cgit_period *period;
367 int top, i;
368 const char *code = "w";
370 if (ctx->qry.period)
371 code = ctx->qry.period;
373 i = cgit_find_stats_period(code, &period);
374 if (!i) {
375 cgit_print_error("Unknown statistics type: %c", code[0]);
376 return;
378 if (i > ctx->repo->max_stats) {
379 cgit_print_error("Statistics type disabled: %s", period->name);
380 return;
382 authors = collect_stats(ctx, period);
383 qsort(authors.items, authors.nr, sizeof(struct string_list_item),
384 cmp_total_commits);
386 top = ctx->qry.ofs;
387 if (!top)
388 top = 10;
390 html("<div class='cgit-panel'>");
391 html("<b>stat options</b>");
392 html("<form method='get' action=''>");
393 cgit_add_hidden_formfields(1, 0, "stats");
394 html("<table><tr><td colspan='2'/></tr>");
395 if (ctx->repo->max_stats > 1) {
396 html("<tr><td class='label'>Period:</td>");
397 html("<td class='ctrl'><select name='period' onchange='this.form.submit();'>");
398 for (i = 0; i < ctx->repo->max_stats; i++)
399 html_option(fmt("%c", periods[i].code),
400 periods[i].name, fmt("%c", period->code));
401 html("</select></td></tr>");
403 html("<tr><td class='label'>Authors:</td>");
404 html("<td class='ctrl'><select name='ofs' onchange='this.form.submit();'>");
405 html_intoption(10, "10", top);
406 html_intoption(25, "25", top);
407 html_intoption(50, "50", top);
408 html_intoption(100, "100", top);
409 html_intoption(-1, "all", top);
410 html("</select></td></tr>");
411 html("<tr><td/><td class='ctrl'>");
412 html("<noscript><input type='submit' value='Reload'/></noscript>");
413 html("</td></tr></table>");
414 html("</form>");
415 html("</div>");
416 htmlf("<h2>Commits per author per %s", period->name);
417 if (ctx->qry.path) {
418 html(" (path '");
419 html_txt(ctx->qry.path);
420 html("')");
422 html("</h2>");
423 print_authors(&authors, top, period);