(no commit message)
[ppastats.git] / src / chart.c
1 /*
2     Copyright (C) 2011 jeanfi@gmail.com
3
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (at your option) any later version.
8
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13
14     You should have received a copy of the GNU General Public License
15     along with this program; if not, write to the Free Software
16     Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17     02110-1301 USA
18 */
19
20 #include <stdio.h>
21 #include <stdlib.h>
22
23 #include <json/json.h>
24
25 #include "chart.h"
26 #include "ppastats.h"
27
28 #define HTML_TEMPLATE \
29 "<html>\n\
30   <head>\n\
31     <script type='text/javascript'\
32             src='https://www.google.com/jsapi'></script>\n\
33     <script type='text/javascript'>\n\
34       google.load('visualization', '1', {packages:['corechart']});\
35       google.setOnLoadCallback(drawChart);\n\
36       function drawChart() {\n\
37         var data = new google.visualization.DataTable();\n\
38         data.addColumn('string', 'Date');\n\
39         data.addColumn('number', 'Daily Download');\n\
40         data.addRows(%s);\n\
41         data.sort(0);\n\
42         var chart = new google.visualization.LineChart\
43                 (document.getElementById('chart_div'));\n\
44         chart.draw(data, {width: 1024, height: 768,\
45                           title: 'PPA Statistics'});\n\
46       }\n\
47     </script>\n\
48   </head>\n\
49 \n\
50   <body>\n\
51     <div id='chart_div'></div>\n\
52   </body>\n\
53 </html>\n"
54
55 char *tm_to_str(struct tm *date)
56 {
57         char *str = malloc(10+1);
58
59         sprintf(str, "%d-%02d-%02d",
60                 date->tm_year+1900,
61                 date->tm_mon+1,
62                 date->tm_mday);
63
64         return str;
65 }
66
67 void
68 generate_chart(const char *path,
69                const char *title,
70                const char *name,
71                struct daily_download_total **totals)
72 {
73         FILE *f;
74         struct daily_download_total **cur;
75         struct daily_download_total *total;
76         char *str_date;
77         json_object *arr, *item;
78
79         if (debug)
80                 fprintf(stderr, "Generates %s\n", path);
81
82         if (!totals) {
83                 fprintf(stderr, "ERROR: no totals\n");
84                 return ;
85         }
86
87         f = fopen(path, "w");
88
89         if (!f) {
90                 fprintf(stderr, "ERROR: failed to open: %s\n", path);
91                 return ;
92         }
93
94         arr = json_object_new_array();
95         cur = totals;
96         while (*cur) {
97                 total = *cur;
98
99                 str_date = tm_to_str(&(total->date));
100
101                 item = json_object_new_array();
102
103                 json_object_array_add(item, json_object_new_string(str_date));
104                 json_object_array_add(item,
105                                       json_object_new_int(total->count));
106
107                 json_object_array_add(arr, item);
108
109                 free(str_date);
110
111                 cur++;
112         }
113
114         fprintf(f,
115                 HTML_TEMPLATE,
116                 json_object_to_json_string(arr));
117
118         json_object_put(arr);
119
120         fclose(f);
121 }