Added distro/arch information in package version page
[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
50         stats->packages = (struct package_stats **)list_add
51                 ((void **)stats->packages, p);
52
53         return p;
54 }
55
56 static struct version_stats *get_version_stats(struct package_stats *package,
57                                                const char *version)
58 {
59         struct version_stats *v, **cur;
60
61         cur = package->versions;
62         while (cur && *cur) {
63                 struct version_stats *v = *cur;
64
65                 if (!strcmp(v->version, version))
66                         return v;
67
68                 cur++;
69         }
70
71         v = malloc(sizeof(struct version_stats));
72         v->version = strdup(version);
73         v->distros = NULL;
74         v->download_count = 0;
75         v->daily_download_totals = NULL;
76
77         package->versions
78                 = (struct version_stats **)list_add((void **)package->versions,
79                                                     v);
80
81         return v;
82 }
83
84 static struct distro_stats *get_distro_stats(struct version_stats *version,
85                                              const char *name)
86 {
87         struct distro_stats **cur = version->distros;
88         struct distro_stats *d;
89
90         while (cur && *cur) {
91                 d = *cur;
92
93                 if (!strcmp(d->name, name))
94                         return d;
95
96                 cur++;
97         }
98
99         d = malloc(sizeof(struct distro_stats));
100         d->name = strdup(name);
101         d->archs = NULL;
102         d->download_count = 0;
103
104         version->distros
105                 = (struct distro_stats **)list_add((void **)version->distros,
106                                                    d);
107
108         return d;
109 }
110
111 static struct arch_stats *get_arch_stats(struct distro_stats *distro,
112                                          const char *name)
113 {
114         struct arch_stats **cur = distro->archs;
115         struct arch_stats *a;
116
117         while (cur && *cur) {
118                 a = *cur;
119
120                 if (!strcmp(a->name, name))
121                         return a;
122
123                 cur++;
124         }
125
126         a = malloc(sizeof(struct arch_stats));
127         a->name = strdup(name);
128         a->download_count = 0;
129
130         distro->archs
131                 = (struct arch_stats **)list_add((void **)distro->archs,
132                                                  a);
133
134         return a;
135 }
136
137
138 static struct daily_download_total **add_total
139 (struct daily_download_total **totals, struct daily_download_total *total)
140 {
141         struct daily_download_total **cur;
142         struct daily_download_total *item;
143
144         if (totals) {
145                 cur = totals;
146                 while (*cur) {
147                         item = *cur;
148
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;
153                                 return totals;
154                         }
155
156                         cur++;
157                 }
158         }
159
160         item = malloc(sizeof(struct daily_download_total));
161         memcpy(item, total, sizeof(struct daily_download_total));
162
163         return (struct daily_download_total **)
164                 list_add((void **)totals, (void *)item);
165 }
166
167 struct daily_download_total **add_totals
168 (struct daily_download_total **total1, struct daily_download_total **total2)
169 {
170         struct daily_download_total **cur;
171         struct daily_download_total **result;
172
173         result = total1;
174         cur = total2;
175         while (*cur) {
176                 result = add_total(result, *cur);
177
178                 cur++;
179         }
180
181         return result;
182 }
183
184 struct ppa_stats *
185 create_ppa_stats(const char *owner,
186                  const char *ppa,
187                  const char *package_status)
188 {
189         struct ppa_stats *ppastats;
190         struct binary_package_publishing_history **history;
191         struct binary_package_publishing_history **h_cur;
192         struct binary_package_publishing_history *h;
193         char *ppa_url, *package_name, *package_version;
194         struct package_stats *package;
195         struct version_stats *version;
196         const struct distro_series *distro_series;
197         const struct distro_arch_series *arch_series;
198         struct distro_stats *distro;
199         struct arch_stats *arch;
200         int count;
201         struct daily_download_total **totals;
202
203         ppa_url = get_archive_url(owner, ppa);
204         history = get_binary_package_publishing_history_list(ppa_url,
205                                                              package_status);
206         free(ppa_url);
207
208         if (!history) {
209                 fprintf(stderr, "Failed to retrieve PPA information\n");
210                 exit(EXIT_FAILURE);
211         }
212
213         ppastats = malloc(sizeof(struct ppa_stats));
214         ppastats->name = strdup(ppa);
215         ppastats->owner = strdup(owner);
216         ppastats->packages = NULL;
217         ppastats->daily_download_totals = NULL;
218         ppastats->download_count = 0;
219
220         h_cur = history;
221         while (*h_cur) {
222                 h = *h_cur;
223                 package_name = h->binary_package_name;
224                 package_version = h->binary_package_version;
225                 arch_series
226                         = get_distro_arch_series(h->distro_arch_series_link);
227                 distro_series
228                         = get_distro_series(arch_series->distroseries_link);
229
230                 count = get_download_count(h->self_link);
231
232                 package = get_package_stats(ppastats, package_name);
233                 package->download_count += count;
234
235                 version = get_version_stats(package, package_version);
236                 version->download_count += count;
237
238                 distro = get_distro_stats(version, distro_series->name);
239                 distro->download_count += count;
240
241                 arch = get_arch_stats(distro, arch_series->architecture_tag);
242                 arch->download_count += count;
243
244                 ppastats->download_count += count;
245
246                 totals = get_daily_download_totals(h->self_link);
247
248                 ppastats->daily_download_totals
249                         = add_totals(ppastats->daily_download_totals,
250                                      totals);
251
252                 package->daily_download_totals
253                         = add_totals(package->daily_download_totals,
254                                      totals);
255
256                 version->daily_download_totals
257                         = add_totals(version->daily_download_totals,
258                                      totals);
259
260                 daily_download_total_list_free(totals);
261
262                 h_cur++;
263         }
264
265         binary_package_publishing_history_list_free(history);
266
267         return ppastats;
268 }
269
270 static void arch_stats_free(struct arch_stats *arch)
271 {
272         free(arch->name);
273         free(arch);
274 }
275
276 static void distro_stats_free(struct distro_stats *distro)
277 {
278         struct arch_stats **archs;
279
280         archs = distro->archs;
281         if (archs) {
282                 while (*archs) {
283                         arch_stats_free(*archs);
284                         archs++;
285                 }
286                 free(distro->archs);
287         }
288
289         free(distro->name);
290         free(distro);
291 }
292
293 static void version_stats_free(struct version_stats *version)
294 {
295         struct distro_stats **distros;
296
297         distros = version->distros;
298         if (distros) {
299                 while (*distros) {
300                         distro_stats_free(*distros);
301                         distros++;
302                 }
303                 free(version->distros);
304         }
305
306         free(version->version);
307         free(version);
308 }
309
310 static void package_stats_free(struct package_stats *package)
311 {
312         struct version_stats **versions;
313
314         versions = package->versions;
315         if (versions) {
316                 while (*versions) {
317                         version_stats_free(*versions);
318                         versions++;
319                 }
320                 free(package->versions);
321         }
322
323         free(package->name);
324         free(package);
325 }
326
327 void ppa_stats_free(struct ppa_stats *ppastats)
328 {
329         struct package_stats **packages;
330
331         packages = ppastats->packages;
332         if (packages) {
333                 while (*packages) {
334                         package_stats_free(*packages);
335                         packages++;
336                 }
337                 free(ppastats->packages);
338         }
339
340         free(ppastats->owner);
341         free(ppastats->name);
342
343         daily_download_total_list_free(ppastats->daily_download_totals);
344
345         free(ppastats);
346 }