added support for ie<9
[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 <stdio.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include <sys/time.h>
24 #include <sys/stat.h>
25 #include <sys/types.h>
26
27 #include <json/json.h>
28
29 #include "html.h"
30 #include "io.h"
31 #include "lp.h"
32 #include "lp_ws.h"
33 #include "ppastats.h"
34
35 #define HTML_FOOTER \
36 " <div id=\"footer\">Generated by \
37 <a href='http://wpitchoune.net/ppastats'>ppastats</a></div>\n\
38   </body>\n\
39 </html>"
40
41 #define HTML_PKG_TEMPLATE \
42 "       <h1><span id=\"pkg_name\">N/A</span></h1>\n\
43         <div id=\"charts\">\n\
44                 <div id=\"chart\"></div>\n\
45                 <div id=\"chart_distro\"></div>\n\
46         </div>\n\
47         <div id=\"details\">\n\
48                 <em>PPA</em>: \n\
49                 <a href=\"index.html\">\n\
50                 <span id=\"ppa_owner\">N/A</span>/\n\
51                 <span id=\"ppa_name\">N/A</span>\n\
52                 </a>\n\
53 \n\
54                 <div id=\"distros\"><em>Distros:</em></div>\n\
55                 <div id=\"versions\"><em>Versions:</em></div>\n\
56         </div>"
57
58 #define HTML_VERSION_TEMPLATE \
59 "<html>\n\
60   <head>\n\
61     <link type=\"text/css\"\n\
62           rel=\"stylesheet\"\n\
63           href=\n\
64 \"http://fonts.googleapis.com/css?family=Ubuntu:regular,bold&subset=Latin\">\n\
65     <link type=\"text/css\" href=\"css/ppastats.css\" rel=\"stylesheet\" />\n\
66     <link type=\"text/css\" href=\"css/jquery.jqplot.min.css\"\n\
67           rel=\"stylesheet\" />\n\
68     <script type=\"text/javascript\" src=\"js/jquery.min.js\"></script>\n\
69     <script type=\"text/javascript\"\n\
70             src=\"js/jquery.jqplot.min.js\"></script>\n\
71     <script type=\"text/javascript\"\n\
72             src=\"js/jqplot.dateAxisRenderer.min.js\"></script>\n\
73     <script type=\"text/javascript\" src=\"js/ppastats.js\"></script>\n\
74     <script>var data = %s;\n\
75             ppastats_ver();\n\
76     </script>\n\
77   </head>\n\
78   <body>\n\
79     <h1><span id=\"pkg_name\">N/A</span></h1>\n\
80     <div id=\"version\"><em>Version:</em></div>\n\
81     <p><em>PPA</em>: \n\
82        <a href=\"index.html\">\n\
83            <span id=\"ppa_owner\">N/A</span>/<span id=\"ppa_name\">N/A</span>\n\
84        </a></p>\n\
85     <div id=\"charts\">\n\
86         <div id=\"chart\"></div>\n\
87     </div>\n\
88     <div id=\"details\">\n\
89       <div class=\"distros\">\n\
90         <em>Distros</em>:\n\
91         <ul id=\"distros\"></ul>\n\
92       </div>\n\
93     </div>\n\
94 %s"
95
96 #define HTML_HEADER \
97 "<html>\n\
98         <head>\n\
99                 <title>%s</title>\n\
100                 <link type=\"text/css\"\n\
101                       rel=\"stylesheet\"\n\
102                       href=\
103 \"http://fonts.googleapis.com/css?family=Ubuntu:regular,bold&subset=Latin\">\n\
104                 <link type=\"text/css\" href=\"css/ppastats.css\"\n\
105                       rel=\"stylesheet\"/>\n\
106                 <link type=\"text/css\" href=\"css/jquery.jqplot.min.css\"\n\
107                       rel=\"stylesheet\" />\n\
108 <!--[if lt IE 9]><script \
109 language=\"javascript\" \
110 type=\"text/javascript\" \
111 src=\"excanvas.js\"></script><![endif]-->\n\
112                 <script type=\"text/javascript\"\n\
113                         src=\"js/jquery.min.js\"></script>\n\
114                 <script type=\"text/javascript\"\n\
115                         src=\"js/jquery.jqplot.min.js\"></script>\n\
116                 <script type=\"text/javascript\"\n\
117                         src=\"js/jqplot.dateAxisRenderer.min.js\"></script>\n\
118                 <script type=\"text/javascript\"\n\
119                         src=\"js/ppastats.js\"></script>\n\
120                 <script>%s</script>\n\
121         </head>\n\
122         <body>\n"
123
124 #define HTML_INDEX_TEMPLATE \
125 "       <h1><span id=\"ppa_name\">N/A</span></h1>\n\
126         <div id=\"details\">\n\
127                 <div class=\"pkgs\">\n\
128                         <em>Packages</em>:\n\
129                         <ul id=\"pkgs\"></ul>\n\
130                 </div>\n\
131         </div>\n\
132         <div  id=\"charts\">\n\
133                 <div id=\"chart\"></div>\n\
134         </div>\n"
135
136 static char *path_new(const char *dir, const char *file, const char *suffixe)
137 {
138         char *path = malloc(strlen(dir)+1+
139                             strlen(file)+
140                             strlen(".html")+
141                             (suffixe ? strlen(suffixe) : 0) +
142                             1);
143
144         strcpy(path, dir);
145         strcat(path, "/");
146         strcat(path, file);
147         strcat(path, suffixe);
148
149         return path;
150 }
151
152 static struct json_object *date_to_json(struct tm *tm)
153 {
154         json_object *json;
155
156         json = json_object_new_array();
157         json_object_array_add(json, json_object_new_int(tm->tm_year+1900));
158         json_object_array_add(json, json_object_new_int(tm->tm_mon+1));
159         json_object_array_add(json, json_object_new_int(tm->tm_mday));
160
161         return json;
162 }
163
164 static void json_add_ddts(json_object *json,
165                           struct daily_download_total **ddts)
166 {
167         json_object *json_ddt, *json_ddts;
168         struct daily_download_total *ddt;
169
170         json_ddts = json_object_new_array();
171         json_object_object_add(json, "ddts", json_ddts);
172
173         if (!ddts)
174                 return ;
175
176         while (*ddts) {
177                 ddt = *ddts;
178
179                 json_ddt = json_object_new_object();
180                 json_object_object_add(json_ddt,
181                                        "value",
182                                        json_object_new_int(ddt->count));
183                 json_object_object_add(json_ddt,
184                                        "time",
185                                        date_to_json(&ddt->date));
186
187                 json_object_array_add(json_ddts, json_ddt);
188
189                 ddts++;
190         }
191 }
192
193 static json_object *distro_to_json(struct distro_stats *d)
194 {
195         json_object *json;
196
197         json = json_object_new_object();
198
199         json_object_object_add(json,
200                                "name",
201                                json_object_new_string(d->name));
202
203         json_object_object_add(json,
204                                "count",
205                                json_object_new_int(d->download_count));
206
207         json_add_ddts(json, d->ddts);
208
209         return json;
210 }
211
212 static json_object *
213 pkg_to_json(struct ppa_stats *ppa, struct package_stats *pkg)
214 {
215         json_object *json, *json_versions, *json_distros, *json_distro;
216         struct version_stats **versions;
217         struct distro_stats **distros, *d;
218
219         json = json_object_new_object();
220
221         json_object_object_add(json,
222                                "ppa_name", json_object_new_string(ppa->name));
223         json_object_object_add(json,
224                                "ppa_owner",
225                                json_object_new_string(ppa->owner));
226
227         json_object_object_add(json,
228                                "name", json_object_new_string(pkg->name));
229
230         json_versions = json_object_new_array();
231         json_object_object_add(json, "versions", json_versions);
232         versions = pkg->versions;
233         while (*versions) {
234                 json_object_array_add
235                         (json_versions,
236                          json_object_new_string((*versions)->version));
237
238                 versions++;
239         }
240
241         distros = pkg->distros;
242         if (distros) {
243                 json_distros = json_object_new_array();
244                 json_object_object_add(json, "distros", json_distros);
245
246                 while (*distros) {
247                         d = *distros;
248
249                         if (d->download_count) {
250                                 json_distro = distro_to_json(d);
251
252                                 json_object_array_add(json_distros,
253                                                       json_distro);
254                         }
255
256                         distros++;
257                 }
258         }
259
260         json_add_ddts(json, pkg->daily_download_totals);
261
262         return json;
263 }
264
265 static char *version_to_json(struct ppa_stats *ppa,
266                              struct package_stats *pkg,
267                              struct version_stats *ver)
268 {
269         char *ret;
270         struct distro_stats **distros, *distro;
271         json_object *json, *json_distros, *json_distro, *json_archs, *json_arch;
272         struct arch_stats **archs;
273
274         json = json_object_new_object();
275
276         json_object_object_add(json,
277                                "ppa_name", json_object_new_string(ppa->name));
278         json_object_object_add(json,
279                                "ppa_owner",
280                                json_object_new_string(ppa->owner));
281
282         json_object_object_add(json,
283                                "pkg_name", json_object_new_string(pkg->name));
284
285         json_object_object_add(json,
286                                "name", json_object_new_string(ver->version));
287
288         json_add_ddts(json, ver->daily_download_totals);
289
290         distros = ver->distros;
291         json_distros = json_object_new_array();
292         json_object_object_add(json, "distros", json_distros);
293         while (*distros) {
294                 distro = *distros;
295                 json_distro = json_object_new_object();
296
297                 json_object_array_add(json_distros, json_distro);
298
299                 json_object_object_add(json_distro,
300                                        "name",
301                                        json_object_new_string(distro->name));
302
303                 archs = distro->archs;
304                 json_archs = json_object_new_array();
305                 json_object_object_add(json_distro, "archs", json_archs);
306                 while (*archs) {
307                         json_arch = json_object_new_object();
308
309                         json_object_object_add
310                                 (json_arch,
311                                  "name",
312                                  json_object_new_string((*archs)->name));
313
314                         json_object_object_add
315                                 (json_arch,
316                                  "count",
317                                  json_object_new_int((*archs)->download_count));
318
319                         json_object_array_add(json_archs, json_arch);
320                         archs++;
321                 }
322
323                 distros++;
324         }
325
326         ret = strdup(json_object_to_json_string(json));
327
328         json_object_put(json);
329
330         return ret;
331 }
332
333 static json_object *ppa_to_json(struct ppa_stats *ppa)
334 {
335         json_object *json, *json_pkgs, *json_pkg;
336         struct package_stats **pkgs;
337
338         json = json_object_new_object();
339
340         json_object_object_add(json,
341                                "ppa_name", json_object_new_string(ppa->name));
342         json_object_object_add(json,
343                                "ppa_owner",
344                                json_object_new_string(ppa->owner));
345
346         json_add_ddts(json, ppa->daily_download_totals);
347
348         pkgs = ppa->packages;
349         json_pkgs = json_object_new_array();
350         json_object_object_add(json, "packages", json_pkgs);
351         while (*pkgs) {
352                 json_pkg = json_object_new_object();
353                 json_object_array_add(json_pkgs, json_pkg);
354
355                 json_object_object_add(json_pkg, "name",
356                                        json_object_new_string((*pkgs)->name));
357
358                 json_object_object_add
359                         (json_pkg, "count",
360                          json_object_new_int((*pkgs)->download_count));
361
362                 pkgs++;
363         }
364
365         return json;
366 }
367
368 static void
369 version_to_html(struct ppa_stats *ppa,
370                 struct package_stats *pkg,
371                 struct version_stats *version,
372                 const char *dir)
373 {
374         char *f_name, *path;
375         FILE *f;
376
377         f_name = malloc(strlen(pkg->name)+1+strlen(version->version)+1);
378         sprintf(f_name, "%s_%s", pkg->name, version->version);
379
380         path = path_new(dir, f_name, ".html");
381         f = fopen(path, "w");
382
383         if (!f) {
384                 fprintf(stderr, "ERROR: failed to open: %s\n", path);
385                 return ;
386         }
387
388         fprintf(f, HTML_VERSION_TEMPLATE,
389                 version_to_json(ppa, pkg, version),
390                 HTML_FOOTER);
391
392         fclose(f);
393
394         free(path);
395         free(f_name);
396 }
397
398 static void
399 create_html(const char *path,
400             const char *title,
401             const char *body_template,
402             const char *script)
403 {
404         FILE *f;
405
406         f = fopen(path, "w");
407
408         if (!f) {
409                 fprintf(stderr, "ERROR: failed to open: %s\n", path);
410                 return ;
411         }
412
413         fprintf(f, HTML_HEADER, title, script);
414         fputs(body_template, f);
415         fputs(HTML_FOOTER, f);
416
417         fclose(f);
418 }
419
420 static char *ppa_display_name(const struct ppa_stats *ppa)
421 {
422         char *ret;
423
424         ret = malloc(4+strlen(ppa->name)+1+strlen(ppa->owner)+1);
425
426         sprintf(ret, "ppa:%s/%s", ppa->owner, ppa->name);
427
428         return ret;
429 }
430
431 static void
432 index_to_html(struct ppa_stats *ppa, const char *dir)
433 {
434         char *path, *json_path, *dname;
435         json_object *json;
436
437         json = ppa_to_json(ppa);
438         json_path = path_new(dir, "index", ".json");
439         if (debug)
440                 printf("DEBUG: generating %s\n", json_path);
441         json_object_to_file(json_path, json);
442         json_object_put(json);
443         free(json_path);
444
445         path = path_new(dir, "index", ".html");
446         dname = ppa_display_name(ppa);
447         create_html(path, dname, HTML_INDEX_TEMPLATE, "ppastats_ppa();");
448         free(path);
449         free(dname);
450 }
451
452 static void
453 pkg_to_html(struct ppa_stats *ppa, struct package_stats *pkg, const char *dir)
454 {
455         char *path, *json_path, *script;
456         json_object *json;
457
458         json_path = path_new(dir, pkg->name, ".json");
459         json = pkg_to_json(ppa, pkg);
460         if (debug)
461                 printf("DEBUG: generating %s\n", json_path);
462         json_object_to_file(json_path, json);
463         json_object_put(json);
464         free(json_path);
465
466         path = path_new(dir, pkg->name, ".html");
467         script = malloc(strlen("ppastats_pkg(\"\");")+
468                         strlen(pkg->name)+
469                         strlen(".json")+
470                         1);
471         sprintf(script, "ppastats_pkg(\"%s%s\");", pkg->name, ".json");
472
473         if (debug)
474                 printf("DEBUG: generating %s\n", path);
475
476         create_html(path, pkg->name, HTML_PKG_TEMPLATE, script);
477         free(path);
478         free(script);
479 }
480
481 static void
482 pkgs_to_html(struct ppa_stats *ppa,
483              struct package_stats **pkgs,
484              const char *dir)
485 {
486         struct version_stats **versions;
487
488         while (*pkgs) {
489                 pkg_to_html(ppa, *pkgs, dir);
490
491                 versions = (*pkgs)->versions;
492                 while (*versions) {
493                         version_to_html(ppa, *pkgs, *versions, dir);
494
495                         versions++;
496                 }
497
498                 pkgs++;
499         }
500 }
501
502 void
503 ppa_to_html(const char *owner,
504             const char *ppa,
505             const char *package_status,
506             const char *output_dir,
507             const int install_static_files)
508 {
509         struct ppa_stats *ppastats;
510         struct daily_download_total **totals;
511         char *path, *f_dst;
512         char *css_dir, *js_dir;
513         int i;
514         static char *www_files[]
515                 = { DEFAULT_WWW_DIR"/jquery.min.js", "js/jquery.min.js",
516                     DEFAULT_WWW_DIR"/ppastats.js", "js/ppastats.js",
517                     DEFAULT_WWW_DIR"/jqplot.dateAxisRenderer.min.js",
518                     "js/jqplot.dateAxisRenderer.min.js",
519                     DEFAULT_WWW_DIR"/jquery.jqplot.min.js",
520                     "js/jquery.jqplot.min.js",
521                     DEFAULT_WWW_DIR"/ppastats.css", "css/ppastats.css",
522                     DEFAULT_WWW_DIR"/jquery.jqplot.min.css",
523                     "css/jquery.jqplot.min.css" };
524
525         mkdirs(output_dir, 0777);
526
527         if (install_static_files) {
528                 css_dir = path_append(output_dir, "css");
529                 js_dir = path_append(output_dir, "js");
530
531                 mkdir(css_dir, 0777);
532                 mkdir(js_dir, 0777);
533
534                 for (i = 0; i < 6; i++) {
535                         f_dst = path_append(output_dir, www_files[2*i+1]);
536
537                         if (debug)
538                                 printf("DEBUG: copying %s %s\n",
539                                        www_files[2*i], f_dst);
540                         fcopy(www_files[2*i], f_dst);
541
542                         free(f_dst);
543                 }
544                 free(css_dir);
545                 free(js_dir);
546         }
547
548         ppastats = create_ppa_stats(owner, ppa, package_status);
549         totals = ppastats->daily_download_totals;
550
551         path = path_new(output_dir, "ppa", ".html");
552
553         pkgs_to_html(ppastats, ppastats->packages, output_dir);
554
555         index_to_html(ppastats, output_dir);
556
557         ppa_stats_free(ppastats);
558
559         free(path);
560 }