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 <script type=\"text/javascript\" src=\"js/jquery.min.js\"></script>\n\
69 <script type=\"text/javascript\"\n\
70 src=\"js/jquery.jqplot.min.js\"></script>\n\
71 <script type=\"text/javascript\"\n\
72 src=\"js/jqplot.dateAxisRenderer.min.js\"></script>\n\
73 <script type=\"text/javascript\" src=\"js/ppastats.js\"></script>\n\
74 <script>var data = %s;\n\
79 <h1><span id=\"pkg_name\">N/A</span></h1>\n\
80 <div id=\"version\"><em>Version:</em></div>\n\
82 <a href=\"index.html\">\n\
83 <span id=\"ppa_owner\">N/A</span>/<span id=\"ppa_name\">N/A</span>\n\
85 <div id=\"charts\">\n\
86 <div id=\"chart\"></div>\n\
88 <div id=\"details\">\n\
89 <div class=\"distros\">\n\
91 <ul id=\"distros\"></ul>\n\
100 <link type=\"text/css\"\n\
101 rel=\"stylesheet\"\n\
103 \"http://fonts.googleapis.com/css?family=Ubuntu:regular,bold&subset=Latin\">\n\
104 <link type=\"text/css\" href=\"css/ppastats.css\"\n\
105 rel=\"stylesheet\"/>\n\
106 <link type=\"text/css\" href=\"css/jquery.jqplot.min.css\"\n\
107 rel=\"stylesheet\" />\n\
108 <script type=\"text/javascript\"\n\
109 src=\"js/jquery.min.js\"></script>\n\
110 <script type=\"text/javascript\"\n\
111 src=\"js/jquery.jqplot.min.js\"></script>\n\
112 <script type=\"text/javascript\"\n\
113 src=\"js/jqplot.dateAxisRenderer.min.js\"></script>\n\
114 <script type=\"text/javascript\"\n\
115 src=\"js/ppastats.js\"></script>\n\
116 <script>%s</script>\n\
120 #define HTML_INDEX_TEMPLATE \
121 " <h1><span id=\"ppa_name\">N/A</span></h1>\n\
122 <div id=\"details\">\n\
123 <div class=\"pkgs\">\n\
124 <em>Packages</em>:\n\
125 <ul id=\"pkgs\"></ul>\n\
128 <div id=\"charts\">\n\
129 <div id=\"chart\"></div>\n\
132 static char *path_new(const char *dir, const char *file, const char *suffixe)
134 char *path = malloc(strlen(dir)+1+
137 (suffixe ? strlen(suffixe) : 0) +
143 strcat(path, suffixe);
148 static struct json_object *date_to_json(struct tm *tm)
152 json = json_object_new_array();
153 json_object_array_add(json, json_object_new_int(tm->tm_year+1900));
154 json_object_array_add(json, json_object_new_int(tm->tm_mon+1));
155 json_object_array_add(json, json_object_new_int(tm->tm_mday));
160 static void json_add_ddts(json_object *json,
161 struct daily_download_total **ddts)
163 json_object *json_ddt, *json_ddts;
164 struct daily_download_total *ddt;
166 json_ddts = json_object_new_array();
167 json_object_object_add(json, "ddts", json_ddts);
175 json_ddt = json_object_new_object();
176 json_object_object_add(json_ddt,
178 json_object_new_int(ddt->count));
179 json_object_object_add(json_ddt,
181 date_to_json(&ddt->date));
183 json_object_array_add(json_ddts, json_ddt);
189 static json_object *distro_to_json(struct distro_stats *d)
193 json = json_object_new_object();
195 json_object_object_add(json,
197 json_object_new_string(d->name));
199 json_object_object_add(json,
201 json_object_new_int(d->download_count));
203 json_add_ddts(json, d->ddts);
209 pkg_to_json(struct ppa_stats *ppa, struct package_stats *pkg)
211 json_object *json, *json_versions, *json_distros, *json_distro;
212 struct version_stats **versions;
213 struct distro_stats **distros, *d;
215 json = json_object_new_object();
217 json_object_object_add(json,
218 "ppa_name", json_object_new_string(ppa->name));
219 json_object_object_add(json,
221 json_object_new_string(ppa->owner));
223 json_object_object_add(json,
224 "name", json_object_new_string(pkg->name));
226 json_versions = json_object_new_array();
227 json_object_object_add(json, "versions", json_versions);
228 versions = pkg->versions;
230 json_object_array_add
232 json_object_new_string((*versions)->version));
237 distros = pkg->distros;
239 json_distros = json_object_new_array();
240 json_object_object_add(json, "distros", json_distros);
245 if (d->download_count) {
246 json_distro = distro_to_json(d);
248 json_object_array_add(json_distros,
256 json_add_ddts(json, pkg->daily_download_totals);
261 static char *version_to_json(struct ppa_stats *ppa,
262 struct package_stats *pkg,
263 struct version_stats *ver)
266 struct distro_stats **distros, *distro;
267 json_object *json, *json_distros, *json_distro, *json_archs, *json_arch;
268 struct arch_stats **archs;
270 json = json_object_new_object();
272 json_object_object_add(json,
273 "ppa_name", json_object_new_string(ppa->name));
274 json_object_object_add(json,
276 json_object_new_string(ppa->owner));
278 json_object_object_add(json,
279 "pkg_name", json_object_new_string(pkg->name));
281 json_object_object_add(json,
282 "name", json_object_new_string(ver->version));
284 json_add_ddts(json, ver->daily_download_totals);
286 distros = ver->distros;
287 json_distros = json_object_new_array();
288 json_object_object_add(json, "distros", json_distros);
291 json_distro = json_object_new_object();
293 json_object_array_add(json_distros, json_distro);
295 json_object_object_add(json_distro,
297 json_object_new_string(distro->name));
299 archs = distro->archs;
300 json_archs = json_object_new_array();
301 json_object_object_add(json_distro, "archs", json_archs);
303 json_arch = json_object_new_object();
305 json_object_object_add
308 json_object_new_string((*archs)->name));
310 json_object_object_add
313 json_object_new_int((*archs)->download_count));
315 json_object_array_add(json_archs, json_arch);
322 ret = strdup(json_object_to_json_string(json));
324 json_object_put(json);
329 static json_object *ppa_to_json(struct ppa_stats *ppa)
331 json_object *json, *json_pkgs, *json_pkg;
332 struct package_stats **pkgs;
334 json = json_object_new_object();
336 json_object_object_add(json,
337 "ppa_name", json_object_new_string(ppa->name));
338 json_object_object_add(json,
340 json_object_new_string(ppa->owner));
342 json_add_ddts(json, ppa->daily_download_totals);
344 pkgs = ppa->packages;
345 json_pkgs = json_object_new_array();
346 json_object_object_add(json, "packages", json_pkgs);
348 json_pkg = json_object_new_object();
349 json_object_array_add(json_pkgs, json_pkg);
351 json_object_object_add(json_pkg, "name",
352 json_object_new_string((*pkgs)->name));
354 json_object_object_add
356 json_object_new_int((*pkgs)->download_count));
365 version_to_html(struct ppa_stats *ppa,
366 struct package_stats *pkg,
367 struct version_stats *version,
373 f_name = malloc(strlen(pkg->name)+1+strlen(version->version)+1);
374 sprintf(f_name, "%s_%s", pkg->name, version->version);
376 path = path_new(dir, f_name, ".html");
377 f = fopen(path, "w");
380 fprintf(stderr, "ERROR: failed to open: %s\n", path);
384 fprintf(f, HTML_VERSION_TEMPLATE,
385 version_to_json(ppa, pkg, version),
395 create_html(const char *path,
397 const char *body_template,
402 f = fopen(path, "w");
405 fprintf(stderr, "ERROR: failed to open: %s\n", path);
409 fprintf(f, HTML_HEADER, title, script);
410 fputs(body_template, f);
411 fputs(HTML_FOOTER, f);
416 static char *ppa_display_name(const struct ppa_stats *ppa)
420 ret = malloc(4+strlen(ppa->name)+1+strlen(ppa->owner)+1);
422 sprintf(ret, "ppa:%s/%s", ppa->owner, ppa->name);
428 index_to_html(struct ppa_stats *ppa, const char *dir)
430 char *path, *json_path, *dname;
433 json = ppa_to_json(ppa);
434 json_path = path_new(dir, "index", ".json");
436 printf("DEBUG: generating %s\n", json_path);
437 json_object_to_file(json_path, json);
438 json_object_put(json);
441 path = path_new(dir, "index", ".html");
442 dname = ppa_display_name(ppa);
443 create_html(path, dname, HTML_INDEX_TEMPLATE, "ppastats_ppa();");
449 pkg_to_html(struct ppa_stats *ppa, struct package_stats *pkg, const char *dir)
451 char *path, *json_path, *script;
454 json_path = path_new(dir, pkg->name, ".json");
455 json = pkg_to_json(ppa, pkg);
457 printf("DEBUG: generating %s\n", json_path);
458 json_object_to_file(json_path, json);
459 json_object_put(json);
462 path = path_new(dir, pkg->name, ".html");
463 script = malloc(strlen("ppastats_pkg(\"\");")+
467 sprintf(script, "ppastats_pkg(\"%s%s\");", pkg->name, ".json");
470 printf("DEBUG: generating %s\n", path);
472 create_html(path, pkg->name, HTML_PKG_TEMPLATE, script);
478 pkgs_to_html(struct ppa_stats *ppa,
479 struct package_stats **pkgs,
482 struct version_stats **versions;
485 pkg_to_html(ppa, *pkgs, dir);
487 versions = (*pkgs)->versions;
489 version_to_html(ppa, *pkgs, *versions, dir);
499 ppa_to_html(const char *owner,
501 const char *package_status,
502 const char *output_dir,
503 const int install_static_files)
505 struct ppa_stats *ppastats;
506 struct daily_download_total **totals;
508 char *css_dir, *js_dir;
510 static char *www_files[]
511 = { DEFAULT_WWW_DIR"/jquery.min.js", "js/jquery.min.js",
512 DEFAULT_WWW_DIR"/ppastats.js", "js/ppastats.js",
513 DEFAULT_WWW_DIR"/jqplot.dateAxisRenderer.min.js",
514 "js/jqplot.dateAxisRenderer.min.js",
515 DEFAULT_WWW_DIR"/jquery.jqplot.min.js",
516 "js/jquery.jqplot.min.js",
517 DEFAULT_WWW_DIR"/ppastats.css", "css/ppastats.css",
518 DEFAULT_WWW_DIR"/jquery.jqplot.min.css",
519 "css/jquery.jqplot.min.css" };
521 mkdirs(output_dir, 0777);
523 if (install_static_files) {
524 css_dir = path_append(output_dir, "css");
525 js_dir = path_append(output_dir, "js");
527 mkdir(css_dir, 0777);
530 for (i = 0; i < 6; i++) {
531 f_dst = path_append(output_dir, www_files[2*i+1]);
534 printf("DEBUG: copying %s %s\n",
535 www_files[2*i], f_dst);
536 fcopy(www_files[2*i], f_dst);
544 ppastats = create_ppa_stats(owner, ppa, package_status);
545 totals = ppastats->daily_download_totals;
547 path = path_new(output_dir, "ppa", ".html");
549 pkgs_to_html(ppastats, ppastats->packages, output_dir);
551 index_to_html(ppastats, output_dir);
553 ppa_stats_free(ppastats);