X-Git-Url: https://gitweb.ps.run/ps-cgit/blobdiff_plain/c6a6aa2186daf39814baa0e71378c2e9e1041002..e34a3b5adc00255f8acb7d674e5fdadef2ac80f7:/ui-stats.c diff --git a/ui-stats.c b/ui-stats.c index 3cc8d70..bdaf9cc 100644 --- a/ui-stats.c +++ b/ui-stats.c @@ -1,26 +1,12 @@ +#include + #include "cgit.h" #include "html.h" -#include +#include "ui-shared.h" +#include "ui-stats.h" #define MONTHS 6 -struct Period { - const char code; - const char *name; - int max_periods; - int count; - - /* Convert a tm value to the first day in the period */ - void (*trunc)(struct tm *tm); - - /* Update tm value to start of next/previous period */ - void (*dec)(struct tm *tm); - void (*inc)(struct tm *tm); - - /* Pretty-print a tm value */ - char *(*pretty)(struct tm *tm); -}; - struct authorstat { long total; struct string_list list; @@ -137,15 +123,47 @@ static char *pretty_year(struct tm *tm) return fmt("%d", tm->tm_year + 1900); } -struct Period periods[] = { +struct cgit_period periods[] = { {'w', "week", 12, 4, trunc_week, dec_week, inc_week, pretty_week}, {'m', "month", 12, 4, trunc_month, dec_month, inc_month, pretty_month}, {'q', "quarter", 12, 4, trunc_quarter, dec_quarter, inc_quarter, pretty_quarter}, {'y', "year", 12, 4, trunc_year, dec_year, inc_year, pretty_year}, }; +/* Given a period code or name, return a period index (1, 2, 3 or 4) + * and update the period pointer to the correcsponding struct. + * If no matching code is found, return 0. + */ +int cgit_find_stats_period(const char *expr, struct cgit_period **period) +{ + int i; + char code = '\0'; + + if (!expr) + return 0; + + if (strlen(expr) == 1) + code = expr[0]; + + for (i = 0; i < sizeof(periods) / sizeof(periods[0]); i++) + if (periods[i].code == code || !strcmp(periods[i].name, expr)) { + if (period) + *period = &periods[i]; + return i+1; + } + return 0; +} + +const char *cgit_find_stats_periodname(int idx) +{ + if (idx > 0 && idx < 4) + return periods[idx - 1].name; + else + return ""; +} + static void add_commit(struct string_list *authors, struct commit *commit, - struct Period *period) + struct cgit_period *period) { struct commitinfo *info; struct string_list_item *author, *item; @@ -190,7 +208,7 @@ static int cmp_total_commits(const void *a1, const void *a2) * timeperiod into a nested string_list collection. */ struct string_list collect_stats(struct cgit_context *ctx, - struct Period *period) + struct cgit_period *period) { struct string_list authors; struct rev_info rev; @@ -233,7 +251,7 @@ struct string_list collect_stats(struct cgit_context *ctx, void print_combined_authorrow(struct string_list *authors, int from, int to, const char *name, const char *leftclass, const char *centerclass, - const char *rightclass, struct Period *period) + const char *rightclass, struct cgit_period *period) { struct string_list_item *author; struct authorstat *authorstat; @@ -271,7 +289,8 @@ void print_combined_authorrow(struct string_list *authors, int from, int to, htmlf("%d", rightclass, total); } -void print_authors(struct string_list *authors, int top, struct Period *period) +void print_authors(struct string_list *authors, int top, + struct cgit_period *period) { struct string_list_item *author; struct authorstat *authorstat; @@ -339,16 +358,22 @@ void print_authors(struct string_list *authors, int top, struct Period *period) void cgit_show_stats(struct cgit_context *ctx) { struct string_list authors; - struct Period *period; + struct cgit_period *period; int top, i; + const char *code = "w"; - period = &periods[0]; - if (ctx->qry.period) { - for (i = 0; i < sizeof(periods) / sizeof(periods[0]); i++) - if (periods[i].code == ctx->qry.period[0]) { - period = &periods[i]; - break; - } + if (ctx->qry.period) + code = ctx->qry.period; + + i = cgit_find_stats_period(code, &period); + if (!i) { + cgit_print_error(fmt("Unknown statistics type: %c", code)); + return; + } + if (i > ctx->repo->max_stats) { + cgit_print_error(fmt("Statistics type disabled: %s", + period->name)); + return; } authors = collect_stats(ctx, period); qsort(authors.items, authors.nr, sizeof(struct string_list_item), @@ -365,17 +390,18 @@ void cgit_show_stats(struct cgit_context *ctx) } html(""); - html("
"); - if (strcmp(ctx->qry.head, ctx->repo->defbranch)) - htmlf("", ctx->qry.head); - html("Period: "); - html("

"); + html(""); + cgit_add_hidden_formfields(1, 0, "stats"); + if (ctx->repo->max_stats > 1) { + html("Period: "); + html("

"); + } html("Authors: "); html(""); html("