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