improved output
authorJean-Philippe Orsini <jeanfi@gmail.com>
Tue, 17 May 2011 08:09:44 +0000 (08:09 +0000)
committerJean-Philippe Orsini <jeanfi@gmail.com>
Tue, 17 May 2011 08:09:44 +0000 (08:09 +0000)
src/Makefile.am
src/Makefile.in
src/cache.c
src/html.c
src/list.c [new file with mode: 0644]
src/list.h [new file with mode: 0644]
src/lp.c
src/lp.h
src/main.c
src/ppastats.c [new file with mode: 0644]
src/ppastats.h

index 1cd1da4..a662a5f 100644 (file)
@@ -8,11 +8,12 @@ bin_PROGRAMS = ppastats
 ppastats_SOURCES = \
        cache.h cache.c\
        html.h html.c\
+       list.h list.c\
        lp.h lp.c\
        lp_json.h lp_json.c\
        lp_ws.h lp_ws.c\
        main.c\
-       ppastats.h
+       ppastats.h ppastats.c
 
 dist_man_MANS = ppastats.1
 
index fc6b2a6..12908ec 100644 (file)
@@ -46,8 +46,9 @@ CONFIG_CLEAN_FILES =
 CONFIG_CLEAN_VPATH_FILES =
 am__installdirs = "$(DESTDIR)$(bindir)" "$(DESTDIR)$(man1dir)"
 PROGRAMS = $(bin_PROGRAMS)
-am_ppastats_OBJECTS = cache.$(OBJEXT) html.$(OBJEXT) lp.$(OBJEXT) \
-       lp_json.$(OBJEXT) lp_ws.$(OBJEXT) main.$(OBJEXT)
+am_ppastats_OBJECTS = cache.$(OBJEXT) html.$(OBJEXT) list.$(OBJEXT) \
+       lp.$(OBJEXT) lp_json.$(OBJEXT) lp_ws.$(OBJEXT) main.$(OBJEXT) \
+       ppastats.$(OBJEXT)
 ppastats_OBJECTS = $(am_ppastats_OBJECTS)
 ppastats_LDADD = $(LDADD)
 DEFAULT_INCLUDES = -I.@am__isrc@ -I$(top_builddir)
@@ -189,11 +190,12 @@ AM_CPPFLAGS = -Wall -std=gnu99 -Werror \
 ppastats_SOURCES = \
        cache.h cache.c\
        html.h html.c\
+       list.h list.c\
        lp.h lp.c\
        lp_json.h lp_json.c\
        lp_ws.h lp_ws.c\
        main.c\
-       ppastats.h
+       ppastats.h ppastats.c
 
 dist_man_MANS = ppastats.1
 all: all-am
@@ -279,10 +281,12 @@ distclean-compile:
 
 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/cache.Po@am__quote@
 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/html.Po@am__quote@
+@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/list.Po@am__quote@
 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/lp.Po@am__quote@
 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/lp_json.Po@am__quote@
 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/lp_ws.Po@am__quote@
 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/main.Po@am__quote@
+@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/ppastats.Po@am__quote@
 
 .c.o:
 @am__fastdepCC_TRUE@   $(COMPILE) -MT $@ -MD -MP -MF $(DEPDIR)/$*.Tpo -c -o $@ $<
index d70e308..940294e 100644 (file)
     02110-1301 USA
 */
 
+#include <string.h>
+
 #include "cache.h"
 #include "ppastats.h"
 
 #include <stdlib.h>
 #include <stdio.h>
