summaryrefslogtreecommitdiffabout
path: root/ui-stats.c
authorLars Hjemli <hjemli@gmail.com>2008-12-07 12:17:21 (UTC)
committer Lars Hjemli <hjemli@gmail.com>2008-12-07 12:17:21 (UTC)
commitfb2f3f6c29bad733723152893c5246a756e4cada (patch) (side-by-side diff)
tree5b2953c0c116f276ca48beee676a6662b6329d95 /ui-stats.c
parentc6a6aa2186daf39814baa0e71378c2e9e1041002 (diff)
downloadcgit-fb2f3f6c29bad733723152893c5246a756e4cada.zip
cgit-fb2f3f6c29bad733723152893c5246a756e4cada.tar.gz
cgit-fb2f3f6c29bad733723152893c5246a756e4cada.tar.bz2
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 <hjemli@gmail.com>
Diffstat (limited to 'ui-stats.c') (more/less context) (ignore whitespace changes)
-rw-r--r--ui-stats.c97
1 files changed, 58 insertions, 39 deletions
diff --git a/ui-stats.c b/ui-stats.c
index 3cc8d70..1104485 100644
--- a/ui-stats.c
+++ b/ui-stats.c
@@ -1,4 +1,7 @@
+#include <string-list.h>
+
#include "cgit.h"
#include "html.h"
-#include <string-list.h>
+#include "ui-shared.h"
+#include "ui-stats.h"
@@ -6,19 +9,2 @@
-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 {
@@ -139,3 +125,3 @@ 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},
@@ -146,4 +132,28 @@ 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)
{
@@ -192,3 +202,3 @@ 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)
{
@@ -235,3 +245,3 @@ 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)
{
@@ -273,3 +283,4 @@ 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)
{
@@ -341,12 +352,18 @@ 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;
}
@@ -370,10 +387,12 @@ void cgit_show_stats(struct cgit_context *ctx)
htmlf("<input type='hidden' name='h' value='%s'/>", ctx->qry.head);
- html("Period: ");
- html("<select name='period' onchange='this.form.submit();'>");
- for (i = 0; i < sizeof(periods) / sizeof(periods[0]); i++)
- htmlf("<option value='%c'%s>%s</option>",
- periods[i].code,
- period == &periods[i] ? " selected" : "",
- periods[i].name);
- html("</select><br/><br/>");
+ if (ctx->repo->max_stats > 1) {
+ html("Period: ");
+ html("<select name='period' onchange='this.form.submit();'>");
+ for (i = 0; i < ctx->repo->max_stats; i++)
+ htmlf("<option value='%c'%s>%s</option>",
+ periods[i].code,
+ period == &periods[i] ? " selected" : "",
+ periods[i].name);
+ html("</select><br/><br/>");
+ }
html("Authors: ");