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