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
28 static struct package_stats *get_package_stats(struct ppa_stats *stats,
32 struct package_stats *p, **p_cur;
34 p_cur = stats->packages;
35 while (p_cur && *p_cur) {
36 struct package_stats *p = *p_cur;
38 if (!strcmp(p->name, name))
44 p = malloc(sizeof(struct package_stats));
45 p->name = strdup(name);
47 p->download_count = 0;
48 p->daily_download_totals = NULL;
50 stats->packages = (struct package_stats **)list_add
51 ((void **)stats->packages, p);
56 static struct version_stats *get_version_stats(struct package_stats *package,
59 struct version_stats *v, **cur;
61 cur = package->versions;
63 struct version_stats *v = *cur;
65 if (!strcmp(v->version, version))
71 v = malloc(sizeof(struct version_stats));
72 v->version = strdup(version);
74 v->download_count = 0;
75 v->daily_download_totals = NULL;
78 = (struct version_stats **)list_add((void **)package->versions,
84 static struct distro_stats *get_distro_stats(struct version_stats *version,
87 struct distro_stats **cur = version->distros;
88 struct distro_stats *d;
93 if (!strcmp(d->name, name))
99 d = malloc(sizeof(struct distro_stats));
100 d->name = strdup(name);
102 d->download_count = 0;
105 = (struct distro_stats **)list_add((void **)version->distros,
111 static struct arch_stats *get_arch_stats(struct distro_stats *distro,
114 struct arch_stats **cur = distro->archs;
115 struct arch_stats *a;
117 while (cur && *cur) {
120 if (!strcmp(a->name, name))
126 a = malloc(sizeof(struct arch_stats));
127 a->name = strdup(name);
128 a->download_count = 0;
131 = (struct arch_stats **)list_add((void **)distro->archs,
138 static struct daily_download_total **add_total
139 (struct daily_download_total **totals, struct daily_download_total *total)
141 struct daily_download_total **cur;
142 struct daily_download_total *item;
149 if (item->date.tm_year == total->date.tm_year &&
150 item->date.tm_mon == total->date.tm_mon &&
151 item->date.tm_mday == total->date.tm_mday) {
152 item->count += total->count;
160 item = malloc(sizeof(struct daily_download_total));
161 memcpy(item, total, sizeof(struct daily_download_total));
163 return (struct daily_download_total **)
164 list_add((void **)totals, (void *)item);
167 struct daily_download_total **add_totals
168 (struct daily_download_total **total1, struct daily_download_total **total2)
170 struct daily_download_total **cur;
171 struct daily_download_total **result;
176 result = add_total(result, *cur);
185 create_ppa_stats(const char *owner,
186 const char *ppa_name,
187 const char *package_status)
189 struct ppa_stats *ppa;
190 struct binary_package_publishing_history **history, **h_cur, *h;
191 char *ppa_url, *pkg_name, *pkg_version;
192 struct package_stats *pkg;
193 struct version_stats *version;
194 const struct distro_series *distro_series;
195 const struct distro_arch_series *arch_series;
196 struct distro_stats *distro;
197 struct arch_stats *arch;
199 struct daily_download_total **totals;
201 ppa_url = get_archive_url(owner, ppa_name);
202 history = get_binary_package_publishing_history_list(ppa_url,
207 fprintf(stderr, "Failed to retrieve PPA information\n");
211 ppa = malloc(sizeof(struct ppa_stats));
212 ppa->name = strdup(ppa_name);
213 ppa->owner = strdup(owner);
214 ppa->packages = NULL;
215 ppa->daily_download_totals = NULL;
216 ppa->download_count = 0;
221 totals = get_daily_download_totals(h->self_link);
222 count = get_download_count(h->self_link);
223 pkg_name = h->binary_package_name;
224 pkg_version = h->binary_package_version;
226 = get_distro_arch_series(h->distro_arch_series_link);
228 = get_distro_series(arch_series->distroseries_link);
230 ppa->download_count += count;
231 ppa->daily_download_totals
232 = add_totals(ppa->daily_download_totals, totals);
234 pkg = get_package_stats(ppa, pkg_name);
235 pkg->download_count += count;
236 pkg->daily_download_totals
237 = add_totals(pkg->daily_download_totals, totals);
239 version = get_version_stats(pkg, pkg_version);
240 version->download_count += count;
241 version->daily_download_totals
242 = add_totals(version->daily_download_totals, totals);
244 distro = get_distro_stats(version, distro_series->name);
245 distro->download_count += count;
247 arch = get_arch_stats(distro, arch_series->architecture_tag);
248 arch->download_count += count;
250 daily_download_total_list_free(totals);
255 binary_package_publishing_history_list_free(history);
260 static void arch_stats_free(struct arch_stats *arch)
266 static void distro_stats_free(struct distro_stats *distro)
268 struct arch_stats **archs;
270 archs = distro->archs;
273 arch_stats_free(*archs);
283 static void version_stats_free(struct version_stats *version)
285 struct distro_stats **distros;
287 distros = version->distros;
290 distro_stats_free(*distros);
293 free(version->distros);
296 free(version->version);
300 static void package_stats_free(struct package_stats *package)
302 struct version_stats **versions;
304 versions = package->versions;
307 version_stats_free(*versions);
310 free(package->versions);
317 void ppa_stats_free(struct ppa_stats *ppastats)
319 struct package_stats **packages;
321 packages = ppastats->packages;
324 package_stats_free(*packages);
327 free(ppastats->packages);
330 free(ppastats->owner);
331 free(ppastats->name);
333 daily_download_total_list_free(ppastats->daily_download_totals);