]>
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 const char *cgit_find_stats_periodname(int idx
)
159 if (idx
> 0 && idx
< 4)
160 return periods
[idx
- 1].name
;
165 static void add_commit(struct string_list
*authors
, struct commit
*commit
,
166 struct cgit_period
*period
)
168 struct commitinfo
*info
;
169 struct string_list_item
*author
, *item
;
170 struct authorstat
*authorstat
;
171 struct string_list
*items
;
176 info
= cgit_parse_commit(commit
);
177 tmp
= xstrdup(info
->author
);
178 author
= string_list_insert(tmp
, authors
);
180 author
->util
= xcalloc(1, sizeof(struct authorstat
));
183 authorstat
= author
->util
;
184 items
= &authorstat
->list
;
185 t
= info
->committer_date
;
188 tmp
= xstrdup(period
->pretty(date
));
189 item
= string_list_insert(tmp
, items
);
194 cgit_free_commitinfo(info
);
197 static int cmp_total_commits(const void *a1
, const void *a2
)
199 const struct string_list_item
*i1
= a1
;
200 const struct string_list_item
*i2
= a2
;
201 const struct authorstat
*auth1
= i1
->util
;
202 const struct authorstat
*auth2
= i2
->util
;
204 return auth2
->total
- auth1
->total
;
207 /* Walk the commit DAG and collect number of commits per author per
208 * timeperiod into a nested string_list collection.
210 struct string_list
collect_stats(struct cgit_context
*ctx
,
211 struct cgit_period
*period
)
213 struct string_list authors
;
215 struct commit
*commit
;
216 const char *argv
[] = {NULL
, ctx
->qry
.head
, NULL
, NULL
, NULL
, NULL
};
226 for (i
= 1; i
< period
->count
; i
++)
228 strftime(tmp
, sizeof(tmp
), "%Y-%m-%d", tm
);
229 argv
[2] = xstrdup(fmt("--since=%s", tmp
));
232 argv
[4] = ctx
->qry
.path
;
235 init_revisions(&rev
, NULL
);
236 rev
.abbrev
= DEFAULT_ABBREV
;
237 rev
.commit_format
= CMIT_FMT_DEFAULT
;
239 rev
.verbose_header
= 1;
240 rev
.show_root_diff
= 0;
241 setup_revisions(argc
, argv
, &rev
, NULL
);
242 prepare_revision_walk(&rev
);
243 memset(&authors
, 0, sizeof(authors
));
244 while ((commit
= get_revision(&rev
)) != NULL
) {
245 add_commit(&authors
, commit
, period
);
246 free(commit
->buffer
);
247 free_commit_list(commit
->parents
);
252 void print_combined_authorrow(struct string_list
*authors
, int from
, int to
,
253 const char *name
, const char *leftclass
, const char *centerclass
,
254 const char *rightclass
, struct cgit_period
*period
)
256 struct string_list_item
*author
;
257 struct authorstat
*authorstat
;
258 struct string_list
*items
;
259 struct string_list_item
*date
;
261 long i
, j
, total
, subtotal
;
268 for (i
= 1; i
< period
->count
; i
++)
272 htmlf("<tr><td class='%s'>%s</td>", leftclass
,
273 fmt(name
, to
- from
+ 1));
274 for (j
= 0; j
< period
->count
; j
++) {
275 tmp
= period
->pretty(tm
);
278 for (i
= from
; i
<= to
; i
++) {
279 author
= &authors
->items
[i
];
280 authorstat
= author
->util
;
281 items
= &authorstat
->list
;
282 date
= string_list_lookup(tmp
, items
);
284 subtotal
+= (size_t)date
->util
;
286 htmlf("<td class='%s'>%d</td>", centerclass
, subtotal
);
289 htmlf("<td class='%s'>%d</td></tr>", rightclass
, total
);
292 void print_authors(struct string_list
*authors
, int top
,
293 struct cgit_period
*period
)
295 struct string_list_item
*author
;
296 struct authorstat
*authorstat
;
297 struct string_list
*items
;
298 struct string_list_item
*date
;
307 for (i
= 1; i
< period
->count
; i
++)
310 html("<table class='stats'><tr><th>Author</th>");
311 for (j
= 0; j
< period
->count
; j
++) {
312 tmp
= period
->pretty(tm
);
313 htmlf("<th>%s</th>", tmp
);
316 html("<th>Total</th></tr>\n");
318 if (top
<= 0 || top
> authors
->nr
)
321 for (i
= 0; i
< top
; i
++) {
322 author
= &authors
->items
[i
];
323 html("<tr><td class='left'>");
324 html_txt(author
->string
);
326 authorstat
= author
->util
;
327 items
= &authorstat
->list
;
329 for (j
= 0; j
< period
->count
; j
++)
331 for (j
= 0; j
< period
->count
; j
++) {
332 tmp
= period
->pretty(tm
);
334 date
= string_list_lookup(tmp
, items
);
338 htmlf("<td>%d</td>", date
->util
);
339 total
+= (size_t)date
->util
;
342 htmlf("<td class='sum'>%d</td></tr>", total
);
345 if (top
< authors
->nr
)
346 print_combined_authorrow(authors
, top
, authors
->nr
- 1,
347 "Others (%d)", "left", "", "sum", period
);
349 print_combined_authorrow(authors
, 0, authors
->nr
- 1, "Total",
350 "total", "sum", "sum", period
);
354 /* Create a sorted string_list with one entry per author. The util-field
355 * for each author is another string_list which is used to calculate the
356 * number of commits per time-interval.
358 void cgit_show_stats(struct cgit_context
*ctx
)
360 struct string_list authors
;
361 struct cgit_period
*period
;
363 const char *code
= "w";
366 code
= ctx
->qry
.period
;
368 i
= cgit_find_stats_period(code
, &period
);
370 cgit_print_error(fmt("Unknown statistics type: %c", code
));
373 if (i
> ctx
->repo
->max_stats
) {
374 cgit_print_error(fmt("Statistics type disabled: %s",
378 authors
= collect_stats(ctx
, period
);
379 qsort(authors
.items
, authors
.nr
, sizeof(struct string_list_item
),
385 htmlf("<h2>Commits per author per %s", period
->name
);
388 html_txt(ctx
->qry
.path
);
393 html("<form method='get' action='' style='float: right; text-align: right;'>");
394 cgit_add_hidden_formfields(1, 0, "stats");
395 if (ctx
->repo
->max_stats
> 1) {
397 html("<select name='period' onchange='this.form.submit();'>");
398 for (i
= 0; i
< ctx
->repo
->max_stats
; i
++)
399 htmlf("<option value='%c'%s>%s</option>",
401 period
== &periods
[i
] ? " selected" : "",
403 html("</select><br/><br/>");
407 html("<select name='ofs' onchange='this.form.submit();'>");
408 htmlf("<option value='10'%s>10</option>", top
== 10 ? " selected" : "");
409 htmlf("<option value='25'%s>25</option>", top
== 25 ? " selected" : "");
410 htmlf("<option value='50'%s>50</option>", top
== 50 ? " selected" : "");
411 htmlf("<option value='100'%s>100</option>", top
== 100 ? " selected" : "");
412 htmlf("<option value='-1'%s>All</option>", top
== -1 ? " selected" : "");
414 html("<noscript> <input type='submit' value='Reload'/></noscript>");
416 print_authors(&authors
, top
, period
);