only save useful att 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%z", &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                  "architecture_specific",
101                  json_object_new_boolean(bpph->architecture_specific));
102
103         json_object_object_add
104                 (json, "status", json_object_new_string(bpph->status));
105
106         date = malloc(strlen("YY-MM-DDThh:mm:ss+xxx") + 1);
107         strftime(date,
108                  strlen("YY-MM-DDThh:mm:ss+xxx") + 1,
109                  "%FT%T%z",
110                  &bpph->date_created);
111
112         json_object_object_add
113                 (json, "date_created", json_object_new_string(date));
114         free(date);
115
116         return json;
117 }
118
119 struct distro_arch_series *json_object_to_distro_arch_series(json_object *o)
120 {
121         const char *display_name;
122         const char *title;
123         const char *architecture_tag;
124         boolean is_nominated_arch_indep;
125         const char *distroseries_link;
126
127         display_name = json_object_get_string
128                 (json_object_object_get(o, "display_name"));
129
130         title = json_object_get_string
131                 (json_object_object_get(o, "title"));
132
133         architecture_tag = json_object_get_string
134                 (json_object_object_get(o, "architecture_tag"));
135
136         distroseries_link = json_object_get_string
137                 (json_object_object_get(o, "distroseries_link"));
138
139         is_nominated_arch_indep = json_object_get_boolean
140                 (json_object_object_get(o, "is_nominated_arch_indep"));
141
142         return distro_arch_series_new(display_name,
143                                       title,
144                                       architecture_tag,
145                                       is_nominated_arch_indep,
146                                       distroseries_link);
147 }
148
149 struct distro_series *json_object_to_distro_series(json_object *o)
150 {
151         const char *displayname;
152         const char *title;
153         const char *name;
154         const char *version;
155
156         displayname = json_object_get_string
157                 (json_object_object_get(o, "displayname"));
158
159         title = json_object_get_string(json_object_object_get(o, "title"));
160
161         version = json_object_get_string(json_object_object_get(o, "version"));
162
163         name = json_object_get_string(json_object_object_get(o, "name"));
164
165         return distro_series_new(name,
166                                  version,
167                                  title,
168                                  displayname);
169 }
170
171 struct bpph **json_object_to_bpph_list(json_object *o)
172 {
173         json_object *o_entries;
174         int i, n, i2;
175         struct bpph **entries, *h;
176         const struct distro_arch_series *distro;
177
178         o_entries = json_object_object_get(o, "entries");
179
180         if (!o_entries)
181                 return NULL;
182
183         n = json_object_array_length(o_entries);
184
185         entries = malloc
186                 (sizeof(struct bpph *)*(n+1));
187
188         for (i = 0, i2 = 0; i < n; i++) {
189                 h = json_to_bpph(json_object_array_get_idx(o_entries,
190                                                            i));
191
192                 if (!h->architecture_specific) {
193                         distro = get_distro_arch_series
194                                 (h->distro_arch_series_link);
195
196                         if (!distro || !distro->is_nominated_arch_indep) {
197                                 bpph_free(h);
198                                 continue ;
199                         }
200                 }
201
202                 entries[i2] = h;
203                 i2++;
204         }
205         entries[i2] = NULL;
206
207         return entries;
208 }
209
210 json_object *bpph_list_to_json(struct bpph **list)
211 {
212         json_object *result, *entries;
213         struct bpph **cur;
214
215         result = json_object_new_object();
216
217         entries = json_object_new_array();
218         json_object_object_add(result, "entries", entries);
219
220         for (cur = list; *cur; cur++)
221                 json_object_array_add(entries, bpph_to_json(*cur));
222
223         return result;
224 }
225
226 struct daily_download_total *
227 json_object_to_daily_download_total(const char *d, json_object *o_c)
228 {
229         struct daily_download_total *result;
230
231         result = malloc(sizeof(struct daily_download_total));
232         result->count = json_object_get_int(o_c);
233
234         strptime(d, "%FT%T%z", &result->date);
235
236         return result;
237 }
238
239 static int json_object_get_fields_count(json_object *o)
240 {
241         int n = 0;
242         struct lh_entry *entry;
243
244         entry = json_object_get_object(o)->head;
245         while (entry) {
246                 entry = entry->next;
247                 n++;
248         }
249
250         return n;
251 }
252
253 struct daily_download_total * *
254 json_object_to_daily_download_totals(json_object *o)
255 {
256         int n, i;
257         struct daily_download_total **result;
258
259         n = json_object_get_fields_count(o);
260
261         result = malloc
262                 (sizeof(struct daily_download_total *)*(n+1));
263
264         i = 0;
265         json_object_object_foreach(o, key, val) {
266                 result[i] = json_object_to_daily_download_total(key, val);
267                 i++;
268         }
269
270         result[n] = NULL;
271
272         return result;
273 }