renamed fct
[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 *path_new(const char *dir, const char *file, const char *suffixe)
213 {
214         char *path = malloc(strlen(dir)+1+
215                             strlen(file)+
216                             strlen(".html")+
217                             (suffixe ? strlen(suffixe) : 0) +
218                             1);
219
220         strcpy(path, dir);
221         strcat(path, "/");
222         strcat(path, file);
223         strcat(path, ".html");
224
225         return path;
226 }
227
228 static struct json_object *date_to_json(struct tm *tm)
229 {
230         json_object *json;
231
232         json = json_object_new_array();
233         json_object_array_add(json, json_object_new_int(tm->tm_year+1900));
234         json_object_array_add(json, json_object_new_int(tm->tm_mon+1));
235         json_object_array_add(json, json_object_new_int(tm->tm_mday));
236
237         return json;
238 }
239
240 static void json_add_ddts(json_object *json,
241                           struct daily_download_total **ddts)
242 {
243         json_object *json_ddt, *json_ddts;
244         struct daily_download_total *ddt;
245
246         json_ddts = json_object_new_array();
247         json_object_object_add(json, "ddts", json_ddts);
248
249         if (!ddts)
250                 return ;
251
252         while (*ddts) {
253                 ddt = *ddts;
254
255                 json_ddt = json_object_new_object();
256                 json_object_object_add(json_ddt,
257                                        "value",
258                                        json_object_new_int(ddt->count));
259                 json_object_object_add(json_ddt,
260                                        "time",
261                                        date_to_json(&ddt->date));
262
263                 json_object_array_add(json_ddts, json_ddt);
264
265                 ddts++;
266         }
267 }
268
269 static char *pkg_to_json(struct ppa_stats *ppa, struct package_stats *pkg)
270 {
271         json_object *json, *json_versions, *json_distros, *json_distro;
272         char *ret;
273         struct version_stats **versions;
274         struct distro_stats **distros, *distro;
275
276         json = json_object_new_object();
277
278         json_object_object_add(json,
279                                "ppa_name", json_object_new_string(ppa->name));
280         json_object_object_add(json,
281                                "ppa_owner",
282                                json_object_new_string(ppa->owner));
283
284         json_object_object_add(json,
285                                "name", json_object_new_string(pkg->name));
286
287         json_versions = json_object_new_array();
288         json_object_object_add(json, "versions", json_versions);
289         versions = pkg->versions;
290         while (*versions) {
291                 json_object_array_add
292                         (json_versions,
293                          json_object_new_string((*versions)->version));
294
295                 versions++;
296         }
297
298         distros = pkg->distros;
299         if (distros) {
300                 json_distros = json_object_new_array();
301                 json_object_object_add(json, "distros", json_distros);
302
303                 while (*distros) {
304                         distro = *distros;
305
306                         json_distro = json_object_new_object();
307                         json_object_array_add(json_distros, json_distro);
308                         json_object_object_add
309                                 (json_distro,
310                                  "name", json_object_new_string(distro->name));
311                         json_object_object_add
312                                 (json_distro,
313                                  "count",
314                                  json_object_new_int(distro->download_count));
315
316                         json_add_ddts(json_distro, distro->ddts);
317
318                         distros++;
319                 }
320         }
321
322         json_add_ddts(json, pkg->daily_download_totals);
323
324         ret = strdup(json_object_to_json_string(json));
325
326         json_object_put(json);
327
328         return ret;
329 }
330
331 static char *version_to_json(struct ppa_stats *ppa,
332                              struct package_stats *pkg,
333                              struct version_stats *ver)
334 {
335         char *ret;
336         struct distro_stats **distros, *distro;
337         json_object *json, *json_distros, *json_distro, *json_archs, *json_arch;
338         struct arch_stats **archs;
339
340         json = json_object_new_object();
341
342         json_object_object_add(json,
343                                "ppa_name", json_object_new_string(ppa->name));
344         json_object_object_add(json,
345                                "ppa_owner",
346                                json_object_new_string(ppa->owner));
347
348         json_object_object_add(json,
349                                "pkg_name", json_object_new_string(pkg->name));
350
351         json_object_object_add(json,
352                                "name", json_object_new_string(ver->version));
353
354         json_add_ddts(json, ver->daily_download_totals);
355
356         distros = ver->distros;
357         json_distros = json_object_new_array();
358         json_object_object_add(json, "distros", json_distros);
359         while (*distros) {
360                 distro = *distros;
361                 json_distro = json_object_new_object();
362
363                 json_object_array_add(json_distros, json_distro);
364
365                 json_object_object_add(json_distro,
366                                        "name",
367                                        json_object_new_string(distro->name));
368
369                 archs = distro->archs;
370                 json_archs = json_object_new_array();
371                 json_object_object_add(json_distro, "archs", json_archs);
372                 while (*archs) {
373                         json_arch = json_object_new_object();
374
375                         json_object_object_add
376                                 (json_arch,
377                                  "name",
378                                  json_object_new_string((*archs)->name));
379
380                         json_object_object_add
381                                 (json_arch,
382                                  "count",
383                                  json_object_new_int((*archs)->download_count));
384
385                         json_object_array_add(json_archs, json_arch);
386                         archs++;
387                 }
388
389                 distros++;
390         }
391
392         ret = strdup(json_object_to_json_string(json));
393
394         json_object_put(json);
395
396         return ret;
397 }
398
399 static char *ppa_to_json(struct ppa_stats *ppa)
400 {
401         char *ret;
402         json_object *json, *json_pkgs, *json_pkg;
403         struct package_stats **pkgs;
404
405         json = json_object_new_object();
406
407         json_object_object_add(json,
408                                "ppa_name", json_object_new_string(ppa->name));
409         json_object_object_add(json,
410                                "ppa_owner",
411                                json_object_new_string(ppa->owner));
412
413         json_add_ddts(json, ppa->daily_download_totals);
414
415         pkgs = ppa->packages;
416         json_pkgs = json_object_new_array();
417         json_object_object_add(json, "packages", json_pkgs);
418         while (*pkgs) {
419                 json_pkg = json_object_new_object();
420                 json_object_array_add(json_pkgs, json_pkg);
421
422                 json_object_object_add(json_pkg, "name",
423                                        json_object_new_string((*pkgs)->name));
424
425                 json_object_object_add
426                         (json_pkg, "count",
427                          json_object_new_int((*pkgs)->download_count));
428
429                 pkgs++;
430         }
431
432         ret = strdup(json_object_to_json_string(json));
433
434         json_object_put(json);
435
436         return ret;
437 }
438
439
440 static void
441 pkg_to_html(struct ppa_stats *ppa, struct package_stats *pkg, const char *dir)
442 {
443         char *path;
444         FILE *f;
445         char *json;
446
447         path = path_new(dir, pkg->name, ".html");
448         f = fopen(path, "w");
449
450         if (!f) {
451                 fprintf(stderr, "ERROR: failed to open: %s\n", path);
452                 return ;
453         }
454
455         json = pkg_to_json(ppa, pkg);
456
457         fprintf(f, HTML_PKG_TEMPLATE, json, HTML_FOOTER);
458
459         fclose(f);
460
461         free(path);
462         free(json);
463 }
464
465 static void
466 version_to_html(struct ppa_stats *ppa,
467                 struct package_stats *pkg,
468                 struct version_stats *version,
469                 const char *dir)
470 {
471         char *f_name, *path;
472         FILE *f;
473
474         f_name = malloc(strlen(pkg->name)+1+strlen(version->version)+1);
475         sprintf(f_name, "%s_%s", pkg->name, version->version);
476
477         path = path_new(dir, f_name, ".html");
478         f = fopen(path, "w");
479
480         if (!f) {
481                 fprintf(stderr, "ERROR: failed to open: %s\n", path);
482                 return ;
483         }
484
485         fprintf(f, HTML_VERSION_TEMPLATE,
486                 version_to_json(ppa, pkg, version),
487                 HTML_FOOTER);
488
489         fclose(f);
490
491         free(path);
492         free(f_name);
493 }
494
495 static void
496 packages_to_html(struct ppa_stats *ppa,
497                  struct package_stats **packages,
498                  const char *dir)
499 {
500         struct package_stats **cur;
501         struct version_stats **versions;
502
503         cur = packages;
504         while (*cur) {
505                 pkg_to_html(ppa, *cur, dir);
506
507                 versions = (*cur)->versions;
508                 while (*versions) {
509                         version_to_html(ppa, *cur, *versions, dir);
510
511                         versions++;
512                 }
513
514                 cur++;
515         }
516 }
517
518 static void
519 index_to_html(struct ppa_stats *ppa, const char *dir)
520 {
521         char *path;
522         FILE *f;
523
524         path = path_new(dir, "index", ".html");
525         f = fopen(path, "w");
526
527         if (!f) {
528                 fprintf(stderr, "ERROR: failed to open: %s\n", path);
529                 return ;
530         }
531
532         fprintf(f, HTML_INDEX_TEMPLATE, ppa_to_json(ppa), HTML_FOOTER);
533
534         fclose(f);
535
536         free(path);
537 }
538
539 static char *append_path(const char *odir, const char *name)
540 {
541         char *dir;
542
543         dir = malloc(strlen(odir)+1+strlen(name)+1);
544
545         sprintf(dir, "%s/%s", odir, name);
546
547         return dir;
548 }
549
550 void
551 ppa_to_html(const char *owner,
552             const char *ppa,
553             const char *package_status,
554             const char *output_dir,
555             const int install_static_files)
556 {
557         struct ppa_stats *ppastats;
558         struct daily_download_total **totals;
559         char *path, *f_dst;
560         char *css_dir, *js_dir;
561         int i;
562         static char *www_files[]
563                 = { DEFAULT_WWW_DIR"/jquery.min.js", "js/jquery.min.js",
564                     DEFAULT_WWW_DIR"/ppastats.js", "js/ppastats.js",
565                     DEFAULT_WWW_DIR"/jqplot.dateAxisRenderer.min.js",
566                     "js/jqplot.dateAxisRenderer.min.js",
567                     DEFAULT_WWW_DIR"/jquery.jqplot.min.js",
568                     "js/jquery.jqplot.min.js",
569                     DEFAULT_WWW_DIR"/ppastats.css", "css/ppastats.css",
570                     DEFAULT_WWW_DIR"/jquery.jqplot.min.css",
571                     "css/jquery.jqplot.min.css" };
572
573         if (install_static_files) {
574                 css_dir = append_path(output_dir, "css");
575                 js_dir = append_path(output_dir, "js");
576
577                 mkdir(css_dir, 0777);
578                 mkdir(js_dir, 0777);
579
580                 for (i = 0; i < 6; i++) {
581                         f_dst = append_path(output_dir, www_files[2*i+1]);
582                         fcopy(www_files[2*i], f_dst);
583
584                         free(f_dst);
585                 }
586                 free(css_dir);
587                 free(js_dir);
588         }
589
590         ppastats = create_ppa_stats(owner, ppa, package_status);
591         totals = ppastats->daily_download_totals;
592
593         path = path_new(output_dir, "ppa", ".html");
594
595         packages_to_html(ppastats, ppastats->packages, output_dir);
596
597         index_to_html(ppastats, output_dir);
598
599         ppa_stats_free(ppastats);
600
601         free(path);
602 }