import private svn
[ppastats.git] / src / html.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 <string.h>
21
22 #include "html.h"
23 #include "lp_ws.h"
24
25 #include <json/json.h>
26
27 #include <stdio.h>
28 #include <stdlib.h>
29
30 #define HTML_TEMPLATE \
31 "<html>\n\
32   <head>\n\
33     <script type='text/javascript' src='https://www.google.com/jsapi'></script>\n\
34     <script type='text/javascript'>\n\
35       google.load('visualization', '1', {packages:['corechart']});\
36       google.setOnLoadCallback(drawChart);\n\
37       function drawChart() {\n\
38         var data = new google.visualization.DataTable();\n\
39         data.addColumn('string', 'Date');\n\
40 %s\
41         data.addRows(%s);\n\
42         data.sort(0);\n\
43         var chart = new google.visualization.LineChart(document.getElementById('chart_div'));\n\
44         chart.draw(data, {width: 1024, height: 768, 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 static json_object *
55 get_count_array(json_object *data, const char *date)
56 {
57         int n, i;
58         json_object *o, *o_date;
59         const char *date2;
60
61         n = json_object_array_length(data);
62         for (i = 0; i < n; i++) {
63                 o = json_object_array_get_idx(data, i);
64                 o_date = json_object_array_get_idx(o, 0);
65                 date2 = json_object_get_string(o_date);
66
67                 if (!strcmp(date2, date))
68                         return o;
69         }
70
71         return NULL;
72 }
73
74 static json_object *count_array_new(json_object *data, const char *d, int n)
75 {
76         json_object *result = json_object_new_array();
77         int i;
78
79         json_object_array_add(result, json_object_new_string(d));
80
81         for (i = 0; i < n; i++)
82                 json_object_array_add(result, json_object_new_int(0));
83
84         json_object_array_add(data, result);
85
86         return result;
87 }
88
89 static char *
90 build_package_display_name(struct binary_package_publishing_history *b)
91 {
92         char *str = malloc(strlen(b->binary_package_name)+
93                            1+
94                            strlen(b->binary_package_version)+
95                            1);
96
97         sprintf(str, "%s-%s",
98                 b->binary_package_name, b->binary_package_version);
99
100         return str;
101 }
102
103 static char *
104 build_addcolums_str(struct binary_package_publishing_history **list)
105 {
106         struct binary_package_publishing_history **cur = list;
107         struct binary_package_publishing_history *b;
108         char *str, *tmpstr;
109         char *dname;
110
111         str = malloc(1);
112         *str = '\0';
113         const char *pattern = "%s        data.addColumn('number', '%s');\n";
114
115         while (*cur) {
116                 b = *cur;
117                 dname = build_package_display_name(b);
118
119                 tmpstr = malloc(strlen(str)+strlen(pattern)+strlen(dname)+1);
120                 sprintf(tmpstr, pattern, str, dname);
121
122                 free(str);
123                 free(dname);
124
125                 str = tmpstr;
126
127                 cur++;
128         }
129
130         return str;
131 }
132
133 static char *
134 build_data_str(struct binary_package_publishing_history **list)
135 {
136         int n, i;
137         json_object *data;
138         char *str;
139
140         n = list_length((void **)list);
141
142         data = json_object_new_array();
143         for (i = 0; i < n; i++) {
144                 struct binary_package_publishing_history *binary;
145                 struct distro_arch_series *dist;
146                 struct daily_download_total **totals;
147                 struct daily_download_total **t_cur;
148
149                 binary = list[i];
150                 dist = get_distro_arch_series(binary->distro_arch_series_link);
151
152                 totals = get_daily_download_totals(binary->self_link);
153
154                 t_cur = totals;
155                 while (*t_cur) {
156                         char *d = malloc(10+1);
157                         json_object *data2;
158
159                         sprintf(d, "%d-%02d-%02d",
160                                 (*t_cur)->date.tm_year+1900,
161                                 (*t_cur)->date.tm_mon+1,
162                                 (*t_cur)->date.tm_mday);
163
164                         data2 = get_count_array(data, d);
165
166                         if (!data2)
167                                 data2 = count_array_new(data, d, n);
168
169                         json_object_array_put_idx
170                                 (data2,
171                                  i+1, json_object_new_int((*t_cur)->count));
172
173                         free(d);
174                         t_cur++;
175                 }
176
177                 daily_download_total_list_free(totals);
178                 distro_arch_series_free(dist);
179         }
180
181         str = strdup(json_object_to_json_string(data));
182
183         json_object_put(data);
184
185         return str;
186 }
187
188 void ppa_to_html(const char *owner, const char *ppa)
189 {
190         struct binary_package_publishing_history **list;
191         char *archive_url = get_archive_url(owner, ppa);
192         char *addcolumns_str;
193         char *data_str;
194
195         list = get_binary_package_publishing_history_list(archive_url);
196         free(archive_url);
197
198         if (!list) {
199                 fprintf(stderr, "Failed to retrieve PPA information\n");
200                 exit(EXIT_FAILURE);
201         }
202
203         addcolumns_str = build_addcolums_str(list);
204         data_str = build_data_str(list);
205
206         printf(HTML_TEMPLATE, addcolumns_str, data_str);
207
208         free(addcolumns_str);
209         free(data_str);
210         binary_package_publishing_history_list_free(list);
211 }