]>
git.cameronkatri.com Git - cgit.git/blob - ui-stats.c
1 #include <string-list.h>
12 struct string_list list
;
15 #define DAY_SECS (60 * 60 * 24)
16 #define WEEK_SECS (DAY_SECS * 7)
18 static void trunc_week(struct tm
*tm
)
20 time_t t
= timegm(tm
);
21 t
-= ((tm
->tm_wday
+ 6) % 7) * DAY_SECS
;
25 static void dec_week(struct tm
*tm
)
27 time_t t
= timegm(tm
);
32 static void inc_week(struct tm
*tm
)
34 time_t t
= timegm(tm
);
39 static char *pretty_week(struct tm
*tm
)
43 strftime(buf
, sizeof(buf
), "W%V %G", tm
);
47 static void trunc_month(struct tm
*tm
)
52 static void dec_month(struct tm
*tm
)
61 static void inc_month(struct tm
*tm
)
64 if (tm
->tm_mon
> 11) {
70 static char *pretty_month(struct tm
*tm
)
72 static const char *months
[] = {
73 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
74 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
76 return fmt("%s %d", months
[tm
->tm_mon
], tm
->tm_year
+ 1900);
79 static void trunc_quarter(struct tm
*tm
)
82 while(tm
->tm_mon
% 3 != 0)
86 static void dec_quarter(struct tm
*tm
)
93 static void inc_quarter(struct tm
*tm
)
100 static char *pretty_quarter(struct tm
*tm
)
102 return fmt("Q%d %d", tm
->tm_mon
/ 3 + 1, tm
->tm_year
+ 1900);
105 static void trunc_year(struct tm
*tm
)
111 static void dec_year(struct tm
*tm
)
116 static void inc_year(struct tm
*tm
)
121 static char *pretty_year(struct tm
*tm
)
123 return fmt("%d", tm
->tm_year
+ 1900);
126 struct cgit_period periods
[] = {
127 {'w', "week", 12, 4, trunc_week
, dec_week
, inc_week
, pretty_week
},
128 {'m', "month", 12, 4, trunc_month
, dec_month
, inc_month
, pretty_month
},
129 {'q', "quarter", 12, 4, trunc_quarter
, dec_quarter
, inc_quarter
, pretty_quarter
},
130 {'y', "year", 12, 4, trunc_year
, dec_year
, inc_year
, pretty_year
},
133 /* Given a period code or name, return a period index (1, 2, 3 or 4)
134 * and update the period pointer to the correcsponding struct.
135 * If no matching code is found, return 0.
137 int cgit_find_stats_period(const char *expr
, struct cgit_period
**period
)
145 if (strlen(expr
) == 1)
148 for (i
= 0; i
< sizeof(periods
) / sizeof(periods
[0]); i
++)
149 if (periods
[i
].code
== code
|| !strcmp(periods
[i
].name
, expr
)) {
151 *period
= &periods
[i
];
157 static void add_commit(struct string_list
*authors
, struct commit
*commit
,
158 struct cgit_period
*period
)
160 struct commitinfo
*info
;
161 struct string_list_item
*author
, *item
;
162 struct authorstat
*authorstat
;
163 struct string_list
*items
;
168 info
= cgit_parse_commit(commit
);
169 tmp
= xstrdup(info
->author
);
170 author
= string_list_insert(tmp
, authors
);
172 author
->util
= xcalloc(1, sizeof(struct authorstat
));
175 authorstat
= author
->util
;
176 items
= &authorstat
->list
;
177 t
= info
->committer_date
;
180 tmp
= xstrdup(period
->pretty(date
));
181 item
= string_list_insert(tmp
, items
);
186 cgit_free_commitinfo(info
);
189 static int cmp_total_commits(const void *a1
, const void *a2
)
191 const struct string_list_item
*i1
= a1
;
192 const struct string_list_item
*i2
= a2
;
193 const struct authorstat
*auth1
= i1
->util
;
194 const struct authorstat
*auth2
= i2
->util
;
196 return auth2
->total
- auth1
->total
;
199 /* Walk the commit DAG and collect number of commits per author per
200 * timeperiod into a nested string_list collection.
202 struct string_list
collect_stats(struct cgit_context
*ctx
,
203 struct cgit_period
*period
)
205 struct string_list authors
;
207 struct commit
*commit
;
208 const char *argv
[] = {NULL
, ctx
->qry
.head
, NULL
, NULL
, NULL
, NULL
};
218 for (i
= 1; i
< period
->count
; i
++)
220 strftime(tmp
, sizeof(tmp
), "%Y-%m-%d", tm
);
221 argv
[2] = xstrdup(fmt("--since=%s", tmp
));
224 argv
[4] = ctx
->qry
.path
;
227 init_revisions(&rev
, NULL
);
228 rev
.abbrev
= DEFAULT_ABBREV
;
229 rev
.commit_format
= CMIT_FMT_DEFAULT
;
231 rev
.verbose_header
= 1;
232 rev
.show_root_diff
= 0;
233 setup_revisions(argc
, argv
, &rev
, NULL
);
234 prepare_revision_walk(&rev
);
235 memset(&authors
, 0, sizeof(authors
));
236 while ((commit
= get_revision(&rev
)) != NULL
) {
237 add_commit(&authors
, commit
, period
);
238 free(commit
->buffer
);
239 free_commit_list(commit
->parents
);
244 void print_combined_authorrow(struct string_list
*authors
, int from
, int to
,
245 const char *name
, const char *leftclass
, const char *centerclass
,
246 const char *rightclass
, struct cgit_period
*period
)
248 struct string_list_item
*author
;
249 struct authorstat
*authorstat
;
250 struct string_list
*items
;
251 struct string_list_item
*date
;
253 long i
, j
, total
, subtotal
;
260 for (i
= 1; i
< period
->count
; i
++)
264 htmlf("<tr><td class='%s'>%s</td>", leftclass
,
265 fmt(name
, to
- from
+ 1));
266 for (j
= 0; j
< period
->count
; j
++) {
267 tmp
= period
->pretty(tm
);
270 for (i
= from
; i
<= to
; i
++) {
271 author
= &authors
->items
[i
];
272 authorstat
= author
->util
;
273 items
= &authorstat
->list
;
274 date
= string_list_lookup(tmp
, items
);
276 subtotal
+= (size_t)date
->util
;
278 htmlf("<td class='%s'>%d</td>", centerclass
, subtotal
);
281 htmlf("<td class='%s'>%d</td></tr>", rightclass
, total
);
284 void print_authors(struct string_list
*authors
, int top
,
285 struct cgit_period
*period
)
287 struct string_list_item
*author
;
288 struct authorstat
*authorstat
;
289 struct string_list
*items
;
290 struct string_list_item
*date
;
299 for (i
= 1; i
< period
->count
; i
++)
302 html("<table class='stats'><tr><th>Author</th>");
303 for (j
= 0; j
< period
->count
; j
++) {
304 tmp
= period
->pretty(tm
);
305 htmlf("<th>%s</th>", tmp
);
308 html("<th>Total</th></tr>\n");
310 if (top
<= 0 || top
> authors
->nr
)
313 for (i
= 0; i
< top
; i
++) {
314 author
= &authors
->items
[i
];
315 html("<tr><td class='left'>");
316 html_txt(author
->string
);
318 authorstat
= author
->util
;
319 items
= &authorstat
->list
;
321 for (j
= 0; j
< period
->count
; j
++)
323 for (j
= 0; j
< period
->count
; j
++) {
324 tmp
= period
->pretty(tm
);
326 date
= string_list_lookup(tmp
, items
);
330 htmlf("<td>%d</td>", date
->util
);
331 total
+= (size_t)date
->util
;
334 htmlf("<td class='sum'>%d</td></tr>", total
);
337 if (top
< authors
->nr
)
338 print_combined_authorrow(authors
, top
, authors
->nr
- 1,
339 "Others (%d)", "left", "", "sum", period
);
341 print_combined_authorrow(authors
, 0, authors
->nr
- 1, "Total",
342 "total", "sum", "sum", period
);
346 /* Create a sorted string_list with one entry per author. The util-field
347 * for each author is another string_list which is used to calculate the
348 * number of commits per time-interval.
350 void cgit_show_stats(struct cgit_context
*ctx
)
352 struct string_list authors
;
353 struct cgit_period
*period
;
355 const char *code
= "w";
358 code
= ctx
->qry
.period
;
360 i
= cgit_find_stats_period(code
, &period
);
362 cgit_print_error(fmt("Unknown statistics type: %c", code
));
365 if (i
> ctx
->repo
->max_stats
) {
366 cgit_print_error(fmt("Statistics type disabled: %s",
370 authors
= collect_stats(ctx
, period
);
371 qsort(authors
.items
, authors
.nr
, sizeof(struct string_list_item
),
377 htmlf("<h2>Commits per author per %s", period
->name
);
380 html_txt(ctx
->qry
.path
);
385 html("<form method='get' action='' style='float: right; text-align: right;'>");
386 cgit_add_hidden_formfields(1, 0, "stats");
387 if (ctx
->repo
->max_stats
> 1) {
389 html("<select name='period' onchange='this.form.submit();'>");
390 for (i
= 0; i
< ctx
->repo
->max_stats
; i
++)
391 htmlf("<option value='%c'%s>%s</option>",
393 period
== &periods
[i
] ? " selected" : "",
395 html("</select><br/><br/>");
399 html("<select name='ofs' onchange='this.form.submit();'>");
400 htmlf("<option value='10'%s>10</option>", top
== 10 ? " selected" : "");
401 htmlf("<option value='25'%s>25</option>", top
== 25 ? " selected" : "");
402 htmlf("<option value='50'%s>50</option>", top
== 50 ? " selected" : "");
403 htmlf("<option value='100'%s>100</option>", top
== 100 ? " selected" : "");
404 htmlf("<option value='-1'%s>All</option>", top
== -1 ? " selected" : "");
406 html("<noscript> <input type='submit' value='Reload'/></noscript>");
408 print_authors(&authors
, top
, period
);