]>
git.cameronkatri.com Git - cgit.git/blob - ui-stats.c
8 struct string_list list
;
11 #define DAY_SECS (60 * 60 * 24)
12 #define WEEK_SECS (DAY_SECS * 7)
14 static void trunc_week(struct tm
*tm
)
16 time_t t
= timegm(tm
);
17 t
-= ((tm
->tm_wday
+ 6) % 7) * DAY_SECS
;
21 static void dec_week(struct tm
*tm
)
23 time_t t
= timegm(tm
);
28 static void inc_week(struct tm
*tm
)
30 time_t t
= timegm(tm
);
35 static char *pretty_week(struct tm
*tm
)
39 strftime(buf
, sizeof(buf
), "W%V %G", tm
);
43 static void trunc_month(struct tm
*tm
)
48 static void dec_month(struct tm
*tm
)
57 static void inc_month(struct tm
*tm
)
60 if (tm
->tm_mon
> 11) {
66 static char *pretty_month(struct tm
*tm
)
68 static const char *months
[] = {
69 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
70 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
72 return fmt("%s %d", months
[tm
->tm_mon
], tm
->tm_year
+ 1900);
75 static void trunc_quarter(struct tm
*tm
)
78 while (tm
->tm_mon
% 3 != 0)
82 static void dec_quarter(struct tm
*tm
)
89 static void inc_quarter(struct tm
*tm
)
96 static char *pretty_quarter(struct tm
*tm
)
98 return fmt("Q%d %d", tm
->tm_mon
/ 3 + 1, tm
->tm_year
+ 1900);
101 static void trunc_year(struct tm
*tm
)
107 static void dec_year(struct tm
*tm
)
112 static void inc_year(struct tm
*tm
)
117 static char *pretty_year(struct tm
*tm
)
119 return fmt("%d", tm
->tm_year
+ 1900);
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
},
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.
133 int cgit_find_stats_period(const char *expr
, const struct cgit_period
**period
)
141 if (strlen(expr
) == 1)
144 for (i
= 0; i
< sizeof(periods
) / sizeof(periods
[0]); i
++)
145 if (periods
[i
].code
== code
|| !strcmp(periods
[i
].name
, expr
)) {
147 *period
= &periods
[i
];
153 const char *cgit_find_stats_periodname(int idx
)
155 if (idx
> 0 && idx
< 4)
156 return periods
[idx
- 1].name
;
161 static void add_commit(struct string_list
*authors
, struct commit
*commit
,
162 const struct cgit_period
*period
)
164 struct commitinfo
*info
;
165 struct string_list_item
*author
, *item
;
166 struct authorstat
*authorstat
;
167 struct string_list
*items
;
173 info
= cgit_parse_commit(commit
);
174 tmp
= xstrdup(info
->author
);
175 author
= string_list_insert(authors
, tmp
);
177 author
->util
= xcalloc(1, sizeof(struct authorstat
));
180 authorstat
= author
->util
;
181 items
= &authorstat
->list
;
182 t
= info
->committer_date
;
185 tmp
= xstrdup(period
->pretty(date
));
186 item
= string_list_insert(items
, tmp
);
187 counter
= (uintptr_t *)&item
->util
;
193 cgit_free_commitinfo(info
);
196 static int cmp_total_commits(const void *a1
, const void *a2
)
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
;
203 return auth2
->total
- auth1
->total
;
206 /* Walk the commit DAG and collect number of commits per author per
207 * timeperiod into a nested string_list collection.
209 static struct string_list
collect_stats(const struct cgit_period
*period
)
211 struct string_list authors
;
213 struct commit
*commit
;
214 const char *argv
[] = {NULL
, ctx
.qry
.head
, NULL
, NULL
, NULL
, NULL
};
224 for (i
= 1; i
< period
->count
; i
++)
226 strftime(tmp
, sizeof(tmp
), "%Y-%m-%d", tm
);
227 argv
[2] = xstrdup(fmt("--since=%s", tmp
));
230 argv
[4] = ctx
.qry
.path
;
233 init_revisions(&rev
, NULL
);
234 rev
.abbrev
= DEFAULT_ABBREV
;
235 rev
.commit_format
= CMIT_FMT_DEFAULT
;
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 free_commit_buffer(commit
);
245 free_commit_list(commit
->parents
);
246 commit
->parents
= NULL
;
251 static void print_combined_authorrow(struct string_list
*authors
, int from
,
252 int to
, const char *name
,
253 const char *leftclass
,
254 const char *centerclass
,
255 const char *rightclass
,
256 const struct cgit_period
*period
)
258 struct string_list_item
*author
;
259 struct authorstat
*authorstat
;
260 struct string_list
*items
;
261 struct string_list_item
*date
;
263 long i
, j
, total
, subtotal
;
270 for (i
= 1; i
< period
->count
; i
++)
274 htmlf("<tr><td class='%s'>%s</td>", leftclass
,
275 fmt(name
, to
- from
+ 1));
276 for (j
= 0; j
< period
->count
; j
++) {
277 tmp
= period
->pretty(tm
);
280 for (i
= from
; i
<= to
; i
++) {
281 author
= &authors
->items
[i
];
282 authorstat
= author
->util
;
283 items
= &authorstat
->list
;
284 date
= string_list_lookup(items
, tmp
);
286 subtotal
+= (uintptr_t)date
->util
;
288 htmlf("<td class='%s'>%ld</td>", centerclass
, subtotal
);
291 htmlf("<td class='%s'>%ld</td></tr>", rightclass
, total
);
294 static void print_authors(struct string_list
*authors
, int top
,
295 const struct cgit_period
*period
)
297 struct string_list_item
*author
;
298 struct authorstat
*authorstat
;
299 struct string_list
*items
;
300 struct string_list_item
*date
;
309 for (i
= 1; i
< period
->count
; i
++)
312 html("<table class='stats'><tr><th>Author</th>");
313 for (j
= 0; j
< period
->count
; j
++) {
314 tmp
= period
->pretty(tm
);
315 htmlf("<th>%s</th>", tmp
);
318 html("<th>Total</th></tr>\n");
320 if (top
<= 0 || top
> authors
->nr
)
323 for (i
= 0; i
< top
; i
++) {
324 author
= &authors
->items
[i
];
325 html("<tr><td class='left'>");
326 html_txt(author
->string
);
328 authorstat
= author
->util
;
329 items
= &authorstat
->list
;
331 for (j
= 0; j
< period
->count
; j
++)
333 for (j
= 0; j
< period
->count
; j
++) {
334 tmp
= period
->pretty(tm
);
336 date
= string_list_lookup(items
, tmp
);
340 htmlf("<td>%lu</td>", (uintptr_t)date
->util
);
341 total
+= (uintptr_t)date
->util
;
344 htmlf("<td class='sum'>%ld</td></tr>", total
);
347 if (top
< authors
->nr
)
348 print_combined_authorrow(authors
, top
, authors
->nr
- 1,
349 "Others (%ld)", "left", "", "sum", period
);
351 print_combined_authorrow(authors
, 0, authors
->nr
- 1, "Total",
352 "total", "sum", "sum", period
);
356 /* Create a sorted string_list with one entry per author. The util-field
357 * for each author is another string_list which is used to calculate the
358 * number of commits per time-interval.
360 void cgit_show_stats(void)
362 struct string_list authors
;
363 const struct cgit_period
*period
;
365 const char *code
= "w";
368 code
= ctx
.qry
.period
;
370 i
= cgit_find_stats_period(code
, &period
);
372 cgit_print_error_page(404, "Not found",
373 "Unknown statistics type: %c", code
[0]);
376 if (i
> ctx
.repo
->max_stats
) {
377 cgit_print_error_page(400, "Bad request",
378 "Statistics type disabled: %s", period
->name
);
381 authors
= collect_stats(period
);
382 qsort(authors
.items
, authors
.nr
, sizeof(struct string_list_item
),
389 cgit_print_layout_start();
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>");
416 htmlf("<h2>Commits per author per %s", period
->name
);
419 html_txt(ctx
.qry
.path
);
423 print_authors(&authors
, top
, period
);
424 cgit_print_layout_end();