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