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