code style
[ppastats.git] / src / html.c
1 /*
2  * Copyright (C) 2011-2012 jeanfi@gmail.com
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License as
6  * published by the Free Software Foundation; either version 2 of the
7  * License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * 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 <libintl.h>
21 #define _(String) gettext(String)
22
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <sys/time.h>
27 #include <sys/stat.h>
28 #include <sys/types.h>
29
30 #include <json/json.h>
31
32 #include "html.h"
33 #include "io.h"
34 #include "log.h"
35 #include "lp.h"
36 #include "lp_ws.h"
37 #include "ppastats.h"
38
39 static char *footer;
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
99 #define HTML_HEADER \
100 "<html>\n\
101         <head>\n\
102                 <title>%s</title>\n\
103                 <link type=\"text/css\"\n\
104                       rel=\"stylesheet\"\n\
105                       href=\
106 \"http://fonts.googleapis.com/css?family=Ubuntu:regular,bold&subset=Latin\">\n\
107                 <link type=\"text/css\" href=\"css/ppastats.css\"\n\
108                       rel=\"stylesheet\"/>\n\
109                 <link type=\"text/css\" href=\"css/jquery.jqplot.min.css\"\n\
110                       rel=\"stylesheet\" />\n\
111 <!--[if lt IE 9]><script \
112 language=\"javascript\" \
113 type=\"text/javascript\" \
114 src=\"js/excanvas.js\"></script><![endif]-->\n\
115                 <script type=\"text/javascript\"\n\
116                         src=\"js/jquery.min.js\"></script>\n\
117                 <script type=\"text/javascript\"\n\
118                         src=\"js/jquery.jqplot.min.js\"></script>\n\
119                 <script type=\"text/javascript\"\n\
120                         src=\"js/jqplot.dateAxisRenderer.min.js\"></script>\n\
121                 <script type=\"text/javascript\"\n\
122                         src=\"js/ppastats.js\"></script>\n\
123                 <script>%s</script>\n\
124         </head>\n\
125         <body>\n"
126
127 #define HTML_INDEX_TEMPLATE \
128 "       <h1><span id=\"ppa_name\">N/A</span></h1>\n\
129         <div id=\"details\">\n\
130                 <div class=\"pkgs\">\n\
131                         <em>Packages</em>:\n\
132                         <ul id=\"pkgs\"></ul>\n\
133                 </div>\n\
134         </div>\n\
135         <div  id=\"charts\">\n\
136                 <div id=\"chart\"></div>\n\
137         </div>\n"
138
139 static char *path_new(const char *dir, const char *file, const char *suffixe)
140 {
141         char *path;
142
143         /* [dir]/[file][suffixe] */
144         path = malloc(strlen(dir)+1+
145                       strlen(file)+
146                       (suffixe ? strlen(suffixe) : 0) +
147                       1);
148
149         strcpy(path, dir);
150         strcat(path, "/");
151         strcat(path, file);
152         strcat(path, suffixe);
153
154         return path;
155 }
156
157
158
159 static const char *get_footer()
160 {
161         const char *path;
162
163         if (!footer) {
164                 path = DEFAULT_WWW_DIR"/footer.tpl";
165                 footer = file_get_content(path);
166
167                 if (!footer)
168                         log_err("Failed to read footer template: %s", path);
169         }
170
171         return footer;
172 }
173
174 static struct json_object *date_to_json(struct tm *tm)
175 {
176         json_object *json;
177
178         json = json_object_new_array();
179         json_object_array_add(json, json_object_new_int(tm->tm_year+1900));
180         json_object_array_add(json, json_object_new_int(tm->tm_mon+1));
181         json_object_array_add(json, json_object_new_int(tm->tm_mday));
182
183         return json;
184 }
185
186 static void json_add_ddts(json_object *json,
187                           struct daily_download_total **ddts)
188 {
189         json_object *json_ddt, *json_ddts;
190         struct daily_download_total *ddt;
191
192         json_ddts = json_object_new_array();
193         json_object_object_add(json, "ddts", json_ddts);
194
195         if (!ddts)
196                 return ;
197
198         while (*ddts) {
199                 ddt = *ddts;
200
201                 json_ddt = json_object_new_object();
202                 json_object_object_add(json_ddt,
203                                        "value",
204                                        json_object_new_int(ddt->count));
205                 json_object_object_add(json_ddt,
206                                        "time",
207                                        date_to_json(&ddt->date));
208
209                 json_object_array_add(json_ddts, json_ddt);
210
211                 ddts++;
212         }
213 }
214
215 static json_object *distro_to_json(struct distro_stats *d)
216 {
217         json_object *json;
218
219         json = json_object_new_object();
220
221         json_object_object_add(json,
222                                "name",
223                                json_object_new_string(d->name));
224
225         json_object_object_add(json,
226                                "count",
227                                json_object_new_int(d->download_count));
228
229         json_add_ddts(json, d->ddts);
230
231         return json;
232 }
233
234 static json_object *
235 pkg_to_json(struct ppa_stats *ppa, struct package_stats *pkg)
236 {
237         json_object *json, *json_versions, *json_distros, *json_distro;
238         struct version_stats **versions;
239         struct distro_stats **distros, *d;
240
241         json = json_object_new_object();
242
243         json_object_object_add(json,
244                                "ppa_name", json_object_new_string(ppa->name));
245         json_object_object_add(json,
246                                "ppa_owner",
247                                json_object_new_string(ppa->owner));
248
249         json_object_object_add(json,
250                                "name", json_object_new_string(pkg->name));
251
252         json_versions = json_object_new_array();
253         json_object_object_add(json, "versions", json_versions);
254         versions = pkg->versions;
255         while (*versions) {
256                 json_object_array_add
257                         (json_versions,
258                          json_object_new_string((*versions)->version));
259
260                 versions++;
261         }
262
263         distros = pkg->distros;
264         if (distros) {
265                 json_distros = json_object_new_array();
266                 json_object_object_add(json, "distros", json_distros);
267
268                 while (*distros) {
269                         d = *distros;
270
271                         if (d->download_count) {
272                                 json_distro = distro_to_json(d);
273
274                                 json_object_array_add(json_distros,
275                                                       json_distro);
276                         }
277
278                         distros++;
279                 }
280         }
281
282         json_add_ddts(json, pkg->daily_download_totals);
283
284         return json;
285 }
286
287 static char *version_to_json(struct ppa_stats *ppa,
288                              struct package_stats *pkg,
289                              struct version_stats *ver)
290 {
291         char *ret;
292         struct distro_stats **distros, *distro;
293         json_object *json, *json_distros, *json_distro, *json_archs, *json_arch;
294         struct arch_stats **archs;
295
296         json = json_object_new_object();
297
298         json_object_object_add(json,
299                                "ppa_name", json_object_new_string(ppa->name));
300         json_object_object_add(json,
301                                "ppa_owner",
302                                json_object_new_string(ppa->owner));
303
304         json_object_object_add(json,
305                                "pkg_name", json_object_new_string(pkg->name));
306
307         json_object_object_add(json,
308                                "name", json_object_new_string(ver->version));
309
310         json_add_ddts(json, ver->daily_download_totals);
311
312         distros = ver->distros;
313         json_distros = json_object_new_array();
314         json_object_object_add(json, "distros", json_distros);
315         while (*distros) {
316                 distro = *distros;
317                 json_distro = json_object_new_object();
318
319                 json_object_array_add(json_distros, json_distro);
320
321                 json_object_object_add(json_distro,
322                                        "name",
323                                        json_object_new_string(distro->name));
324
325                 archs = distro->archs;
326                 json_archs = json_object_new_array();
327                 json_object_object_add(json_distro, "archs", json_archs);
328                 while (*archs) {
329                         json_arch = json_object_new_object();
330
331                         json_object_object_add
332                                 (json_arch,
333                                  "name",
334                                  json_object_new_string((*archs)->name));
335
336                         json_object_object_add
337                                 (json_arch,
338                                  "count",
339                                  json_object_new_int((*archs)->download_count));
340
341                         json_object_array_add(json_archs, json_arch);
342                         archs++;
343                 }
344
345                 distros++;
346         }
347
348         ret = strdup(json_object_to_json_string(json));
349
350         json_object_put(json);
351
352         return ret;
353 }
354
355 static json_object *ppa_to_json(struct ppa_stats *ppa)
356 {
357         json_object *json, *json_pkgs, *json_pkg;
358         struct package_stats **pkgs;
359
360         json = json_object_new_object();
361
362         json_object_object_add(json,
363                                "ppa_name", json_object_new_string(ppa->name));
364         json_object_object_add(json,
365                                "ppa_owner",
366                                json_object_new_string(ppa->owner));
367
368         json_add_ddts(json, ppa->daily_download_totals);
369
370         pkgs = ppa->packages;
371         json_pkgs = json_object_new_array();
372         json_object_object_add(json, "packages", json_pkgs);
373         while (*pkgs) {
374                 json_pkg = json_object_new_object();
375                 json_object_array_add(json_pkgs, json_pkg);
376
377                 json_object_object_add(json_pkg, "name",
378                                        json_object_new_string((*pkgs)->name));
379
380                 json_object_object_add
381                         (json_pkg, "count",
382                          json_object_new_int((*pkgs)->download_count));
383
384                 pkgs++;
385         }
386
387         return json;
388 }
389
390 static void
391 version_to_html(struct ppa_stats *ppa,
392                 struct package_stats *pkg,
393                 struct version_stats *version,
394                 const char *dir)
395 {
396         char *f_name, *path;
397         FILE *f;
398         const char *footer;
399
400         f_name = malloc(strlen(pkg->name)+1+strlen(version->version)+1);
401         sprintf(f_name, "%s_%s", pkg->name, version->version);
402
403         path = path_new(dir, f_name, ".html");
404         f = fopen(path, "w");
405
406         if (!f) {
407                 log_err(_("failed to open: %s"), path);
408                 return ;
409         }
410
411         fprintf(f, HTML_VERSION_TEMPLATE,
412                 version_to_json(ppa, pkg, version));
413
414         footer = get_footer();
415         if (footer)
416                 fputs(footer, f);
417
418         fclose(f);
419
420         free(path);
421         free(f_name);
422 }
423
424 static void
425 create_html(const char *path,
426             const char *title,
427             const char *body_template,
428             const char *script)
429 {
430         FILE *f;
431         const char *footer;
432
433         f = fopen(path, "w");
434
435         if (!f) {
436                 log_err(_("failed to open: %s"), path);
437                 return ;
438         }
439
440         fprintf(f, HTML_HEADER, title, script);
441         fputs(body_template, f);
442
443         footer = get_footer();
444         if (footer)
445                 fputs(footer, f);
446
447         fclose(f);
448 }
449
450 static char *ppa_display_name(const struct ppa_stats *ppa)
451 {
452         char *ret;
453
454         ret = malloc(4+strlen(ppa->name)+1+strlen(ppa->owner)+1);
455
456         sprintf(ret, "ppa:%s/%s", ppa->owner, ppa->name);
457
458         return ret;
459 }
460
461 static void
462 index_to_html(struct ppa_stats *ppa, const char *dir)
463 {
464         char *path, *json_path, *dname;
465         json_object *json;
466
467         json = ppa_to_json(ppa);
468         json_path = path_new(dir, "index", ".json");
469
470         log_debug(_("generating %s"), json_path);
471         json_object_to_file(json_path, json);
472         json_object_put(json);
473         free(json_path);
474
475         path = path_new(dir, "index", ".html");
476         dname = ppa_display_name(ppa);
477         create_html(path, dname, HTML_INDEX_TEMPLATE, "ppastats_ppa();");
478         free(path);
479         free(dname);
480 }
481
482 static void
483 pkg_to_html(struct ppa_stats *ppa, struct package_stats *pkg, const char *dir)
484 {
485         char *path, *json_path, *script;
486         json_object *json;
487
488         json_path = path_new(dir, pkg->name, ".json");
489         json = pkg_to_json(ppa, pkg);
490         log_debug(_("generating %s"), json_path);
491
492         json_object_to_file(json_path, json);
493         json_object_put(json);
494         free(json_path);
495
496         path = path_new(dir, pkg->name, ".html");
497         script = malloc(strlen("ppastats_pkg(\"\");")+
498                         strlen(pkg->name)+
499                         strlen(".json")+
500                         1);
501         sprintf(script, "ppastats_pkg(\"%s%s\");", pkg->name, ".json");
502
503         log_debug(_("generating %s"), path);
504
505         create_html(path, pkg->name, HTML_PKG_TEMPLATE, script);
506         free(path);
507         free(script);
508 }
509
510 static void
511 pkgs_to_html(struct ppa_stats *ppa,
512              struct package_stats **pkgs,
513              const char *dir)
514 {
515         struct version_stats **versions;
516
517         while (*pkgs) {
518                 pkg_to_html(ppa, *pkgs, dir);
519
520                 versions = (*pkgs)->versions;
521                 while (*versions) {
522                         version_to_html(ppa, *pkgs, *versions, dir);
523
524                         versions++;
525                 }
526
527                 pkgs++;
528         }
529 }
530
531 void
532 ppa_to_html(const char *owner,
533             const char *ppa,
534             const char *package_status,
535             const char *output_dir,
536             const int install_static_files)
537 {
538         struct ppa_stats *ppastats;
539         char *path, *f_dst;
540         char *css_dir, *js_dir;
541         int i;
542         static char *www_files[]
543                 = { DEFAULT_WWW_DIR"/jquery.min.js", "js/jquery.min.js",
544                     DEFAULT_WWW_DIR"/ppastats.js", "js/ppastats.js",
545                     DEFAULT_WWW_DIR"/jqplot.dateAxisRenderer.min.js",
546                     "js/jqplot.dateAxisRenderer.min.js",
547                     DEFAULT_WWW_DIR"/jquery.jqplot.min.js",
548                     "js/jquery.jqplot.min.js",
549                     DEFAULT_WWW_DIR"/excanvas.js", "js/excanvas.js",
550                     DEFAULT_WWW_DIR"/ppastats.css", "css/ppastats.css",
551                     DEFAULT_WWW_DIR"/jquery.jqplot.min.css",
552                     "css/jquery.jqplot.min.css" };
553
554         mkdirs(output_dir, 0777);
555
556         if (install_static_files) {
557                 css_dir = path_append(output_dir, "css");
558                 js_dir = path_append(output_dir, "js");
559
560                 mkdir(css_dir, 0777);
561                 mkdir(js_dir, 0777);
562
563                 for (i = 0; i < 7; i++) {
564                         f_dst = path_append(output_dir, www_files[2*i+1]);
565
566                         log_debug(_("copying %s %s"), www_files[2*i], f_dst);
567                         fcopy(www_files[2*i], f_dst);
568
569                         free(f_dst);
570                 }
571                 free(css_dir);
572                 free(js_dir);
573         }
574
575         ppastats = create_ppa_stats(owner, ppa, package_status);
576
577         path = path_new(output_dir, "ppa", ".html");
578
579         pkgs_to_html(ppastats, ppastats->packages, output_dir);
580
581         index_to_html(ppastats, output_dir);
582
583         ppa_stats_free(ppastats);
584
585         free(path);
586 }