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