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