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