2 * Copyright (C) 2011-2012 jeanfi@gmail.com
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License as
6 * published by the Free Software Foundation; either version 2 of the
7 * License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
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
21 #define _(String) gettext(String)
28 #include <sys/types.h>
30 #include <json/json.h>
40 static char *ppa_body;
41 static char *pkg_body;
44 #define HTML_VERSION_TEMPLATE \
47 <link type=\"text/css\"\n\
50 \"http://fonts.googleapis.com/css?family=Ubuntu:regular,bold&subset=Latin\">\n\
51 <link type=\"text/css\" href=\"css/ppastats.css\" rel=\"stylesheet\" />\n\
52 <link type=\"text/css\" href=\"css/jquery.jqplot.min.css\"\n\
53 rel=\"stylesheet\" />\n\
54 <!--[if lt IE 9]><script \
55 language=\"javascript\" \
56 type=\"text/javascript\" \
57 src=\"js/excanvas.js\"></script><![endif]-->\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\
69 <h1><span id=\"pkg_name\">N/A</span></h1>\n\
70 <div id=\"version\"><em>Version:</em></div>\n\
72 <a href=\"index.html\">\n\
73 <span id=\"ppa_owner\">N/A</span>/<span id=\"ppa_name\">N/A</span>\n\
75 <div id=\"charts\">\n\
76 <div id=\"chart\"></div>\n\
78 <div id=\"details\">\n\
79 <div class=\"distros\">\n\
81 <ul id=\"distros\"></ul>\n\
85 static char *path_new(const char *dir, const char *file, const char *suffixe)
89 /* [dir]/[file][suffixe] */
90 path = malloc(strlen(dir)+1+
92 (suffixe ? strlen(suffixe) : 0) +
98 strcat(path, suffixe);
103 static const char *get_header()
108 path = DEFAULT_WWW_DIR"/header.tpl";
109 header = file_get_content(path);
112 log_err("Failed to read header template: %s", path);
118 static const char *get_footer()
123 path = DEFAULT_WWW_DIR"/footer.tpl";
124 footer = file_get_content(path);
127 log_err("Failed to read footer template: %s", path);
133 static const char *get_ppa_body()
138 path = DEFAULT_WWW_DIR"/ppa.tpl";
139 ppa_body = file_get_content(path);
142 log_err("Failed to read PPA template: %s", path);
148 static const char *get_pkg_body()
153 path = DEFAULT_WWW_DIR"/pkg.tpl";
154 pkg_body = file_get_content(path);
157 log_err("Failed to read package template: %s", path);
163 static struct json_object *date_to_json(struct tm *tm)
167 json = json_object_new_array();
168 json_object_array_add(json, json_object_new_int(tm->tm_year+1900));
169 json_object_array_add(json, json_object_new_int(tm->tm_mon+1));
170 json_object_array_add(json, json_object_new_int(tm->tm_mday));
175 static void json_add_ddts(json_object *json,
176 struct daily_download_total **ddts)
178 json_object *json_ddt, *json_ddts;
179 struct daily_download_total *ddt;
181 json_ddts = json_object_new_array();
182 json_object_object_add(json, "ddts", json_ddts);
190 json_ddt = json_object_new_object();
191 json_object_object_add(json_ddt,
193 json_object_new_int(ddt->count));
194 json_object_object_add(json_ddt,
196 date_to_json(&ddt->date));
198 json_object_array_add(json_ddts, json_ddt);
204 static json_object *distro_to_json(struct distro_stats *d)
208 json = json_object_new_object();
210 json_object_object_add(json,
212 json_object_new_string(d->name));
214 json_object_object_add(json,
216 json_object_new_int(d->download_count));
218 json_add_ddts(json, d->ddts);
224 pkg_to_json(struct ppa_stats *ppa, struct package_stats *pkg)
226 json_object *json, *json_versions, *json_distros, *json_distro;
227 struct version_stats **versions;
228 struct distro_stats **distros, *d;
230 json = json_object_new_object();
232 json_object_object_add(json,
233 "ppa_name", json_object_new_string(ppa->name));
234 json_object_object_add(json,
236 json_object_new_string(ppa->owner));
238 json_object_object_add(json,
239 "name", json_object_new_string(pkg->name));
241 json_versions = json_object_new_array();
242 json_object_object_add(json, "versions", json_versions);
243 versions = pkg->versions;
245 json_object_array_add
247 json_object_new_string((*versions)->version));
252 distros = pkg->distros;
254 json_distros = json_object_new_array();
255 json_object_object_add(json, "distros", json_distros);
260 if (d->download_count) {
261 json_distro = distro_to_json(d);
263 json_object_array_add(json_distros,
271 json_add_ddts(json, pkg->daily_download_totals);
276 static char *version_to_json(struct ppa_stats *ppa,
277 struct package_stats *pkg,
278 struct version_stats *ver)
281 struct distro_stats **distros, *distro;
282 json_object *json, *json_distros, *json_distro, *json_archs, *json_arch;
283 struct arch_stats **archs;
285 json = json_object_new_object();
287 json_object_object_add(json,
288 "ppa_name", json_object_new_string(ppa->name));
289 json_object_object_add(json,
291 json_object_new_string(ppa->owner));
293 json_object_object_add(json,
294 "pkg_name", json_object_new_string(pkg->name));
296 json_object_object_add(json,
297 "name", json_object_new_string(ver->version));
299 json_add_ddts(json, ver->daily_download_totals);
301 distros = ver->distros;
302 json_distros = json_object_new_array();
303 json_object_object_add(json, "distros", json_distros);
306 json_distro = json_object_new_object();
308 json_object_array_add(json_distros, json_distro);
310 json_object_object_add(json_distro,
312 json_object_new_string(distro->name));
314 archs = distro->archs;
315 json_archs = json_object_new_array();
316 json_object_object_add(json_distro, "archs", json_archs);
318 json_arch = json_object_new_object();
320 json_object_object_add
323 json_object_new_string((*archs)->name));
325 json_object_object_add
328 json_object_new_int((*archs)->download_count));
330 json_object_array_add(json_archs, json_arch);
337 ret = strdup(json_object_to_json_string(json));
339 json_object_put(json);
344 static json_object *ppa_to_json(struct ppa_stats *ppa)
346 json_object *json, *json_pkgs, *json_pkg;
347 struct package_stats **pkgs;
349 json = json_object_new_object();
351 json_object_object_add(json,
352 "ppa_name", json_object_new_string(ppa->name));
353 json_object_object_add(json,
355 json_object_new_string(ppa->owner));
357 json_add_ddts(json, ppa->daily_download_totals);
359 pkgs = ppa->packages;
360 json_pkgs = json_object_new_array();
361 json_object_object_add(json, "packages", json_pkgs);
363 json_pkg = json_object_new_object();
364 json_object_array_add(json_pkgs, json_pkg);
366 json_object_object_add(json_pkg, "name",
367 json_object_new_string((*pkgs)->name));
369 json_object_object_add
371 json_object_new_int((*pkgs)->download_count));
380 version_to_html(struct ppa_stats *ppa,
381 struct package_stats *pkg,
382 struct version_stats *version,
389 f_name = malloc(strlen(pkg->name)+1+strlen(version->version)+1);
390 sprintf(f_name, "%s_%s", pkg->name, version->version);
392 path = path_new(dir, f_name, ".html");
393 f = fopen(path, "w");
396 log_err(_("failed to open: %s"), path);
400 fprintf(f, HTML_VERSION_TEMPLATE,
401 version_to_json(ppa, pkg, version));
403 footer = get_footer();
414 create_html(const char *path,
416 const char *body_template,
423 header = get_header();
425 log_err(_("Failed to get the header template"));
429 f = fopen(path, "w");
432 log_err(_("Failed to open: %s"), path);
436 fprintf(f, header, title, script);
437 fputs(body_template, f);
439 footer = get_footer();
446 static char *ppa_display_name(const struct ppa_stats *ppa)
450 ret = malloc(4+strlen(ppa->name)+1+strlen(ppa->owner)+1);
452 sprintf(ret, "ppa:%s/%s", ppa->owner, ppa->name);
458 index_to_html(struct ppa_stats *ppa, const char *dir)
460 char *path, *json_path, *dname;
464 body = get_ppa_body();
466 log_err("Failed to create PPA page");
470 json = ppa_to_json(ppa);
471 json_path = path_new(dir, "index", ".json");
473 log_debug(_("generating %s"), json_path);
474 json_object_to_file(json_path, json);
475 json_object_put(json);
478 path = path_new(dir, "index", ".html");
479 dname = ppa_display_name(ppa);
480 create_html(path, dname, body, "ppastats_ppa();");
486 pkg_to_html(struct ppa_stats *ppa, struct package_stats *pkg, const char *dir)
488 char *path, *json_path, *script;
492 body = get_pkg_body();
494 log_err("Failed to create package page: %s", pkg->name);
498 json_path = path_new(dir, pkg->name, ".json");
499 json = pkg_to_json(ppa, pkg);
500 log_debug(_("Generating %s"), json_path);
502 json_object_to_file(json_path, json);
503 json_object_put(json);
506 path = path_new(dir, pkg->name, ".html");
507 script = malloc(strlen("ppastats_pkg(\"\");")+
511 sprintf(script, "ppastats_pkg(\"%s%s\");", pkg->name, ".json");
513 log_debug(_("Generating %s"), path);
515 create_html(path, pkg->name, body, script);
521 pkgs_to_html(struct ppa_stats *ppa,
522 struct package_stats **pkgs,
525 struct version_stats **versions;
528 pkg_to_html(ppa, *pkgs, dir);
530 versions = (*pkgs)->versions;
532 version_to_html(ppa, *pkgs, *versions, dir);
542 ppa_to_html(const char *owner,
544 const char *package_status,
545 const char *output_dir,
546 const int install_static_files)
548 struct ppa_stats *ppastats;
550 char *css_dir, *js_dir;
552 static char *www_files[]
553 = { DEFAULT_WWW_DIR"/jquery.min.js", "js/jquery.min.js",
554 DEFAULT_WWW_DIR"/ppastats.js", "js/ppastats.js",
555 DEFAULT_WWW_DIR"/jqplot.dateAxisRenderer.min.js",
556 "js/jqplot.dateAxisRenderer.min.js",
557 DEFAULT_WWW_DIR"/jquery.jqplot.min.js",
558 "js/jquery.jqplot.min.js",
559 DEFAULT_WWW_DIR"/excanvas.js", "js/excanvas.js",
560 DEFAULT_WWW_DIR"/ppastats.css", "css/ppastats.css",
561 DEFAULT_WWW_DIR"/jquery.jqplot.min.css",
562 "css/jquery.jqplot.min.css" };
564 mkdirs(output_dir, 0777);
566 if (install_static_files) {
567 css_dir = path_append(output_dir, "css");
568 js_dir = path_append(output_dir, "js");
570 mkdir(css_dir, 0777);
573 for (i = 0; i < 7; i++) {
574 f_dst = path_append(output_dir, www_files[2*i+1]);
576 log_debug(_("Copying %s %s"), www_files[2*i], f_dst);
577 fcopy(www_files[2*i], f_dst);
585 ppastats = create_ppa_stats(owner, ppa, package_status);
587 path = path_new(output_dir, "ppa", ".html");
589 pkgs_to_html(ppastats, ppastats->packages, output_dir);
591 index_to_html(ppastats, output_dir);
593 ppa_stats_free(ppastats);