f11feb814a9762f26d65649c623beb21783ee067
[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
27 #define HTML_TEMPLATE \
28 "<html>\n\
29   <head>\n\
30     <script type='text/javascript'\
31             src='https://www.google.com/jsapi'></script>\n\
32     <script type='text/javascript'>\n\
33       google.load('visualization', '1', {packages:['corechart']});\
34       google.setOnLoadCallback(drawChart);\n\
35       function drawChart() {\n\
36         var data = new google.visualization.DataTable();\n\
37         data.addColumn('string', 'Date');\n\
38         data.addColumn('number', 'Daily Download');\n\
39         data.addRows(%s);\n\
40         data.sort(0);\n\
41         var chart = new google.visualization.LineChart\
42                 (document.getElementById('chart_div'));\n\
43         chart.draw(data, {width: 1024, height: 768,\
44                           title: 'PPA Statistics'});\n\
45       }\n\
46     </script>\n\
47   </head>\n\
48 \n\
49   <body>\n\
50     <div id='chart_div'></div>\n\
51   </body>\n\
52 </html>\n"
53
54 char *tm_to_str(struct tm *date)
55 {
56         char *str = malloc(10+1);
57
58         sprintf(str, "%d-%02d-%02d",
59                 date->tm_year+1900,
60                 date->tm_mon+1,
61                 date->tm_mday);
62         
63         return str;
64 }
65
66 void
67 generate_chart(const char *path,
68                const char *title,
69                const char *name,
70                struct daily_download_total **totals)
71 {
72         FILE *f;
73         struct daily_download_total **cur;
74         struct daily_download_total *total;
75         char *str_date;
76         json_object *arr, *item;
77
78         if (!totals) {
79                 fprintf(stderr, "ERROR: no totals\n");
80                 return ;
81         }
82
83         f = fopen(path, "w");
84
85         if (!f) {
86                 fprintf(stderr, "ERROR: failed to open: %s\n", path);
87                 return ;
88         }
89
90         arr = json_object_new_array();
91         cur = totals;
92         while(*cur) {
93                 total = *cur;
94
95                 str_date = tm_to_str(&(total->date));
96
97                 item = json_object_new_array();
98
99                 json_object_array_add(item, json_object_new_string(str_date));
100                 json_object_array_add(item,
101                                       json_object_new_int(total->count));
102
103                 json_object_array_add(arr, item);
104
105                 free(str_date);
106                 
107                 cur++;
108         }
109
110         fprintf(f,
111                 HTML_TEMPLATE,
112                 json_object_to_json_string(arr));
113
114         json_object_put(arr);
115
116         fclose(f);
117 }