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