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