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