]>
git.cameronkatri.com Git - cgit.git/blob - ui-stats.c
16 struct string_list list
;
19 #define DAY_SECS (60 * 60 * 24)
20 #define WEEK_SECS (DAY_SECS * 7)
22 static void trunc_week(struct tm
*tm
)
24 time_t t
= timegm(tm
);
25 t
-= ((tm
->tm_wday
+ 6) % 7) * DAY_SECS
;
29 static void dec_week(struct tm
*tm
)
31 time_t t
= timegm(tm
);
36 static void inc_week(struct tm
*tm
)
38 time_t t
= timegm(tm
);
43 static char *pretty_week(struct tm
*tm
)
47 strftime(buf
, sizeof(buf
), "W%V %G", tm
);
51 static void trunc_month(struct tm
*tm
)
56 static void dec_month(struct tm
*tm
)
65 static void inc_month(struct tm
*tm
)
68 if (tm
->tm_mon
> 11) {
74 static char *pretty_month(struct tm
*tm
)
76 static const char *months
[] = {
77 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
78 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
80 return fmt("%s %d", months
[tm
->tm_mon
], tm
->tm_year
+ 1900);
83 static void trunc_quarter(struct tm
*tm
)
86 while (tm
->tm_mon
% 3 != 0)
90 static void dec_quarter(struct tm
*tm
)
97 static void inc_quarter(struct tm
*tm
)
104 static char *pretty_quarter(struct tm
*tm
)
106 return fmt("Q%d %d", tm
->tm_mon
/ 3 + 1, tm
->tm_year
+ 1900);
109 static void trunc_year(struct tm
*tm
)
115 static void dec_year(struct tm
*tm
)
120 static void inc_year(struct tm
*tm
)
125 static char *pretty_year(struct tm
*tm
)
127 return fmt("%d", tm
->tm_year
+ 1900);
130 struct cgit_period periods
[] = {
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
},
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.
141 int cgit_find_stats_period(const char *expr
, struct cgit_period
**period
)
149 if (strlen(expr
) == 1)
152 for (i
= 0; i
< sizeof(periods
) / sizeof(periods
[0]); i
++)
153 if (periods
[i
].code
== code
|| !strcmp(periods
[i
].name
, expr
)) {
155 *period
= &periods
[i
];
161 const char *cgit_find_stats_periodname(int idx
)
163 if (idx
> 0 && idx
< 4)
164 return periods
[idx
- 1].name
;
169 static void add_commit(struct string_list
*authors
, struct commit
*commit
,
170 struct cgit_period
*period
)
172 struct commitinfo
*info
;
173 struct string_list_item
*author
, *item
;
174 struct authorstat
*authorstat
;
175 struct string_list
*items
;
180 info
= cgit_parse_commit(commit
);
181 tmp
= xstrdup(info
->author
);
182 author
= string_list_insert(authors
, tmp
);
184 author
->util
= xcalloc(1, sizeof(struct authorstat
));
187 authorstat
= author
->util
;
188 items
= &authorstat
->list
;
189 t
= info
->committer_date
;
192 tmp
= xstrdup(period
->pretty(date
));
193 item
= string_list_insert(items
, tmp
);
198 cgit_free_commitinfo(info
);
201 static int cmp_total_commits(const void *a1
, const void *a2
)
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
;
208 return auth2
->total
- auth1
->total
;
211 /* Walk the commit DAG and collect number of commits per author per
212 * timeperiod into a nested string_list collection.
214 static struct string_list
collect_stats(struct cgit_context
*ctx
,
215 struct cgit_period
*period
)
217 struct string_list authors
;
219 struct commit
*commit
;
220 const char *argv
[] = {NULL
, ctx
->qry
.head
, NULL
, NULL
, NULL
, NULL
};
230 for (i
= 1; i
< period
->count
; i
++)
232 strftime(tmp
, sizeof(tmp
), "%Y-%m-%d", tm
);
233 argv
[2] = xstrdup(fmt("--since=%s", tmp
));
236 argv
[4] = ctx
->qry
.path
;
239 init_revisions(&rev
, NULL
);
240 rev
.abbrev
= DEFAULT_ABBREV
;
241 rev
.commit_format
= CMIT_FMT_DEFAULT
;
243 rev
.verbose_header
= 1;
244 rev
.show_root_diff
= 0;
245 setup_revisions(argc
, argv
, &rev
, NULL
);
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
);
256 static void print_combined_authorrow(struct string_list
*authors
, int from
,
257 int to
, const char *name
,
258 const char *leftclass
,
259 const char *centerclass
,
260 const char *rightclass
,
261 struct cgit_period
*period
)
263 struct string_list_item
*author
;
264 struct authorstat
*authorstat
;
265 struct string_list
*items
;
266 struct string_list_item
*date
;
268 long i
, j
, total
, subtotal
;
275 for (i
= 1; i
< period
->count
; i
++)
279 htmlf("<tr><td class='%s'>%s</td>", leftclass
,
280 fmt(name
, to
- from
+ 1));
281 for (j
= 0; j
< period
->count
; j
++) {
282 tmp
= period
->pretty(tm
);
285 for (i
= from
; i
<= to
; i
++) {
286 author
= &authors
->items
[i
];
287 authorstat
= author
->util
;
288 items
= &authorstat
->list
;
289 date
= string_list_lookup(items
, tmp
);
291 subtotal
+= (size_t)date
->util
;
293 htmlf("<td class='%s'>%ld</td>", centerclass
, subtotal
);
296 htmlf("<td class='%s'>%ld</td></tr>", rightclass
, total
);
299 static void print_authors(struct string_list
*authors
, int top
,
300 struct cgit_period
*period
)
302 struct string_list_item
*author
;
303 struct authorstat
*authorstat
;
304 struct string_list
*items
;
305 struct string_list_item
*date
;
314 for (i
= 1; i
< period
->count
; i
++)
317 html("<table class='stats'><tr><th>Author</th>");
318 for (j
= 0; j
< period
->count
; j
++) {
319 tmp
= period
->pretty(tm
);
320 htmlf("<th>%s</th>", tmp
);
323 html("<th>Total</th></tr>\n");
325 if (top
<= 0 || top
> authors
->nr
)
328 for (i
= 0; i
< top
; i
++) {
329 author
= &authors
->items
[i
];
330 html("<tr><td class='left'>");
331 html_txt(author
->string
);
333 authorstat
= author
->util
;
334 items
= &authorstat
->list
;
336 for (j
= 0; j
< period
->count
; j
++)
338 for (j
= 0; j
< period
->count
; j
++) {
339 tmp
= period
->pretty(tm
);
341 date
= string_list_lookup(items
, tmp
);
345 htmlf("<td>"SZ_FMT
"</td>", (size_t)date
->util
);
346 total
+= (size_t)date
->util
;
349 htmlf("<td class='sum'>%ld</td></tr>", total
);
352 if (top
< authors
->nr
)
353 print_combined_authorrow(authors
, top
, authors
->nr
- 1,
354 "Others (%ld)", "left", "", "sum", period
);
356 print_combined_authorrow(authors
, 0, authors
->nr
- 1, "Total",
357 "total", "sum", "sum", period
);
361 /* Create a sorted string_list with one entry per author. The util-field
362 * for each author is another string_list which is used to calculate the
363 * number of commits per time-interval.
365 void cgit_show_stats(struct cgit_context
*ctx
)
367 struct string_list authors
;
368 struct cgit_period
*period
;
370 const char *code
= "w";
373 code
= ctx
->qry
.period
;
375 i
= cgit_find_stats_period(code
, &period
);
377 cgit_print_error("Unknown statistics type: %c", code
[0]);
380 if (i
> ctx
->repo
->max_stats
) {
381 cgit_print_error("Statistics type disabled: %s", period
->name
);
384 authors
= collect_stats(ctx
, period
);
385 qsort(authors
.items
, authors
.nr
, sizeof(struct string_list_item
),
392 html("<div class='cgit-panel'>");
393 html("<b>stat options</b>");
394 html("<form method='get' action=''>");
395 cgit_add_hidden_formfields(1, 0, "stats");
396 html("<table><tr><td colspan='2'/></tr>");
397 if (ctx
->repo
->max_stats
> 1) {
398 html("<tr><td class='label'>Period:</td>");
399 html("<td class='ctrl'><select name='period' onchange='this.form.submit();'>");
400 for (i
= 0; i
< ctx
->repo
->max_stats
; i
++)
401 html_option(fmt("%c", periods
[i
].code
),
402 periods
[i
].name
, fmt("%c", period
->code
));
403 html("</select></td></tr>");
405 html("<tr><td class='label'>Authors:</td>");
406 html("<td class='ctrl'><select name='ofs' onchange='this.form.submit();'>");
407 html_intoption(10, "10", top
);
408 html_intoption(25, "25", top
);
409 html_intoption(50, "50", top
);
410 html_intoption(100, "100", top
);
411 html_intoption(-1, "all", top
);
412 html("</select></td></tr>");
413 html("<tr><td/><td class='ctrl'>");
414 html("<noscript><input type='submit' value='Reload'/></noscript>");
415 html("</td></tr></table>");
418 htmlf("<h2>Commits per author per %s", period
->name
);
421 html_txt(ctx
->qry
.path
);
425 print_authors(&authors
, top
, period
);