Code

store: Let JSON formatter know about the to-be-formatted object type.
[sysdb.git] / src / core / store_json.c
1 /*
2  * SysDB - src/core/store_json.c
3  * Copyright (C) 2013-2014 Sebastian 'tokkee' Harl <sh@tokkee.org>
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
17  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
18  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR
19  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
20  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
28 /*
29  * This module implements JSON support.
30  */
32 #if HAVE_CONFIG_H
33 #       include "config.h"
34 #endif /* HAVE_CONFIG_H */
36 #include "sysdb.h"
37 #include "core/store-private.h"
38 #include "utils/error.h"
40 #include <assert.h>
42 #include <stdlib.h>
44 /*
45  * private data types
46  */
48 struct sdb_store_json_formatter {
49         sdb_strbuf_t *buf;
51         /* The context describes the state of the formatter through
52          * the path pointing to the current object */
53         int context[8];
54         size_t current;
56         /* The current host context when processing non-host objects */
57         sdb_store_obj_t *current_host;
59         int type;
60         int flags;
61 };
63 /*
64  * private helper functions
65  */
67 static int
68 json_emit(sdb_store_json_formatter_t *f, sdb_store_obj_t *obj)
69 {
70         char time_str[64];
71         char interval_str[64];
72         size_t i;
74         assert(f && obj);
76         if ((f->type != SDB_HOST) && (f->type == obj->type)) {
77                 /* create the host for the current entry first */
78                 assert(obj->parent && (obj->parent->type == SDB_HOST));
79                 if (f->current_host != obj->parent) {
80                         json_emit(f, obj->parent);
81                         sdb_strbuf_append(f->buf, ", \"%ss\": [",
82                                         SDB_STORE_TYPE_TO_NAME(obj->type));
83                         f->current_host = obj->parent;
84                 }
85         }
87         sdb_strbuf_append(f->buf, "{\"name\": \"%s\", ", SDB_OBJ(obj)->name);
88         if (obj->type == SDB_ATTRIBUTE) {
89                 char tmp[sdb_data_strlen(&ATTR(obj)->value) + 1];
90                 if (sdb_data_format(&ATTR(obj)->value, tmp, sizeof(tmp),
91                                         SDB_DOUBLE_QUOTED) < 0)
92                         snprintf(tmp, sizeof(tmp), "<error>");
93                 sdb_strbuf_append(f->buf, "\"value\": %s, ", tmp);
94         }
96         /* TODO: make time and interval formats configurable */
97         if (! sdb_strftime(time_str, sizeof(time_str),
98                                 "%F %T %z", obj->last_update))
99                 snprintf(time_str, sizeof(time_str), "<error>");
100         time_str[sizeof(time_str) - 1] = '\0';
102         if (! sdb_strfinterval(interval_str, sizeof(interval_str),
103                                 obj->interval))
104                 snprintf(interval_str, sizeof(interval_str), "<error>");
105         interval_str[sizeof(interval_str) - 1] = '\0';
107         sdb_strbuf_append(f->buf, "\"last_update\": \"%s\", "
108                         "\"update_interval\": \"%s\", \"backends\": [",
109                         time_str, interval_str);
111         for (i = 0; i < obj->backends_num; ++i) {
112                 sdb_strbuf_append(f->buf, "\"%s\"", obj->backends[i]);
113                 if (i < obj->backends_num - 1)
114                         sdb_strbuf_append(f->buf, ",");
115         }
116         sdb_strbuf_append(f->buf, "]");
117         return 0;
118 } /* json_emit */
120 /*
121  * public API
122  */
124 sdb_store_json_formatter_t *
125 sdb_store_json_formatter(sdb_strbuf_t *buf, int type, int flags)
127         sdb_store_json_formatter_t *f;
129         if (! buf)
130                 return NULL;
132         if ((type != SDB_HOST) && (type != SDB_SERVICE) && (type != SDB_METRIC))
133                 return NULL;
135         f = calloc(1, sizeof(*f));
136         if (! f)
137                 return NULL;
139         f->buf = buf;
140         f->context[0] = 0;
141         f->current = 0;
143         f->current_host = NULL;
145         f->type = type;
146         f->flags = flags;
147         return f;
148 } /* sdb_store_json_formatter */
150 int
151 sdb_store_json_emit(sdb_store_json_formatter_t *f, sdb_store_obj_t *obj)
153         if ((! f) || (! obj))
154                 return -1;
156         if ((f->type != SDB_HOST) && (obj->type == SDB_HOST)) {
157                 sdb_log(SDB_LOG_ERR, "store: Unexpected object of type host "
158                                 "during %s JSON serialization",
159                                 SDB_STORE_TYPE_TO_NAME(f->type));
160                 return -1;
161         }
163         /* first top-level object */
164         if (! f->context[0]) {
165                 if (obj->type != f->type) {
166                         sdb_log(SDB_LOG_ERR, "store: Unexpected object of type %s "
167                                         "as the first element during %s JSON serialization",
168                                         SDB_STORE_TYPE_TO_NAME(obj->type),
169                                         SDB_STORE_TYPE_TO_NAME(f->type));
170                         return -1;
171                 }
172                 if (f->flags & SDB_WANT_ARRAY)
173                         sdb_strbuf_append(f->buf, "[");
174                 assert(f->current == 0);
175                 f->context[0] = obj->type;
176                 return json_emit(f, obj);
177         }
179         if ((f->current >= 1) && (obj->type != SDB_ATTRIBUTE)) {
180                 /* new entry of a previous type or a new type on the same level;
181                  * rewind to the right state */
182                 while (f->current > 0) {
183                         if (f->context[f->current] == obj->type)
184                                 break;
185                         sdb_strbuf_append(f->buf, "}]");
186                         --f->current;
187                 }
188         }
189         if ((obj->type == f->type) && (f->type != SDB_HOST))
190                 if (obj->parent != f->current_host)
191                         sdb_strbuf_append(f->buf, "}]");
193         if (obj->type == f->context[f->current]) {
194                 /* new entry of the same type */
195                 sdb_strbuf_append(f->buf, "},");
196         }
197         else if ((f->context[f->current] == SDB_HOST)
198                         || (obj->type == SDB_ATTRIBUTE)) {
199                 assert(obj->type != SDB_HOST);
200                 /* all object types may be children of a host;
201                  * attributes may be children of any type */
202                 sdb_strbuf_append(f->buf, ", \"%ss\": [",
203                                 SDB_STORE_TYPE_TO_NAME(obj->type));
204                 ++f->current;
205         }
206         else {
207                 sdb_log(SDB_LOG_ERR, "store: Unexpected object of type %s "
208                                 "on level %zu during JSON serialization",
209                                 SDB_STORE_TYPE_TO_NAME(obj->type), f->current);
210                 return -1;
211         }
213         json_emit(f, obj);
214         assert(f->current < SDB_STATIC_ARRAY_LEN(f->context));
215         f->context[f->current] = obj->type;
216         return 0;
217 } /* sdb_store_json_emit */
219 int
220 sdb_store_json_emit_full(sdb_store_json_formatter_t *f, sdb_store_obj_t *obj,
221                 sdb_store_matcher_t *filter)
223         sdb_avltree_t *trees[] = { NULL, NULL, NULL };
224         size_t i;
226         if (sdb_store_json_emit(f, obj))
227                 return -1;
229         if (obj->type == SDB_HOST) {
230                 trees[0] = HOST(obj)->attributes;
231                 trees[1] = HOST(obj)->metrics;
232                 trees[2] = HOST(obj)->services;
233         }
234         else if (obj->type == SDB_SERVICE)
235                 trees[0] = SVC(obj)->attributes;
236         else if (obj->type == SDB_METRIC)
237                 trees[0] = METRIC(obj)->attributes;
238         else if (obj->type == SDB_ATTRIBUTE)
239                 return 0;
240         else
241                 return -1;
243         for (i = 0; i < SDB_STATIC_ARRAY_LEN(trees); ++i) {
244                 sdb_avltree_iter_t *iter;
246                 if (! trees[i])
247                         continue;
249                 iter = sdb_avltree_get_iter(trees[i]);
250                 while (sdb_avltree_iter_has_next(iter)) {
251                         sdb_store_obj_t *child;
252                         child = STORE_OBJ(sdb_avltree_iter_get_next(iter));
254                         if (filter && (! sdb_store_matcher_matches(filter, child, NULL)))
255                                 continue;
257                         if (sdb_store_json_emit_full(f, child, filter)) {
258                                 sdb_avltree_iter_destroy(iter);
259                                 return -1;
260                         }
261                 }
262                 sdb_avltree_iter_destroy(iter);
263         }
264         return 0;
265 } /* sdb_store_json_emit_full */
267 int
268 sdb_store_json_finish(sdb_store_json_formatter_t *f)
270         if (! f)
271                 return -1;
273         if (! f->context[0]) {
274                 /* no content */
275                 if (f->flags & SDB_WANT_ARRAY)
276                         sdb_strbuf_append(f->buf, "[]");
277                 return 0;
278         }
280         while (f->current > 0) {
281                 sdb_strbuf_append(f->buf, "}]");
282                 --f->current;
283         }
284         if (f->context[0] != SDB_HOST) {
285                 assert(f->type != SDB_HOST);
286                 sdb_strbuf_append(f->buf, "}]}");
287         }
288         else
289                 sdb_strbuf_append(f->buf, "}");
291         if (f->flags & SDB_WANT_ARRAY)
292                 sdb_strbuf_append(f->buf, "]");
293         return 0;
294 } /* sdb_store_json_finish */
296 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */