From: Jean-Philippe Orsini Date: Thu, 8 Sep 2011 22:44:20 +0000 (+0000) Subject: HTML output: fixed pkg chart not displayed when distro with download count of 0. X-Git-Tag: v1.3.0~127 X-Git-Url: http://git.wpitchoune.net/gitweb/?p=ppastats.git;a=commitdiff_plain;h=5fd162d56d63c2c2829db25f88574962797efe87 HTML output: fixed pkg chart not displayed when distro with download count of 0. data of pkg in a separate data json file --- diff --git a/NEWS b/NEWS index a944fb1..3100fb2 100644 --- a/NEWS +++ b/NEWS @@ -1,6 +1,9 @@ * v0.0.5 ** Added information about distribution in the package page. ** Added distribution chart in the package page. +** HTML output: data are generated in a separate JSON file. +** HTML output: fixed pkg chart not displayed when distro with + download count of 0. * v0.0.4 ** Fixed memory issue (patch submitted by Lekensteyn). diff --git a/src/html.c b/src/html.c index 4eddeb3..b3f6175 100644 --- a/src/html.c +++ b/src/html.c @@ -39,40 +39,21 @@ " #define HTML_PKG_TEMPLATE \ -"\n\ - \n\ - \n\ - \n\ - \n\ - \n\ - \n\ - \n\ - \n\ - \n\ - \n\ - \n\ -

N/A

\n\ -

PPA: \n\ - \n\ - N/A/N/A\n\ -

\n\ -
\n\ -
\n\ -
\n\ -
\n\ -
\n\ -
Versions:
\n\ -
Distros:
\n\ -
\n\ -%s" +"

N/A

