add distros stats in each package
[ppastats.git] / src / ppastats.c
1 /*
2     Copyright (C) 2011 jeanfi@gmail.com
3
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.
8
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.
13
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
17     02110-1301 USA
18 */
19
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <string.h>
23
24 #include "list.h"
25 #include "lp_ws.h"
26 #include "ppastats.h"
27
28 static struct package_stats *get_package_stats(struct ppa_stats *stats,
29                                                const char *name)
30
31 {
32         struct package_stats *p, **p_cur;
33
34         p_cur = stats->packages;
35         while (p_cur && *p_cur) {
36                 struct package_stats *p = *p_cur;
37
38                 if (!strcmp(p->name, name))
39                         return p;
40
41                 p_cur++;
42         }
43
44         p = malloc(sizeof(struct package_stats));
45         p->name = strdup(name);
46         p->versions = NULL;
47         p->download_count = 0;
48         p->daily_download_totals = NULL;
49         p->distros = NULL;
50
51         stats->packages = (struct package_stats **)list_add
52                 ((void **)stats->packages, p);
53
54         return p;
55 }
56
57 static struct version_stats *get_version_stats(struct package_stats *package,
58                                                const char *version)
59 {
60         struct version_stats *v, **cur;
61
62         cur = package->versions;
63         while (cur && *cur) {
64                 struct version_stats *v = *cur;
65
66                 if (!strcmp(v->version, version))
67                         return v;
68
69                 cur++;
70         }
71
72         v = malloc(sizeof(struct version_stats));
73         v->version = strdup(version);
74         v->distros = NULL;
75         v->download_count = 0;
76         v->daily_download_totals = NULL;
77
78         package->versions
79                 = (struct version_stats **)list_add((void **)package->versions,
80                                                     v);
81
82         return v;
83 }
84
85 static struct distro_stats *distro_stats_new(const char *name)
86 {
87         struct distro_stats *d;
88
89         d = malloc(sizeof(struct distro_stats));
90         d->name = strdup(name);
91         d->archs = NULL;
92         d->download_count = 0;
93
94         return d;
95 }
96
97 static struct distro_stats *get_distro_stats(struct version_stats *version,
98                                              const char *name)
99 {
100         struct distro_stats **cur = version->distros;
101         struct distro_stats *d;
102
103         while (cur && *cur) {
104                 d = *cur;
105
106                 if (!strcmp(d->name, name))
107                         return d;
108
109                 cur++;
110         }
111
112         d = distro_stats_new(name);
113
114         version->distros
115                 = (struct distro_stats **)list_add((void **)version->distros,
116                                                    d);
117
118         return d;
119 }
120
121 static struct arch_stats *get_arch_stats(struct distro_stats *distro,
122                                          const char *name)
123 {
124         struct arch_stats **cur = distro->archs;
125         struct arch_stats *a;
126
127         while (cur && *cur) {
128                 a = *cur;
129
130                 if (!strcmp(a->name, name))
131                         return a;
132
133                 cur++;
134         }
135
136         a = malloc(sizeof(struct arch_stats));
137         a->name = strdup(name);
138         a->download_count = 0;
139
140         distro->archs
141                 = (struct arch_stats **)list_add((void **)distro->archs,
142                                                  a);
143
144         return a;
145 }
146
147
148 static struct daily_download_total **add_total
149 (struct daily_download_total **totals, struct daily_download_total *total)
150 {
151         struct daily_download_total **cur;
152         struct daily_download_total *item;
153
154         if (totals) {
155                 cur = totals;
156                 while (*cur) {
157                         item = *cur;
158
159                         if (item->date.tm_year == total->date.tm_year &&
160                             item->date.tm_mon == total->date.tm_mon &&
161                             item->date.tm_mday == total->date.tm_mday) {
162                                 item->count += total->count;
163                                 return totals;
164                         }
165
166                         cur++;
167                 }
168         }
169
170         item = malloc(sizeof(struct daily_download_total));
171         memcpy(item, total, sizeof(struct daily_download_total));
172
173         return (struct daily_download_total **)
174                 list_add((void **)totals, (void *)item);
175 }
176
177 struct daily_download_total **add_totals
178 (struct daily_download_total **total1, struct daily_download_total **total2)
179 {
180         struct daily_download_total **cur;
181         struct daily_download_total **result;
182
183         result = total1;
184         cur = total2;
185         while (*cur) {
186                 result = add_total(result, *cur);
187
188                 cur++;
189         }
190
191         return result;
192 }
193
194 static void
195 pkg_add_distro(struct package_stats *pkg,
196                const char *distro_name,
197                int distro_count)
198 {
199         struct distro_stats **pkg_distros, *pkg_distro;
200
201         pkg_distros = pkg->distros;
202         pkg_distro = NULL;
203
204         if (pkg_distros)
205                 while (*pkg_distros)  {
206                         if (!strcmp((*pkg_distros)->name, distro_name)) {
207                                 pkg_distro = *pkg_distros;
208                                 break;
209                         }
210
211                         pkg_distros++;
212                 }
213
214         if (!pkg_distro) {
215                 pkg_distro = distro_stats_new(distro_name);
216                 pkg->distros
217                         = (struct distro_stats **)
218                         list_add((void **)pkg->distros, (void *)pkg_distro);
219         }
220
221         pkg_distro->download_count += distro_count;
222 }
223
224 struct ppa_stats *
225 create_ppa_stats(const char *owner,
226                  const char *ppa_name,
227                  const char *package_status)
228 {
229         struct ppa_stats *ppa;
230         struct binary_package_publishing_history **history, **h_cur, *h;
231         char *ppa_url, *pkg_name, *pkg_version;
232         struct package_stats *pkg;
233         struct version_stats *version;
234         const struct distro_series *distro_series;
235         const struct distro_arch_series *arch_series;
236         struct distro_stats *distro;
237         struct arch_stats *arch;
238         int count;
239         struct daily_download_total **totals;
240
241         ppa_url = get_archive_url(owner, ppa_name);
242         history = get_binary_package_publishing_history_list(ppa_url,
243                                                              package_status);
244         free(ppa_url);
245
246         if (!history) {
247                 fprintf(stderr, "Failed to retrieve PPA information\n");
248                 exit(EXIT_FAILURE);
249         }
250
251         ppa = malloc(sizeof(struct ppa_stats));
252         ppa->name = strdup(ppa_name);
253         ppa->owner = strdup(owner);
254         ppa->packages = NULL;
255         ppa->daily_download_totals = NULL;
256         ppa->download_count = 0;
257
258         h_cur = history;
259         while (*h_cur) {
260                 h = *h_cur;
261                 totals = get_daily_download_totals(h->self_link);
262                 count = get_download_count(h->self_link);
263                 pkg_name = h->binary_package_name;
264                 pkg_version = h->binary_package_version;
265                 arch_series
266                         = get_distro_arch_series(h->distro_arch_series_link);
267                 distro_series
268                         = get_distro_series(arch_series->distroseries_link);
269
270                 ppa->download_count += count;
271                 ppa->daily_download_totals
272                         = add_totals(ppa->daily_download_totals, totals);
273
274                 pkg = get_package_stats(ppa, pkg_name);
275                 pkg->download_count += count;
276                 pkg->daily_download_totals
277                         = add_totals(pkg->daily_download_totals, totals);
278
279                 version = get_version_stats(pkg, pkg_version);
280                 version->download_count += count;
281                 version->daily_download_totals
282                         = add_totals(version->daily_download_totals, totals);
283
284                 distro = get_distro_stats(version, distro_series->name);
285                 distro->download_count += count;
286
287                 arch = get_arch_stats(distro, arch_series->architecture_tag);
288                 arch->download_count += count;
289
290                 pkg_add_distro(pkg, distro_series->name, count);
291
292                 daily_download_total_list_free(totals);
293
294                 h_cur++;
295         }
296
297         binary_package_publishing_history_list_free(history);
298
299         return ppa;
300 }
301
302 static void arch_stats_free(struct arch_stats *arch)
303 {
304         free(arch->name);
305         free(arch);
306 }
307
308 static void distro_stats_free(struct distro_stats *distro)
309 {
310         struct arch_stats **archs;
311
312         archs = distro->archs;
313         if (archs) {
314                 while (*archs) {
315                         arch_stats_free(*archs);
316                         archs++;
317                 }
318                 free(distro->archs);
319         }
320
321         free(distro->name);
322         free(distro);
323 }
324
325 static void version_stats_free(struct version_stats *version)
326 {
327         struct distro_stats **distros;
328
329         distros = version->distros;
330         if (distros) {
331                 while (*distros) {
332                         distro_stats_free(*distros);
333                         distros++;
334                 }
335                 free(version->distros);
336         }
337
338         free(version->version);
339         free(version);
340 }
341
342 static void package_stats_free(struct package_stats *package)
343 {
344         struct version_stats **versions;
345
346         versions = package->versions;
347         if (versions) {
348                 while (*versions) {
349                         version_stats_free(*versions);
350                         versions++;
351                 }
352                 free(package->versions);
353         }
354
355         free(package->name);
356         free(package);
357 }
358
359 void ppa_stats_free(struct ppa_stats *ppastats)
360 {
361         struct package_stats **packages;
362
363         packages = ppastats->packages;
364         if (packages) {
365                 while (*packages) {
366                         package_stats_free(*packages);
367                         packages++;
368                 }
369                 free(ppastats->packages);
370         }
371
372         free(ppastats->owner);
373         free(ppastats->name);
374
375         daily_download_total_list_free(ppastats->daily_download_totals);
376
377         free(ppastats);
378 }