From f86a23ff537258d36bf8f1876fa7a4bede6673d8 Mon Sep 17 00:00:00 2001 From: Lars Hjemli Date: Sat, 6 Dec 2008 17:38:19 +0100 Subject: Add a 'stats' page to each repo This new page, which is disabled by default, can be used to print some statistics about the number of commits per period in the repository, where period can be either weeks, months, quarters or years. The function can be activated globally by setting 'enable-stats=1' in cgitrc and disabled for individual repos by setting 'repo.enable-stats=0'. Signed-off-by: Lars Hjemli --- Makefile | 1 + cgit.c | 6 + cgit.css | 77 ++++++++++++ cgit.h | 3 + cgitrc.5.txt | 8 ++ cmd.c | 10 ++ shared.c | 1 + ui-shared.c | 3 + ui-stats.c | 380 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ ui-stats.h | 8 ++ 10 files changed, 497 insertions(+) create mode 100644 ui-stats.c create mode 100644 ui-stats.h diff --git a/Makefile b/Makefile index 561af76..f426f98 100644 --- a/Makefile +++ b/Makefile @@ -68,6 +68,7 @@ OBJECTS += ui-refs.o OBJECTS += ui-repolist.o OBJECTS += ui-shared.o OBJECTS += ui-snapshot.o +OBJECTS += ui-stats.o OBJECTS += ui-summary.o OBJECTS += ui-tag.o OBJECTS += ui-tree.o diff --git a/cgit.c b/cgit.c index c82587b..22b6d7c 100644 --- a/cgit.c +++ b/cgit.c @@ -54,6 +54,8 @@ void config_cb(const char *name, const char *value) ctx.cfg.enable_log_filecount = atoi(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, "cache-size")) ctx.cfg.cache_size = atoi(value); else if (!strcmp(name, "cache-root")) @@ -112,6 +114,8 @@ void config_cb(const char *name, const char *value) ctx.repo->enable_log_filecount = ctx.cfg.enable_log_filecount * atoi(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.module-link")) ctx.repo->module_link= xstrdup(value); else if (ctx.repo && !strcmp(name, "repo.readme") && value != NULL) { @@ -154,6 +158,8 @@ static void querystring_cb(const char *name, const char *value) ctx.qry.name = xstrdup(value); } else if (!strcmp(name, "mimetype")) { ctx.qry.mimetype = xstrdup(value); + } else if (!strcmp(name, "period")) { + ctx.qry.period = xstrdup(value); } } diff --git a/cgit.css b/cgit.css index a37d218..ef30fbf 100644 --- a/cgit.css +++ b/cgit.css @@ -456,3 +456,80 @@ div.footer { font-size: 80%; color: #ccc; } +table.stats { + border: solid 1px black; + border-collapse: collapse; +} + +table.stats th { + text-align: left; + padding: 1px 0.5em; + background-color: #eee; + border: solid 1px black; +} + +table.stats td { + text-align: right; + padding: 1px 0.5em; + border: solid 1px black; +} + +table.stats td.total { + font-weight: bold; + text-align: left; +} + +table.stats td.sum { + color: #c00; + font-weight: bold; +/* background-color: #eee; */ +} + +table.stats td.left { + text-align: left; +} + +table.vgraph { + border-collapse: separate; + border: solid 1px black; + height: 200px; +} + +table.vgraph th { + background-color: #eee; + font-weight: bold; + border: solid 1px white; + padding: 1px 0.5em; +} + +table.vgraph td { + vertical-align: bottom; + padding: 0px 10px; +} + +table.vgraph div.bar { + background-color: #eee; +} + +table.hgraph { + border: solid 1px black; + width: 800px; +} + +table.hgraph th { + background-color: #eee; + font-weight: bold; + border: solid 1px black; + padding: 1px 0.5em; +} + +table.hgraph td { + vertical-align: center; + padding: 2px 2px; +} + +table.hgraph div.bar { + background-color: #eee; + height: 1em; +} + diff --git a/cgit.h b/cgit.h index 91db98a..85045c4 100644 --- a/cgit.h +++ b/cgit.h @@ -61,6 +61,7 @@ struct cgit_repo { int snapshots; int enable_log_filecount; int enable_log_linecount; + int enable_stats; }; struct cgit_repolist { @@ -119,6 +120,7 @@ struct cgit_query { char *name; char *mimetype; char *url; + char *period; int ofs; int nohead; }; @@ -151,6 +153,7 @@ struct cgit_config { int enable_index_links; int enable_log_filecount; int enable_log_linecount; + int enable_stats; int local_time; int max_repo_count; int max_commit_count; diff --git a/cgitrc.5.txt b/cgitrc.5.txt index 7887b02..60d3ea4 100644 --- a/cgitrc.5.txt +++ b/cgitrc.5.txt @@ -74,6 +74,10 @@ enable-log-linecount and removed lines for each commit on the repository log page. Default 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 suggested to use the value "/favicon.ico" since certain browsers will @@ -218,6 +222,10 @@ repo.enable-log-linecount A flag which can be used to disable the global setting `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.name The value to show as repository name. Default value: . diff --git a/cmd.c b/cmd.c index 5b3c14c..744bf84 100644 --- a/cmd.c +++ b/cmd.c @@ -21,6 +21,7 @@ #include "ui-refs.h" #include "ui-repolist.h" #include "ui-snapshot.h" +#include "ui-stats.h" #include "ui-summary.h" #include "ui-tag.h" #include "ui-tree.h" @@ -109,6 +110,14 @@ static void snapshot_fn(struct cgit_context *ctx) ctx->repo->snapshots, ctx->qry.nohead); } +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"); +} + static void summary_fn(struct cgit_context *ctx) { cgit_print_summary(); @@ -145,6 +154,7 @@ struct cgit_cmd *cgit_get_cmd(struct cgit_context *ctx) def_cmd(refs, 1, 1), def_cmd(repolist, 0, 0), def_cmd(snapshot, 1, 0), + def_cmd(stats, 1, 1), def_cmd(summary, 1, 1), def_cmd(tag, 1, 1), def_cmd(tree, 1, 1), diff --git a/shared.c b/shared.c index f5875e4..37333f0 100644 --- a/shared.c +++ b/shared.c @@ -58,6 +58,7 @@ struct cgit_repo *cgit_add_repo(const char *url) ret->snapshots = ctx.cfg.snapshots; 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->module_link = ctx.cfg.module_link; ret->readme = NULL; return ret; diff --git a/ui-shared.c b/ui-shared.c index 224e5f3..0e688a0 100644 --- a/ui-shared.c +++ b/ui-shared.c @@ -641,6 +641,9 @@ void cgit_print_pageheader(struct cgit_context *ctx) ctx->qry.head, ctx->qry.sha1); cgit_diff_link("diff", NULL, hc(cmd, "diff"), ctx->qry.head, ctx->qry.sha1, ctx->qry.sha2, NULL); + if (ctx->repo->enable_stats) + reporevlink("stats", "stats", NULL, hc(cmd, "stats"), + ctx->qry.head, NULL, NULL); if (ctx->repo->readme) reporevlink("about", "about", NULL, hc(cmd, "about"), ctx->qry.head, NULL, diff --git a/ui-stats.c b/ui-stats.c new file mode 100644 index 0000000..9150840 --- /dev/null +++ b/ui-stats.c @@ -0,0 +1,380 @@ +#include "cgit.h" +#include "html.h" +#include + +#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; +}; + +#define DAY_SECS (60 * 60 * 24) +#define WEEK_SECS (DAY_SECS * 7) + +static void trunc_week(struct tm *tm) +{ + time_t t = timegm(tm); + t -= ((tm->tm_wday + 6) % 7) * DAY_SECS; + gmtime_r(&t, tm); +} + +static void dec_week(struct tm *tm) +{ + time_t t = timegm(tm); + t -= WEEK_SECS; + gmtime_r(&t, tm); +} + +static void inc_week(struct tm *tm) +{ + time_t t = timegm(tm); + t += WEEK_SECS; + gmtime_r(&t, tm); +} + +static char *pretty_week(struct tm *tm) +{ + static char buf[10]; + + strftime(buf, sizeof(buf), "W%V %G", tm); + return buf; +} + +static void trunc_month(struct tm *tm) +{ + tm->tm_mday = 1; +} + +static void dec_month(struct tm *tm) +{ + tm->tm_mon--; + if (tm->tm_mon < 0) { + tm->tm_year--; + tm->tm_mon = 11; + } +} + +static void inc_month(struct tm *tm) +{ + tm->tm_mon++; + if (tm->tm_mon > 11) { + tm->tm_year++; + tm->tm_mon = 0; + } +} + +static char *pretty_month(struct tm *tm) +{ + static const char *months[] = { + "Jan", "Feb", "Mar", "Apr", "May", "Jun", + "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" + }; + return fmt("%s %d", months[tm->tm_mon], tm->tm_year + 1900); +} + +static void trunc_quarter(struct tm *tm) +{ + trunc_month(tm); + while(tm->tm_mon % 3 != 0) + dec_month(tm); +} + +static void dec_quarter(struct tm *tm) +{ + dec_month(tm); + dec_month(tm); + dec_month(tm); +} + +static void inc_quarter(struct tm *tm) +{ + inc_month(tm); + inc_month(tm); + inc_month(tm); +} + +static char *pretty_quarter(struct tm *tm) +{ + return fmt("Q%d %d", tm->tm_mon / 3 + 1, tm->tm_year + 1900); +} + +static void trunc_year(struct tm *tm) +{ + trunc_month(tm); + tm->tm_mon = 0; +} + +static void dec_year(struct tm *tm) +{ + tm->tm_year--; +} + +static void inc_year(struct tm *tm) +{ + tm->tm_year++; +} + +static char *pretty_year(struct tm *tm) +{ + return fmt("%d", tm->tm_year + 1900); +} + +struct 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}, +}; + +static void add_commit(struct string_list *authors, struct commit *commit, + struct Period *period) +{ + struct commitinfo *info; + struct string_list_item *author, *item; + struct authorstat *authorstat; + struct string_list *items; + char *tmp; + struct tm *date; + time_t t; + + info = cgit_parse_commit(commit); + tmp = xstrdup(info->author); + author = string_list_insert(tmp, authors); + if (!author->util) + author->util = xcalloc(1, sizeof(struct authorstat)); + else + free(tmp); + authorstat = author->util; + items = &authorstat->list; + t = info->committer_date; + date = gmtime(&t); + period->trunc(date); + tmp = xstrdup(period->pretty(date)); + item = string_list_insert(tmp, items); + if (item->util) + free(tmp); + item->util++; + authorstat->total++; + cgit_free_commitinfo(info); +} + +static int cmp_total_commits(const void *a1, const void *a2) +{ + const struct string_list_item *i1 = a1; + const struct string_list_item *i2 = a2; + const struct authorstat *auth1 = i1->util; + const struct authorstat *auth2 = i2->util; + + return auth2->total - auth1->total; +} + +/* Walk the commit DAG and collect number of commits per author per + * timeperiod into a nested string_list collection. + */ +struct string_list collect_stats(struct cgit_context *ctx, + struct Period *period) +{ + struct string_list authors; + struct rev_info rev; + struct commit *commit; + const char *argv[] = {NULL, ctx->qry.head, NULL, NULL}; + time_t now; + long i; + struct tm *tm; + char tmp[11]; + + time(&now); + tm = gmtime(&now); + period->trunc(tm); + for (i = 1; i < period->count; i++) + period->dec(tm); + strftime(tmp, sizeof(tmp), "%Y-%m-%d", tm); + argv[2] = xstrdup(fmt("--since=%s", tmp)); + init_revisions(&rev, NULL); + rev.abbrev = DEFAULT_ABBREV; + rev.commit_format = CMIT_FMT_DEFAULT; + rev.no_merges = 1; + rev.verbose_header = 1; + rev.show_root_diff = 0; + setup_revisions(3, argv, &rev, NULL); + prepare_revision_walk(&rev); + memset(&authors, 0, sizeof(authors)); + while ((commit = get_revision(&rev)) != NULL) { + add_commit(&authors, commit, period); + free(commit->buffer); + free_commit_list(commit->parents); + } + return authors; +} + +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) +{ + struct string_list_item *author; + struct authorstat *authorstat; + struct string_list *items; + struct string_list_item *date; + time_t now; + long i, j, total, subtotal; + struct tm *tm; + char *tmp; + + time(&now); + tm = gmtime(&now); + period->trunc(tm); + for (i = 1; i < period->count; i++) + period->dec(tm); + + total = 0; + htmlf("%s", leftclass, + fmt(name, to - from + 1)); + for (j = 0; j < period->count; j++) { + tmp = period->pretty(tm); + period->inc(tm); + subtotal = 0; + for (i = from; i <= to; i++) { + author = &authors->items[i]; + authorstat = author->util; + items = &authorstat->list; + date = string_list_lookup(tmp, items); + if (date) + subtotal += (size_t)date->util; + } + htmlf("%d", centerclass, subtotal); + total += subtotal; + } + htmlf("%d", rightclass, total); +} + +void print_authors(struct string_list *authors, int top, struct Period *period) +{ + struct string_list_item *author; + struct authorstat *authorstat; + struct string_list *items; + struct string_list_item *date; + time_t now; + long i, j, total; + struct tm *tm; + char *tmp; + + time(&now); + tm = gmtime(&now); + period->trunc(tm); + for (i = 1; i < period->count; i++) + period->dec(tm); + + html(""); + for (j = 0; j < period->count; j++) { + tmp = period->pretty(tm); + htmlf("", tmp); + period->inc(tm); + } + html("\n"); + + if (top <= 0 || top > authors->nr) + top = authors->nr; + + for (i = 0; i < top; i++) { + author = &authors->items[i]; + html(""); + authorstat = author->util; + items = &authorstat->list; + total = 0; + for (j = 0; j < period->count; j++) + period->dec(tm); + for (j = 0; j < period->count; j++) { + tmp = period->pretty(tm); + period->inc(tm); + date = string_list_lookup(tmp, items); + if (!date) + html(""); + else { + htmlf("", date->util); + total += (size_t)date->util; + } + } + htmlf("", total); + } + + if (top < authors->nr) + print_combined_authorrow(authors, top, authors->nr - 1, + "Others (%d)", "left", "", "sum", period); + + print_combined_authorrow(authors, 0, authors->nr - 1, "Total", + "total", "sum", "sum", period); + html("
Author%sTotal
"); + html_txt(author->string); + html("0%d%d
"); +} + +/* Create a sorted string_list with one entry per author. The util-field + * for each author is another string_list which is used to calculate the + * number of commits per time-interval. + */ +void cgit_show_stats(struct cgit_context *ctx) +{ + struct string_list authors; + struct Period *period; + int top, i; + + 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; + } + } + authors = collect_stats(ctx, period); + qsort(authors.items, authors.nr, sizeof(struct string_list_item), + cmp_total_commits); + + top = ctx->qry.ofs; + if (!top) + top = 10; + htmlf("

Commits per author per %s

", period->name); + + html("
"); + if (strcmp(ctx->qry.head, ctx->repo->defbranch)) + htmlf("", ctx->qry.head); + html("Period: "); + html("

"); + html("Authors: "); + html(""); + html(""); + html(""); + html("
"); + print_authors(&authors, top, period); +} + diff --git a/ui-stats.h b/ui-stats.h new file mode 100644 index 0000000..f1d744c --- /dev/null +++ b/ui-stats.h @@ -0,0 +1,8 @@ +#ifndef UI_STATS_H +#define UI_STATS_H + +#include "cgit.h" + +extern void cgit_show_stats(struct cgit_context *ctx); + +#endif /* UI_STATS_H */ -- cgit v1.2.3 From c6a6aa2186daf39814baa0e71378c2e9e1041002 Mon Sep 17 00:00:00 2001 From: Lars Hjemli Date: Sun, 7 Dec 2008 11:45:28 +0100 Subject: ui-stats: enable path-filtered stats When a path is specified on the querystring the commit statistics will now be filtered by this path. Signed-off-by: Lars Hjemli --- ui-stats.c | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/ui-stats.c b/ui-stats.c index 9150840..3cc8d70 100644 --- a/ui-stats.c +++ b/ui-stats.c @@ -195,7 +195,8 @@ struct string_list collect_stats(struct cgit_context *ctx, struct string_list authors; struct rev_info rev; struct commit *commit; - const char *argv[] = {NULL, ctx->qry.head, NULL, NULL}; + const char *argv[] = {NULL, ctx->qry.head, NULL, NULL, NULL, NULL}; + int argc = 3; time_t now; long i; struct tm *tm; @@ -208,13 +209,18 @@ struct string_list collect_stats(struct cgit_context *ctx, period->dec(tm); strftime(tmp, sizeof(tmp), "%Y-%m-%d", tm); argv[2] = xstrdup(fmt("--since=%s", tmp)); + if (ctx->qry.path) { + argv[3] = "--"; + argv[4] = ctx->qry.path; + argc += 2; + } init_revisions(&rev, NULL); rev.abbrev = DEFAULT_ABBREV; rev.commit_format = CMIT_FMT_DEFAULT; rev.no_merges = 1; rev.verbose_header = 1; rev.show_root_diff = 0; - setup_revisions(3, argv, &rev, NULL); + setup_revisions(argc, argv, &rev, NULL); prepare_revision_walk(&rev); memset(&authors, 0, sizeof(authors)); while ((commit = get_revision(&rev)) != NULL) { @@ -351,7 +357,13 @@ void cgit_show_stats(struct cgit_context *ctx) top = ctx->qry.ofs; if (!top) top = 10; - htmlf("

Commits per author per %s

", period->name); + htmlf("

Commits per author per %s", period->name); + if (ctx->qry.path) { + html(" (path '"); + html_txt(ctx->qry.path); + html("')"); + } + html("

"); html("
"); if (strcmp(ctx->qry.head, ctx->repo->defbranch)) -- cgit v1.2.3 From fb2f3f6c29bad733723152893c5246a756e4cada Mon Sep 17 00:00:00 2001 From: Lars Hjemli Date: Sun, 7 Dec 2008 13:17:21 +0100 Subject: ui-stats: replace 'enable-stats' setting with 'max-stats' The new 'max-stats' and 'repo.max-stats' settings makes it possible to define the maximum statistics period, both globally and per repo. Hence, it is now feasible to allow statistics on repositories with a high commit frequency, like linux-2.6, by setting repo.max-stats to e.g. 'month'. Signed-off-by: Lars Hjemli --- cgit.c | 10 ++++--- cgit.h | 4 +-- cgitrc.5.txt | 23 +++++++++----- cmd.c | 5 +--- shared.c | 2 +- ui-shared.c | 2 +- ui-stats.c | 97 ++++++++++++++++++++++++++++++++++++------------------------ ui-stats.h | 19 ++++++++++++ 8 files changed, 104 insertions(+), 58 deletions(-) diff --git a/cgit.c b/cgit.c index 22b6d7c..57e11cd 100644 --- a/cgit.c +++ b/cgit.c @@ -12,6 +12,7 @@ #include "configfile.h" #include "html.h" #include "ui-shared.h" +#include "ui-stats.h" #include "scan-tree.h" const char *cgit_version = CGIT_VERSION; @@ -54,8 +55,8 @@ void config_cb(const char *name, const char *value) ctx.cfg.enable_log_filecount = atoi(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); else if (!strcmp(name, "cache-root")) @@ -114,8 +115,8 @@ void config_cb(const char *name, const char *value) ctx.repo->enable_log_filecount = ctx.cfg.enable_log_filecount * atoi(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); else if (ctx.repo && !strcmp(name, "repo.readme") && value != NULL) { @@ -183,6 +184,7 @@ static void prepare_context(struct cgit_context *ctx) ctx->cfg.max_lock_attempts = 5; 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; ctx->cfg.robots = "index, nofollow"; diff --git a/cgit.h b/cgit.h index 85045c4..f2cb671 100644 --- a/cgit.h +++ b/cgit.h @@ -61,7 +61,7 @@ struct cgit_repo { int snapshots; int enable_log_filecount; int enable_log_linecount; - int enable_stats; + int max_stats; }; struct cgit_repolist { @@ -153,13 +153,13 @@ struct cgit_config { int enable_index_links; int enable_log_filecount; int enable_log_linecount; - int enable_stats; int local_time; int max_repo_count; int max_commit_count; int max_lock_attempts; int max_msg_len; int max_repodesc_len; + int max_stats; int nocache; int renamelimit; int snapshots; diff --git a/cgitrc.5.txt b/cgitrc.5.txt index 60d3ea4..0bbbea3 100644 --- a/cgitrc.5.txt +++ b/cgitrc.5.txt @@ -74,10 +74,6 @@ enable-log-linecount and removed lines for each commit on the repository log page. Default 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 suggested to use the value "/favicon.ico" since certain browsers will @@ -133,6 +129,11 @@ max-repodesc-length Specifies the maximum number of repo description characters to display 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 submodule is printed in a directory listing. The arguments for the @@ -222,9 +223,10 @@ repo.enable-log-linecount A flag which can be used to disable the global setting `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 The value to show as repository name. Default value: . @@ -284,6 +286,10 @@ favicon=/favicon.ico 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 @@ -356,6 +362,9 @@ repo.snapshots=0 # Disable line-counts for this repo repo.enable-log-linecount=0 +# Restrict the max statistics period for this repo +repo.max-stats=month + BUGS ---- diff --git a/cmd.c b/cmd.c index 744bf84..763a558 100644 --- a/cmd.c +++ b/cmd.c @@ -112,10 +112,7 @@ 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"); + cgit_show_stats(ctx); } static void summary_fn(struct cgit_context *ctx) diff --git a/shared.c b/shared.c index 37333f0..7382609 100644 --- a/shared.c +++ b/shared.c @@ -58,7 +58,7 @@ struct cgit_repo *cgit_add_repo(const char *url) ret->snapshots = ctx.cfg.snapshots; 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; return ret; diff --git a/ui-shared.c b/ui-shared.c index 0e688a0..97b9d46 100644 --- a/ui-shared.c +++ b/ui-shared.c @@ -641,7 +641,7 @@ void cgit_print_pageheader(struct cgit_context *ctx) ctx->qry.head, ctx->qry.sha1); 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); if (ctx->repo->readme) diff --git a/ui-stats.c b/ui-stats.c index 3cc8d70..1104485 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,39 @@ 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; +} + 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 +200,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 +243,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 +281,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 +350,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), @@ -368,14 +385,16 @@ void cgit_show_stats(struct cgit_context *ctx) html(""); if (strcmp(ctx->qry.head, ctx->repo->defbranch)) htmlf("", ctx->qry.head); - html("Period: "); - html("

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

"); + } html("Authors: "); html(""); html("\n"); for_each_branch_ref(print_branch_option, ctx->qry.head); html(" "); @@ -660,7 +660,7 @@ void cgit_print_pageheader(struct cgit_context *ctx) html_url_path(cgit_fileurl(ctx->qry.repo, "log", ctx->qry.path, NULL)); html("'>\n"); - add_hidden_formfields(1, 0, "log"); + cgit_add_hidden_formfields(1, 0, "log"); html("", ctx->qry.head); + html(""); + cgit_add_hidden_formfields(1, 0, "stats"); if (ctx->repo->max_stats > 1) { html("Period: "); html("