\n\ +
\n\ +
\n\ +
\n\ +
\n\ +
\n\ + PPA: \n\ + \n\ + N/A/\n\ + N/A\n\ + \n\ +\n\ +
Distros:
\n\ +
Versions:
\n\ +
" #define HTML_VERSION_TEMPLATE \ "\n\ @@ -205,12 +186,31 @@ static void json_add_ddts(json_object *json, } } -static char *pkg_to_json(struct ppa_stats *ppa, struct package_stats *pkg) +static json_object *distro_to_json(struct distro_stats *d) +{ + json_object *json; + + json = json_object_new_object(); + + json_object_object_add(json, + "name", + json_object_new_string(d->name)); + + json_object_object_add(json, + "count", + json_object_new_int(d->download_count)); + + json_add_ddts(json, d->ddts); + + return json; +} + +static json_object * +pkg_to_json(struct ppa_stats *ppa, struct package_stats *pkg) { json_object *json, *json_versions, *json_distros, *json_distro; - char *ret; struct version_stats **versions; - struct distro_stats **distros, *distro; + struct distro_stats **distros, *d; json = json_object_new_object(); @@ -240,19 +240,14 @@ static char *pkg_to_json(struct ppa_stats *ppa, struct package_stats *pkg) json_object_object_add(json, "distros", json_distros); while (*distros) { - distro = *distros; + d = *distros; - json_distro = json_object_new_object(); - json_object_array_add(json_distros, json_distro); - json_object_object_add - (json_distro, - "name", json_object_new_string(distro->name)); - json_object_object_add - (json_distro, - "count", - json_object_new_int(distro->download_count)); + if (d->download_count) { + json_distro = distro_to_json(d); - json_add_ddts(json_distro, distro->ddts); + json_object_array_add(json_distros, + json_distro); + } distros++; } @@ -260,11 +255,7 @@ static char *pkg_to_json(struct ppa_stats *ppa, struct package_stats *pkg) json_add_ddts(json, pkg->daily_download_totals); - ret = strdup(json_object_to_json_string(json)); - - json_object_put(json); - - return ret; + return json; } static char *version_to_json(struct ppa_stats *ppa, @@ -370,32 +361,6 @@ static json_object *ppa_to_json(struct ppa_stats *ppa) return json; } - -static void -pkg_to_html(struct ppa_stats *ppa, struct package_stats *pkg, const char *dir) -{ - char *path; - FILE *f; - char *json; - - path = path_new(dir, pkg->name, ".html"); - f = fopen(path, "w"); - - if (!f) { - fprintf(stderr, "ERROR: failed to open: %s\n", path); - return ; - } - - json = pkg_to_json(ppa, pkg); - - fprintf(f, HTML_PKG_TEMPLATE, json, HTML_FOOTER); - - fclose(f); - - free(path); - free(json); -} - static void version_to_html(struct ppa_stats *ppa, struct package_stats *pkg, @@ -427,29 +392,6 @@ version_to_html(struct ppa_stats *ppa, } static void -packages_to_html(struct ppa_stats *ppa, - struct package_stats **packages, - const char *dir) -{ - struct package_stats **cur; - struct version_stats **versions; - - cur = packages; - while (*cur) { - pkg_to_html(ppa, *cur, dir); - - versions = (*cur)->versions; - while (*versions) { - version_to_html(ppa, *cur, *versions, dir); - - versions++; - } - - cur++; - } -} - -static void create_html(const char *path, const char *title, const char *body_template, @@ -465,7 +407,7 @@ create_html(const char *path, } fprintf(f, HTML_HEADER, title, script); - fputs(HTML_INDEX_TEMPLATE, f); + fputs(body_template, f); fputs(HTML_FOOTER, f); fclose(f); @@ -489,24 +431,68 @@ index_to_html(struct ppa_stats *ppa, const char *dir) json_object *json; json = ppa_to_json(ppa); - json_path = path_new(dir, "index", ".json"); - if (debug) printf("DEBUG: generating %s\n", json_path); - json_object_to_file(json_path, json); - json_object_put(json); + free(json_path); path = path_new(dir, "index", ".html"); dname = ppa_display_name(ppa); - create_html(path, dname, HTML_INDEX_TEMPLATE, "ppastats_ppa();"); - free(path); free(dname); +} + +static void +pkg_to_html(struct ppa_stats *ppa, struct package_stats *pkg, const char *dir) +{ + char *path, *json_path, *script; + json_object *json; + + json_path = path_new(dir, pkg->name, ".json"); + json = pkg_to_json(ppa, pkg); + if (debug) + printf("DEBUG: generating %s\n", json_path); + json_object_to_file(json_path, json); + json_object_put(json); free(json_path); + + path = path_new(dir, pkg->name, ".html"); + script = malloc(strlen("ppastats_pkg(\"\");")+ + strlen(pkg->name)+ + strlen(".json")+ + 1); + sprintf(script, "ppastats_pkg(\"%s%s\");", pkg->name, ".json"); + + if (debug) + printf("DEBUG: generating %s\n", path); + + create_html(path, pkg->name, HTML_PKG_TEMPLATE, script); + free(path); + free(script); +} + +static void +pkgs_to_html(struct ppa_stats *ppa, + struct package_stats **pkgs, + const char *dir) +{ + struct version_stats **versions; + + while (*pkgs) { + pkg_to_html(ppa, *pkgs, dir); + + versions = (*pkgs)->versions; + while (*versions) { + version_to_html(ppa, *pkgs, *versions, dir); + + versions++; + } + + pkgs++; + } } void @@ -560,7 +546,7 @@ ppa_to_html(const char *owner, path = path_new(output_dir, "ppa", ".html"); - packages_to_html(ppastats, ppastats->packages, output_dir); + pkgs_to_html(ppastats, ppastats->packages, output_dir); index_to_html(ppastats, output_dir); diff --git a/www/js/ppastats.js b/www/js/ppastats.js index 5615480..86ae69b 100644 --- a/www/js/ppastats.js +++ b/www/js/ppastats.js @@ -87,26 +87,30 @@ function ppastats_distros(distros) { } -function ppastats_pkg() { +function ppastats_pkg(json_url) { $(document).ready(function() { - $("#ppa_owner").html(data["ppa_owner"]); - $("#ppa_name").html(data["ppa_name"]); - $("#pkg_name").html(data["name"]); - - $.each(data["versions"], function(i, v) { - var v_url = data["name"]+"_"+v+".html"; - - $("#versions").append("
  • "+v+"
  • "); - }); + $.get(json_url, function(str_json) { + data = JSON.parse(str_json); - $("#distros").append("