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 " <div id=\"footer\">Generated by \
41 <a href='http://wpitchoune.net/ppastats'>ppastats</a></div>\n\
45 #define HTML_PKG_TEMPLATE \
46 " <h1><span id=\"pkg_name\">N/A</span></h1>\n\
47 <div id=\"charts\">\n\
48 <div id=\"chart\"></div>\n\
49 <div id=\"chart_distro\"></div>\n\
51 <div id=\"details\">\n\
53 <a href=\"index.html\">\n\
54 <span id=\"ppa_owner\">N/A</span>/\n\
55 <span id=\"ppa_name\">N/A</span>\n\
58 <div id=\"distros\"><em>Distros:</em></div>\n\
59 <div id=\"versions\"><em>Versions:</em></div>\n\
62 #define HTML_VERSION_TEMPLATE \
65 <link type=\"text/css\"\n\
68 \"http://fonts.googleapis.com/css?family=Ubuntu:regular,bold&subset=Latin\">\n\
69 <link type=\"text/css\" href=\"css/ppastats.css\" rel=\"stylesheet\" />\n\
70 <link type=\"text/css\" href=\"css/jquery.jqplot.min.css\"\n\
71 rel=\"stylesheet\" />\n\
72 <!--[if lt IE 9]><script \
73 language=\"javascript\" \
74 type=\"text/javascript\" \
75 src=\"js/excanvas.js\"></script><![endif]-->\n\
76 <script type=\"text/javascript\" src=\"js/jquery.min.js\"></script>\n\
77 <script type=\"text/javascript\"\n\
78 src=\"js/jquery.jqplot.min.js\"></script>\n\
79 <script type=\"text/javascript\"\n\
80 src=\"js/jqplot.dateAxisRenderer.min.js\"></script>\n\
81 <script type=\"text/javascript\" src=\"js/ppastats.js\"></script>\n\
82 <script>var data = %s;\n\
87 <h1><span id=\"pkg_name\">N/A</span></h1>\n\
88 <div id=\"version\"><em>Version:</em></div>\n\
90 <a href=\"index.html\">\n\
91 <span id=\"ppa_owner\">N/A</span>/<span id=\"ppa_name\">N/A</span>\n\
93 <div id=\"charts\">\n\
94 <div id=\"chart\"></div>\n\
96 <div id=\"details\">\n\
97 <div class=\"distros\">\n\
99 <ul id=\"distros\"></ul>\n\
104 #define HTML_HEADER \
108 <link type=\"text/css\"\n\
109 rel=\"stylesheet\"\n\
111 \"http://fonts.googleapis.com/css?family=Ubuntu:regular,bold&subset=Latin\">\n\
112 <link type=\"text/css\" href=\"css/ppastats.css\"\n\
113 rel=\"stylesheet\"/>\n\
114 <link type=\"text/css\" href=\"css/jquery.jqplot.min.css\"\n\
115 rel=\"stylesheet\" />\n\
116 <!--[if lt IE 9]><script \
117 language=\"javascript\" \
118 type=\"text/javascript\" \
119 src=\"js/excanvas.js\"></script><![endif]-->\n\
120 <script type=\"text/javascript\"\n\
121 src=\"js/jquery.min.js\"></script>\n\
122 <script type=\"text/javascript\"\n\
123 src=\"js/jquery.jqplot.min.js\"></script>\n\
124 <script type=\"text/javascript\"\n\
125 src=\"js/jqplot.dateAxisRenderer.min.js\"></script>\n\
126 <script type=\"text/javascript\"\n\
127 src=\"js/ppastats.js\"></script>\n\
128 <script>%s</script>\n\
132 #define HTML_INDEX_TEMPLATE \
133 " <h1><span id=\"ppa_name\">N/A</span></h1>\n\
134 <div id=\"details\">\n\
135 <div class=\"pkgs\">\n\
136 <em>Packages</em>:\n\
137 <ul id=\"pkgs\"></ul>\n\
140 <div id=\"charts\">\n\
141 <div id=\"chart\"></div>\n\
144 static char *path_new(const char *dir, const char *file, const char *suffixe)
146 char *path = malloc(strlen(dir)+1+
149 (suffixe ? strlen(suffixe) : 0) +
155 strcat(path, suffixe);
160 static struct json_object *date_to_json(struct tm *tm)
164 json = json_object_new_array();
165 json_object_array_add(json, json_object_new_int(tm->tm_year+1900));
166 json_object_array_add(json, json_object_new_int(tm->tm_mon+1));
167 json_object_array_add(json, json_object_new_int(tm->tm_mday));
172 static void json_add_ddts(json_object *json,
173 struct daily_download_total **ddts)
175 json_object *json_ddt, *json_ddts;
176 struct daily_download_total *ddt;
178 json_ddts = json_object_new_array();
179 json_object_object_add(json, "ddts", json_ddts);
187 json_ddt = json_object_new_object();
188 json_object_object_add(json_ddt,
190 json_object_new_int(ddt->count));
191 json_object_object_add(json_ddt,
193 date_to_json(&ddt->date));
195 json_object_array_add(json_ddts, json_ddt);
201 static json_object *distro_to_json(struct distro_stats *d)
205 json = json_object_new_object();
207 json_object_object_add(json,
209 json_object_new_string(d->name));
211 json_object_object_add(json,
213 json_object_new_int(d->download_count));
215 json_add_ddts(json, d->ddts);
221 pkg_to_json(struct ppa_stats *ppa, struct package_stats *pkg)
223 json_object *json, *json_versions, *json_distros, *json_distro;
224 struct version_stats **versions;
225 struct distro_stats **distros, *d;
227 json = json_object_new_object();
229 json_object_object_add(json,
230 "ppa_name", json_object_new_string(ppa->name));
231 json_object_object_add(json,
233 json_object_new_string(ppa->owner));
235 json_object_object_add(json,
236 "name", json_object_new_string(pkg->name));
238 json_versions = json_object_new_array();
239 json_object_object_add(json, "versions", json_versions);
240 versions = pkg->versions;
242 json_object_array_add
244 json_object_new_string((*versions)->version));
249 distros = pkg->distros;
251 json_distros = json_object_new_array();
252 json_object_object_add(json, "distros", json_distros);
257 if (d->download_count) {
258 json_distro = distro_to_json(d);
260 json_object_array_add(json_distros,
268 json_add_ddts(json, pkg->daily_download_totals);
273 static char *version_to_json(struct ppa_stats *ppa,
274 struct package_stats *pkg,
275 struct version_stats *ver)
278 struct distro_stats **distros, *distro;
279 json_object *json, *json_distros, *json_distro, *json_archs, *json_arch;
280 struct arch_stats **archs;
282 json = json_object_new_object();
284 json_object_object_add(json,
285 "ppa_name", json_object_new_string(ppa->name));
286 json_object_object_add(json,
288 json_object_new_string(ppa->owner));
290 json_object_object_add(json,
291 "pkg_name", json_object_new_string(pkg->name));
293 json_object_object_add(json,
294 "name", json_object_new_string(ver->version));
296 json_add_ddts(json, ver->daily_download_totals);
298 distros = ver->distros;
299 json_distros = json_object_new_array();
300 json_object_object_add(json, "distros", json_distros);
303 json_distro = json_object_new_object();
305 json_object_array_add(json_distros, json_distro);
307 json_object_object_add(json_distro,
309 json_object_new_string(distro->name));
311 archs = distro->archs;
312 json_archs = json_object_new_array();
313 json_object_object_add(json_distro, "archs", json_archs);
315 json_arch = json_object_new_object();
317 json_object_object_add
320 json_object_new_string((*archs)->name));
322 json_object_object_add
325 json_object_new_int((*archs)->download_count));
327 json_object_array_add(json_archs, json_arch);
334 ret = strdup(json_object_to_json_string(json));
336 json_object_put(json);
341 static json_object *ppa_to_json(struct ppa_stats *ppa)
343 json_object *json, *json_pkgs, *json_pkg;
344 struct package_stats **pkgs;
346 json = json_object_new_object();
348 json_object_object_add(json,
349 "ppa_name", json_object_new_string(ppa->name));
350 json_object_object_add(json,
352 json_object_new_string(ppa->owner));
354 json_add_ddts(json, ppa->daily_download_totals);
356 pkgs = ppa->packages;
357 json_pkgs = json_object_new_array();
358 json_object_object_add(json, "packages", json_pkgs);
360 json_pkg = json_object_new_object();
361 json_object_array_add(json_pkgs, json_pkg);
363 json_object_object_add(json_pkg, "name",
364 json_object_new_string((*pkgs)->name));
366 json_object_object_add
368 json_object_new_int((*pkgs)->download_count));
377 version_to_html(struct ppa_stats *ppa,
378 struct package_stats *pkg,
379 struct version_stats *version,
385 f_name = malloc(strlen(pkg->name)+1+strlen(version->version)+1);
386 sprintf(f_name, "%s_%s", pkg->name, version->version);
388 path = path_new(dir, f_name, ".html");
389 f = fopen(path, "w");
392 log_err(_("failed to open: %s"), path);
396 fprintf(f, HTML_VERSION_TEMPLATE,
397 version_to_json(ppa, pkg, version),
407 create_html(const char *path,
409 const char *body_template,
414 f = fopen(path, "w");
417 log_err(_("failed to open: %s"), path);
421 fprintf(f, HTML_HEADER, title, script);
422 fputs(body_template, f);
423 fputs(HTML_FOOTER, f);
428 static char *ppa_display_name(const struct ppa_stats *ppa)
432 ret = malloc(4+strlen(ppa->name)+1+strlen(ppa->owner)+1);
434 sprintf(ret, "ppa:%s/%s", ppa->owner, ppa->name);
440 index_to_html(struct ppa_stats *ppa, const char *dir)
442 char *path, *json_path, *dname;
445 json = ppa_to_json(ppa);
446 json_path = path_new(dir, "index", ".json");
448 log_debug(_("generating %s"), json_path);
449 json_object_to_file(json_path, json);
450 json_object_put(json);
453 path = path_new(dir, "index", ".html");
454 dname = ppa_display_name(ppa);
455 create_html(path, dname, HTML_INDEX_TEMPLATE, "ppastats_ppa();");
461 pkg_to_html(struct ppa_stats *ppa, struct package_stats *pkg, const char *dir)
463 char *path, *json_path, *script;
466 json_path = path_new(dir, pkg->name, ".json");
467 json = pkg_to_json(ppa, pkg);
468 log_debug(_("generating %s"), json_path);
470 json_object_to_file(json_path, json);
471 json_object_put(json);
474 path = path_new(dir, pkg->name, ".html");
475 script = malloc(strlen("ppastats_pkg(\"\");")+
479 sprintf(script, "ppastats_pkg(\"%s%s\");", pkg->name, ".json");
481 log_debug(_("generating %s"), path);
483 create_html(path, pkg->name, HTML_PKG_TEMPLATE, script);
489 pkgs_to_html(struct ppa_stats *ppa,
490 struct package_stats **pkgs,
493 struct version_stats **versions;
496 pkg_to_html(ppa, *pkgs, dir);
498 versions = (*pkgs)->versions;
500 version_to_html(ppa, *pkgs, *versions, dir);
510 ppa_to_html(const char *owner,
512 const char *package_status,
513 const char *output_dir,
514 const int install_static_files)
516 struct ppa_stats *ppastats;
518 char *css_dir, *js_dir;
520 static char *www_files[]
521 = { DEFAULT_WWW_DIR"/jquery.min.js", "js/jquery.min.js",
522 DEFAULT_WWW_DIR"/ppastats.js", "js/ppastats.js",
523 DEFAULT_WWW_DIR"/jqplot.dateAxisRenderer.min.js",
524 "js/jqplot.dateAxisRenderer.min.js",
525 DEFAULT_WWW_DIR"/jquery.jqplot.min.js",
526 "js/jquery.jqplot.min.js",
527 DEFAULT_WWW_DIR"/excanvas.js", "js/excanvas.js",
528 DEFAULT_WWW_DIR"/ppastats.css", "css/ppastats.css",
529 DEFAULT_WWW_DIR"/jquery.jqplot.min.css",
530 "css/jquery.jqplot.min.css" };
532 mkdirs(output_dir, 0777);
534 if (install_static_files) {
535 css_dir = path_append(output_dir, "css");
536 js_dir = path_append(output_dir, "js");
538 mkdir(css_dir, 0777);
541 for (i = 0; i < 7; i++) {
542 f_dst = path_append(output_dir, www_files[2*i+1]);
544 log_debug(_("copying %s %s"), www_files[2*i], f_dst);
545 fcopy(www_files[2*i], f_dst);
553 ppastats = create_ppa_stats(owner, ppa, package_status);
555 path = path_new(output_dir, "ppa", ".html");
557 pkgs_to_html(ppastats, ppastats->packages, output_dir);
559 index_to_html(ppastats, output_dir);
561 ppa_stats_free(ppastats);