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=\"chart\"></div>\n\
75 <div id=\"details\">\n\
76 <div id=\"versions\"><em>Versions:</em></div>\n\
77 <div id=\"distros\"><em>Distros:</em></div>\n\
81 #define HTML_VERSION_TEMPLATE \
84 <link type=\"text/css\"\n\
87 \"http://fonts.googleapis.com/css?family=Ubuntu:regular,bold&subset=Latin\">\n\
88 <link type=\"text/css\" href=\"css/ppastats.css\" rel=\"stylesheet\" />\n\
89 <link type=\"text/css\" href=\"css/jquery.jqplot.min.css\"\n\
90 rel=\"stylesheet\" />\n\
91 <script type=\"text/javascript\" src=\"js/jquery.min.js\"></script>\n\
92 <script type=\"text/javascript\"\n\
93 src=\"js/jquery.jqplot.min.js\"></script>\n\
94 <script type=\"text/javascript\"\n\
95 src=\"js/jqplot.dateAxisRenderer.min.js\"></script>\n\
96 <script type=\"text/javascript\" src=\"js/ppastats.js\"></script>\n\
97 <script>var data = %s;\n\
102 <h1><span id=\"pkg_name\">N/A</span></h1>\n\
103 <div id=\"version\"><em>Version:</em></div>\n\
105 <a href=\"index.html\">\n\
106 <span id=\"ppa_owner\">N/A</span>/<span id=\"ppa_name\">N/A</span>\n\
108 <div id=\"chart\"></div>\n\
109 <div id=\"details\">\n\
110 <div class=\"distros\">\n\
112 <ul id=\"distros\"></ul>\n\
117 #define HTML_INDEX_TEMPLATE \
120 <link type=\"text/css\"\n\
121 rel=\"stylesheet\"\n\
123 \"http://fonts.googleapis.com/css?family=Ubuntu:regular,bold&subset=Latin\">\n\
124 <link type=\"text/css\" href=\"css/ppastats.css\" rel=\"stylesheet\" />\n\
125 <link type=\"text/css\" href=\"css/jquery.jqplot.min.css\"\n\
126 rel=\"stylesheet\" />\n\
127 <script type=\"text/javascript\" src=\"js/jquery.min.js\"></script>\n\
128 <script type=\"text/javascript\"\n\
129 src=\"js/jquery.jqplot.min.js\"></script>\n\
130 <script type=\"text/javascript\"\n\
131 src=\"js/jqplot.dateAxisRenderer.min.js\"></script>\n\
132 <script type=\"text/javascript\" src=\"js/ppastats.js\"></script>\n\
133 <script>var data = %s;\n\
138 <h1><span id=\"ppa_name\">N/A</span></h1>\n\
139 <div id=\"details\">\n\
140 <div class=\"pkgs\">\n\
141 <em>Packages</em>:\n\
142 <ul id=\"pkgs\"></ul>\n\
145 <div id=\"chart\"></div>\n\
148 #define FCOPY_BUF_SZ 4096
149 static int file_copy(FILE * src, FILE * dst)
152 char *buf = malloc(FCOPY_BUF_SZ);
156 return FILE_COPY_ERROR_ALLOC_BUFFER;
159 n = fread(buf, 1, FCOPY_BUF_SZ, src);
161 if (fwrite(buf, 1, n, dst) != n)
162 ret = FILE_COPY_ERROR_WRITE;
165 ret = FILE_COPY_ERROR_READ;
177 fcopy(const char *src, const char *dst)
183 printf("DEBUG: copy: %s to %s\n", src, dst);
185 fsrc = fopen(src, "r");
188 fdst = fopen(dst, "w+");
191 ret = file_copy(fsrc, fdst);
194 ret = FILE_COPY_ERROR_OPEN_DST;
199 ret = FILE_COPY_ERROR_OPEN_SRC;
205 static char *get_path(const char *dir, const char *file)
207 char *path = malloc(strlen(dir)+1+
215 strcat(path, ".html");
220 static struct json_object *date_to_json(struct tm *tm)
224 json = json_object_new_array();
225 json_object_array_add(json, json_object_new_int(tm->tm_year+1900));
226 json_object_array_add(json, json_object_new_int(tm->tm_mon+1));
227 json_object_array_add(json, json_object_new_int(tm->tm_mday));
232 static void json_add_ddts(json_object *json,
233 struct daily_download_total **ddts)
235 json_object *json_ddt, *json_ddts;
236 struct daily_download_total *ddt;
238 json_ddts = json_object_new_array();
239 json_object_object_add(json, "ddts", json_ddts);
247 json_ddt = json_object_new_object();
248 json_object_object_add(json_ddt,
250 json_object_new_int(ddt->count));
251 json_object_object_add(json_ddt,
253 date_to_json(&ddt->date));
255 json_object_array_add(json_ddts, json_ddt);
261 static char *pkg_to_json(struct ppa_stats *ppa, struct package_stats *pkg)
263 json_object *json, *json_versions, *json_distros, *json_distro;
265 struct version_stats **versions;
266 struct distro_stats **distros, *distro;
268 json = json_object_new_object();
270 json_object_object_add(json,
271 "ppa_name", json_object_new_string(ppa->name));
272 json_object_object_add(json,
274 json_object_new_string(ppa->owner));
276 json_object_object_add(json,
277 "name", json_object_new_string(pkg->name));
279 json_versions = json_object_new_array();
280 json_object_object_add(json, "versions", json_versions);
281 versions = pkg->versions;
283 json_object_array_add
285 json_object_new_string((*versions)->version));
290 distros = pkg->distros;
292 json_distros = json_object_new_array();
293 json_object_object_add(json, "distros", json_distros);
298 json_distro = json_object_new_object();
299 json_object_array_add(json_distros, json_distro);
300 json_object_object_add
302 "name", json_object_new_string(distro->name));
303 json_object_object_add
306 json_object_new_int(distro->download_count));
311 json_add_ddts(json, pkg->daily_download_totals);
313 ret = strdup(json_object_to_json_string(json));
315 json_object_put(json);
320 static char *version_to_json(struct ppa_stats *ppa,
321 struct package_stats *pkg,
322 struct version_stats *ver)
325 struct distro_stats **distros, *distro;
326 json_object *json, *json_distros, *json_distro, *json_archs, *json_arch;
327 struct arch_stats **archs;
329 json = json_object_new_object();
331 json_object_object_add(json,
332 "ppa_name", json_object_new_string(ppa->name));
333 json_object_object_add(json,
335 json_object_new_string(ppa->owner));
337 json_object_object_add(json,
338 "pkg_name", json_object_new_string(pkg->name));
340 json_object_object_add(json,
341 "name", json_object_new_string(ver->version));
343 json_add_ddts(json, ver->daily_download_totals);
345 distros = ver->distros;
346 json_distros = json_object_new_array();
347 json_object_object_add(json, "distros", json_distros);
350 json_distro = json_object_new_object();
352 json_object_array_add(json_distros, json_distro);
354 json_object_object_add(json_distro,
356 json_object_new_string(distro->name));
358 archs = distro->archs;
359 json_archs = json_object_new_array();
360 json_object_object_add(json_distro, "archs", json_archs);
362 json_arch = json_object_new_object();
364 json_object_object_add
367 json_object_new_string((*archs)->name));
369 json_object_object_add
372 json_object_new_int((*archs)->download_count));
374 json_object_array_add(json_archs, json_arch);
381 ret = strdup(json_object_to_json_string(json));
383 json_object_put(json);
388 static char *ppa_to_json(struct ppa_stats *ppa)
391 json_object *json, *json_pkgs, *json_pkg;
392 struct package_stats **pkgs;
394 json = json_object_new_object();
396 json_object_object_add(json,
397 "ppa_name", json_object_new_string(ppa->name));
398 json_object_object_add(json,
400 json_object_new_string(ppa->owner));
402 json_add_ddts(json, ppa->daily_download_totals);
404 pkgs = ppa->packages;
405 json_pkgs = json_object_new_array();
406 json_object_object_add(json, "packages", json_pkgs);
408 json_pkg = json_object_new_object();
409 json_object_array_add(json_pkgs, json_pkg);
411 json_object_object_add(json_pkg, "name",
412 json_object_new_string((*pkgs)->name));
414 json_object_object_add
416 json_object_new_int((*pkgs)->download_count));
421 ret = strdup(json_object_to_json_string(json));
423 json_object_put(json);
430 pkg_to_html(struct ppa_stats *ppa, struct package_stats *pkg, const char *dir)
436 path = get_path(dir, pkg->name);
437 f = fopen(path, "w");
440 fprintf(stderr, "ERROR: failed to open: %s\n", path);
444 json = pkg_to_json(ppa, pkg);
446 fprintf(f, HTML_PKG_TEMPLATE, json, HTML_FOOTER);
455 version_to_html(struct ppa_stats *ppa,
456 struct package_stats *pkg,
457 struct version_stats *version,
463 f_name = malloc(strlen(pkg->name)+1+strlen(version->version)+1);
464 sprintf(f_name, "%s_%s", pkg->name, version->version);
466 path = get_path(dir, f_name);
467 f = fopen(path, "w");
470 fprintf(stderr, "ERROR: failed to open: %s\n", path);
474 fprintf(f, HTML_VERSION_TEMPLATE,
475 version_to_json(ppa, pkg, version),
485 packages_to_html(struct ppa_stats *ppa,
486 struct package_stats **packages,
489 struct package_stats **cur;
490 struct version_stats **versions;
494 pkg_to_html(ppa, *cur, dir);
496 versions = (*cur)->versions;
498 version_to_html(ppa, *cur, *versions, dir);
508 index_to_html(struct ppa_stats *ppa, const char *dir)
513 path = get_path(dir, "index");
514 f = fopen(path, "w");
517 fprintf(stderr, "ERROR: failed to open: %s\n", path);
521 fprintf(f, HTML_INDEX_TEMPLATE, ppa_to_json(ppa), HTML_FOOTER);
528 static char *append_path(const char *odir, const char *name)
532 dir = malloc(strlen(odir)+1+strlen(name)+1);
534 sprintf(dir, "%s/%s", odir, name);
540 ppa_to_html(const char *owner,
542 const char *package_status,
543 const char *output_dir,
544 const int install_static_files)
546 struct ppa_stats *ppastats;
547 struct daily_download_total **totals;
549 char *css_dir, *js_dir;
551 static char *www_files[]
552 = { DEFAULT_WWW_DIR"/jquery.min.js", "js/jquery.min.js",
553 DEFAULT_WWW_DIR"/ppastats.js", "js/ppastats.js",
554 DEFAULT_WWW_DIR"/jqplot.dateAxisRenderer.min.js",
555 "js/jqplot.dateAxisRenderer.min.js",
556 DEFAULT_WWW_DIR"/jquery.jqplot.min.js",
557 "js/jquery.jqplot.min.js",
558 DEFAULT_WWW_DIR"/ppastats.css", "css/ppastats.css",
559 DEFAULT_WWW_DIR"/jquery.jqplot.min.css",
560 "css/jquery.jqplot.min.css" };
562 if (install_static_files) {
563 css_dir = append_path(output_dir, "css");
564 js_dir = append_path(output_dir, "js");
566 mkdir(css_dir, 0777);
569 for (i = 0; i < 6; i++) {
570 f_dst = append_path(output_dir, www_files[2*i+1]);
571 fcopy(www_files[2*i], f_dst);
579 ppastats = create_ppa_stats(owner, ppa, package_status);
580 totals = ppastats->daily_download_totals;
582 path = get_path(output_dir, "ppa");
584 packages_to_html(ppastats, ppastats->packages, output_dir);
586 index_to_html(ppastats, output_dir);
588 ppa_stats_free(ppastats);