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>
34 enum file_copy_error {
35 FILE_COPY_ERROR_OPEN_SRC = 1,
36 FILE_COPY_ERROR_OPEN_DST,
38 FILE_COPY_ERROR_WRITE,
39 FILE_COPY_ERROR_ALLOC_BUFFER
43 " <div id=\"footer\">Generated by \
44 <a href='http://wpitchoune.net/ppastats'>ppastats</a></div>\n\
48 #define HTML_PKG_TEMPLATE \
51 <link type=\"text/css\"\n\
54 \"http://fonts.googleapis.com/css?family=Ubuntu:regular,bold&subset=Latin\">\n\
55 <link type=\"text/css\" href=\"css/ppastats.css\" rel=\"stylesheet\" />\n\
56 <link type=\"text/css\" href=\"css/jquery.jqplot.min.css\"\n\
57 rel=\"stylesheet\" />\n\
58 <script type=\"text/javascript\" src=\"js/jquery.min.js\"></script>\n\
59 <script type=\"text/javascript\"\n\
60 src=\"js/jquery.jqplot.min.js\"></script>\n\
61 <script type=\"text/javascript\"\n\
62 src=\"js/jqplot.dateAxisRenderer.min.js\"></script>\n\
63 <script type=\"text/javascript\" src=\"js/ppastats.js\"></script>\n\
64 <script>var data = %s;\n\
69 <h1><span id=\"pkg_name\">N/A</span></h1>\n\
71 <a href=\"index.html\">\n\
72 <span id=\"ppa_owner\">N/A</span>/<span id=\"ppa_name\">N/A</span>\n\
74 <div id=\"charts\">\n\
75 <div id=\"chart\"></div>\n\
76 <div id=\"chart_distro\"></div>\n\
78 <div id=\"details\">\n\
79 <div id=\"versions\"><em>Versions:</em></div>\n\
80 <div id=\"distros\"><em>Distros:</em></div>\n\
84 #define HTML_VERSION_TEMPLATE \
87 <link type=\"text/css\"\n\
90 \"http://fonts.googleapis.com/css?family=Ubuntu:regular,bold&subset=Latin\">\n\
91 <link type=\"text/css\" href=\"css/ppastats.css\" rel=\"stylesheet\" />\n\
92 <link type=\"text/css\" href=\"css/jquery.jqplot.min.css\"\n\
93 rel=\"stylesheet\" />\n\
94 <script type=\"text/javascript\" src=\"js/jquery.min.js\"></script>\n\
95 <script type=\"text/javascript\"\n\
96 src=\"js/jquery.jqplot.min.js\"></script>\n\
97 <script type=\"text/javascript\"\n\
98 src=\"js/jqplot.dateAxisRenderer.min.js\"></script>\n\
99 <script type=\"text/javascript\" src=\"js/ppastats.js\"></script>\n\
100 <script>var data = %s;\n\
105 <h1><span id=\"pkg_name\">N/A</span></h1>\n\
106 <div id=\"version\"><em>Version:</em></div>\n\
108 <a href=\"index.html\">\n\
109 <span id=\"ppa_owner\">N/A</span>/<span id=\"ppa_name\">N/A</span>\n\
111 <div id=\"charts\">\n\
112 <div id=\"chart\"></div>\n\
114 <div id=\"details\">\n\
115 <div class=\"distros\">\n\
117 <ul id=\"distros\"></ul>\n\
122 #define HTML_INDEX_TEMPLATE \
125 <link type=\"text/css\"\n\
126 rel=\"stylesheet\"\n\
128 \"http://fonts.googleapis.com/css?family=Ubuntu:regular,bold&subset=Latin\">\n\
129 <link type=\"text/css\" href=\"css/ppastats.css\" rel=\"stylesheet\" />\n\
130 <link type=\"text/css\" href=\"css/jquery.jqplot.min.css\"\n\
131 rel=\"stylesheet\" />\n\
132 <script type=\"text/javascript\" src=\"js/jquery.min.js\"></script>\n\
133 <script type=\"text/javascript\"\n\
134 src=\"js/jquery.jqplot.min.js\"></script>\n\
135 <script type=\"text/javascript\"\n\
136 src=\"js/jqplot.dateAxisRenderer.min.js\"></script>\n\
137 <script type=\"text/javascript\" src=\"js/ppastats.js\"></script>\n\
138 <script>var data = %s;\n\
143 <h1><span id=\"ppa_name\">N/A</span></h1>\n\
144 <div id=\"details\">\n\
145 <div class=\"pkgs\">\n\
146 <em>Packages</em>:\n\
147 <ul id=\"pkgs\"></ul>\n\
150 <div id=\"charts\">\n\
151 <div id=\"chart\"></div>\n\
155 #define FCOPY_BUF_SZ 4096
156 static int file_copy(FILE * src, FILE * dst)
159 char *buf = malloc(FCOPY_BUF_SZ);
163 return FILE_COPY_ERROR_ALLOC_BUFFER;
166 n = fread(buf, 1, FCOPY_BUF_SZ, src);
168 if (fwrite(buf, 1, n, dst) != n)
169 ret = FILE_COPY_ERROR_WRITE;
172 ret = FILE_COPY_ERROR_READ;
184 fcopy(const char *src, const char *dst)
190 printf("DEBUG: copy: %s to %s\n", src, dst);
192 fsrc = fopen(src, "r");
195 fdst = fopen(dst, "w+");
198 ret = file_copy(fsrc, fdst);
201 ret = FILE_COPY_ERROR_OPEN_DST;
206 ret = FILE_COPY_ERROR_OPEN_SRC;
212 static char *get_path(const char *dir, const char *file)
214 char *path = malloc(strlen(dir)+1+
222 strcat(path, ".html");
227 static struct json_object *date_to_json(struct tm *tm)
231 json = json_object_new_array();
232 json_object_array_add(json, json_object_new_int(tm->tm_year+1900));
233 json_object_array_add(json, json_object_new_int(tm->tm_mon+1));
234 json_object_array_add(json, json_object_new_int(tm->tm_mday));
239 static void json_add_ddts(json_object *json,
240 struct daily_download_total **ddts)
242 json_object *json_ddt, *json_ddts;
243 struct daily_download_total *ddt;
245 json_ddts = json_object_new_array();
246 json_object_object_add(json, "ddts", json_ddts);
254 json_ddt = json_object_new_object();
255 json_object_object_add(json_ddt,
257 json_object_new_int(ddt->count));
258 json_object_object_add(json_ddt,
260 date_to_json(&ddt->date));
262 json_object_array_add(json_ddts, json_ddt);
268 static char *pkg_to_json(struct ppa_stats *ppa, struct package_stats *pkg)
270 json_object *json, *json_versions, *json_distros, *json_distro;
272 struct version_stats **versions;
273 struct distro_stats **distros, *distro;
275 json = json_object_new_object();
277 json_object_object_add(json,
278 "ppa_name", json_object_new_string(ppa->name));
279 json_object_object_add(json,
281 json_object_new_string(ppa->owner));
283 json_object_object_add(json,
284 "name", json_object_new_string(pkg->name));
286 json_versions = json_object_new_array();
287 json_object_object_add(json, "versions", json_versions);
288 versions = pkg->versions;
290 json_object_array_add
292 json_object_new_string((*versions)->version));
297 distros = pkg->distros;
299 json_distros = json_object_new_array();
300 json_object_object_add(json, "distros", json_distros);
305 json_distro = json_object_new_object();
306 json_object_array_add(json_distros, json_distro);
307 json_object_object_add
309 "name", json_object_new_string(distro->name));
310 json_object_object_add
313 json_object_new_int(distro->download_count));
315 json_add_ddts(json_distro, distro->ddts);
321 json_add_ddts(json, pkg->daily_download_totals);
323 ret = strdup(json_object_to_json_string(json));
325 json_object_put(json);
330 static char *version_to_json(struct ppa_stats *ppa,
331 struct package_stats *pkg,
332 struct version_stats *ver)
335 struct distro_stats **distros, *distro;
336 json_object *json, *json_distros, *json_distro, *json_archs, *json_arch;
337 struct arch_stats **archs;
339 json = json_object_new_object();
341 json_object_object_add(json,
342 "ppa_name", json_object_new_string(ppa->name));
343 json_object_object_add(json,
345 json_object_new_string(ppa->owner));
347 json_object_object_add(json,
348 "pkg_name", json_object_new_string(pkg->name));
350 json_object_object_add(json,
351 "name", json_object_new_string(ver->version));
353 json_add_ddts(json, ver->daily_download_totals);
355 distros = ver->distros;
356 json_distros = json_object_new_array();
357 json_object_object_add(json, "distros", json_distros);
360 json_distro = json_object_new_object();
362 json_object_array_add(json_distros, json_distro);
364 json_object_object_add(json_distro,
366 json_object_new_string(distro->name));
368 archs = distro->archs;
369 json_archs = json_object_new_array();
370 json_object_object_add(json_distro, "archs", json_archs);
372 json_arch = json_object_new_object();
374 json_object_object_add
377 json_object_new_string((*archs)->name));
379 json_object_object_add
382 json_object_new_int((*archs)->download_count));
384 json_object_array_add(json_archs, json_arch);
391 ret = strdup(json_object_to_json_string(json));
393 json_object_put(json);
398 static char *ppa_to_json(struct ppa_stats *ppa)
401 json_object *json, *json_pkgs, *json_pkg;
402 struct package_stats **pkgs;
404 json = json_object_new_object();
406 json_object_object_add(json,
407 "ppa_name", json_object_new_string(ppa->name));
408 json_object_object_add(json,
410 json_object_new_string(ppa->owner));
412 json_add_ddts(json, ppa->daily_download_totals);
414 pkgs = ppa->packages;
415 json_pkgs = json_object_new_array();
416 json_object_object_add(json, "packages", json_pkgs);
418 json_pkg = json_object_new_object();
419 json_object_array_add(json_pkgs, json_pkg);
421 json_object_object_add(json_pkg, "name",
422 json_object_new_string((*pkgs)->name));
424 json_object_object_add
426 json_object_new_int((*pkgs)->download_count));
431 ret = strdup(json_object_to_json_string(json));
433 json_object_put(json);
440 pkg_to_html(struct ppa_stats *ppa, struct package_stats *pkg, const char *dir)
446 path = get_path(dir, pkg->name);
447 f = fopen(path, "w");
450 fprintf(stderr, "ERROR: failed to open: %s\n", path);
454 json = pkg_to_json(ppa, pkg);
456 fprintf(f, HTML_PKG_TEMPLATE, json, HTML_FOOTER);
465 version_to_html(struct ppa_stats *ppa,
466 struct package_stats *pkg,
467 struct version_stats *version,
473 f_name = malloc(strlen(pkg->name)+1+strlen(version->version)+1);
474 sprintf(f_name, "%s_%s", pkg->name, version->version);
476 path = get_path(dir, f_name);
477 f = fopen(path, "w");
480 fprintf(stderr, "ERROR: failed to open: %s\n", path);
484 fprintf(f, HTML_VERSION_TEMPLATE,
485 version_to_json(ppa, pkg, version),
495 packages_to_html(struct ppa_stats *ppa,
496 struct package_stats **packages,
499 struct package_stats **cur;
500 struct version_stats **versions;
504 pkg_to_html(ppa, *cur, dir);
506 versions = (*cur)->versions;
508 version_to_html(ppa, *cur, *versions, dir);
518 index_to_html(struct ppa_stats *ppa, const char *dir)
523 path = get_path(dir, "index");
524 f = fopen(path, "w");
527 fprintf(stderr, "ERROR: failed to open: %s\n", path);
531 fprintf(f, HTML_INDEX_TEMPLATE, ppa_to_json(ppa), HTML_FOOTER);
538 static char *append_path(const char *odir, const char *name)
542 dir = malloc(strlen(odir)+1+strlen(name)+1);
544 sprintf(dir, "%s/%s", odir, name);
550 ppa_to_html(const char *owner,
552 const char *package_status,
553 const char *output_dir,
554 const int install_static_files)
556 struct ppa_stats *ppastats;
557 struct daily_download_total **totals;
559 char *css_dir, *js_dir;
561 static char *www_files[]
562 = { DEFAULT_WWW_DIR"/jquery.min.js", "js/jquery.min.js",
563 DEFAULT_WWW_DIR"/ppastats.js", "js/ppastats.js",
564 DEFAULT_WWW_DIR"/jqplot.dateAxisRenderer.min.js",
565 "js/jqplot.dateAxisRenderer.min.js",
566 DEFAULT_WWW_DIR"/jquery.jqplot.min.js",
567 "js/jquery.jqplot.min.js",
568 DEFAULT_WWW_DIR"/ppastats.css", "css/ppastats.css",
569 DEFAULT_WWW_DIR"/jquery.jqplot.min.css",
570 "css/jquery.jqplot.min.css" };
572 if (install_static_files) {
573 css_dir = append_path(output_dir, "css");
574 js_dir = append_path(output_dir, "js");
576 mkdir(css_dir, 0777);
579 for (i = 0; i < 6; i++) {
580 f_dst = append_path(output_dir, www_files[2*i+1]);
581 fcopy(www_files[2*i], f_dst);
589 ppastats = create_ppa_stats(owner, ppa, package_status);
590 totals = ppastats->daily_download_totals;
592 path = get_path(output_dir, "ppa");
594 packages_to_html(ppastats, ppastats->packages, output_dir);
596 index_to_html(ppastats, output_dir);
598 ppa_stats_free(ppastats);