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=\"versions\"><em>Versions:</em></div>\n\
78 #define HTML_VERSION_TEMPLATE \
81 <link type=\"text/css\"\n\
84 \"http://fonts.googleapis.com/css?family=Ubuntu:regular,bold&subset=Latin\">\n\
85 <link type=\"text/css\" href=\"css/ppastats.css\" rel=\"stylesheet\" />\n\
86 <link type=\"text/css\" href=\"css/jquery.jqplot.min.css\"\n\
87 rel=\"stylesheet\" />\n\
88 <script type=\"text/javascript\" src=\"js/jquery.min.js\"></script>\n\
89 <script type=\"text/javascript\"\n\
90 src=\"js/jquery.jqplot.min.js\"></script>\n\
91 <script type=\"text/javascript\"\n\
92 src=\"js/jqplot.dateAxisRenderer.min.js\"></script>\n\
93 <script type=\"text/javascript\" src=\"js/ppastats.js\"></script>\n\
94 <script>var data = %s;\n\
99 <h1><span id=\"pkg_name\">N/A</span></h1>\n\
100 <div id=\"version\"><em>Version:</em></div>\n\
102 <a href=\"index.html\">\n\
103 <span id=\"ppa_owner\">N/A</span>/<span id=\"ppa_name\">N/A</span>\n\
105 <div id=\"chart\"></div>\n\
106 <div class=\"distros\">\n\
108 <ul id=\"distros\"></ul>\n\
112 #define HTML_INDEX_TEMPLATE \
115 <link type=\"text/css\"\n\
116 rel=\"stylesheet\"\n\
118 \"http://fonts.googleapis.com/css?family=Ubuntu:regular,bold&subset=Latin\">\n\
119 <link type=\"text/css\" href=\"css/ppastats.css\" rel=\"stylesheet\" />\n\
120 <link type=\"text/css\" href=\"css/jquery.jqplot.min.css\"\n\
121 rel=\"stylesheet\" />\n\
122 <script type=\"text/javascript\" src=\"js/jquery.min.js\"></script>\n\
123 <script type=\"text/javascript\"\n\
124 src=\"js/jquery.jqplot.min.js\"></script>\n\
125 <script type=\"text/javascript\"\n\
126 src=\"js/jqplot.dateAxisRenderer.min.js\"></script>\n\
127 <script type=\"text/javascript\" src=\"js/ppastats.js\"></script>\n\
128 <script>var data = %s;\n\
133 <h1><span id=\"ppa_name\">N/A</span></h1>\n\
134 <div class=\"pkgs\">\n\
135 <em>Packages</em>:\n\
136 <ul id=\"pkgs\"></ul>\n\
138 <div id=\"chart\"></div>\n\
141 #define FCOPY_BUF_SZ 4096
142 static int file_copy(FILE * src, FILE * dst)
145 char *buf = malloc(FCOPY_BUF_SZ);
149 return FILE_COPY_ERROR_ALLOC_BUFFER;
152 n = fread(buf, 1, FCOPY_BUF_SZ, src);
154 if (fwrite(buf, 1, n, dst) != n)
155 ret = FILE_COPY_ERROR_WRITE;
158 ret = FILE_COPY_ERROR_READ;
170 fcopy(const char *src, const char *dst)
176 printf("DEBUG: copy: %s to %s\n", src, dst);
178 fsrc = fopen(src, "r");
181 fdst = fopen(dst, "w+");
184 ret = file_copy(fsrc, fdst);
187 ret = FILE_COPY_ERROR_OPEN_DST;
192 ret = FILE_COPY_ERROR_OPEN_SRC;
198 static char *get_path(const char *dir, const char *file)
200 char *path = malloc(strlen(dir)+1+
208 strcat(path, ".html");
213 static struct json_object *date_to_json(struct tm *tm)
217 json = json_object_new_array();
218 json_object_array_add(json, json_object_new_int(tm->tm_year+1900));
219 json_object_array_add(json, json_object_new_int(tm->tm_mon+1));
220 json_object_array_add(json, json_object_new_int(tm->tm_mday));
225 static void json_add_ddts(json_object *json,
226 struct daily_download_total **ddts)
228 json_object *json_ddt, *json_ddts;
229 struct daily_download_total *ddt;
231 json_ddts = json_object_new_array();
232 json_object_object_add(json, "ddts", json_ddts);
240 json_ddt = json_object_new_object();
241 json_object_object_add(json_ddt,
243 json_object_new_int(ddt->count));
244 json_object_object_add(json_ddt,
246 date_to_json(&ddt->date));
248 json_object_array_add(json_ddts, json_ddt);
254 static char *pkg_to_json(struct ppa_stats *ppa, struct package_stats *pkg)
256 json_object *json, *json_versions;
258 struct version_stats **versions;
260 json = json_object_new_object();
262 json_object_object_add(json,
263 "ppa_name", json_object_new_string(ppa->name));
264 json_object_object_add(json,
266 json_object_new_string(ppa->owner));
268 json_object_object_add(json,
269 "name", json_object_new_string(pkg->name));
271 json_versions = json_object_new_array();
272 json_object_object_add(json, "versions", json_versions);
273 versions = pkg->versions;
275 json_object_array_add
277 json_object_new_string((*versions)->version));
282 json_add_ddts(json, pkg->daily_download_totals);
284 ret = strdup(json_object_to_json_string(json));
286 json_object_put(json);
291 static char *version_to_json(struct ppa_stats *ppa,
292 struct package_stats *pkg,
293 struct version_stats *ver)
296 struct distro_stats **distros, *distro;
297 json_object *json, *json_distros, *json_distro, *json_archs, *json_arch;
298 struct arch_stats **archs;
300 json = json_object_new_object();
302 json_object_object_add(json,
303 "ppa_name", json_object_new_string(ppa->name));
304 json_object_object_add(json,
306 json_object_new_string(ppa->owner));
308 json_object_object_add(json,
309 "pkg_name", json_object_new_string(pkg->name));
311 json_object_object_add(json,
312 "name", json_object_new_string(ver->version));
314 json_add_ddts(json, ver->daily_download_totals);
316 distros = ver->distros;
317 json_distros = json_object_new_array();
318 json_object_object_add(json, "distros", json_distros);
321 json_distro = json_object_new_object();
323 json_object_array_add(json_distros, json_distro);
325 json_object_object_add(json_distro,
327 json_object_new_string(distro->name));
329 archs = distro->archs;
330 json_archs = json_object_new_array();
331 json_object_object_add(json_distro, "archs", json_archs);
333 json_arch = json_object_new_object();
335 json_object_object_add
338 json_object_new_string((*archs)->name));
340 json_object_object_add
343 json_object_new_int((*archs)->download_count));
345 json_object_array_add(json_archs, json_arch);
352 ret = strdup(json_object_to_json_string(json));
354 json_object_put(json);
359 static char *ppa_to_json(struct ppa_stats *ppa)
362 json_object *json, *json_pkgs, *json_pkg;
363 struct package_stats **pkgs;
365 json = json_object_new_object();
367 json_object_object_add(json,
368 "ppa_name", json_object_new_string(ppa->name));
369 json_object_object_add(json,
371 json_object_new_string(ppa->owner));
373 json_add_ddts(json, ppa->daily_download_totals);
375 pkgs = ppa->packages;
376 json_pkgs = json_object_new_array();
377 json_object_object_add(json, "packages", json_pkgs);
379 json_pkg = json_object_new_object();
380 json_object_array_add(json_pkgs, json_pkg);
382 json_object_object_add(json_pkg, "name",
383 json_object_new_string((*pkgs)->name));
385 json_object_object_add
387 json_object_new_int((*pkgs)->download_count));
392 ret = strdup(json_object_to_json_string(json));
394 json_object_put(json);
401 pkg_to_html(struct ppa_stats *ppa, struct package_stats *pkg, const char *dir)
407 path = get_path(dir, pkg->name);
408 f = fopen(path, "w");
411 fprintf(stderr, "ERROR: failed to open: %s\n", path);
415 json = pkg_to_json(ppa, pkg);
417 fprintf(f, HTML_PKG_TEMPLATE, json, HTML_FOOTER);
426 version_to_html(struct ppa_stats *ppa,
427 struct package_stats *pkg,
428 struct version_stats *version,
434 f_name = malloc(strlen(pkg->name)+1+strlen(version->version)+1);
435 sprintf(f_name, "%s_%s", pkg->name, version->version);
437 path = get_path(dir, f_name);
438 f = fopen(path, "w");
441 fprintf(stderr, "ERROR: failed to open: %s\n", path);
445 fprintf(f, HTML_VERSION_TEMPLATE,
446 version_to_json(ppa, pkg, version),
456 packages_to_html(struct ppa_stats *ppa,
457 struct package_stats **packages,
460 struct package_stats **cur;
461 struct version_stats **versions;
465 pkg_to_html(ppa, *cur, dir);
467 versions = (*cur)->versions;
469 version_to_html(ppa, *cur, *versions, dir);
479 index_to_html(struct ppa_stats *ppa, const char *dir)
484 path = get_path(dir, "index");
485 f = fopen(path, "w");
488 fprintf(stderr, "ERROR: failed to open: %s\n", path);
492 fprintf(f, HTML_INDEX_TEMPLATE, ppa_to_json(ppa), HTML_FOOTER);
499 static char *append_path(const char *odir, const char *name)
503 dir = malloc(strlen(odir)+1+strlen(name)+1);
505 sprintf(dir, "%s/%s", odir, name);
511 ppa_to_html(const char *owner,
513 const char *package_status,
514 const char *output_dir,
515 const int install_static_files)
517 struct ppa_stats *ppastats;
518 struct daily_download_total **totals;
520 char *css_dir, *js_dir;
522 static char *www_files[]
523 = { DEFAULT_WWW_DIR"/jquery.min.js", "js/jquery.min.js",
524 DEFAULT_WWW_DIR"/ppastats.js", "js/ppastats.js",
525 DEFAULT_WWW_DIR"/jqplot.dateAxisRenderer.min.js",
526 "js/jqplot.dateAxisRenderer.min.js",
527 DEFAULT_WWW_DIR"/jquery.jqplot.min.js",
528 "js/jquery.jqplot.min.js",
529 DEFAULT_WWW_DIR"/ppastats.css", "css/ppastats.css",
530 DEFAULT_WWW_DIR"/jquery.jqplot.min.css",
531 "css/jquery.jqplot.min.css" };
533 if (install_static_files) {
534 css_dir = append_path(output_dir, "css");
535 js_dir = append_path(output_dir, "js");
537 mkdir(css_dir, 0777);
540 for (i = 0; i < 6; i++) {
541 f_dst = append_path(output_dir, www_files[2*i+1]);
542 fcopy(www_files[2*i], f_dst);
550 ppastats = create_ppa_stats(owner, ppa, package_status);
551 totals = ppastats->daily_download_totals;
553 path = get_path(output_dir, "ppa");
555 packages_to_html(ppastats, ppastats->packages, output_dir);
557 index_to_html(ppastats, output_dir);
559 ppa_stats_free(ppastats);