Lars Hjemli | fb2f3f6 | 2008-12-07 13:17:21 +0100 | [diff] [blame] | 1 | #include "cgit.h" |
| 2 | #include "html.h" |
| 3 | #include "ui-shared.h" |
| 4 | #include "ui-stats.h" |
| 5 | |
Mark Lodato | e4ddc8f | 2010-09-04 11:30:18 -0400 | [diff] [blame] | 6 | #ifdef NO_C99_FORMAT |
| 7 | #define SZ_FMT "%u" |
| 8 | #else |
| 9 | #define SZ_FMT "%zu" |
| 10 | #endif |
| 11 | |
Lars Hjemli | f86a23f | 2008-12-06 17:38:19 +0100 | [diff] [blame] | 12 | #define MONTHS 6 |
| 13 | |
Lars Hjemli | f86a23f | 2008-12-06 17:38:19 +0100 | [diff] [blame] | 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; |
Lukas Fleischer | 53bc747 | 2013-03-03 16:04:29 +0100 | [diff] [blame] | 26 | gmtime_r(&t, tm); |
Lars Hjemli | f86a23f | 2008-12-06 17:38:19 +0100 | [diff] [blame] | 27 | } |
| 28 | |
| 29 | static void dec_week(struct tm *tm) |
| 30 | { |
| 31 | time_t t = timegm(tm); |
| 32 | t -= WEEK_SECS; |
Lukas Fleischer | 53bc747 | 2013-03-03 16:04:29 +0100 | [diff] [blame] | 33 | gmtime_r(&t, tm); |
Lars Hjemli | f86a23f | 2008-12-06 17:38:19 +0100 | [diff] [blame] | 34 | } |
| 35 | |
| 36 | static void inc_week(struct tm *tm) |
| 37 | { |
| 38 | time_t t = timegm(tm); |
| 39 | t += WEEK_SECS; |
Lukas Fleischer | 53bc747 | 2013-03-03 16:04:29 +0100 | [diff] [blame] | 40 | gmtime_r(&t, tm); |
Lars Hjemli | f86a23f | 2008-12-06 17:38:19 +0100 | [diff] [blame] | 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); |
Jason A. Donenfeld | bdae1d8 | 2013-03-03 23:21:33 -0500 | [diff] [blame^] | 86 | while (tm->tm_mon % 3 != 0) |
Lars Hjemli | f86a23f | 2008-12-06 17:38:19 +0100 | [diff] [blame] | 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 | |
Lars Hjemli | fb2f3f6 | 2008-12-07 13:17:21 +0100 | [diff] [blame] | 130 | struct cgit_period periods[] = { |
Lars Hjemli | f86a23f | 2008-12-06 17:38:19 +0100 | [diff] [blame] | 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 | |
Lars Hjemli | fb2f3f6 | 2008-12-07 13:17:21 +0100 | [diff] [blame] | 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, 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]; |
Lukas Fleischer | 53bc747 | 2013-03-03 16:04:29 +0100 | [diff] [blame] | 156 | return i + 1; |
Lars Hjemli | fb2f3f6 | 2008-12-07 13:17:21 +0100 | [diff] [blame] | 157 | } |
| 158 | return 0; |
| 159 | } |
| 160 | |
Lars Hjemli | b47b7bd | 2009-08-24 11:02:48 +0200 | [diff] [blame] | 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 | |
Lars Hjemli | f86a23f | 2008-12-06 17:38:19 +0100 | [diff] [blame] | 169 | static void add_commit(struct string_list *authors, struct commit *commit, |
Lars Hjemli | fb2f3f6 | 2008-12-07 13:17:21 +0100 | [diff] [blame] | 170 | struct cgit_period *period) |
Lars Hjemli | f86a23f | 2008-12-06 17:38:19 +0100 | [diff] [blame] | 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 | |
| 180 | info = cgit_parse_commit(commit); |
| 181 | tmp = xstrdup(info->author); |
Lars Hjemli | 6d7552b | 2010-08-22 13:29:57 +0200 | [diff] [blame] | 182 | author = string_list_insert(authors, tmp); |
Lars Hjemli | f86a23f | 2008-12-06 17:38:19 +0100 | [diff] [blame] | 183 | if (!author->util) |
| 184 | author->util = xcalloc(1, sizeof(struct authorstat)); |
| 185 | else |
| 186 | free(tmp); |
| 187 | authorstat = author->util; |
| 188 | items = &authorstat->list; |
| 189 | t = info->committer_date; |
| 190 | date = gmtime(&t); |
| 191 | period->trunc(date); |
| 192 | tmp = xstrdup(period->pretty(date)); |
Lars Hjemli | 6d7552b | 2010-08-22 13:29:57 +0200 | [diff] [blame] | 193 | item = string_list_insert(items, tmp); |
Lars Hjemli | f86a23f | 2008-12-06 17:38:19 +0100 | [diff] [blame] | 194 | if (item->util) |
| 195 | free(tmp); |
| 196 | item->util++; |
| 197 | authorstat->total++; |
| 198 | cgit_free_commitinfo(info); |
| 199 | } |
| 200 | |
| 201 | static int cmp_total_commits(const void *a1, const void *a2) |
| 202 | { |
| 203 | const struct string_list_item *i1 = a1; |
| 204 | const struct string_list_item *i2 = a2; |
| 205 | const struct authorstat *auth1 = i1->util; |
| 206 | const struct authorstat *auth2 = i2->util; |
| 207 | |
| 208 | return auth2->total - auth1->total; |
| 209 | } |
| 210 | |
| 211 | /* Walk the commit DAG and collect number of commits per author per |
| 212 | * timeperiod into a nested string_list collection. |
| 213 | */ |
| 214 | struct string_list collect_stats(struct cgit_context *ctx, |
Lars Hjemli | fb2f3f6 | 2008-12-07 13:17:21 +0100 | [diff] [blame] | 215 | struct cgit_period *period) |
Lars Hjemli | f86a23f | 2008-12-06 17:38:19 +0100 | [diff] [blame] | 216 | { |
| 217 | struct string_list authors; |
| 218 | struct rev_info rev; |
| 219 | struct commit *commit; |
Lars Hjemli | c6a6aa2 | 2008-12-07 11:45:28 +0100 | [diff] [blame] | 220 | const char *argv[] = {NULL, ctx->qry.head, NULL, NULL, NULL, NULL}; |
| 221 | int argc = 3; |
Lars Hjemli | f86a23f | 2008-12-06 17:38:19 +0100 | [diff] [blame] | 222 | time_t now; |
| 223 | long i; |
| 224 | struct tm *tm; |
| 225 | char tmp[11]; |
| 226 | |
| 227 | time(&now); |
| 228 | tm = gmtime(&now); |
| 229 | period->trunc(tm); |
| 230 | for (i = 1; i < period->count; i++) |
| 231 | period->dec(tm); |
| 232 | strftime(tmp, sizeof(tmp), "%Y-%m-%d", tm); |
| 233 | argv[2] = xstrdup(fmt("--since=%s", tmp)); |
Lars Hjemli | c6a6aa2 | 2008-12-07 11:45:28 +0100 | [diff] [blame] | 234 | if (ctx->qry.path) { |
| 235 | argv[3] = "--"; |
| 236 | argv[4] = ctx->qry.path; |
| 237 | argc += 2; |
| 238 | } |
Lars Hjemli | f86a23f | 2008-12-06 17:38:19 +0100 | [diff] [blame] | 239 | init_revisions(&rev, NULL); |
| 240 | rev.abbrev = DEFAULT_ABBREV; |
| 241 | rev.commit_format = CMIT_FMT_DEFAULT; |
John Keeping | bfc14d0 | 2013-03-02 12:32:10 +0000 | [diff] [blame] | 242 | rev.max_parents = 1; |
Lars Hjemli | f86a23f | 2008-12-06 17:38:19 +0100 | [diff] [blame] | 243 | rev.verbose_header = 1; |
| 244 | rev.show_root_diff = 0; |
Lars Hjemli | c6a6aa2 | 2008-12-07 11:45:28 +0100 | [diff] [blame] | 245 | setup_revisions(argc, argv, &rev, NULL); |
Lars Hjemli | f86a23f | 2008-12-06 17:38:19 +0100 | [diff] [blame] | 246 | prepare_revision_walk(&rev); |
| 247 | memset(&authors, 0, sizeof(authors)); |
| 248 | while ((commit = get_revision(&rev)) != NULL) { |
| 249 | add_commit(&authors, commit, period); |
| 250 | free(commit->buffer); |
| 251 | free_commit_list(commit->parents); |
| 252 | } |
| 253 | return authors; |
| 254 | } |
| 255 | |
| 256 | void print_combined_authorrow(struct string_list *authors, int from, int to, |
| 257 | const char *name, const char *leftclass, const char *centerclass, |
Lars Hjemli | fb2f3f6 | 2008-12-07 13:17:21 +0100 | [diff] [blame] | 258 | const char *rightclass, struct cgit_period *period) |
Lars Hjemli | f86a23f | 2008-12-06 17:38:19 +0100 | [diff] [blame] | 259 | { |
| 260 | struct string_list_item *author; |
| 261 | struct authorstat *authorstat; |
| 262 | struct string_list *items; |
| 263 | struct string_list_item *date; |
| 264 | time_t now; |
| 265 | long i, j, total, subtotal; |
| 266 | struct tm *tm; |
| 267 | char *tmp; |
| 268 | |
| 269 | time(&now); |
| 270 | tm = gmtime(&now); |
| 271 | period->trunc(tm); |
| 272 | for (i = 1; i < period->count; i++) |
| 273 | period->dec(tm); |
| 274 | |
| 275 | total = 0; |
| 276 | htmlf("<tr><td class='%s'>%s</td>", leftclass, |
| 277 | fmt(name, to - from + 1)); |
| 278 | for (j = 0; j < period->count; j++) { |
| 279 | tmp = period->pretty(tm); |
| 280 | period->inc(tm); |
| 281 | subtotal = 0; |
| 282 | for (i = from; i <= to; i++) { |
| 283 | author = &authors->items[i]; |
| 284 | authorstat = author->util; |
| 285 | items = &authorstat->list; |
Lars Hjemli | 6d7552b | 2010-08-22 13:29:57 +0200 | [diff] [blame] | 286 | date = string_list_lookup(items, tmp); |
Lars Hjemli | f86a23f | 2008-12-06 17:38:19 +0100 | [diff] [blame] | 287 | if (date) |
| 288 | subtotal += (size_t)date->util; |
| 289 | } |
Mark Lodato | e4ddc8f | 2010-09-04 11:30:18 -0400 | [diff] [blame] | 290 | htmlf("<td class='%s'>%ld</td>", centerclass, subtotal); |
Lars Hjemli | f86a23f | 2008-12-06 17:38:19 +0100 | [diff] [blame] | 291 | total += subtotal; |
| 292 | } |
Mark Lodato | e4ddc8f | 2010-09-04 11:30:18 -0400 | [diff] [blame] | 293 | htmlf("<td class='%s'>%ld</td></tr>", rightclass, total); |
Lars Hjemli | f86a23f | 2008-12-06 17:38:19 +0100 | [diff] [blame] | 294 | } |
| 295 | |
Lars Hjemli | fb2f3f6 | 2008-12-07 13:17:21 +0100 | [diff] [blame] | 296 | void print_authors(struct string_list *authors, int top, |
| 297 | struct cgit_period *period) |
Lars Hjemli | f86a23f | 2008-12-06 17:38:19 +0100 | [diff] [blame] | 298 | { |
| 299 | struct string_list_item *author; |
| 300 | struct authorstat *authorstat; |
| 301 | struct string_list *items; |
| 302 | struct string_list_item *date; |
| 303 | time_t now; |
| 304 | long i, j, total; |
| 305 | struct tm *tm; |
| 306 | char *tmp; |
| 307 | |
| 308 | time(&now); |
| 309 | tm = gmtime(&now); |
| 310 | period->trunc(tm); |
| 311 | for (i = 1; i < period->count; i++) |
| 312 | period->dec(tm); |
| 313 | |
| 314 | html("<table class='stats'><tr><th>Author</th>"); |
| 315 | for (j = 0; j < period->count; j++) { |
| 316 | tmp = period->pretty(tm); |
| 317 | htmlf("<th>%s</th>", tmp); |
| 318 | period->inc(tm); |
| 319 | } |
| 320 | html("<th>Total</th></tr>\n"); |
| 321 | |
| 322 | if (top <= 0 || top > authors->nr) |
| 323 | top = authors->nr; |
| 324 | |
| 325 | for (i = 0; i < top; i++) { |
| 326 | author = &authors->items[i]; |
| 327 | html("<tr><td class='left'>"); |
| 328 | html_txt(author->string); |
| 329 | html("</td>"); |
| 330 | authorstat = author->util; |
| 331 | items = &authorstat->list; |
| 332 | total = 0; |
| 333 | for (j = 0; j < period->count; j++) |
| 334 | period->dec(tm); |
| 335 | for (j = 0; j < period->count; j++) { |
| 336 | tmp = period->pretty(tm); |
| 337 | period->inc(tm); |
Lars Hjemli | 6d7552b | 2010-08-22 13:29:57 +0200 | [diff] [blame] | 338 | date = string_list_lookup(items, tmp); |
Lars Hjemli | f86a23f | 2008-12-06 17:38:19 +0100 | [diff] [blame] | 339 | if (!date) |
| 340 | html("<td>0</td>"); |
| 341 | else { |
Mark Lodato | e4ddc8f | 2010-09-04 11:30:18 -0400 | [diff] [blame] | 342 | htmlf("<td>"SZ_FMT"</td>", (size_t)date->util); |
Lars Hjemli | f86a23f | 2008-12-06 17:38:19 +0100 | [diff] [blame] | 343 | total += (size_t)date->util; |
| 344 | } |
| 345 | } |
Mark Lodato | e4ddc8f | 2010-09-04 11:30:18 -0400 | [diff] [blame] | 346 | htmlf("<td class='sum'>%ld</td></tr>", total); |
Lars Hjemli | f86a23f | 2008-12-06 17:38:19 +0100 | [diff] [blame] | 347 | } |
| 348 | |
| 349 | if (top < authors->nr) |
| 350 | print_combined_authorrow(authors, top, authors->nr - 1, |
Mark Lodato | e4ddc8f | 2010-09-04 11:30:18 -0400 | [diff] [blame] | 351 | "Others (%ld)", "left", "", "sum", period); |
Lars Hjemli | f86a23f | 2008-12-06 17:38:19 +0100 | [diff] [blame] | 352 | |
| 353 | print_combined_authorrow(authors, 0, authors->nr - 1, "Total", |
| 354 | "total", "sum", "sum", period); |
| 355 | html("</table>"); |
| 356 | } |
| 357 | |
| 358 | /* Create a sorted string_list with one entry per author. The util-field |
| 359 | * for each author is another string_list which is used to calculate the |
| 360 | * number of commits per time-interval. |
| 361 | */ |
| 362 | void cgit_show_stats(struct cgit_context *ctx) |
| 363 | { |
| 364 | struct string_list authors; |
Lars Hjemli | fb2f3f6 | 2008-12-07 13:17:21 +0100 | [diff] [blame] | 365 | struct cgit_period *period; |
Lars Hjemli | f86a23f | 2008-12-06 17:38:19 +0100 | [diff] [blame] | 366 | int top, i; |
Lars Hjemli | fb2f3f6 | 2008-12-07 13:17:21 +0100 | [diff] [blame] | 367 | const char *code = "w"; |
Lars Hjemli | f86a23f | 2008-12-06 17:38:19 +0100 | [diff] [blame] | 368 | |
Lars Hjemli | fb2f3f6 | 2008-12-07 13:17:21 +0100 | [diff] [blame] | 369 | if (ctx->qry.period) |
| 370 | code = ctx->qry.period; |
| 371 | |
| 372 | i = cgit_find_stats_period(code, &period); |
| 373 | if (!i) { |
Mark Lodato | e4ddc8f | 2010-09-04 11:30:18 -0400 | [diff] [blame] | 374 | cgit_print_error(fmt("Unknown statistics type: %c", code[0])); |
Lars Hjemli | fb2f3f6 | 2008-12-07 13:17:21 +0100 | [diff] [blame] | 375 | return; |
| 376 | } |
| 377 | if (i > ctx->repo->max_stats) { |
| 378 | cgit_print_error(fmt("Statistics type disabled: %s", |
| 379 | period->name)); |
| 380 | return; |
Lars Hjemli | f86a23f | 2008-12-06 17:38:19 +0100 | [diff] [blame] | 381 | } |
| 382 | authors = collect_stats(ctx, period); |
| 383 | qsort(authors.items, authors.nr, sizeof(struct string_list_item), |
| 384 | cmp_total_commits); |
| 385 | |
| 386 | top = ctx->qry.ofs; |
| 387 | if (!top) |
| 388 | top = 10; |
Lars Hjemli | 1b5c336 | 2011-03-07 00:00:24 +0100 | [diff] [blame] | 389 | |
| 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>"); |
| 402 | } |
| 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); |
Lars Hjemli | 9acd1cf | 2011-05-30 22:21:22 +0000 | [diff] [blame] | 410 | html("</select></td></tr>"); |
Lars Hjemli | 1b5c336 | 2011-03-07 00:00:24 +0100 | [diff] [blame] | 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>"); |
Lars Hjemli | c6a6aa2 | 2008-12-07 11:45:28 +0100 | [diff] [blame] | 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("')"); |
| 421 | } |
| 422 | html("</h2>"); |
Lars Hjemli | f86a23f | 2008-12-06 17:38:19 +0100 | [diff] [blame] | 423 | print_authors(&authors, top, period); |
| 424 | } |
| 425 | |