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;
42 #define HTML_PKG_TEMPLATE \
43 " <h1><span id=\"pkg_name\">N/A</span></h1>\n\
44 <div id=\"charts\">\n\
45 <div id=\"chart\"></div>\n\
46 <div id=\"chart_distro\"></div>\n\
48 <div id=\"details\">\n\
50 <a href=\"index.html\">\n\
51 <span id=\"ppa_owner\">N/A</span>/\n\
52 <span id=\"ppa_name\">N/A</span>\n\
55 <div id=\"distros\"><em>Distros:</em></div>\n\
56 <div id=\"versions\"><em>Versions:</em></div>\n\
59 #define HTML_VERSION_TEMPLATE \
62 <link type=\"text/css\"\n\
65 \"http://fonts.googleapis.com/css?family=Ubuntu:regular,bold&subset=Latin\">\n\
66 <link type=\"text/css\" href=\"css/ppastats.css\" rel=\"stylesheet\" />\n\
67 <link type=\"text/css\" href=\"css/jquery.jqplot.min.css\"\n\
68 rel=\"stylesheet\" />\n\
69 <!--[if lt IE 9]><script \
70 language=\"javascript\" \
71 type=\"text/javascript\" \
72 src=\"js/excanvas.js\"></script><![endif]-->\n\
73 <script type=\"text/javascript\" src=\"js/jquery.min.js\"></script>\n\
74 <script type=\"text/javascript\"\n\
75 src=\"js/jquery.jqplot.min.js\"></script>\n\
76 <script type=\"text/javascript\"\n\
77 src=\"js/jqplot.dateAxisRenderer.min.js\"></script>\n\
78 <script type=\"text/javascript\" src=\"js/ppastats.js\"></script>\n\
79 <script>var data = %s;\n\
84 <h1><span id=\"pkg_name\">N/A</span></h1>\n\
85 <div id=\"version\"><em>Version:</em></div>\n\
87 <a href=\"index.html\">\n\
88 <span id=\"ppa_owner\">N/A</span>/<span id=\"ppa_name\">N/A</span>\n\
90 <div id=\"charts\">\n\
91 <div id=\"chart\"></div>\n\
93 <div id=\"details\">\n\
94 <div class=\"distros\">\n\
96 <ul id=\"distros\"></ul>\n\
100 #define HTML_HEADER \
104 <link type=\"text/css\"\n\
105 rel=\"stylesheet\"\n\
107 \"http://fonts.googleapis.com/css?family=Ubuntu:regular,bold&subset=Latin\">\n\
108 <link type=\"text/css\" href=\"css/ppastats.css\"\n\
109 rel=\"stylesheet\"/>\n\
110 <link type=\"text/css\" href=\"css/jquery.jqplot.min.css\"\n\
111 rel=\"stylesheet\" />\n\
112 <!--[if lt IE 9]><script \
113 language=\"javascript\" \
114 type=\"text/javascript\" \
115 src=\"js/excanvas.js\"></script><![endif]-->\n\
116 <script type=\"text/javascript\"\n\
117 src=\"js/jquery.min.js\"></script>\n\
118 <script type=\"text/javascript\"\n\
119 src=\"js/jquery.jqplot.min.js\"></script>\n\
120 <script type=\"text/javascript\"\n\
121 src=\"js/jqplot.dateAxisRenderer.min.js\"></script>\n\
122 <script type=\"text/javascript\"\n\
123 src=\"js/ppastats.js\"></script>\n\
124 <script>%s</script>\n\
128 static char *path_new(const char *dir, const char *file, const char *suffixe)
132 /* [dir]/[file][suffixe] */
133 path = malloc(strlen(dir)+1+
135 (suffixe ? strlen(suffixe) : 0) +
141 strcat(path, suffixe);
146 static const char *get_footer()
151 path = DEFAULT_WWW_DIR"/footer.tpl";
152 footer = file_get_content(path);
155 log_err("Failed to read footer template: %s", path);
161 static const char *get_ppa_body()
166 path = DEFAULT_WWW_DIR"/ppa.tpl";
167 ppa_body = file_get_content(path);
170 log_err("Failed to read PPA template: %s", path);
177 static struct json_object *date_to_json(struct tm *tm)
181 json = json_object_new_array();
182 json_object_array_add(json, json_object_new_int(tm->tm_year+1900));
183 json_object_array_add(json, json_object_new_int(tm->tm_mon+1));
184 json_object_array_add(json, json_object_new_int(tm->tm_mday));
189 static void json_add_ddts(json_object *json,
190 struct daily_download_total **ddts)
192 json_object *json_ddt, *json_ddts;
193 struct daily_download_total *ddt;
195 json_ddts = json_object_new_array();
196 json_object_object_add(json, "ddts", json_ddts);
204 json_ddt = json_object_new_object();
205 json_object_object_add(json_ddt,
207 json_object_new_int(ddt->count));
208 json_object_object_add(json_ddt,
210 date_to_json(&ddt->date));
212 json_object_array_add(json_ddts, json_ddt);
218 static json_object *distro_to_json(struct distro_stats *d)
222 json = json_object_new_object();
224 json_object_object_add(json,
226 json_object_new_string(d->name));
228 json_object_object_add(json,
230 json_object_new_int(d->download_count));
232 json_add_ddts(json, d->ddts);
238 pkg_to_json(struct ppa_stats *ppa, struct package_stats *pkg)
240 json_object *json, *json_versions, *json_distros, *json_distro;
241 struct version_stats **versions;
242 struct distro_stats **distros, *d;
244 json = json_object_new_object();
246 json_object_object_add(json,
247 "ppa_name", json_object_new_string(ppa->name));
248 json_object_object_add(json,
250 json_object_new_string(ppa->owner));
252 json_object_object_add(json,
253 "name", json_object_new_string(pkg->name));
255 json_versions = json_object_new_array();
256 json_object_object_add(json, "versions", json_versions);
257 versions = pkg->versions;
259 json_object_array_add
261 json_object_new_string((*versions)->version));
266 distros = pkg->distros;
268 json_distros = json_object_new_array();
269 json_object_object_add(json, "distros", json_distros);
274 if (d->download_count) {
275 json_distro = distro_to_json(d);
277 json_object_array_add(json_distros,
285 json_add_ddts(json, pkg->daily_download_totals);
290 static char *version_to_json(struct ppa_stats *ppa,
291 struct package_stats *pkg,
292 struct version_stats *ver)
295 struct distro_stats **distros, *distro;
296 json_object *json, *json_distros, *json_distro, *json_archs, *json_arch;
297 struct arch_stats **archs;
299 json = json_object_new_object();
301 json_object_object_add(json,
302 "ppa_name", json_object_new_string(ppa->name));
303 json_object_object_add(json,
305 json_object_new_string(ppa->owner));
307 json_object_object_add(json,
308 "pkg_name", json_object_new_string(pkg->name));
310 json_object_object_add(json,
311 "name", json_object_new_string(ver->version));
313 json_add_ddts(json, ver->daily_download_totals);
315 distros = ver->distros;
316 json_distros = json_object_new_array();
317 json_object_object_add(json, "distros", json_distros);
320 json_distro = json_object_new_object();
322 json_object_array_add(json_distros, json_distro);
324 json_object_object_add(json_distro,
326 json_object_new_string(distro->name));
328 archs = distro->archs;
329 json_archs = json_object_new_array();
330 json_object_object_add(json_distro, "archs", json_archs);
332 json_arch = json_object_new_object();
334 json_object_object_add
337 json_object_new_string((*archs)->name));
339 json_object_object_add
342 json_object_new_int((*archs)->download_count));
344 json_object_array_add(json_archs, json_arch);
351 ret = strdup(json_object_to_json_string(json));
353 json_object_put(json);
358 static json_object *ppa_to_json(struct ppa_stats *ppa)
360 json_object *json, *json_pkgs, *json_pkg;
361 struct package_stats **pkgs;
363 json = json_object_new_object();
365 json_object_object_add(json,
366 "ppa_name", json_object_new_string(ppa->name));
367 json_object_object_add(json,
369 json_object_new_string(ppa->owner));
371 json_add_ddts(json, ppa->daily_download_totals);
373 pkgs = ppa->packages;
374 json_pkgs = json_object_new_array();
375 json_object_object_add(json, "packages", json_pkgs);
377 json_pkg = json_object_new_object();
378 json_object_array_add(json_pkgs, json_pkg);
380 json_object_object_add(json_pkg, "name",
381 json_object_new_string((*pkgs)->name));
383 json_object_object_add
385 json_object_new_int((*pkgs)->download_count));
394 version_to_html(struct ppa_stats *ppa,
395 struct package_stats *pkg,
396 struct version_stats *version,
403 f_name = malloc(strlen(pkg->name)+1+strlen(version->version)+1);
404 sprintf(f_name, "%s_%s", pkg->name, version->version);
406 path = path_new(dir, f_name, ".html");
407 f = fopen(path, "w");
410 log_err(_("failed to open: %s"), path);
414 fprintf(f, HTML_VERSION_TEMPLATE,
415 version_to_json(ppa, pkg, version));
417 footer = get_footer();
428 create_html(const char *path,
430 const char *body_template,
436 f = fopen(path, "w");
439 log_err(_("failed to open: %s"), path);
443 fprintf(f, HTML_HEADER, title, script);
444 fputs(body_template, f);
446 footer = get_footer();
453 static char *ppa_display_name(const struct ppa_stats *ppa)
457 ret = malloc(4+strlen(ppa->name)+1+strlen(ppa->owner)+1);
459 sprintf(ret, "ppa:%s/%s", ppa->owner, ppa->name);
465 index_to_html(struct ppa_stats *ppa, const char *dir)
467 char *path, *json_path, *dname;
471 body = get_ppa_body();
473 log_err("Failed to create PPA page");
477 json = ppa_to_json(ppa);
478 json_path = path_new(dir, "index", ".json");
480 log_debug(_("generating %s"), json_path);
481 json_object_to_file(json_path, json);
482 json_object_put(json);
485 path = path_new(dir, "index", ".html");
486 dname = ppa_display_name(ppa);
487 create_html(path, dname, body, "ppastats_ppa();");
493 pkg_to_html(struct ppa_stats *ppa, struct package_stats *pkg, const char *dir)
495 char *path, *json_path, *script;
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, HTML_PKG_TEMPLATE, 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);