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