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