-#include <string.h>
 
 /*
-  Stupid cache implementation but should be enough for storing LP data.
+  Simple cache implementation but should be enough for storing LP data.
 */
 
 struct entry {
index fb4dac1..786ea98 100644 (file)
@@ -20,6 +20,7 @@
 #include <string.h>
 
 #include "html.h"
+#include "list.h"
 #include "lp_ws.h"
 
 #include <json/json.h>
@@ -30,7 +31,8 @@
 #define HTML_TEMPLATE \
 "<html>\n\
   <head>\n\
-    <script type='text/javascript' src='https://www.google.com/jsapi'></script>\n\
+    <script type='text/javascript'\
+           src='https://www.google.com/jsapi'></script>\n\
     <script type='text/javascript'>\n\
       google.load('visualization', '1', {packages:['corechart']});\
       google.setOnLoadCallback(drawChart);\n\
 %s\
        data.addRows(%s);\n\
        data.sort(0);\n\
-        var chart = new google.visualization.LineChart(document.getElementById('chart_div'));\n\
-       chart.draw(data, {width: 1024, height: 768, title: 'PPA Statistics'});\n\
+       var chart = new google.visualization.LineChart\
+               (document.getElementById('chart_div'));\n\
+       chart.draw(data, {width: 1024, height: 768,\
+                         title: 'PPA Statistics'});\n\
       }\n\
     </script>\n\
   </head>\n\
diff --git a/src/list.c b/src/list.c
new file mode 100644 (file)
index 0000000..ceb4efa
--- /dev/null
@@ -0,0 +1,57 @@
+/*
+    Copyright (C) 2011 jeanfi@gmail.com
+
+    This program is free software; you can redistribute it and/or modify
+    it under the terms of the GNU General Public License as published by
+    the Free Software Foundation; either version 2 of the License, or
+    (at your option) any later version.
+
+    This program is distributed in the hope that it will be useful,
+    but WITHOUT ANY WARRANTY; without even the implied warranty of
+    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+    GNU General Public License for more details.
+
+    You should have received a copy of the GNU General Public License
+    along with this program; if not, write to the Free Software
+    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+    02110-1301 USA
+*/
+
+#include <stdlib.h>
+#include <string.h>
+
+#include "list.h"
+
+int list_length(void **list)
+{
+       int n;
+
+       n = 0;
+       while (list && *list) {
+               list++;
+               n++;
+       }
+
+       return n;
+}
+
+void **list_add(void **list, void *new_item)
+{
+       int n;
+       void **new_list;
+
+       n = list_length(list);
+
+       new_list = malloc(sizeof(void *)*(n+2));
+
+       if (n)
+               memcpy(new_list, list, sizeof(void *)*n);
+
+       new_list[n] = new_item;
+       new_list[n+1] = NULL;
+
+       free(list);
+
+       return new_list;
+}
+
diff --git a/src/list.h b/src/list.h
new file mode 100644 (file)
index 0000000..f2115c8
--- /dev/null
@@ -0,0 +1,26 @@
+/*
+    Copyright (C) 2011 jeanfi@gmail.com
+
+    This program is free software; you can redistribute it and/or modify
+    it under the terms of the GNU General Public License as published by
+    the Free Software Foundation; either version 2 of the License, or
+    (at your option) any later version.
+
+    This program is distributed in the hope that it will be useful,
+    but WITHOUT ANY WARRANTY; without even the implied warranty of
+    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+    GNU General Public License for more details.
+
+    You should have received a copy of the GNU General Public License
+    along with this program; if not, write to the Free Software
+    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+    02110-1301 USA
+*/
+
+#ifndef _PPASTATS_LIST_H_
+#define _PPASTATS_LIST_H_
+
+int list_length(void **list);
+void **list_add(void **list, void *new_item);
+
+#endif
index f9eca43..adb1e2b 100644 (file)
--- a/src/lp.c
+++ b/src/lp.c
@@ -19,6 +19,7 @@
 
 
 #include <stdlib.h>
+#include <stdio.h>
 #include <string.h>
 
 #include "lp.h"
@@ -53,7 +54,8 @@ void distro_series_free(struct distro_series *d)
 }
 
 void
