Code

Let the JSON formatter include a metric's data_names.
[sysdb.git] / src / core / store_json.c
index 961e54c0606ae84206d1199758506ad4ff212cbf..256fdeac0496f9a55f97fa53bf3ee10f7c375ada 100644 (file)
@@ -34,8 +34,9 @@
 #endif /* HAVE_CONFIG_H */
 
 #include "sysdb.h"
-#include "core/store-private.h"
+#include "core/store.h"
 #include "utils/error.h"
+#include "utils/strings.h"
 
 #include <assert.h>
 
@@ -103,6 +104,8 @@ typedef struct {
         *  0: false
         *  1: true */
        int timeseries;
+       char **data_names;
+       size_t data_names_len;
 
        /* generic meta-data */
        sdb_time_t last_update;
@@ -235,6 +238,18 @@ json_emit(sdb_store_json_formatter_t *f, obj_t *obj)
                        sdb_strbuf_append(f->buf, "\"timeseries\": true, ");
                else
                        sdb_strbuf_append(f->buf, "\"timeseries\": false, ");
+
+               if (obj->data_names_len > 0) {
+                       sdb_strbuf_append(f->buf, "\"data_names\": [");
+                       for (i = 0; i < obj->data_names_len; i++) {
+                               char dn[2 * strlen(obj->data_names[i]) + 3];
+                               escape_string(obj->data_names[i], dn);
+                               sdb_strbuf_append(f->buf, "%s", dn);
+                               if (i < obj->data_names_len - 1)
+                                       sdb_strbuf_append(f->buf, ", ");
+                       }
+                       sdb_strbuf_append(f->buf, "], ");
+               }
        }
 
        /* TODO: make time and interval formats configurable */
@@ -274,7 +289,7 @@ emit_host(sdb_store_host_t *host, sdb_object_t *user_data)
                        host->name,
 
                        /* value */ NULL,
-                       /* timeseries */ -1,
+                       /* timeseries */ -1, NULL, 0,
 
                        host->last_update,
                        host->interval,
@@ -300,7 +315,7 @@ emit_service(sdb_store_service_t *service, sdb_object_t *user_data)
                        service->name,
 
                        /* value */ NULL,
-                       /* timeseries */ -1,
+                       /* timeseries */ -1, NULL, 0,
 
                        service->last_update,
                        service->interval,
@@ -316,6 +331,8 @@ static int
 emit_metric(sdb_store_metric_t *metric, sdb_object_t *user_data)
 {
        sdb_store_json_formatter_t *f = F(user_data);
+       int status;
+       size_t i;
 
        if ((! metric) || (! user_data))
                return -1;
@@ -326,7 +343,7 @@ emit_metric(sdb_store_metric_t *metric, sdb_object_t *user_data)
                        metric->name,
 
                        /* value */ NULL,
-                       /* timeseries */ metric->store.type != NULL,
+                       /* timeseries */ metric->stores_num > 0, NULL, 0,
 
                        metric->last_update,
                        metric->interval,
@@ -334,7 +351,28 @@ emit_metric(sdb_store_metric_t *metric, sdb_object_t *user_data)
                        (const char * const *)metric->backends,
                };
 
-               return json_emit(f, &o);
+               for (i = 0; i < metric->stores_num; i++) {
+                       const sdb_metric_store_t *s = metric->stores + i;
+                       size_t j;
+
+                       if (! s->info)
+                               continue;
+
+                       if (! o.data_names) {
+                               stringv_copy(&o.data_names, &o.data_names_len,
+                                               (const char * const *)s->info->data_names,
+                                               s->info->data_names_len);
+                               continue;
+                       }
+
+                       for (j = 0; j < s->info->data_names_len; j++)
+                               stringv_append_if_missing(&o.data_names, &o.data_names_len,
+                                               s->info->data_names[j]);
+               }
+
+               status = json_emit(f, &o);
+               stringv_free(&o.data_names, &o.data_names_len);
+               return status;
        }
 } /* emit_metric */
 
@@ -352,7 +390,7 @@ emit_attribute(sdb_store_attribute_t *attr, sdb_object_t *user_data)
                        attr->key,
 
                        /* value */ &attr->value,
-                       /* timeseries */ -1,
+                       /* timeseries */ -1, NULL, 0,
 
                        attr->last_update,
                        attr->interval,
