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