-binary_package_publishing_history_free(struct binary_package_publishing_history *b)
+binary_package_publishing_history_free \
+(struct binary_package_publishing_history *b)
 {
        if (b) {
                free(b->binary_package_name);
@@ -83,7 +85,8 @@ binary_package_publishing_history_new(const char *binary_package_name,
 }
 
 void
-binary_package_publishing_history_list_free(struct binary_package_publishing_history **list)
+binary_package_publishing_history_list_free\
+(struct binary_package_publishing_history **list)
 {
        struct binary_package_publishing_history **l_cur = list;
 
@@ -167,16 +170,3 @@ void daily_download_total_list_free(struct daily_download_total **list)
        }
 }
 
-int list_length(void **list)
-{
-       int n;
-
-       n = 0;
-       while (*list) {
-               list++;
-               n++;
-       }
-
-       return n;
-}
-
index 95415c6..49c648f 100644 (file)
--- a/src/lp.h
+++ b/src/lp.h
@@ -87,6 +87,4 @@ void binary_package_publishing_history_list_free
 
 char *get_archive_url(const char *owner, const char  *ppa);
 
-int list_length(void **list);
-
 #endif
index 02861ea..662a87e 100644 (file)
@@ -33,37 +33,52 @@ static const char *program_name;
 
 static void display_published_binaries(const char *owner, const char *ppa)
 {
-       struct binary_package_publishing_history **e_cur, **list;
-       char *archive_url = get_archive_url(owner, ppa);
+       struct ppa_stats *ppastats;
+       struct package_stats **packages;
+       struct version_stats **versions;
+       struct distro_stats **distros;
+       struct arch_stats **archs;
 
-       list = get_binary_package_publishing_history_list(archive_url);
-       free(archive_url);
+       ppastats = create_ppa_stats(owner, ppa);
+       packages = ppastats->packages;
+       while (packages && *packages) {
+               struct package_stats *p = *packages;
 
-       if (!list) {
-               fprintf(stderr, "Failed to retrieve PPA information\n");
-               exit(EXIT_FAILURE);
-       }
+               printf("%s (%d)\n", p->name, p->download_count);
+
+               versions = p->versions;
+
+               while (*versions) {
+                       printf("\t%s (%d)\n", (*versions)->version,
+                              (*versions)->download_count);
+
+                       distros = (*versions)->distros;
+
+                       while (*distros) {
+                               printf("\t\t%s (%d)\n",
+                                      (*distros)->name,
+                                      (*distros)->download_count);
+
+                               archs = (*distros)->archs;
+
+                               while (*archs) {
+                                       printf("\t\t\t%s (%d)\n",
+                                              (*archs)->name,
+                                              (*archs)->download_count);
+
+                                       archs++;
+                               }
+
+                               distros++;
+                       }
+
+                       versions++;
+               }
 
-       e_cur = list;
-       while (*e_cur) {
-               struct binary_package_publishing_history *binary;
-               const struct distro_arch_series *arch;
-               const struct distro_series *distro;
-
-               binary = *e_cur;
-               arch = get_distro_arch_series(binary->distro_arch_series_link);
-               distro = get_distro_series(arch->distroseries_link);
-
-               printf("%s %s %s(%s): %d\n",
-                      binary->binary_package_name,
-                      binary->binary_package_version,
-                      distro->name,
-                      arch->architecture_tag,
-                      get_download_count(binary->self_link));
-               e_cur++;
+               packages++;
        }
 
-       binary_package_publishing_history_list_free(list);
+       ppa_stats_free(ppastats);
 }
 
 static struct option long_options[] = {
diff --git a/src/ppastats.c b/src/ppastats.c
new file mode 100644 (file)
index 0000000..078eeab
--- /dev/null
@@ -0,0 +1,278 @@
+/*
+    Copyright (C) 2011 jeanfi@gmail.com
+
+    This program is free software; you can redistribute it and/or modify
+    it under the terms of the GNU General Public License as published by
+    the Free Software Foundation; either version 2 of the License, or
+    (at your option) any later version.
+
+    This program is distributed in the hope that it will be useful,
+    but WITHOUT ANY WARRANTY; without even the implied warranty of
+    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+    GNU General Public License for more details.
+
+    You should have received a copy of the GNU General Public License
+    along with this program; if not, write to the Free Software
+    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+    02110-1301 USA
+*/
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include "list.h"
+#include "lp_ws.h"
+#include "ppastats.h"
+
+static struct package_stats *get_package_stats(struct ppa_stats *stats,
+                                              const char *name)
+
+{
+       struct package_stats *p, **p_cur;
+
+       p_cur = stats->packages;
+       while (p_cur && *p_cur) {
+               struct package_stats *p = *p_cur;
+
+               if (!strcmp(p->name, name))
+                       return p;
+
+               p_cur++;
+       }
+
+       p = malloc(sizeof(struct package_stats));
+       p->name = strdup(name);
+       p->versions = NULL;
+       p->download_count = 0;
+       p->daily_download_totals = NULL;
+
+       stats->packages = (struct package_stats **)list_add
+               ((void **)stats->packages, p);
+
+       return p;
+}
+
+static struct version_stats *get_version_stats(struct package_stats *package,
+                                              const char *version)
+{
+       struct version_stats *v, **cur;
+
+       cur = package->versions;
+       while (cur && *cur) {
+               struct version_stats *v = *cur;
+
+               if (!strcmp(v->version, version))
+                       return v;
+
+               cur++;
+       }
+
+       v = malloc(sizeof(struct version_stats));
+       v->version = strdup(version);
+       v->distros = NULL;
+       v->download_count = 0;
+       v->daily_download_totals = NULL;
+
+       package->versions
+               = (struct version_stats **)list_add((void **)package->versions,
+                                                   v);
+
+       return v;
+}
+
+static struct distro_stats *get_distro_stats(struct version_stats *version,
+                                            const char *name)
+{
+       struct distro_stats **cur = version->distros;
+       struct distro_stats *d;
+
+       while (cur && *cur) {
+               d = *cur;
+
+               if (!strcmp(d->name, name))
+                       return d;
+
+               cur++;
+       }
+
+       d = malloc(sizeof(struct distro_stats));
+       d->name = strdup(name);
+       d->archs = NULL;
+       d->download_count = 0;
+       d->daily_download_totals = NULL;
+
+       version->distros
+               = (struct distro_stats **)list_add((void **)version->distros,
+                                                  d);
+
+       return d;
+}
+
+static struct arch_stats *get_arch_stats(struct distro_stats *distro,
+                                        const char *name)
+{
+       struct arch_stats **cur = distro->archs;
+       struct arch_stats *a;
+
+       while (cur && *cur) {
+               a = *cur;
+
+               if (!strcmp(a->name, name))
+                       return a;
+
+               cur++;
+       }
+
+       a = malloc(sizeof(struct arch_stats));
+       a->name = strdup(name);
+       a->daily_download_totals = NULL;
+       a->download_count = 0;
+
+       distro->archs
+               = (struct arch_stats **)list_add((void **)distro->archs,
+                                                a);
+
+       return a;
+}
+
+
+struct ppa_stats *create_ppa_stats(const char *owner, const char *ppa)
+{
+       struct ppa_stats *ppastats;
+       struct binary_package_publishing_history **history;
+       struct binary_package_publishing_history **h_cur;
+       struct binary_package_publishing_history *h;
+       char *ppa_url, *package_name, *package_version;
+       struct package_stats *package;
+       struct version_stats *version;
+       const struct distro_series *distro_series;
+       const struct distro_arch_series *arch_series;
+       struct distro_stats *distro;
+       struct arch_stats *arch;
+       int count;
+
+       ppa_url = get_archive_url(owner, ppa);
+       history = get_binary_package_publishing_history_list(ppa_url);
+       free(ppa_url);
+
+       if (!history) {
+               fprintf(stderr, "Failed to retrieve PPA information\n");
+               exit(EXIT_FAILURE);
+       }
+
+       ppastats = malloc(sizeof(struct ppa_stats));
+       ppastats->name = strdup(ppa);
+       ppastats->owner = strdup(owner);
+       ppastats->packages = NULL;
+       ppastats->daily_download_totals = NULL;
+       ppastats->download_count = 0;
+
+       h_cur = history;
+       while (*h_cur) {
+               h = *h_cur;
+               package_name = h->binary_package_name;
+               package_version = h->binary_package_version;
+               arch_series
+                       = get_distro_arch_series(h->distro_arch_series_link);
+               distro_series
+                       = get_distro_series(arch_series->distroseries_link);
+               count = get_download_count(h->self_link);
+
+               package = get_package_stats(ppastats, package_name);
+               package->download_count += count;
+
+               version = get_version_stats(package, package_version);
+               version->download_count += count;
+
+               distro = get_distro_stats(version, distro_series->name);
+               distro->download_count += count;
+
+               arch = get_arch_stats(distro, arch_series->architecture_tag);
+               arch->download_count += count;
+
+               ppastats->download_count += count;
+
+               h_cur++;
+       }
+
+       binary_package_publishing_history_list_free(history);
+
+       return ppastats;
+}
+
+static void arch_stats_free(struct arch_stats *arch)
+{
+       free(arch->name);
+       free(arch);
+}
+
+static void distro_stats_free(struct distro_stats *distro)
+{
+       struct arch_stats **archs;
+
+       archs = distro->archs;
+       if (archs) {
+               while (*archs) {
+                       arch_stats_free(*archs);
+                       archs++;
+               }
+               free(distro->archs);
+       }
+
+       free(distro->name);
+       free(distro);
+}
+
+static void version_stats_free(struct version_stats *version)
+{
+       struct distro_stats **distros;
+
+       distros = version->distros;
+       if (distros) {
+               while (*distros) {
+                       distro_stats_free(*distros);
+                       distros++;
+               }
+               free(version->distros);
+       }
+
+       free(version->version);
+       free(version);
+}
+
+static void package_stats_free(struct package_stats *package)
+{
+       struct version_stats **versions;
+
+       versions = package->versions;
+       if (versions) {
+               while (*versions) {
+                       version_stats_free(*versions);
+                       versions++;
+               }
+               free(package->versions);
+       }
+
+       free(package->name);
+       free(package);
+}
+
+void ppa_stats_free(struct ppa_stats *ppastats)
+{
+       struct package_stats **packages;
+
+       packages = ppastats->packages;
+       if (packages) {
+               while (*packages) {
+                       package_stats_free(*packages);
+                       packages++;
+               }
+               free(ppastats->packages);
+       }
+
+       free(ppastats->owner);
+       free(ppastats->name);
+
+       free(ppastats);
+}
index 4eabf01..f6bace5 100644 (file)
 
 extern int debug;
 
+#include "lp.h"
+
+struct arch_stats {
+       char *name;
+
+       struct daily_download_total *daily_download_totals;
+       int download_count;
+};
+
+struct distro_stats {
+       char *name;
+
+       struct arch_stats **archs;
+       int download_count;
+       struct daily_download_total *daily_download_totals;
+};
+
+struct version_stats {
+       char *version;
+
+       struct distro_stats **distros;
+       int download_count;
+       struct daily_download_total *daily_download_totals;
+};
+
+struct package_stats {
+       char *name;
+
+       struct version_stats **versions;
+       int download_count;
+       struct daily_download_total *daily_download_totals;
+};
+
+struct ppa_stats {
+       char *name;
+       char *owner;
+
+       struct package_stats **packages;
+       int download_count;
+       struct daily_download_total *daily_download_totals;
+};
+
+struct ppa_stats *create_ppa_stats(const char *owner, const char *ppa);
+void ppa_stats_free(struct ppa_stats *ppastats);
+
 #endif