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