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