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