fixed style
[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_add_ddts(json, ver->daily_download_totals);
258
259         distros = ver->distros;
260         json_distros = json_object_new_array();
261         json_object_object_add(json, "distros", json_distros);
262         while (*distros) {
263                 distro = *distros;
264                 json_distro = json_object_new_object();
265
266                 json_object_array_add(json_distros, json_distro);
267
268                 json_object_object_add(json_distro,
269                                        "name",
270                                        json_object_new_string(distro->name));
271
272                 archs = distro->archs;
273                 json_archs = json_object_new_array();
274                 json_object_object_add(json_distro, "archs", json_archs);
275                 while (*archs) {
276                         json_arch = json_object_new_object();
277
278                         json_object_object_add
279                                 (json_arch,
280                                  "name",
281                                  json_object_new_string((*archs)->name));
282
283                         json_object_object_add
284                                 (json_arch,
285                                  "count",
286                                  json_object_new_int((*archs)->download_count));
287
288                         json_object_array_add(json_archs, json_arch);
289                         archs++;
290                 }
291
292                 distros++;
293         }
294
295         ret = strdup(json_object_to_json_string(json));
296
297         json_object_put(json);
298
299         return ret;
300 }
301
302 static json_object *ppa_to_json(struct ppa_stats *ppa)
303 {
304         json_object *json, *json_pkgs, *json_pkg;
305         struct package_stats **pkgs;
306
307         json = json_object_new_object();
308
309         json_object_object_add(json,
310                                "ppa_name", json_object_new_string(ppa->name));
311         json_object_object_add(json,
312                                "ppa_owner",
313                                json_object_new_string(ppa->owner));
314
315         json_add_ddts(json, ppa->daily_download_totals);
316
317         pkgs = ppa->packages;
318         json_pkgs = json_object_new_array();
319         json_object_object_add(json, "packages", json_pkgs);
320         while (*pkgs) {
321                 json_pkg = json_object_new_object();
322                 json_object_array_add(json_pkgs, json_pkg);
323
324                 json_object_object_add(json_pkg, "name",
325                                        json_object_new_string((*pkgs)->name));
326
327                 json_object_object_add
328                         (json_pkg, "count",
329                          json_object_new_int((*pkgs)->download_count));
330
331                 pkgs++;
332         }
333
334         return json;
335 }
336
337 static void
338 create_html(const char *path,
339             const char *title,
340             const char *body_template,
341             const char *script)
342 {
343         FILE *f;
344         const char *footer;
345         char *header;
346
347         f = NULL;
348
349         header = get_header(title, script);
350         if (!header) {
351                 log_err(_("Failed to get the header template"));
352                 goto on_error;
353         }
354
355         f = fopen(path, "w");
356
357         if (!f) {
358                 log_err(_("Failed to open: %s"), path);
359                 goto on_error;
360         }
361
362         fputs(header, f);
363         fputs(body_template, f);
364
365         footer = get_footer();
366         if (footer)
367                 fputs(footer, f);
368
369  on_error:
370         if (header)
371                 free(header);
372
373         if (f)
374                 fclose(f);
375 }
376
377 static char *ppa_display_name(const struct ppa_stats *ppa)
378 {
379         char *ret;
380
381         ret = malloc(4+strlen(ppa->name)+1+strlen(ppa->owner)+1);
382
383         sprintf(ret, "ppa:%s/%s", ppa->owner, ppa->name);
384
385         return ret;
386 }
387
388 static void
389 index_to_html(struct ppa_stats *ppa, const char *dir)
390 {
391         char *path, *json_path, *dname;
392         json_object *json;
393         const char *body;
394
395         body = get_ppa_body();
396         if (!body) {
397                 log_err("Failed to create PPA page");
398                 return ;
399         }
400
401         json = ppa_to_json(ppa);
402         json_path = path_new(dir, "index", ".json");
403
404         log_debug(_("generating %s"), json_path);
405         json_object_to_file(json_path, json);
406         json_object_put(json);
407         free(json_path);
408
409         path = path_new(dir, "index", ".html");
410         dname = ppa_display_name(ppa);
411         create_html(path, dname, body, "ppastats_ppa();");
412         free(path);
413         free(dname);
414 }
415
416 static void
417 version_to_html(struct ppa_stats *ppa,
418                 struct package_stats *pkg,
419                 struct version_stats *version,
420                 const char *dir)
421 {
422         char *f_name, *path;
423         const char *body;
424         const char *script_tpl;
425         char *script, *json;
426
427         body = get_pkg_version_body();
428         if (!body) {
429                 log_err("Failed to create package version page");
430                 return ;
431         }
432
433         json = version_to_json(ppa, pkg, version);
434         if (!json) {
435                 log_err("Failed to create package version page");
436                 return ;
437         }
438
439         f_name = malloc(strlen(pkg->name)+1+strlen(version->version)+1);
440         sprintf(f_name, "%s_%s", pkg->name, version->version);
441
442         path = path_new(dir, f_name, ".html");
443
444         script_tpl = "var data = %s;\n ppastats_ver();";
445         script = malloc(strlen(script_tpl) - 2 + strlen(json) + 1);
446         sprintf(script, script_tpl, json);
447
448         create_html(path, f_name, body, script);
449
450         free(script);
451         free(json);
452         free(path);
453         free(f_name);
454 }
455
456 static void
457 pkg_to_html(struct ppa_stats *ppa, struct package_stats *pkg, const char *dir)
458 {
459         char *path, *json_path, *script;
460         json_object *json;
461         const char *body;
462
463         body = get_pkg_body();
464         if (!body) {
465                 log_err("Failed to create package page: %s", pkg->name);
466                 return ;
467         }
468
469         json_path = path_new(dir, pkg->name, ".json");
470         json = pkg_to_json(ppa, pkg);
471         log_debug(_("Generating %s"), json_path);
472
473         json_object_to_file(json_path, json);
474         json_object_put(json);
475         free(json_path);
476
477         path = path_new(dir, pkg->name, ".html");
478         script = malloc(strlen("ppastats_pkg(\"\");")+
479                         strlen(pkg->name)+
480                         strlen(".json")+
481                         1);
482         sprintf(script, "ppastats_pkg(\"%s%s\");", pkg->name, ".json");
483
484         log_debug(_("Generating %s"), path);
485
486         create_html(path, pkg->name, body, script);
487         free(path);
488         free(script);
489 }
490
491 static void
492 pkgs_to_html(struct ppa_stats *ppa,
493              struct package_stats **pkgs,
494              const char *dir)
495 {
496         struct version_stats **versions;
497
498         while (*pkgs) {
499                 pkg_to_html(ppa, *pkgs, dir);
500
501                 versions = (*pkgs)->versions;
502                 while (*versions) {
503                         version_to_html(ppa, *pkgs, *versions, dir);
504
505                         versions++;
506                 }
507
508                 pkgs++;
509         }
510 }
511
512 void
513 ppa_to_html(const char *owner,
514             const char *ppa,
515             const char *package_status,
516             const char *output_dir,
517             const int install_static_files,
518             int ws_size)
519 {
520         struct ppa_stats *ppastats;
521         char *path, *f_dst;
522         char *css_dir, *js_dir;
523         int i;
524         static char *www_files[]
525                 = { DEFAULT_WWW_DIR"/jquery.min.js", "js/jquery.min.js",
526                     DEFAULT_WWW_DIR"/ppastats.js", "js/ppastats.js",
527                     DEFAULT_WWW_DIR"/jqplot.dateAxisRenderer.min.js",
528                     "js/jqplot.dateAxisRenderer.min.js",
529                     DEFAULT_WWW_DIR"/jquery.jqplot.min.js",
530                     "js/jquery.jqplot.min.js",
531                     DEFAULT_WWW_DIR"/excanvas.js", "js/excanvas.js",
532                     DEFAULT_WWW_DIR"/ppastats.css", "css/ppastats.css",
533                     DEFAULT_WWW_DIR"/wpitchoune.css", "css/wpitchoune.css",
534                     DEFAULT_WWW_DIR"/jquery.jqplot.min.css",
535                     "css/jquery.jqplot.min.css" };
536
537         mkdirs(output_dir, 0777);
538
539         if (install_static_files) {
540                 css_dir = path_append(output_dir, "css");
541                 js_dir = path_append(output_dir, "js");
542
543                 mkdir(css_dir, 0777);
544                 mkdir(js_dir, 0777);
545
546                 for (i = 0; i < 8; i++) {
547                         f_dst = path_append(output_dir, www_files[2*i+1]);
548
549                         log_debug(_("Copying %s %s"), www_files[2*i], f_dst);
550                         file_copy(www_files[2*i], f_dst);
551
552                         free(f_dst);
553                 }
554                 free(css_dir);
555                 free(js_dir);
556         }
557
558         ppastats = create_ppa_stats(owner, ppa, package_status, ws_size);
559
560         path = path_new(output_dir, "ppa", ".html");
561
562         pkgs_to_html(ppastats, ppastats->packages, output_dir);
563
564         index_to_html(ppastats, output_dir);
565
566         ppa_stats_free(ppastats);
567
568         free(path);
569 }
570
571 void html_cleanup()
572 {
573         free(header);
574         free(footer);
575         free(ppa_body);
576         free(pkg_body);
577         free(pkg_version_body);
578 }