]> gitweb.ps.run Git - ps-cgit/blobdiff - ui-stats.c
ui-repolist: Rename section-sort to repository-sort.
[ps-cgit] / ui-stats.c
index 3cc8d70e031b9ce571601598ba71d779a01ede9d..59f4c1e7cd31d7f758b78c3a4d35f3801041e680 100644 (file)
@@ -1,25 +1,15 @@
 #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);
+#ifdef NO_C99_FORMAT
+#define SZ_FMT "%u"
+#else
+#define SZ_FMT "%zu"
+#endif
 
-       /* Pretty-print a tm value */
-       char *(*pretty)(struct tm *tm);
-};
+#define MONTHS 6
 
 struct authorstat {
        long total;
@@ -137,15 +127,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;
@@ -157,7 +179,7 @@ static void add_commit(struct string_list *authors, struct commit *commit,
 
        info = cgit_parse_commit(commit);
        tmp = xstrdup(info->author);
-       author = string_list_insert(tmp, authors);
+       author = string_list_insert(authors, tmp);
        if (!author->util)
                author->util = xcalloc(1, sizeof(struct authorstat));
        else
@@ -168,7 +190,7 @@ static void add_commit(struct string_list *authors, struct commit *commit,
        date = gmtime(&t);
        period->trunc(date);
        tmp = xstrdup(period->pretty(date));
-       item = string_list_insert(tmp, items);
+       item = string_list_insert(items, tmp);
        if (item->util)
                free(tmp);
        item->util++;
@@ -190,7 +212,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 +255,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;
@@ -261,17 +283,18 @@ void print_combined_authorrow(struct string_list *authors, int from, int to,
                        author = &authors->items[i];
                        authorstat = author->util;
                        items = &authorstat->list;
-                       date = string_list_lookup(tmp, items);
+                       date = string_list_lookup(items, tmp);
                        if (date)
                                subtotal += (size_t)date->util;
                }
-               htmlf("<td class='%s'>%d</td>", centerclass, subtotal);
+               htmlf("<td class='%s'>%ld</td>", centerclass, subtotal);
                total += subtotal;
        }
-       htmlf("<td class='%s'>%d</td></tr>", rightclass, total);
+       htmlf("<td class='%s'>%ld</td></tr>", 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;
@@ -312,20 +335,20 @@ void print_authors(struct string_list *authors, int top, struct Period *period)
                for (j = 0; j < period->count; j++) {
                        tmp = period->pretty(tm);
                        period->inc(tm);
-                       date = string_list_lookup(tmp, items);
+                       date = string_list_lookup(items, tmp);
                        if (!date)
                                html("<td>0</td>");
                        else {
-                               htmlf("<td>%d</td>", date->util);
+                               htmlf("<td>"SZ_FMT"</td>", (size_t)date->util);
                                total += (size_t)date->util;
                        }
                }
-               htmlf("<td class='sum'>%d</td></tr>", total);
+               htmlf("<td class='sum'>%ld</td></tr>", total);
        }
 
        if (top < authors->nr)
                print_combined_authorrow(authors, top, authors->nr - 1,
-                       "Others (%d)", "left", "", "sum", period);
+                       "Others (%ld)", "left", "", "sum", period);
 
        print_combined_authorrow(authors, 0, authors->nr - 1, "Total",
                "total", "sum", "sum", period);
@@ -339,16 +362,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[0]));
+               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),
@@ -357,6 +386,33 @@ void cgit_show_stats(struct cgit_context *ctx)
        top = ctx->qry.ofs;
        if (!top)
                top = 10;
+
+       html("<div class='cgit-panel'>");
+       html("<b>stat options</b>");
+       html("<form method='get' action=''>");
+       cgit_add_hidden_formfields(1, 0, "stats");
+       html("<table><tr><td colspan='2'/></tr>");
+       if (ctx->repo->max_stats > 1) {
+               html("<tr><td class='label'>Period:</td>");
+               html("<td class='ctrl'><select name='period' onchange='this.form.submit();'>");
+               for (i = 0; i < ctx->repo->max_stats; i++)
+                       html_option(fmt("%c", periods[i].code),
+                                   periods[i].name, fmt("%c", period->code));
+               html("</select></td></tr>");
+       }
+       html("<tr><td class='label'>Authors:</td>");
+       html("<td class='ctrl'><select name='ofs' onchange='this.form.submit();'>");
+       html_intoption(10, "10", top);
+       html_intoption(25, "25", top);
+       html_intoption(50, "50", top);
+       html_intoption(100, "100", top);
+       html_intoption(-1, "all", top);
+       html("</select></td></tr>");
+       html("<tr><td/><td class='ctrl'>");
+       html("<noscript><input type='submit' value='Reload'/></noscript>");
+       html("</td></tr></table>");
+       html("</form>");
+       html("</div>");
        htmlf("<h2>Commits per author per %s", period->name);
        if (ctx->qry.path) {
                html(" (path '");
@@ -364,29 +420,6 @@ void cgit_show_stats(struct cgit_context *ctx)
                html("')");
        }
        html("</h2>");
-
-       html("<form method='get' action='.' style='float: right; text-align: right;'>");
-       if (strcmp(ctx->qry.head, ctx->repo->defbranch))
-               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/>");
-       html("Authors: ");
-       html("");
-       html("<select name='ofs' onchange='this.form.submit();'>");
-       htmlf("<option value='10'%s>10</option>", top == 10 ? " selected" : "");
-       htmlf("<option value='25'%s>25</option>", top == 25 ? " selected" : "");
-       htmlf("<option value='50'%s>50</option>", top == 50 ? " selected" : "");
-       htmlf("<option value='100'%s>100</option>", top == 100 ? " selected" : "");
-       htmlf("<option value='-1'%s>All</option>", top == -1 ? " selected" : "");
-       html("</select>");
-       html("<noscript>&nbsp;&nbsp;<input type='submit' value='Reload'/></noscript>");
-       html("</form>");
        print_authors(&authors, top, period);
 }