-rw-r--r-- | cgit.c | 10 | ||||
-rw-r--r-- | cgit.h | 4 | ||||
-rw-r--r-- | cgitrc.5.txt | 23 | ||||
-rw-r--r-- | cmd.c | 3 | ||||
-rw-r--r-- | shared.c | 2 | ||||
-rw-r--r-- | ui-shared.c | 2 | ||||
-rw-r--r-- | ui-stats.c | 81 | ||||
-rw-r--r-- | ui-stats.h | 19 |
8 files changed, 95 insertions, 49 deletions
@@ -13,4 +13,5 @@ #include "html.h" #include "ui-shared.h" +#include "ui-stats.h" #include "scan-tree.h" @@ -55,6 +56,6 @@ void config_cb(const char *name, const char *value) else if (!strcmp(name, "enable-log-linecount")) ctx.cfg.enable_log_linecount = atoi(value); - else if (!strcmp(name, "enable-stats")) - ctx.cfg.enable_stats = atoi(value); + else if (!strcmp(name, "max-stats")) + ctx.cfg.max_stats = cgit_find_stats_period(value, NULL); else if (!strcmp(name, "cache-size")) ctx.cfg.cache_size = atoi(value); @@ -115,6 +116,6 @@ void config_cb(const char *name, const char *value) else if (ctx.repo && !strcmp(name, "repo.enable-log-linecount")) ctx.repo->enable_log_linecount = ctx.cfg.enable_log_linecount * atoi(value); - else if (ctx.repo && !strcmp(name, "repo.enable-stats")) - ctx.repo->enable_stats = ctx.cfg.enable_stats && atoi(value); + else if (ctx.repo && !strcmp(name, "repo.max-stats")) + ctx.repo->max_stats = cgit_find_stats_period(value, NULL); else if (ctx.repo && !strcmp(name, "repo.module-link")) ctx.repo->module_link= xstrdup(value); @@ -184,4 +185,5 @@ static void prepare_context(struct cgit_context *ctx) ctx->cfg.max_msg_len = 80; ctx->cfg.max_repodesc_len = 80; + ctx->cfg.max_stats = 0; ctx->cfg.module_link = "./?repo=%s&page=commit&id=%s"; ctx->cfg.renamelimit = -1; @@ -62,5 +62,5 @@ struct cgit_repo { int enable_log_filecount; int enable_log_linecount; - int enable_stats; + int max_stats; }; @@ -154,5 +154,4 @@ struct cgit_config { int enable_log_filecount; int enable_log_linecount; - int enable_stats; int local_time; int max_repo_count; @@ -161,4 +160,5 @@ struct cgit_config { int max_msg_len; int max_repodesc_len; + int max_stats; int nocache; int renamelimit; diff --git a/cgitrc.5.txt b/cgitrc.5.txt index 60d3ea4..0bbbea3 100644 --- a/cgitrc.5.txt +++ b/cgitrc.5.txt @@ -75,8 +75,4 @@ enable-log-linecount value: "0". -enable-stats - Globally enable/disable statistics for each repository. Default - value: "0". - favicon Url used as link to a shortcut icon for cgit. If specified, it is @@ -134,4 +130,9 @@ max-repodesc-length on the repository index page. Default value: "80". +max-stats + Set the default maximum statistics period. Valid values are "week", + "month", "quarter" and "year". If unspecified, statistics are + disabled. Default value: none. See also: "repo.max-stats". + module-link Text which will be used as the formatstring for a hyperlink when a @@ -223,7 +224,8 @@ repo.enable-log-linecount `enable-log-linecount'. Default value: none. -repo.enable-stats - A flag which can be used to disable the global setting - `enable-stats'. Default value: none. +repo.max-stats + Override the default maximum statistics period. Valid values are equal + to the values specified for the global "max-stats" setting. Default + value: none. repo.name @@ -285,4 +287,8 @@ logo=/img/mylogo.png +# Enable statistics per week, month and quarter +max-stats=quarter + + # Set the title and heading of the repository index page root-title=foobar.com git repositories @@ -357,4 +363,7 @@ repo.snapshots=0 repo.enable-log-linecount=0 +# Restrict the max statistics period for this repo +repo.max-stats=month + BUGS @@ -113,8 +113,5 @@ static void snapshot_fn(struct cgit_context *ctx) static void stats_fn(struct cgit_context *ctx) { - if (ctx->repo->enable_stats) cgit_show_stats(ctx); - else - cgit_print_error("Stats disabled for this repo"); } @@ -59,5 +59,5 @@ struct cgit_repo *cgit_add_repo(const char *url) ret->enable_log_filecount = ctx.cfg.enable_log_filecount; ret->enable_log_linecount = ctx.cfg.enable_log_linecount; - ret->enable_stats = ctx.cfg.enable_stats; + ret->max_stats = ctx.cfg.max_stats; ret->module_link = ctx.cfg.module_link; ret->readme = NULL; diff --git a/ui-shared.c b/ui-shared.c index 0e688a0..97b9d46 100644 --- a/ui-shared.c +++ b/ui-shared.c @@ -642,5 +642,5 @@ void cgit_print_pageheader(struct cgit_context *ctx) cgit_diff_link("diff", NULL, hc(cmd, "diff"), ctx->qry.head, ctx->qry.sha1, ctx->qry.sha2, NULL); - if (ctx->repo->enable_stats) + if (ctx->repo->max_stats) reporevlink("stats", "stats", NULL, hc(cmd, "stats"), ctx->qry.head, NULL, NULL); @@ -1,25 +1,11 @@ +#include <string-list.h> + #include "cgit.h" #include "html.h" -#include <string-list.h> +#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; @@ -138,5 +124,5 @@ static char *pretty_year(struct tm *tm) } -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}, @@ -145,6 +131,30 @@ struct Period periods[] = { }; +/* 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; +} + static void add_commit(struct string_list *authors, struct commit *commit, - struct Period *period) + struct cgit_period *period) { struct commitinfo *info; @@ -191,5 +201,5 @@ static int cmp_total_commits(const void *a1, const void *a2) */ struct string_list collect_stats(struct cgit_context *ctx, - struct Period *period) + struct cgit_period *period) { struct string_list authors; @@ -234,5 +244,5 @@ 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; @@ -272,5 +282,6 @@ void print_combined_authorrow(struct string_list *authors, int from, int to, } -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; @@ -340,14 +351,20 @@ 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); @@ -369,7 +386,8 @@ void cgit_show_stats(struct cgit_context *ctx) if (strcmp(ctx->qry.head, ctx->repo->defbranch)) htmlf("<input type='hidden' name='h' value='%s'/>", ctx->qry.head); + if (ctx->repo->max_stats > 1) { html("Period: "); html("<select name='period' onchange='this.form.submit();'>"); - for (i = 0; i < sizeof(periods) / sizeof(periods[0]); i++) + for (i = 0; i < ctx->repo->max_stats; i++) htmlf("<option value='%c'%s>%s</option>", periods[i].code, @@ -377,4 +395,5 @@ void cgit_show_stats(struct cgit_context *ctx) periods[i].name); html("</select><br/><br/>"); + } html("Authors: "); html(""); @@ -4,4 +4,23 @@ #include "cgit.h" +struct cgit_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); +}; + +extern int cgit_find_stats_period(const char *expr, struct cgit_period **period); + extern void cgit_show_stats(struct cgit_context *ctx); |