2 Copyright (C) 2011 jeanfi@gmail.com
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU 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
25 #include <sys/types.h>
27 #include <json/json.h>
36 " <div id=\"footer\">Generated by \
37 <a href='http://wpitchoune.net/ppastats'>ppastats</a></div>\n\
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\
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 #define HTML_INDEX_TEMPLATE \
129 " <h1><span id=\"ppa_name\">N/A</span></h1>\n\
130 <div id=\"details\">\n\
131 <div class=\"pkgs\">\n\
132 <em>Packages</em>:\n\
133 <ul id=\"pkgs\"></ul>\n\
136 <div id=\"charts\">\n\
137 <div id=\"chart\"></div>\n\
140 static char *path_new(const char *dir, const char *file, const char *suffixe)
142 char *path = malloc(strlen(dir)+1+
145 (suffixe ? strlen(suffixe) : 0) +
151 strcat(path, suffixe);
156 static struct json_object *date_to_json(struct tm *tm)
160 json = json_object_new_array();
161 json_object_array_add(json, json_object_new_int(tm->tm_year+1900));
162 json_object_array_add(json, json_object_new_int(tm->tm_mon+1));
163 json_object_array_add(json, json_object_new_int(tm->tm_mday));
168 static void json_add_ddts(json_object *json,
169 struct daily_download_total **ddts)
171 json_object *json_ddt, *json_ddts;
172 struct daily_download_total *ddt;
174 json_ddts = json_object_new_array();
175 json_object_object_add(json, "ddts", json_ddts);
183 json_ddt = json_object_new_object();
184 json_object_object_add(json_ddt,
186 json_object_new_int(ddt->count));
187 json_object_object_add(json_ddt,
189 date_to_json(&ddt->date));
191 json_object_array_add(json_ddts, json_ddt);
197 static json_object *distro_to_json(struct distro_stats *d)
201 json = json_object_new_object();
203 json_object_object_add(json,
205 json_object_new_string(d->name));
207 json_object_object_add(json,
209 json_object_new_int(d->download_count));
211 json_add_ddts(json, d->ddts);
217 pkg_to_json(struct ppa_stats *ppa, struct package_stats *pkg)
219 json_object *json, *json_versions, *json_distros, *json_distro;
220 struct version_stats **versions;
221 struct distro_stats **distros, *d;
223 json = json_object_new_object();
225 json_object_object_add(json,
226 "ppa_name", json_object_new_string(ppa->name));
227 json_object_object_add(json,
229 json_object_new_string(ppa->owner));
231 json_object_object_add(json,
232 "name", json_object_new_string(pkg->name));
234 json_versions = json_object_new_array();
235 json_object_object_add(json, "versions", json_versions);
236 versions = pkg->versions;
238 json_object_array_add
240 json_object_new_string((*versions)->version));
245 distros = pkg->distros;
247 json_distros = json_object_new_array();
248 json_object_object_add(json, "distros", json_distros);
253 if (d->download_count) {
254 json_distro = distro_to_json(d);
256 json_object_array_add(json_distros,
264 json_add_ddts(json, pkg->daily_download_totals);
269 static char *version_to_json(struct ppa_stats *ppa,
270 struct package_stats *pkg,
271 struct version_stats *ver)
274 struct distro_stats **distros, *distro;
275 json_object *json, *json_distros, *json_distro, *json_archs, *json_arch;
276 struct arch_stats **archs;
278 json = json_object_new_object();
280 json_object_object_add(json,
281 "ppa_name", json_object_new_string(ppa->name));
282 json_object_object_add(json,
284 json_object_new_string(ppa->owner));
286 json_object_object_add(json,
287 "pkg_name", json_object_new_string(pkg->name));
289 json_object_object_add(json,
290 "name", json_object_new_string(ver->version));
292 json_add_ddts(json, ver->daily_download_totals);
294 distros = ver->distros;
295 json_distros = json_object_new_array();
296 json_object_object_add(json, "distros", json_distros);
299 json_distro = json_object_new_object();
301 json_object_array_add(json_distros, json_distro);
303 json_object_object_add(json_distro,
305 json_object_new_string(distro->name));
307 archs = distro->archs;
308 json_archs = json_object_new_array();
309 json_object_object_add(json_distro, "archs", json_archs);
311 json_arch = json_object_new_object();
313 json_object_object_add
316 json_object_new_string((*archs)->name));
318 json_object_object_add
321 json_object_new_int((*archs)->download_count));
323 json_object_array_add(json_archs, json_arch);
330 ret = strdup(json_object_to_json_string(json));
332 json_object_put(json);
337 static json_object *ppa_to_json(struct ppa_stats *ppa)
339 json_object *json, *json_pkgs, *json_pkg;
340 struct package_stats **pkgs;
342 json = json_object_new_object();
344 json_object_object_add(json,
345 "ppa_name", json_object_new_string(ppa->name));
346 json_object_object_add(json,
348 json_object_new_string(ppa->owner));
350 json_add_ddts(json, ppa->daily_download_totals);
352 pkgs = ppa->packages;
353 json_pkgs = json_object_new_array();
354 json_object_object_add(json, "packages", json_pkgs);
356 json_pkg = json_object_new_object();
357 json_object_array_add(json_pkgs, json_pkg);
359 json_object_object_add(json_pkg, "name",
360 json_object_new_string((*pkgs)->name));
362 json_object_object_add
364 json_object_new_int((*pkgs)->download_count));
373 version_to_html(struct ppa_stats *ppa,
374 struct package_stats *pkg,
375 struct version_stats *version,
381 f_name = malloc(strlen(pkg->name)+1+strlen(version->version)+1);
382 sprintf(f_name, "%s_%s", pkg->name, version->version);
384 path = path_new(dir, f_name, ".html");
385 f = fopen(path, "w");
388 fprintf(stderr, "ERROR: failed to open: %s\n", path);
392 fprintf(f, HTML_VERSION_TEMPLATE,
393 version_to_json(ppa, pkg, version),
403 create_html(const char *path,
405 const char *body_template,
410 f = fopen(path, "w");
413 fprintf(stderr, "ERROR: failed to open: %s\n", path);
417 fprintf(f, HTML_HEADER, title, script);
418 fputs(body_template, f);
419 fputs(HTML_FOOTER, f);
424 static char *ppa_display_name(const struct ppa_stats *ppa)
428 ret = malloc(4+strlen(ppa->name)+1+strlen(ppa->owner)+1);
430 sprintf(ret, "ppa:%s/%s", ppa->owner, ppa->name);
436 index_to_html(struct ppa_stats *ppa, const char *dir)
438 char *path, *json_path, *dname;
441 json = ppa_to_json(ppa);
442 json_path = path_new(dir, "index", ".json");
444 printf("DEBUG: generating %s\n", json_path);
445 json_object_to_file(json_path, json);
446 json_object_put(json);
449 path = path_new(dir, "index", ".html");
450 dname = ppa_display_name(ppa);
451 create_html(path, dname, HTML_INDEX_TEMPLATE, "ppastats_ppa();");
457 pkg_to_html(struct ppa_stats *ppa, struct package_stats *pkg, const char *dir)
459 char *path, *json_path, *script;
462 json_path = path_new(dir, pkg->name, ".json");
463 json = pkg_to_json(ppa, pkg);
465 printf("DEBUG: generating %s\n", json_path);
466 json_object_to_file(json_path, json);
467 json_object_put(json);
470 path = path_new(dir, pkg->name, ".html");
471 script = malloc(strlen("ppastats_pkg(\"\");")+
475 sprintf(script, "ppastats_pkg(\"%s%s\");", pkg->name, ".json");
478 printf("DEBUG: generating %s\n", path);
480 create_html(path, pkg->name, HTML_PKG_TEMPLATE, script);
486 pkgs_to_html(struct ppa_stats *ppa,
487 struct package_stats **pkgs,
490 struct version_stats **versions;
493 pkg_to_html(ppa, *pkgs, dir);
495 versions = (*pkgs)->versions;
497 version_to_html(ppa, *pkgs, *versions, dir);
507 ppa_to_html(const char *owner,
509 const char *package_status,
510 const char *output_dir,
511 const int install_static_files)
513 struct ppa_stats *ppastats;
515 char *css_dir, *js_dir;
517 static char *www_files[]
518 = { DEFAULT_WWW_DIR"/jquery.min.js", "js/jquery.min.js",
519 DEFAULT_WWW_DIR"/ppastats.js", "js/ppastats.js",
520 DEFAULT_WWW_DIR"/jqplot.dateAxisRenderer.min.js",
521 "js/jqplot.dateAxisRenderer.min.js",
522 DEFAULT_WWW_DIR"/jquery.jqplot.min.js",
523 "js/jquery.jqplot.min.js",
524 DEFAULT_WWW_DIR"/excanvas.js", "js/excanvas.js",
525 DEFAULT_WWW_DIR"/ppastats.css", "css/ppastats.css",
526 DEFAULT_WWW_DIR"/jquery.jqplot.min.css",
527 "css/jquery.jqplot.min.css" };
529 mkdirs(output_dir, 0777);
531 if (install_static_files) {
532 css_dir = path_append(output_dir, "css");
533 js_dir = path_append(output_dir, "js");
535 mkdir(css_dir, 0777);
538 for (i = 0; i < 7; i++) {
539 f_dst = path_append(output_dir, www_files[2*i+1]);
542 printf("DEBUG: copying %s %s\n",
543 www_files[2*i], f_dst);
544 fcopy(www_files[2*i], f_dst);
552 ppastats = create_ppa_stats(owner, ppa, package_status);
554 path = path_new(output_dir, "ppa", ".html");
556 pkgs_to_html(ppastats, ppastats->packages, output_dir);
558 index_to_html(ppastats, output_dir);
560 ppa_stats_free(ppastats);