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