generate html page for the ppa itself
[ppastats.git] / src / html.c
index 6ae462d..a903009 100644 (file)
@@ -105,6 +105,37 @@ enum file_copy_error {
   </body>\n\
 </html>"
 
+#define HTML_INDEX_TEMPLATE \
+"<html>\n\
+  <head>\n\
+    <link type=\"text/css\"\n\
+         rel=\"stylesheet\"\n\
+         href=\n\
+\"http://fonts.googleapis.com/css?family=Ubuntu:regular,bold&subset=Latin\">\n\
+    <link type=\"text/css\" href=\"css/ppastats.css\" rel=\"stylesheet\" />\n\
+    <link type=\"text/css\" href=\"css/jquery.jqplot.min.css\"\n\
+         rel=\"stylesheet\" />\n\
+    <script type=\"text/javascript\" src=\"js/jquery.min.js\"></script>\n\
+    <script type=\"text/javascript\"\n\
+           src=\"js/jquery.jqplot.min.js\"></script>\n\
+    <script type=\"text/javascript\"\n\
+           src=\"js/jqplot.dateAxisRenderer.min.js\"></script>\n\
+    <script type=\"text/javascript\" src=\"js/ppastats.js\"></script>\n\
+    <script>var data = %s;\n\
+           ppastats_ppa();\n\
+    </script>\n\
+  </head>\n\
+  <body>\n\
+    <h1><span id=\"ppa_name\">N/A</span></h1>\n\
+    <div class=\"pkgs\">\n\
+       <em>Packages</em>:\n\
+       <ul id=\"pkgs\"></ul>\n\
+    </div>\n\
+    <div id=\"chart\"></div>\n\
+  </body>\n\
+</html>"
+
+
 #define FCOPY_BUF_SZ 4096
 static int file_copy(FILE * src, FILE * dst)
 {
@@ -323,6 +354,47 @@ static char *version_to_json(struct ppa_stats *ppa,
        return ret;
 }
 
+static char *ppa_to_json(struct ppa_stats *ppa)
+{
+       char *ret;
+       json_object *json, *json_pkgs, *json_pkg;
+       struct package_stats **pkgs;
+
+       json = json_object_new_object();
+
+       json_object_object_add(json,
+                              "ppa_name", json_object_new_string(ppa->name));
+       json_object_object_add(json,
+                              "ppa_owner",
+                              json_object_new_string(ppa->owner));
+
+       json_add_ddts(json, ppa->daily_download_totals);
+
+       pkgs = ppa->packages;
+       json_pkgs = json_object_new_array();
+       json_object_object_add(json, "packages", json_pkgs);
+       while (*pkgs) {
+               json_pkg = json_object_new_object();
+               json_object_array_add(json_pkgs, json_pkg);
+
+               json_object_object_add(json_pkg, "name",
+                                      json_object_new_string((*pkgs)->name));
+
+               json_object_object_add
+                       (json_pkg, "count",
+                        json_object_new_int((*pkgs)->download_count));
+
+               pkgs++;
+       }
+
+       ret = strdup(json_object_to_json_string(json));
+
+       json_object_put(json);
+
+       return ret;
+}
+
+
 static void
 pkg_to_html(struct ppa_stats *ppa, struct package_stats *pkg, const char *dir)
 {
@@ -399,6 +471,27 @@ packages_to_html(struct ppa_stats *ppa,
        }
 }
 
+static void
+index_to_html(struct ppa_stats *ppa, const char *dir)
+{
+       char *path;
+       FILE *f;
+
+       path = get_path(dir, "index");
+       f = fopen(path, "w");
+
+       if (!f) {
+               fprintf(stderr, "ERROR: failed to open: %s\n", path);
+               return ;
+       }
+
+       fprintf(f, HTML_INDEX_TEMPLATE, ppa_to_json(ppa));
+
+       fclose(f);
+
+       free(path);
+}
+
 static char *append_path(const char *odir, const char *name)
 {
        char *dir;
@@ -452,6 +545,8 @@ ppa_to_html(const char *owner,
 
        packages_to_html(ppastats, ppastats->packages, output_dir);
 
+       index_to_html(ppastats, output_dir);
+
        ppa_stats_free(ppastats);
 
        free(path);