filter out duplicated arch-independant packages
[ppastats.git] / src / lp_json.c
1 /*
2     Copyright (C) 2011 jeanfi@gmail.com
3
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (at your option) any later version.
8
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU 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 #define _XOPEN_SOURCE
21 #include <time.h>
22
23
24 #include <stdlib.h>
25 #include <stdio.h>
26
27 #include "lp_json.h"
28 #include "lp_ws.h"
29
30 struct binary_package_publishing_history *
31 json_object_to_binary_package_publishing_history(json_object *o)
32 {
33         const char *binary_package_name;
34         const char *binary_package_version;
35         const char *distro_arch_series_link;
36         const char *self_link;
37         int architecture_specific;
38
39         binary_package_name = json_object_get_string
40                 (json_object_object_get(o, "binary_package_name"));
41
42         binary_package_version = json_object_get_string
43                 (json_object_object_get(o, "binary_package_version"));
44
45         distro_arch_series_link = json_object_get_string
46                 (json_object_object_get(o, "distro_arch_series_link"));
47
48         self_link = json_object_get_string
49                 (json_object_object_get(o, "self_link"));
50
51         if (json_object_get_boolean
52             (json_object_object_get(o, "architecture_specific")))
53                 architecture_specific = 1;
54         else
55                 architecture_specific = 0;
56
57         return binary_package_publishing_history_new(binary_package_name,
58                                                      binary_package_version,
59                                                      distro_arch_series_link,
60                                                      self_link,
61                                                      architecture_specific);
62 }
63
64 struct distro_arch_series *json_object_to_distro_arch_series(json_object *o)
65 {
66         const char *display_name;
67         const char *title;
68         const char *architecture_tag;
69         boolean is_nominated_arch_indep;
70         const char *distroseries_link;
71
72
73         display_name = json_object_get_string
74                 (json_object_object_get(o, "display_name"));
75
76         title = json_object_get_string
77                 (json_object_object_get(o, "title"));
78
79         architecture_tag = json_object_get_string
80                 (json_object_object_get(o, "architecture_tag"));
81
82         distroseries_link = json_object_get_string
83                 (json_object_object_get(o, "distroseries_link"));
84
85         is_nominated_arch_indep = json_object_get_boolean
86                 (json_object_object_get(o, "is_nominated_arch_indep"));
87
88         return distro_arch_series_new(display_name,
89                                       title,
90                                       architecture_tag,
91                                       is_nominated_arch_indep,
92                                       distroseries_link);
93 }
94 struct distro_series *json_object_to_distro_series(json_object *o)
95 {
96         const char *displayname;
97         const char *title;
98         const char *name;
99         const char *version;
100
101         displayname = json_object_get_string
102                 (json_object_object_get(o, "displayname"));
103
104         title = json_object_get_string
105                 (json_object_object_get(o, "title"));
106
107         version = json_object_get_string
108                 (json_object_object_get(o, "version"));
109
110         name = json_object_get_string
111                 (json_object_object_get(o, "name"));
112
113         return distro_series_new(name,
114                                  version,
115                                  title,
116                                  displayname);
117 }
118
119
120
121 struct binary_package_publishing_history * *
122 json_object_to_binary_package_publishing_history_list(json_object *o)
123 {
124         json_object *o_entries;
125         int i, n, i2;
126         struct binary_package_publishing_history **entries;
127         struct binary_package_publishing_history *h;
128         const struct distro_arch_series *distro;
129
130         o_entries = json_object_object_get(o, "entries");
131
132         if (!o_entries)
133                 return NULL;
134
135         n = json_object_array_length(o_entries);
136
137         entries = malloc
138                 (sizeof(struct binary_package_publishing_history *)*(n+1));
139
140         for (i = 0, i2 = 0; i < n; i++) {
141                 h = json_object_to_binary_package_publishing_history
142                         (json_object_array_get_idx(o_entries, i));
143
144                 if (!h->architecture_specific) {
145                         distro = get_distro_arch_series
146                                 (h->distro_arch_series_link);
147
148                         if (!distro->is_nominated_arch_indep) {
149                                 binary_package_publishing_history_free(h);
150                                 continue ;
151                         }
152                 }
153
154                 entries[i2] = h;
155                 i2++;
156         }
157         entries[i2] = NULL;
158
159         return entries;
160 }
161
162 struct daily_download_total *
163 json_object_to_daily_download_total(const char *d, json_object *o_c)
164 {
165         struct daily_download_total *result;
166
167         result = malloc(sizeof(struct daily_download_total));
168         result->count = json_object_get_int(o_c);
169
170         strptime(d, "%FT%T%z", &result->date);
171
172         return result;
173 }
174
175 static int json_object_get_fields_count(json_object *o)
176 {
177         int n = 0;
178
179         json_object_object_foreach(o, key, val)
180                 n++;
181
182         return n;
183 }
184
185 struct daily_download_total * *
186 json_object_to_daily_download_totals(json_object *o)
187 {
188         int n, i;
189         struct daily_download_total **result;
190
191         n = json_object_get_fields_count(o);
192
193         result = malloc
194                 (sizeof(struct daily_download_total *)*(n+1));
195
196         i = 0;
197         json_object_object_foreach(o, key, val) {
198                 result[i] = json_object_to_daily_download_total(key, val);
199                 i++;
200         }
201
202         result[n] = NULL;
203
204         return result;
205 }