@@ -379,133 +417,6 @@ sdb_store_json_formatter(sdb_strbuf_t *buf, int type, int flags)
                                buf, type, flags));
 } /* sdb_store_json_formatter */
 
-/* TODO: Move sdb_store_emit* somewhere else. */
-
-int
-sdb_store_emit(sdb_store_obj_t *obj, sdb_store_writer_t *w, sdb_object_t *wd)
-{
-       if ((! obj) || (! w))
-               return -1;
-
-       switch (obj->type) {
-       case SDB_HOST:
-               {
-                       sdb_store_host_t host = {
-                               obj->_name,
-                               obj->last_update,
-                               obj->interval,
-                               (const char * const *)obj->backends,
-                               obj->backends_num,
-                       };
-                       if (! w->store_host)
-                               return -1;
-                       return w->store_host(&host, wd);
-               }
-       case SDB_SERVICE:
-               {
-                       sdb_store_service_t service = {
-                               obj->parent ? obj->parent->_name : NULL,
-                               obj->_name,
-                               obj->last_update,
-                               obj->interval,
-                               (const char * const *)obj->backends,
-                               obj->backends_num,
-                       };
-                       if (! w->store_service)
-                               return -1;
-                       return w->store_service(&service, wd);
-               }
-       case SDB_METRIC:
-               {
-                       sdb_store_metric_t metric = {
-                               obj->parent ? obj->parent->_name : NULL,
-                               obj->_name,
-                               {
-                                       METRIC(obj)->store.type,
-                                       METRIC(obj)->store.id,
-                               },
-                               obj->last_update,
-                               obj->interval,
-                               (const char * const *)obj->backends,
-                               obj->backends_num,
-                       };
-                       if (! w->store_metric)
-                               return -1;
-                       return w->store_metric(&metric, wd);
-               }
-       case SDB_ATTRIBUTE:
-               {
-                       sdb_store_attribute_t attr = {
-                               NULL,
-                               obj->parent ? obj->parent->type : 0,
-                               obj->parent ? obj->parent->_name : NULL,
-                               obj->_name,
-                               ATTR(obj)->value,
-                               obj->last_update,
-                               obj->interval,
-                               (const char * const *)obj->backends,
-                               obj->backends_num,
-                       };
-                       if (obj->parent && (obj->parent->type != SDB_HOST)
-                                       && obj->parent->parent)
-                               attr.hostname = obj->parent->parent->_name;
-                       if (! w->store_attribute)
-                               return -1;
-                       return w->store_attribute(&attr, wd);
-               }
-       }
-
-       return -1;
-} /* sdb_store_emit */
-
-int
-sdb_store_emit_full(sdb_store_obj_t *obj, sdb_store_matcher_t *filter,
-               sdb_store_writer_t *w, sdb_object_t *wd)
-{
-       sdb_avltree_t *trees[] = { NULL, NULL, NULL };
-       size_t i;
-
-       if (sdb_store_emit(obj, w, wd))
-               return -1;
-
-       if (obj->type == SDB_HOST) {
-               trees[0] = HOST(obj)->attributes;
-               trees[1] = HOST(obj)->metrics;
-               trees[2] = HOST(obj)->services;
-       }
-       else if (obj->type == SDB_SERVICE)
-               trees[0] = SVC(obj)->attributes;
-       else if (obj->type == SDB_METRIC)
-               trees[0] = METRIC(obj)->attributes;
-       else if (obj->type == SDB_ATTRIBUTE)
-               return 0;
-       else
-               return -1;
-
-       for (i = 0; i < SDB_STATIC_ARRAY_LEN(trees); ++i) {
-               sdb_avltree_iter_t *iter;
-
-               if (! trees[i])
-                       continue;
-
-               iter = sdb_avltree_get_iter(trees[i]);
-               while (sdb_avltree_iter_has_next(iter)) {
-                       sdb_store_obj_t *child;
-                       child = STORE_OBJ(sdb_avltree_iter_get_next(iter));
-
-                       if (filter && (! sdb_store_matcher_matches(filter, child, NULL)))
-                               continue;
-
-                       if (sdb_store_emit_full(child, filter, w, wd)) {
-                               sdb_avltree_iter_destroy(iter);
-                               return -1;
-                       }
-               }
-               sdb_avltree_iter_destroy(iter);
-       }
-       return 0;
-} /* sdb_store_emit_full */
-
 int
 sdb_store_json_finish(sdb_store_json_formatter_t *f)
 {