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