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>
41 #define HTML_PKG_TEMPLATE \
42 " <h1><span id=\"pkg_name\">N/A</span></h1>\n\
43 <div id=\"charts\">\n\
44 <div id=\"chart\"></div>\n\
45 <div id=\"chart_distro\"></div>\n\
47 <div id=\"details\">\n\
49 <a href=\"index.html\">\n\
50 <span id=\"ppa_owner\">N/A</span>/\n\
51 <span id=\"ppa_name\">N/A</span>\n\
54 <div id=\"distros\"><em>Distros:</em></div>\n\
55 <div id=\"versions\"><em>Versions:</em></div>\n\
58 #define HTML_VERSION_TEMPLATE \
61 <link type=\"text/css\"\n\
64 \"http://fonts.googleapis.com/css?family=Ubuntu:regular,bold&subset=Latin\">\n\
65 <link type=\"text/css\" href=\"css/ppastats.css\" rel=\"stylesheet\" />\n\
66 <link type=\"text/css\" href=\"css/jquery.jqplot.min.css\"\n\
67 rel=\"stylesheet\" />\n\
68 <!--[if lt IE 9]><script \
69 language=\"javascript\" \
70 type=\"text/javascript\" \
71 src=\"js/excanvas.js\"></script><![endif]-->\n\
72 <script type=\"text/javascript\" src=\"js/jquery.min.js\"></script>\n\
73 <script type=\"text/javascript\"\n\
74 src=\"js/jquery.jqplot.min.js\"></script>\n\
75 <script type=\"text/javascript\"\n\
76 src=\"js/jqplot.dateAxisRenderer.min.js\"></script>\n\
77 <script type=\"text/javascript\" src=\"js/ppastats.js\"></script>\n\
78 <script>var data = %s;\n\
83 <h1><span id=\"pkg_name\">N/A</span></h1>\n\
84 <div id=\"version\"><em>Version:</em></div>\n\
86 <a href=\"index.html\">\n\
87 <span id=\"ppa_owner\">N/A</span>/<span id=\"ppa_name\">N/A</span>\n\
89 <div id=\"charts\">\n\
90 <div id=\"chart\"></div>\n\
92 <div id=\"details\">\n\
93 <div class=\"distros\">\n\
95 <ul id=\"distros\"></ul>\n\
103 <link type=\"text/css\"\n\
104 rel=\"stylesheet\"\n\
106 \"http://fonts.googleapis.com/css?family=Ubuntu:regular,bold&subset=Latin\">\n\
107 <link type=\"text/css\" href=\"css/ppastats.css\"\n\
108 rel=\"stylesheet\"/>\n\
109 <link type=\"text/css\" href=\"css/jquery.jqplot.min.css\"\n\
110 rel=\"stylesheet\" />\n\
111 <!--[if lt IE 9]><script \
112 language=\"javascript\" \
113 type=\"text/javascript\" \
114 src=\"js/excanvas.js\"></script><![endif]-->\n\
115 <script type=\"text/javascript\"\n\
116 src=\"js/jquery.min.js\"></script>\n\
117 <script type=\"text/javascript\"\n\
118 src=\"js/jquery.jqplot.min.js\"></script>\n\
119 <script type=\"text/javascript\"\n\
120 src=\"js/jqplot.dateAxisRenderer.min.js\"></script>\n\
121 <script type=\"text/javascript\"\n\
122 src=\"js/ppastats.js\"></script>\n\
123 <script>%s</script>\n\
127 #define HTML_INDEX_TEMPLATE \
128 " <h1><span id=\"ppa_name\">N/A</span></h1>\n\
129 <div id=\"details\">\n\
130 <div class=\"pkgs\">\n\
131 <em>Packages</em>:\n\
132 <ul id=\"pkgs\"></ul>\n\
135 <div id=\"charts\">\n\
136 <div id=\"chart\"></div>\n\
139 static char *path_new(const char *dir, const char *file, const char *suffixe)
143 /* [dir]/[file][suffixe] */
144 path = malloc(strlen(dir)+1+
146 (suffixe ? strlen(suffixe) : 0) +
152 strcat(path, suffixe);
159 static const char *get_footer()
164 path = DEFAULT_WWW_DIR"/footer.tpl";
165 footer = file_get_content(path);
168 log_err("Failed to get footer template: %s", path);
175 static struct json_object *date_to_json(struct tm *tm)
179 json = json_object_new_array();
180 json_object_array_add(json, json_object_new_int(tm->tm_year+1900));
181 json_object_array_add(json, json_object_new_int(tm->tm_mon+1));
182 json_object_array_add(json, json_object_new_int(tm->tm_mday));
187 static void json_add_ddts(json_object *json,
188 struct daily_download_total **ddts)
190 json_object *json_ddt, *json_ddts;
191 struct daily_download_total *ddt;
193 json_ddts = json_object_new_array();
194 json_object_object_add(json, "ddts", json_ddts);
202 json_ddt = json_object_new_object();
203 json_object_object_add(json_ddt,
205 json_object_new_int(ddt->count));
206 json_object_object_add(json_ddt,
208 date_to_json(&ddt->date));
210 json_object_array_add(json_ddts, json_ddt);
216 static json_object *distro_to_json(struct distro_stats *d)
220 json = json_object_new_object();
222 json_object_object_add(json,
224 json_object_new_string(d->name));
226 json_object_object_add(json,
228 json_object_new_int(d->download_count));
230 json_add_ddts(json, d->ddts);
236 pkg_to_json(struct ppa_stats *ppa, struct package_stats *pkg)
238 json_object *json, *json_versions, *json_distros, *json_distro;
239 struct version_stats **versions;
240 struct distro_stats **distros, *d;
242 json = json_object_new_object();
244 json_object_object_add(json,
245 "ppa_name", json_object_new_string(ppa->name));
246 json_object_object_add(json,
248 json_object_new_string(ppa->owner));
250 json_object_object_add(json,
251 "name", json_object_new_string(pkg->name));
253 json_versions = json_object_new_array();
254 json_object_object_add(json, "versions", json_versions);
255 versions = pkg->versions;
257 json_object_array_add
259 json_object_new_string((*versions)->version));
264 distros = pkg->distros;
266 json_distros = json_object_new_array();
267 json_object_object_add(json, "distros", json_distros);
272 if (d->download_count) {
273 json_distro = distro_to_json(d);
275 json_object_array_add(json_distros,
283 json_add_ddts(json, pkg->daily_download_totals);
288 static char *version_to_json(struct ppa_stats *ppa,
289 struct package_stats *pkg,
290 struct version_stats *ver)
293 struct distro_stats **distros, *distro;
294 json_object *json, *json_distros, *json_distro, *json_archs, *json_arch;
295 struct arch_stats **archs;
297 json = json_object_new_object();
299 json_object_object_add(json,
300 "ppa_name", json_object_new_string(ppa->name));
301 json_object_object_add(json,
303 json_object_new_string(ppa->owner));
305 json_object_object_add(json,
306 "pkg_name", json_object_new_string(pkg->name));
308 json_object_object_add(json,
309 "name", json_object_new_string(ver->version));
311 json_add_ddts(json, ver->daily_download_totals);
313 distros = ver->distros;
314 json_distros = json_object_new_array();
315 json_object_object_add(json, "distros", json_distros);
318 json_distro = json_object_new_object();
320 json_object_array_add(json_distros, json_distro);
322 json_object_object_add(json_distro,
324 json_object_new_string(distro->name));
326 archs = distro->archs;
327 json_archs = json_object_new_array();
328 json_object_object_add(json_distro, "archs", json_archs);
330 json_arch = json_object_new_object();
332 json_object_object_add
335 json_object_new_string((*archs)->name));
337 json_object_object_add
340 json_object_new_int((*archs)->download_count));
342 json_object_array_add(json_archs, json_arch);
349 ret = strdup(json_object_to_json_string(json));
351 json_object_put(json);
356 static json_object *ppa_to_json(struct ppa_stats *ppa)
358 json_object *json, *json_pkgs, *json_pkg;
359 struct package_stats **pkgs;
361 json = json_object_new_object();
363 json_object_object_add(json,
364 "ppa_name", json_object_new_string(ppa->name));
365 json_object_object_add(json,
367 json_object_new_string(ppa->owner));
369 json_add_ddts(json, ppa->daily_download_totals);
371 pkgs = ppa->packages;
372 json_pkgs = json_object_new_array();
373 json_object_object_add(json, "packages", json_pkgs);
375 json_pkg = json_object_new_object();
376 json_object_array_add(json_pkgs, json_pkg);
378 json_object_object_add(json_pkg, "name",
379 json_object_new_string((*pkgs)->name));
381 json_object_object_add
383 json_object_new_int((*pkgs)->download_count));
392 version_to_html(struct ppa_stats *ppa,
393 struct package_stats *pkg,
394 struct version_stats *version,
401 f_name = malloc(strlen(pkg->name)+1+strlen(version->version)+1);
402 sprintf(f_name, "%s_%s", pkg->name, version->version);
404 path = path_new(dir, f_name, ".html");
405 f = fopen(path, "w");
408 log_err(_("failed to open: %s"), path);
412 fprintf(f, HTML_VERSION_TEMPLATE,
413 version_to_json(ppa, pkg, version));
415 footer = get_footer();
426 create_html(const char *path,
428 const char *body_template,
434 f = fopen(path, "w");
437 log_err(_("failed to open: %s"), path);
441 fprintf(f, HTML_HEADER, title, script);
442 fputs(body_template, f);
444 footer = get_footer();
451 static char *ppa_display_name(const struct ppa_stats *ppa)
455 ret = malloc(4+strlen(ppa->name)+1+strlen(ppa->owner)+1);
457 sprintf(ret, "ppa:%s/%s", ppa->owner, ppa->name);
463 index_to_html(struct ppa_stats *ppa, const char *dir)
465 char *path, *json_path, *dname;
468 json = ppa_to_json(ppa);
469 json_path = path_new(dir, "index", ".json");
471 log_debug(_("generating %s"), json_path);
472 json_object_to_file(json_path, json);
473 json_object_put(json);
476 path = path_new(dir, "index", ".html");
477 dname = ppa_display_name(ppa);
478 create_html(path, dname, HTML_INDEX_TEMPLATE, "ppastats_ppa();");
484 pkg_to_html(struct ppa_stats *ppa, struct package_stats *pkg, const char *dir)
486 char *path, *json_path, *script;
489 json_path = path_new(dir, pkg->name, ".json");
490 json = pkg_to_json(ppa, pkg);
491 log_debug(_("generating %s"), json_path);
493 json_object_to_file(json_path, json);
494 json_object_put(json);
497 path = path_new(dir, pkg->name, ".html");
498 script = malloc(strlen("ppastats_pkg(\"\");")+
502 sprintf(script, "ppastats_pkg(\"%s%s\");", pkg->name, ".json");
504 log_debug(_("generating %s"), path);
506 create_html(path, pkg->name, HTML_PKG_TEMPLATE, script);
512 pkgs_to_html(struct ppa_stats *ppa,
513 struct package_stats **pkgs,
516 struct version_stats **versions;
519 pkg_to_html(ppa, *pkgs, dir);
521 versions = (*pkgs)->versions;
523 version_to_html(ppa, *pkgs, *versions, dir);
533 ppa_to_html(const char *owner,
535 const char *package_status,
536 const char *output_dir,
537 const int install_static_files)
539 struct ppa_stats *ppastats;
541 char *css_dir, *js_dir;
543 static char *www_files[]
544 = { DEFAULT_WWW_DIR"/jquery.min.js", "js/jquery.min.js",
545 DEFAULT_WWW_DIR"/ppastats.js", "js/ppastats.js",
546 DEFAULT_WWW_DIR"/jqplot.dateAxisRenderer.min.js",
547 "js/jqplot.dateAxisRenderer.min.js",
548 DEFAULT_WWW_DIR"/jquery.jqplot.min.js",
549 "js/jquery.jqplot.min.js",
550 DEFAULT_WWW_DIR"/excanvas.js", "js/excanvas.js",
551 DEFAULT_WWW_DIR"/ppastats.css", "css/ppastats.css",
552 DEFAULT_WWW_DIR"/jquery.jqplot.min.css",
553 "css/jquery.jqplot.min.css" };
555 mkdirs(output_dir, 0777);
557 if (install_static_files) {
558 css_dir = path_append(output_dir, "css");
559 js_dir = path_append(output_dir, "js");
561 mkdir(css_dir, 0777);
564 for (i = 0; i < 7; i++) {
565 f_dst = path_append(output_dir, www_files[2*i+1]);
567 log_debug(_("copying %s %s"), www_files[2*i], f_dst);
568 fcopy(www_files[2*i], f_dst);
576 ppastats = create_ppa_stats(owner, ppa, package_status);
578 path = path_new(output_dir, "ppa", ".html");
580 pkgs_to_html(ppastats, ppastats->packages, output_dir);
582 index_to_html(ppastats, output_dir);
584 ppa_stats_free(ppastats);