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>
39 static const char *footer;
40 static const char *ppa_body;
41 static const char *pkg_body;
42 static const char *pkg_version_body;
43 static const char *header;
45 static char *path_new(const char *dir, const char *file, const char *suffixe)
49 /* [dir]/[file][suffixe] */
50 path = malloc(strlen(dir)+1+
52 (suffixe ? strlen(suffixe) : 0) +
58 strcat(path, suffixe);
63 static const char *get_header()
68 path = DEFAULT_WWW_DIR"/header.tpl";
69 header = file_get_content(path);
72 log_err("Failed to read header template: %s", path);
78 static const char *get_footer()
83 path = DEFAULT_WWW_DIR"/footer.tpl";
84 footer = file_get_content(path);
87 log_err("Failed to read footer template: %s", path);
93 static const char *get_pkg_version_body()
97 if (!pkg_version_body) {
98 path = DEFAULT_WWW_DIR"/pkg_version.tpl";
99 pkg_version_body = file_get_content(path);
101 if (!pkg_version_body)
102 log_err("Failed to read package version template: %s",
106 return pkg_version_body;
108 static const char *get_ppa_body()
113 path = DEFAULT_WWW_DIR"/ppa.tpl";
114 ppa_body = file_get_content(path);
117 log_err("Failed to read PPA template: %s", path);
123 static const char *get_pkg_body()
128 path = DEFAULT_WWW_DIR"/pkg.tpl";
129 pkg_body = file_get_content(path);
132 log_err("Failed to read package template: %s", path);
138 static struct json_object *date_to_json(struct tm *tm)
142 json = json_object_new_array();
143 json_object_array_add(json, json_object_new_int(tm->tm_year+1900));
144 json_object_array_add(json, json_object_new_int(tm->tm_mon+1));
145 json_object_array_add(json, json_object_new_int(tm->tm_mday));
150 static void json_add_ddts(json_object *json,
151 struct daily_download_total **ddts)
153 json_object *json_ddt, *json_ddts;
154 struct daily_download_total *ddt;
156 json_ddts = json_object_new_array();
157 json_object_object_add(json, "ddts", json_ddts);
165 json_ddt = json_object_new_object();
166 json_object_object_add(json_ddt,
168 json_object_new_int(ddt->count));
169 json_object_object_add(json_ddt,
171 date_to_json(&ddt->date));
173 json_object_array_add(json_ddts, json_ddt);
179 static json_object *distro_to_json(struct distro_stats *d)
183 json = json_object_new_object();
185 json_object_object_add(json,
187 json_object_new_string(d->name));
189 json_object_object_add(json,
191 json_object_new_int(d->download_count));
193 json_add_ddts(json, d->ddts);
199 pkg_to_json(struct ppa_stats *ppa, struct package_stats *pkg)
201 json_object *json, *json_versions, *json_distros, *json_distro;
202 struct version_stats **versions;
203 struct distro_stats **distros, *d;
205 json = json_object_new_object();
207 json_object_object_add(json,
208 "ppa_name", json_object_new_string(ppa->name));
209 json_object_object_add(json,
211 json_object_new_string(ppa->owner));
213 json_object_object_add(json,
214 "name", json_object_new_string(pkg->name));
216 json_versions = json_object_new_array();
217 json_object_object_add(json, "versions", json_versions);
218 versions = pkg->versions;
220 json_object_array_add
222 json_object_new_string((*versions)->version));
227 distros = pkg->distros;
229 json_distros = json_object_new_array();
230 json_object_object_add(json, "distros", json_distros);
235 if (d->download_count) {
236 json_distro = distro_to_json(d);
238 json_object_array_add(json_distros,
246 json_add_ddts(json, pkg->daily_download_totals);
251 static char *version_to_json(struct ppa_stats *ppa,
252 struct package_stats *pkg,
253 struct version_stats *ver)
256 struct distro_stats **distros, *distro;
257 json_object *json, *json_distros, *json_distro, *json_archs, *json_arch;
258 struct arch_stats **archs;
260 json = json_object_new_object();
262 json_object_object_add(json,
263 "ppa_name", json_object_new_string(ppa->name));
264 json_object_object_add(json,
266 json_object_new_string(ppa->owner));
268 json_object_object_add(json,
269 "pkg_name", json_object_new_string(pkg->name));
271 json_object_object_add(json,
272 "name", json_object_new_string(ver->version));
274 json_add_ddts(json, ver->daily_download_totals);
276 distros = ver->distros;
277 json_distros = json_object_new_array();
278 json_object_object_add(json, "distros", json_distros);
281 json_distro = json_object_new_object();
283 json_object_array_add(json_distros, json_distro);
285 json_object_object_add(json_distro,
287 json_object_new_string(distro->name));
289 archs = distro->archs;
290 json_archs = json_object_new_array();
291 json_object_object_add(json_distro, "archs", json_archs);
293 json_arch = json_object_new_object();
295 json_object_object_add
298 json_object_new_string((*archs)->name));
300 json_object_object_add
303 json_object_new_int((*archs)->download_count));
305 json_object_array_add(json_archs, json_arch);
312 ret = strdup(json_object_to_json_string(json));
314 json_object_put(json);
319 static json_object *ppa_to_json(struct ppa_stats *ppa)
321 json_object *json, *json_pkgs, *json_pkg;
322 struct package_stats **pkgs;
324 json = json_object_new_object();
326 json_object_object_add(json,
327 "ppa_name", json_object_new_string(ppa->name));
328 json_object_object_add(json,
330 json_object_new_string(ppa->owner));
332 json_add_ddts(json, ppa->daily_download_totals);
334 pkgs = ppa->packages;
335 json_pkgs = json_object_new_array();
336 json_object_object_add(json, "packages", json_pkgs);
338 json_pkg = json_object_new_object();
339 json_object_array_add(json_pkgs, json_pkg);
341 json_object_object_add(json_pkg, "name",
342 json_object_new_string((*pkgs)->name));
344 json_object_object_add
346 json_object_new_int((*pkgs)->download_count));
355 version_to_html(struct ppa_stats *ppa,
356 struct package_stats *pkg,
357 struct version_stats *version,
365 pkg_ver = get_pkg_version_body();
367 log_err("Failed to get package version template");
371 f_name = malloc(strlen(pkg->name)+1+strlen(version->version)+1);
372 sprintf(f_name, "%s_%s", pkg->name, version->version);
374 path = path_new(dir, f_name, ".html");
375 f = fopen(path, "w");
378 log_err(_("failed to open: %s"), path);
382 fprintf(f, pkg_ver, version_to_json(ppa, pkg, version));
384 footer = get_footer();
395 create_html(const char *path,
397 const char *body_template,
404 header = get_header();
406 log_err(_("Failed to get the header template"));
410 f = fopen(path, "w");
413 log_err(_("Failed to open: %s"), path);
417 fprintf(f, header, title, script);
418 fputs(body_template, f);
420 footer = get_footer();
427 static char *ppa_display_name(const struct ppa_stats *ppa)
431 ret = malloc(4+strlen(ppa->name)+1+strlen(ppa->owner)+1);
433 sprintf(ret, "ppa:%s/%s", ppa->owner, ppa->name);
439 index_to_html(struct ppa_stats *ppa, const char *dir)
441 char *path, *json_path, *dname;
445 body = get_ppa_body();
447 log_err("Failed to create PPA page");
451 json = ppa_to_json(ppa);
452 json_path = path_new(dir, "index", ".json");
454 log_debug(_("generating %s"), json_path);
455 json_object_to_file(json_path, json);
456 json_object_put(json);
459 path = path_new(dir, "index", ".html");
460 dname = ppa_display_name(ppa);
461 create_html(path, dname, body, "ppastats_ppa();");
467 pkg_to_html(struct ppa_stats *ppa, struct package_stats *pkg, const char *dir)
469 char *path, *json_path, *script;
473 body = get_pkg_body();
475 log_err("Failed to create package page: %s", pkg->name);
479 json_path = path_new(dir, pkg->name, ".json");
480 json = pkg_to_json(ppa, pkg);
481 log_debug(_("Generating %s"), json_path);
483 json_object_to_file(json_path, json);
484 json_object_put(json);
487 path = path_new(dir, pkg->name, ".html");
488 script = malloc(strlen("ppastats_pkg(\"\");")+
492 sprintf(script, "ppastats_pkg(\"%s%s\");", pkg->name, ".json");
494 log_debug(_("Generating %s"), path);
496 create_html(path, pkg->name, body, script);
502 pkgs_to_html(struct ppa_stats *ppa,
503 struct package_stats **pkgs,
506 struct version_stats **versions;
509 pkg_to_html(ppa, *pkgs, dir);
511 versions = (*pkgs)->versions;
513 version_to_html(ppa, *pkgs, *versions, dir);
523 ppa_to_html(const char *owner,
525 const char *package_status,
526 const char *output_dir,
527 const int install_static_files)
529 struct ppa_stats *ppastats;
531 char *css_dir, *js_dir;
533 static char *www_files[]
534 = { DEFAULT_WWW_DIR"/jquery.min.js", "js/jquery.min.js",
535 DEFAULT_WWW_DIR"/ppastats.js", "js/ppastats.js",
536 DEFAULT_WWW_DIR"/jqplot.dateAxisRenderer.min.js",
537 "js/jqplot.dateAxisRenderer.min.js",
538 DEFAULT_WWW_DIR"/jquery.jqplot.min.js",
539 "js/jquery.jqplot.min.js",
540 DEFAULT_WWW_DIR"/excanvas.js", "js/excanvas.js",
541 DEFAULT_WWW_DIR"/ppastats.css", "css/ppastats.css",
542 DEFAULT_WWW_DIR"/jquery.jqplot.min.css",
543 "css/jquery.jqplot.min.css" };
545 mkdirs(output_dir, 0777);
547 if (install_static_files) {
548 css_dir = path_append(output_dir, "css");
549 js_dir = path_append(output_dir, "js");
551 mkdir(css_dir, 0777);
554 for (i = 0; i < 7; i++) {
555 f_dst = path_append(output_dir, www_files[2*i+1]);
557 log_debug(_("Copying %s %s"), www_files[2*i], f_dst);
558 fcopy(www_files[2*i], f_dst);
566 ppastats = create_ppa_stats(owner, ppa, package_status);
568 path = path_new(output_dir, "ppa", ".html");
570 pkgs_to_html(ppastats, ppastats->packages, output_dir);
572 index_to_html(ppastats, output_dir);
574 ppa_stats_free(ppastats);