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