a7cd7e9d20b160190223456c5db87fa70a20fa97
[ppastats.git] / src / ppastats.c
1 /*
2  * Copyright (C) 2011-2014 jeanfi@gmail.com
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License as
6  * published by the Free Software Foundation; either version 2 of the
7  * License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * 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 <libintl.h>
21 #define _(String) gettext(String)
22
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26
27 #include "list.h"
28 #include "lp_ws.h"
29 #include <plog.h>
30 #include "ppastats.h"
31
32 static struct package_stats *get_package_stats(struct ppa_stats *stats,
33                                                const char *name)
34
35 {
36         struct package_stats *p, **p_cur, **tmp;
37
38         p_cur = stats->packages;
39         while (p_cur && *p_cur) {
40                 struct package_stats *p = *p_cur;
41
42                 if (!strcmp(p->name, name))
43                         return p;
44
45                 p_cur++;
46         }
47
48         p = malloc(sizeof(struct package_stats));
49         p->name = strdup(name);
50         p->versions = NULL;
51         p->download_count = 0;
52         p->daily_download_totals = NULL;
53         p->distros = NULL;
54
55         tmp = (struct package_stats **)list_add
56                 ((void **)stats->packages, p);
57         free(stats->packages);
58         stats->packages = tmp;
59
60         return p;
61 }
62
63 static struct version_stats *get_version_stats(struct package_stats *package,
64                                                const char *version)
65 {
66         struct version_stats *v, **cur, **tmp;
67
68         cur = package->versions;
69         while (cur && *cur) {
70                 struct version_stats *v = *cur;
71
72                 if (!strcmp(v->version, version))
73                         return v;
74
75                 cur++;
76         }
77
78         v = malloc(sizeof(struct version_stats));
79         v->version = strdup(version);
80         v->distros = NULL;
81         v->download_count = 0;
82         v->daily_download_totals = NULL;
83
84         tmp = (struct version_stats **)list_add((void **)package->versions,
85                                                 v);
86         free((void **)package->versions);
87         package->versions = tmp;
88
89         return v;
90 }
91
92 static struct distro_stats *distro_stats_new(const char *name)
93 {
94         struct distro_stats *d;
95
96         d = malloc(sizeof(struct distro_stats));
97         d->name = strdup(name);
98         d->archs = NULL;
99         d->download_count = 0;
100         d->ddts = NULL;
101
102         return d;
103 }
104
105 static struct distro_stats *get_distro_stats(struct version_stats *version,
106                                              const char *name)
107 {
108         struct distro_stats **cur = version->distros;
109         struct distro_stats *d;
110         struct distro_stats **tmp;
111
112         while (cur && *cur) {
113                 d = *cur;
114
115                 if (!strcmp(d->name, name))
116                         return d;
117
118                 cur++;
119         }
120
121         d = distro_stats_new(name);
122
123
124         tmp = (struct distro_stats **)list_add((void **)version->distros,
125                                                    d);
126         free(version->distros);
127         version->distros = tmp;
128
129         return d;
130 }
131
132 static struct arch_stats *get_arch_stats(struct distro_stats *distro,
133                                          const char *name)
134 {
135         struct arch_stats **cur = distro->archs;
136         struct arch_stats *a;
137         struct arch_stats **tmp;
138
139         while (cur && *cur) {
140                 a = *cur;
141
142                 if (!strcmp(a->name, name))
143                         return a;
144
145                 cur++;
146         }
147
148         a = malloc(sizeof(struct arch_stats));
149         a->name = strdup(name);
150         a->download_count = 0;
151
152         tmp = (struct arch_stats **)list_add((void **)distro->archs,
153                                              a);
154         free((void **)distro->archs);
155         distro->archs = tmp;
156
157         return a;
158 }
159
160
161 static struct daily_download_total **add_total
162 (struct daily_download_total **totals, struct daily_download_total *total)
163 {
164         struct daily_download_total **cur, **result;
165         struct daily_download_total *item;
166
167         if (totals) {
168                 cur = totals;
169                 while (*cur) {
170                         item = *cur;
171
172                         if (item->date.tm_year == total->date.tm_year &&
173                             item->date.tm_mon == total->date.tm_mon &&
174                             item->date.tm_mday == total->date.tm_mday) {
175                                 item->count += total->count;
176                                 return totals;
177                         }
178
179                         cur++;
180                 }
181         }
182
183         item = malloc(sizeof(struct daily_download_total));
184         memcpy(item, total, sizeof(struct daily_download_total));
185
186         result = (struct daily_download_total **)
187                 list_add((void **)totals, (void *)item);
188
189         free(totals);
190
191         return result;
192 }
193
194 struct daily_download_total **add_totals
195 (struct daily_download_total **total1, struct daily_download_total **total2)
196 {
197         struct daily_download_total **cur;
198         struct daily_download_total **result;
199
200         result = total1;
201         cur = total2;
202         while (*cur) {
203                 result = add_total(result, *cur);
204
205                 cur++;
206         }
207
208         return result;
209 }
210
211 static void
212 pkg_add_distro(struct package_stats *pkg,
213                const char *distro_name,
214                int distro_count,
215                struct daily_download_total **ddts)
216 {
217         struct distro_stats **pkg_distros, *pkg_distro, **tmp;
218
219         pkg_distros = pkg->distros;
220         pkg_distro = NULL;
221
222         if (pkg_distros)
223                 while (*pkg_distros)  {
224                         if (!strcmp((*pkg_distros)->name, distro_name)) {
225                                 pkg_distro = *pkg_distros;
226                                 break;
227                         }
228
229                         pkg_distros++;
230                 }
231
232         if (!pkg_distro) {
233                 pkg_distro = distro_stats_new(distro_name);
234                 tmp
235                         = (struct distro_stats **)
236                         list_add((void **)pkg->distros, (void *)pkg_distro);
237                 free(pkg->distros);
238                 pkg->distros = tmp;
239         }
240
241         pkg_distro->download_count += distro_count;
242         pkg_distro->ddts = add_totals(pkg_distro->ddts, ddts);
243 }
244
245 struct ppa_stats *
246 create_ppa_stats(const char *owner,
247                  const char *ppa_name,
248                  const char *package_status,
249                  int ws_size)
250 {
251         struct ppa_stats *ppa;
252         struct bpph **history, **h_cur, *h;
253         char *ppa_url, *pkg_name, *pkg_version;
254         struct package_stats *pkg;
255         struct version_stats *version;
256         const struct distro_series *distro_series;
257         const struct distro_arch_series *arch_series;
258         struct distro_stats *distro;
259         struct arch_stats *arch;
260         int count;
261         struct daily_download_total **totals;
262
263         ppa_url = get_archive_url(owner, ppa_name);
264         history = get_bpph_list(ppa_url, package_status, ws_size);
265         free(ppa_url);
266
267         if (!history) {
268                 log_err(_("Failed to retrieve PPA information"));
269                 exit(EXIT_FAILURE);
270         }
271
272         ppa = malloc(sizeof(struct ppa_stats));
273         ppa->name = strdup(ppa_name);
274         ppa->owner = strdup(owner);
275         ppa->packages = NULL;
276         ppa->daily_download_totals = NULL;
277         ppa->download_count = 0;
278
279         for (h_cur = history; *h_cur; ++h_cur) {
280                 h = *h_cur;
281                 totals = get_daily_download_totals(h->self_link);
282                 if (!totals) {
283                         log_err(_("Failed to retrieve download totals for %s"),
284                                 h->self_link);
285                         continue;
286                 }
287                 count = ddts_get_count(totals);
288                 pkg_name = h->binary_package_name;
289                 pkg_version = h->binary_package_version;
290                 arch_series
291                         = get_distro_arch_series(h->distro_arch_series_link);
292                 distro_series
293                         = get_distro_series(arch_series->distroseries_link);
294
295                 ppa->download_count += count;
296                 ppa->daily_download_totals
297                         = add_totals(ppa->daily_download_totals, totals);
298
299                 pkg = get_package_stats(ppa, pkg_name);
300                 pkg->download_count += count;
301                 pkg->daily_download_totals
302                         = add_totals(pkg->daily_download_totals, totals);
303
304                 version = get_version_stats(pkg, pkg_version);
305                 version->download_count += count;
306                 version->daily_download_totals
307                         = add_totals(version->daily_download_totals, totals);
308
309                 distro = get_distro_stats(version, distro_series->name);
310                 distro->download_count += count;
311
312                 arch = get_arch_stats(distro, arch_series->architecture_tag);
313                 arch->download_count += count;
314
315                 pkg_add_distro(pkg, distro_series->name, count, totals);
316
317                 daily_download_total_list_free(totals);
318         }
319
320         bpph_list_free(history);
321
322         return ppa;
323 }
324
325 static void arch_stats_free(struct arch_stats *arch)
326 {
327         free(arch->name);
328         free(arch);
329 }
330
331 static void distro_stats_free(struct distro_stats *distro)
332 {
333         struct arch_stats **archs;
334
335         archs = distro->archs;
336         if (archs) {
337                 while (*archs) {
338                         arch_stats_free(*archs);
339                         archs++;
340                 }
341                 free(distro->archs);
342         }
343
344         free(distro->name);
345         free(distro);
346 }
347
348 static void distro_stats_list_free(struct distro_stats **distros)
349 {
350         if (distros) {
351                 while (*distros) {
352                         distro_stats_free(*distros);
353                         distros++;
354                 }
355         }
356 }
357
358 static void version_stats_free(struct version_stats *version)
359 {
360         struct distro_stats **distros;
361
362         distros = version->distros;
363         if (distros) {
364                 while (*distros) {
365                         distro_stats_free(*distros);
366                         distros++;
367                 }
368                 free(version->distros);
369         }
370
371         free(version->version);
372         free(version);
373 }
374
375 static void package_stats_free(struct package_stats *package)
376 {
377         struct version_stats **versions;
378
379         versions = package->versions;
380         if (versions) {
381                 while (*versions) {
382                         version_stats_free(*versions);
383                         versions++;
384                 }
385                 free(package->versions);
386         }
387         distro_stats_list_free(package->distros);
388         free(package->name);
389         free(package);
390 }
391
392 void ppa_stats_free(struct ppa_stats *ppastats)
393 {
394         struct package_stats **packages;
395
396         packages = ppastats->packages;
397         if (packages) {
398                 while (*packages) {
399                         package_stats_free(*packages);
400                         packages++;
401                 }
402                 free(ppastats->packages);
403         }
404
405         free(ppastats->owner);
406         free(ppastats->name);
407
408         daily_download_total_list_free(ppastats->daily_download_totals);
409
410         free(ppastats);
411 }