some cleanup
[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, *item;
165
166         if (totals) {
167                 cur = totals;
168                 while (*cur) {
169                         item = *cur;
170
171                         if (item->date.tm_year == total->date.tm_year &&
172                             item->date.tm_mon == total->date.tm_mon &&
173                             item->date.tm_mday == total->date.tm_mday) {
174                                 item->count += total->count;
175                                 return totals;
176                         }
177
178                         cur++;
179                 }
180         }
181
182         result = (struct daily_download_total **)list_add((void **)totals,
183                                                           ddt_clone(total));
184
185         return result;
186 }
187
188 static struct daily_download_total **add_totals
189 (struct daily_download_total **total1, struct daily_download_total **total2)
190 {
191         struct daily_download_total **cur, **result, **tmp;
192
193         result = total1;
194         cur = total2;
195         while (*cur) {
196                 tmp = add_total(result, *cur);
197                 if (result != total1 && result != tmp)
198                         free(result);
199                 result = tmp;
200                 cur++;
201         }
202
203         return result;
204 }
205
206 static void
207 pkg_add_distro(struct package_stats *pkg,
208                const char *distro_name,
209                int distro_count,
210                struct daily_download_total **ddts)
211 {
212         struct distro_stats **pkg_distros, *pkg_distro, **tmp;
213         struct daily_download_total **tmp_ddts;
214
215         pkg_distros = pkg->distros;
216         pkg_distro = NULL;
217
218         if (pkg_distros)
219                 while (*pkg_distros)  {
220                         if (!strcmp((*pkg_distros)->name, distro_name)) {
221                                 pkg_distro = *pkg_distros;
222                                 break;
223                         }
224
225                         pkg_distros++;
226                 }
227
228         if (!pkg_distro) {
229                 pkg_distro = distro_stats_new(distro_name);
230                 tmp = (struct distro_stats **)list_add((void **)pkg->distros,
231                                                        (void *)pkg_distro);
232                 free(pkg->distros);
233                 pkg->distros = tmp;
234         }
235
236         pkg_distro->download_count += distro_count;
237
238         tmp_ddts = add_totals(pkg_distro->ddts, ddts);
239         if (pkg_distro->ddts && pkg_distro->ddts != tmp_ddts)
240                 free(pkg_distro->ddts);
241         pkg_distro->ddts = tmp_ddts;
242 }
243
244 struct ppa_stats *
245 create_ppa_stats(const char *owner,
246                  const char *ppa_name,
247                  const char *package_status,
248                  int ws_size)
249 {
250         struct ppa_stats *ppa;
251         struct bpph **history, **h_cur, *h;
252         char *ppa_url, *pkg_name, *pkg_version;
253         struct package_stats *pkg;
254         struct version_stats *version;
255         const struct distro_series *distro_series;
256         const struct distro_arch_series *arch_series;
257         struct distro_stats *distro;
258         struct arch_stats *arch;
259         int count;
260         struct daily_download_total **totals, **tmp;
261
262         ppa_url = get_archive_url(owner, ppa_name);
263         history = get_bpph_list(ppa_url, package_status, ws_size);
264         free(ppa_url);
265
266         if (!history) {
267                 log_err(_("Failed to retrieve PPA information"));
268                 exit(EXIT_FAILURE);
269         }
270
271         ppa = malloc(sizeof(struct ppa_stats));
272         ppa->name = strdup(ppa_name);
273         ppa->owner = strdup(owner);
274         ppa->packages = NULL;
275         ppa->daily_download_totals = NULL;
276         ppa->download_count = 0;
277
278         for (h_cur = history; *h_cur; ++h_cur) {
279                 h = *h_cur;
280                 totals = get_daily_download_totals(h->self_link);
281                 if (!totals) {
282                         log_err(_("Failed to retrieve download totals for %s"),
283                                 h->self_link);
284                         continue;
285                 }
286                 count = ddts_get_count(totals);
287                 pkg_name = h->binary_package_name;
288                 pkg_version = h->binary_package_version;
289                 arch_series
290                         = get_distro_arch_series(h->distro_arch_series_link);
291                 distro_series
292                         = get_distro_series(arch_series->distroseries_link);
293
294                 ppa->download_count += count;
295                 tmp = add_totals(ppa->daily_download_totals, totals);
296                 if (ppa->daily_download_totals != tmp)
297                         free(ppa->daily_download_totals);
298                 ppa->daily_download_totals = tmp;
299
300                 pkg = get_package_stats(ppa, pkg_name);
301                 pkg->download_count += count;
302                 tmp = add_totals(pkg->daily_download_totals, totals);
303                 if (pkg->daily_download_totals != tmp)
304                         free(pkg->daily_download_totals);
305                 pkg->daily_download_totals = tmp;
306
307                 version = get_version_stats(pkg, pkg_version);
308                 version->download_count += count;
309                 tmp = add_totals(version->daily_download_totals, totals);
310                 if (version->daily_download_totals != tmp)
311                         free(version->daily_download_totals);
312                 version->daily_download_totals = tmp;
313
314                 distro = get_distro_stats(version, distro_series->name);
315                 distro->download_count += count;
316
317                 arch = get_arch_stats(distro, arch_series->architecture_tag);
318                 arch->download_count += count;
319
320                 pkg_add_distro(pkg, distro_series->name, count, totals);
321
322                 daily_download_total_list_free(totals);
323         }
324
325         bpph_list_free(history);
326
327         return ppa;
328 }
329
330 static void arch_stats_free(struct arch_stats *arch)
331 {
332         free(arch->name);
333         free(arch);
334 }
335
336 static void distro_stats_free(struct distro_stats *distro)
337 {
338         struct arch_stats **archs;
339
340         archs = distro->archs;
341         if (archs) {
342                 while (*archs) {
343                         arch_stats_free(*archs);
344                         archs++;
345                 }
346                 free(distro->archs);
347         }
348
349         free(distro->name);
350         free(distro);
351 }
352
353 static void distro_stats_list_free(struct distro_stats **distros)
354 {
355         if (distros) {
356                 while (*distros) {
357                         distro_stats_free(*distros);
358                         distros++;
359                 }
360         }
361 }
362
363 static void version_stats_free(struct version_stats *version)
364 {
365         struct distro_stats **distros;
366
367         distros = version->distros;
368         if (distros) {
369                 while (*distros) {
370                         distro_stats_free(*distros);
371                         distros++;
372                 }
373                 free(version->distros);
374         }
375
376         free(version->version);
377         free(version);
378 }
379
380 static void package_stats_free(struct package_stats *package)
381 {
382         struct version_stats **versions;
383
384         versions = package->versions;
385         if (versions) {
386                 while (*versions) {
387                         version_stats_free(*versions);
388                         versions++;
389                 }
390                 free(package->versions);
391         }
392         distro_stats_list_free(package->distros);
393         free(package->name);
394         free(package);
395 }
396
397 void ppa_stats_free(struct ppa_stats *ppastats)
398 {
399         struct package_stats **packages;
400
401         packages = ppastats->packages;
402         if (packages) {
403                 while (*packages) {
404                         package_stats_free(*packages);
405                         packages++;
406                 }
407                 free(ppastats->packages);
408         }
409
410         free(ppastats->owner);
411         free(ppastats->name);
412
413         daily_download_total_list_free(ppastats->daily_download_totals);
414
415         free(ppastats);
416 }