ddb8f31f836121afa2ab8efff6becda751bff893
[ppastats.git] / src / lp_json.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 #define _XOPEN_SOURCE_EXTENDED
20 #define _XOPEN_SOURCE
21 #include <stdlib.h>
22 #include <stdio.h>
23 #include <string.h>
24 #include <time.h>
25
26 #include "lp_json.h"
27 #include "lp_ws.h"
28
29 /* Declares json_bool to have consistent code even with
30    old json lib releases using boolean instead of json_bool.*/
31 #ifndef json_bool
32 typedef boolean json_bool;
33 #endif
34
35 static time_t json_to_time(json_object *json)
36 {
37         const char *str;
38         struct tm tm;
39         char *ret;
40
41         str = json_object_get_string(json);
42         if (!str)
43                 return -1;
44
45         tm.tm_isdst = -1;
46         ret = strptime(str, "%FT%T", &tm);
47
48         if (ret)
49                 return mktime(&tm);
50         else
51                 return -1;
52 }
53
54 static json_object *time_to_json(time_t t)
55 {
56         char *str;
57
58         str = time_to_str(t);
59
60         if (str)
61                 return json_object_new_string(str);
62         else
63                 return NULL;
64 }
65
66 static struct bpph *json_to_bpph(json_object *o)
67 {
68         const char *binary_package_name;
69         const char *binary_package_version;
70         const char *distro_arch_series_link;
71         const char *self_link;
72         int arch_specific;
73         struct bpph *bpph;
74         const char *status;
75         time_t date_created;
76
77         binary_package_name = json_object_get_string
78                 (json_object_object_get(o, "binary_package_name"));
79
80         binary_package_version = json_object_get_string
81                 (json_object_object_get(o, "binary_package_version"));
82
83         distro_arch_series_link = json_object_get_string
84                 (json_object_object_get(o, "distro_arch_series_link"));
85
86         self_link = json_object_get_string
87                 (json_object_object_get(o, "self_link"));
88
89         arch_specific = json_object_get_boolean
90                 (json_object_object_get(o, "architecture_specific"));
91
92         date_created = json_to_time(json_object_object_get(o, "date_created"));
93
94         status = json_object_get_string(json_object_object_get(o, "status"));
95
96         bpph =  bpph_new(binary_package_name,
97                          binary_package_version,
98                          distro_arch_series_link,
99                          self_link,
100                          status,
101                          arch_specific,
102                          date_created);
103
104         return bpph;
105 }
106
107 static json_object *bpph_to_json(struct bpph *bpph)
108 {
109         json_object *json;
110
111         json = json_object_new_object();
112
113         json_object_object_add
114                 (json,
115                  "binary_package_name",
116                  json_object_new_string(bpph->binary_package_name));
117
118         json_object_object_add
119                 (json,
120                  "binary_package_version",
121                  json_object_new_string(bpph->binary_package_version));
122
123         json_object_object_add
124                 (json,
125                  "distro_arch_series_link",
126                  json_object_new_string(bpph->distro_arch_series_link));
127
128         json_object_object_add
129                 (json, "self_link", json_object_new_string(bpph->self_link));
130
131         json_object_object_add
132                 (json,
133                  "architecture_specific",
134                  json_object_new_boolean(bpph->architecture_specific));
135
136         json_object_object_add
137                 (json, "status", json_object_new_string(bpph->status));
138
139         json_object_object_add
140                 (json, "date_created", time_to_json(bpph->date_created));
141
142         return json;
143 }
144
145 struct distro_arch_series *json_object_to_distro_arch_series(json_object *o)
146 {
147         const char *display_name;
148         const char *title;
149         const char *architecture_tag;
150         json_bool is_nominated_arch_indep;
151         const char *distroseries_link;
152
153         display_name = json_object_get_string
154                 (json_object_object_get(o, "display_name"));
155
156         title = json_object_get_string
157                 (json_object_object_get(o, "title"));
158
159         architecture_tag = json_object_get_string
160                 (json_object_object_get(o, "architecture_tag"));
161
162         distroseries_link = json_object_get_string
163                 (json_object_object_get(o, "distroseries_link"));
164
165         is_nominated_arch_indep = json_object_get_boolean
166                 (json_object_object_get(o, "is_nominated_arch_indep"));
167
168         return distro_arch_series_new(display_name,
169                                       title,
170                                       architecture_tag,
171                                       is_nominated_arch_indep,
172                                       distroseries_link);
173 }
174
175 struct distro_series *json_object_to_distro_series(json_object *o)
176 {
177         const char *displayname;
178         const char *title;
179         const char *name;
180         const char *version;
181
182         displayname = json_object_get_string
183                 (json_object_object_get(o, "displayname"));
184
185         title = json_object_get_string(json_object_object_get(o, "title"));
186
187         version = json_object_get_string(json_object_object_get(o, "version"));
188
189         name = json_object_get_string(json_object_object_get(o, "name"));
190
191         return distro_series_new(name,
192                                  version,
193                                  title,
194                                  displayname);
195 }
196
197 struct bpph **json_object_to_bpph_list(json_object *o)
198 {
199         json_object *o_entries;
200         int i, n, i2;
201         struct bpph **entries, *h;
202         const struct distro_arch_series *distro;
203
204         o_entries = json_object_object_get(o, "entries");
205
206         if (!o_entries)
207                 return NULL;
208
209         n = json_object_array_length(o_entries);
210
211         entries = malloc
212                 (sizeof(struct bpph *)*(n+1));
213
214         for (i = 0, i2 = 0; i < n; i++) {
215                 h = json_to_bpph(json_object_array_get_idx(o_entries,
216                                                            i));
217
218                 if (!h->architecture_specific) {
219                         distro = get_distro_arch_series
220                                 (h->distro_arch_series_link);
221
222                         if (!distro || !distro->is_nominated_arch_indep) {
223                                 bpph_free(h);
224                                 continue ;
225                         }
226                 }
227
228                 entries[i2] = h;
229                 i2++;
230         }
231         entries[i2] = NULL;
232
233         return entries;
234 }
235
236 json_object *bpph_list_to_json(struct bpph **list)
237 {
238         json_object *result, *entries;
239         struct bpph **cur;
240
241         result = json_object_new_object();
242
243         entries = json_object_new_array();
244         json_object_object_add(result, "entries", entries);
245
246         if (list)
247                 for (cur = list; *cur; cur++)
248                         json_object_array_add(entries, bpph_to_json(*cur));
249
250         return result;
251 }
252
253 struct daily_download_total *
254 json_object_to_daily_download_total(const char *d, json_object *o_c)
255 {
256         struct daily_download_total *result;
257
258         result = malloc(sizeof(struct daily_download_total));
259         result->count = json_object_get_int(o_c);
260
261         strptime(d, "%FT%T%z", &result->date);
262
263         return result;
264 }
265
266 static int json_object_get_fields_count(json_object *o)
267 {
268         int n = 0;
269         struct lh_entry *entry;
270
271         entry = json_object_get_object(o)->head;
272         while (entry) {
273                 entry = entry->next;
274                 n++;
275         }
276
277         return n;
278 }
279
280 struct daily_download_total * *
281 json_object_to_daily_download_totals(json_object *o)
282 {
283         int n, i;
284         struct daily_download_total **result;
285
286         n = json_object_get_fields_count(o);
287
288         result = malloc
289                 (sizeof(struct daily_download_total *)*(n+1));
290
291         i = 0;
292         json_object_object_foreach(o, key, val) {
293                 result[i] = json_object_to_daily_download_total(key, val);
294                 i++;
295         }
296
297         result[n] = NULL;
298
299         return result;
300 }