ppa stats graph
[ppastats.git] / src / chart.c
diff --git a/src/chart.c b/src/chart.c
new file mode 100644 (file)
index 0000000..f11feb8
--- /dev/null
@@ -0,0 +1,117 @@
+/*
+    Copyright (C) 2011 jeanfi@gmail.com
+
+    This program is free software; you can redistribute it and/or modify
+    it under the terms of the GNU General Public License as published by
+    the Free Software Foundation; either version 2 of the License, or
+    (at your option) any later version.
+
+    This program is distributed in the hope that it will be useful,
+    but WITHOUT ANY WARRANTY; without even the implied warranty of
+    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+    GNU General Public License for more details.
+
+    You should have received a copy of the GNU General Public License
+    along with this program; if not, write to the Free Software
+    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+    02110-1301 USA
+*/
+
+#include <stdio.h>
+#include <stdlib.h>
+
+#include <json/json.h>
+
+#include "chart.h"
+
+#define HTML_TEMPLATE \
+"<html>\n\
+  <head>\n\
+    <script type='text/javascript'\
+           src='https://www.google.com/jsapi'></script>\n\
+    <script type='text/javascript'>\n\
+      google.load('visualization', '1', {packages:['corechart']});\
+      google.setOnLoadCallback(drawChart);\n\
+      function drawChart() {\n\
+       var data = new google.visualization.DataTable();\n\
+       data.addColumn('string', 'Date');\n\
+        data.addColumn('number', 'Daily Download');\n\
+       data.addRows(%s);\n\
+       data.sort(0);\n\
+       var chart = new google.visualization.LineChart\
+               (document.getElementById('chart_div'));\n\
+       chart.draw(data, {width: 1024, height: 768,\
+                         title: 'PPA Statistics'});\n\
+      }\n\
+    </script>\n\
+  </head>\n\
+\n\
+  <body>\n\
+    <div id='chart_div'></div>\n\
+  </body>\n\
+</html>\n"
+
+char *tm_to_str(struct tm *date)
+{
+       char *str = malloc(10+1);
+
+       sprintf(str, "%d-%02d-%02d",
+               date->tm_year+1900,
+               date->tm_mon+1,
+               date->tm_mday);
+       
+       return str;
+}
+
+void
+generate_chart(const char *path,
+              const char *title,
+              const char *name,
+              struct daily_download_total **totals)
+{
+       FILE *f;
+       struct daily_download_total **cur;
+       struct daily_download_total *total;
+       char *str_date;
+       json_object *arr, *item;
+
+       if (!totals) {
+               fprintf(stderr, "ERROR: no totals\n");
+               return ;
+       }
+
+       f = fopen(path, "w");
+
+       if (!f) {
+               fprintf(stderr, "ERROR: failed to open: %s\n", path);
+               return ;
+       }
+
+       arr = json_object_new_array();
+       cur = totals;
+       while(*cur) {
+               total = *cur;
+
+               str_date = tm_to_str(&(total->date));
+
+               item = json_object_new_array();
+
+               json_object_array_add(item, json_object_new_string(str_date));
+               json_object_array_add(item,
+                                     json_object_new_int(total->count));
+
+               json_object_array_add(arr, item);
+
+               free(str_date);
+               
+               cur++;
+       }
+
+       fprintf(f,
+               HTML_TEMPLATE,
+               json_object_to_json_string(arr));
+
+       json_object_put(arr);
+
+       fclose(f);
+}