Code

parser: Let the TIMESERIES command accept optional data-source names.
[sysdb.git] / src / core / store_json.c
index 878cc4fe265886653c754a60a83ef23889af1668..256fdeac0496f9a55f97fa53bf3ee10f7c375ada 100644 (file)
@@ -1,6 +1,6 @@
 /*
  * SysDB - src/core/store_json.c
- * Copyright (C) 2013-2014 Sebastian 'tokkee' Harl <sh@tokkee.org>
+ * Copyright (C) 2013-2015 Sebastian 'tokkee' Harl <sh@tokkee.org>
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
@@ -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>
 
@@ -58,9 +59,6 @@ struct sdb_store_json_formatter {
        int context[8];
        size_t current;
 
-       /* The current host context when processing non-host objects */
-       sdb_store_obj_t *current_host;
-
        int type;
        int flags;
 };
@@ -83,8 +81,6 @@ formatter_init(sdb_object_t *obj, va_list ap)
 
        f->context[0] = 0;
        f->current = 0;
-
-       f->current_host = NULL;
        return 0;
 } /* formatter_init */
 
@@ -94,6 +90,30 @@ static sdb_type_t formatter_type = {
        /* destroy = */ NULL,
 };
 
+/* A generic representation of a stored object. */
+typedef struct {
+       /* identifier */
+       int type;
+       const char *name;
+
+       /* attribute value (optional) */
+       sdb_data_t *value;
+
+       /* metric's timeseries (optional)
+        * -1: unset
+        *  0: false
+        *  1: true */
+       int timeseries;
+       char **data_names;
+       size_t data_names_len;
+
+       /* generic meta-data */
+       sdb_time_t last_update;
+       sdb_time_t interval;
+       size_t backends_num;
+       const char * const *backends;
+} obj_t;
+
 /*
  * private helper functions
  */
@@ -125,33 +145,82 @@ escape_string(const char *src, char *dest)
        dest[i + 1] = '\0';
 } /* escape_string */
 
+/* handle_new_object takes care of all maintenance logic related to adding a
+ * new object. That is, it manages context information and emit the prefix and
+ * suffix of an object. */
 static int
-json_emit(sdb_store_json_formatter_t *f, sdb_store_obj_t *obj)
+handle_new_object(sdb_store_json_formatter_t *f, int type)
+{
+       /* first top-level object */
+       if (! f->context[0]) {
+               if ((type != f->type) && (type != SDB_HOST)) {
+                       sdb_log(SDB_LOG_ERR, "store: Unexpected object of type %s "
+                                       "as the first element during %s JSON serialization",
+                                       SDB_STORE_TYPE_TO_NAME(type),
+                                       SDB_STORE_TYPE_TO_NAME(f->type));
+                       return -1;
+               }
+               if (f->flags & SDB_WANT_ARRAY)
+                       sdb_strbuf_append(f->buf, "[");
+               assert(f->current == 0);
+               f->context[f->current] = type;
+               return 0;
+       }
+
+       if ((f->context[f->current] != SDB_HOST)
+                       && (type != SDB_ATTRIBUTE)) {
+               /* new entry of the same type or a parent object;
+                * rewind to the right state */
+               while ((f->current > 0)
+                               && (f->context[f->current] != type)) {
+                       sdb_strbuf_append(f->buf, "}]");
+                       --f->current;
+               }
+       }
+
+       if (type == f->context[f->current]) {
+               /* new entry of the same type */
+               sdb_strbuf_append(f->buf, "},");
+       }
+       else if ((f->context[f->current] == SDB_HOST)
+                       || (type == SDB_ATTRIBUTE)) {
+               assert(type != SDB_HOST);
+               /* all object types may be children of a host;
+                * attributes may be children of any type */
+               sdb_strbuf_append(f->buf, ", \"%ss\": [",
+                               SDB_STORE_TYPE_TO_NAME(type));
+               ++f->current;
+       }
+       else {
+               sdb_log(SDB_LOG_ERR, "store: Unexpected object of type %s "
+                               "on level %zu during JSON serialization",
+                               SDB_STORE_TYPE_TO_NAME(type), f->current);
+               return -1;
+       }
+
+       assert(f->current < SDB_STATIC_ARRAY_LEN(f->context));
+       f->context[f->current] = type;
+       return 0;
+} /* handle_new_object */
+
+static int
+json_emit(sdb_store_json_formatter_t *f, obj_t *obj)
 {
        char time_str[64];
        char interval_str[64];
-       char name[2 * strlen(SDB_OBJ(obj)->name) + 3];
+       char name[2 * strlen(obj->name) + 3];
        size_t i;
 
        assert(f && obj);
 
-       if ((f->type != SDB_HOST) && (f->type == obj->type)) {
-               /* create the host for the current entry first */
-               assert(obj->parent && (obj->parent->type == SDB_HOST));
-               if (f->current_host != obj->parent) {
-                       json_emit(f, obj->parent);
-                       sdb_strbuf_append(f->buf, ", \"%ss\": [",
-                                       SDB_STORE_TYPE_TO_NAME(obj->type));
-                       f->current_host = obj->parent;
-               }
-       }
+       handle_new_object(f, obj->type);
 
-       escape_string(SDB_OBJ(obj)->name, name);
+       escape_string(obj->name, name);
        sdb_strbuf_append(f->buf, "{\"name\": %s, ", name);
-       if (obj->type == SDB_ATTRIBUTE) {
-               char tmp[sdb_data_strlen(&ATTR(obj)->value) + 1];
+       if ((obj->type == SDB_ATTRIBUTE) && (obj->value)) {
+               char tmp[sdb_data_strlen(obj->value) + 1];
                char val[2 * sizeof(tmp) + 3];
-               if (! sdb_data_format(&ATTR(obj)->value, tmp, sizeof(tmp),
+               if (! sdb_data_format(obj->value, tmp, sizeof(tmp),
                                        SDB_DOUBLE_QUOTED))
                        snprintf(tmp, sizeof(tmp), "<error>");
 
@@ -164,11 +233,23 @@ json_emit(sdb_store_json_formatter_t *f, sdb_store_obj_t *obj)
                else
                        sdb_strbuf_append(f->buf, "\"value\": %s, ", tmp);
        }
-       else if (obj->type == SDB_METRIC) {
-               if (METRIC(obj)->store.type != NULL)
+       else if ((obj->type == SDB_METRIC) && (obj->timeseries >= 0)) {
+               if (obj->timeseries)
                        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 */
@@ -194,133 +275,147 @@ json_emit(sdb_store_json_formatter_t *f, sdb_store_obj_t *obj)
        return 0;
 } /* json_emit */
 
-/*
- * public API
- */
-
-sdb_store_json_formatter_t *
-sdb_store_json_formatter(sdb_strbuf_t *buf, int type, int flags)
+static int
+emit_host(sdb_store_host_t *host, sdb_object_t *user_data)
 {
-       return F(sdb_object_create("json-formatter", formatter_type,
-                               buf, type, flags));
-} /* sdb_store_json_formatter */
+       sdb_store_json_formatter_t *f = F(user_data);
 
-int
-sdb_store_json_emit(sdb_store_json_formatter_t *f, sdb_store_obj_t *obj)
-{
-       if ((! f) || (! obj))
+       if ((! host) || (! user_data))
                return -1;
 
-       if ((f->type != SDB_HOST) && (obj->type == SDB_HOST)) {
-               sdb_log(SDB_LOG_ERR, "store: Unexpected object of type host "
-                               "during %s JSON serialization",
-                               SDB_STORE_TYPE_TO_NAME(f->type));
-               return -1;
-       }
+       {
+               obj_t o = {
+                       SDB_HOST,
+                       host->name,
 
-       /* first top-level object */
-       if (! f->context[0]) {
-               if (obj->type != f->type) {
-                       sdb_log(SDB_LOG_ERR, "store: Unexpected object of type %s "
-                                       "as the first element during %s JSON serialization",
-                                       SDB_STORE_TYPE_TO_NAME(obj->type),
-                                       SDB_STORE_TYPE_TO_NAME(f->type));
-                       return -1;
-               }
-               if (f->flags & SDB_WANT_ARRAY)
-                       sdb_strbuf_append(f->buf, "[");
-               assert(f->current == 0);
-               f->context[0] = obj->type;
-               return json_emit(f, obj);
-       }
+                       /* value */ NULL,
+                       /* timeseries */ -1, NULL, 0,
 
-       if ((f->current >= 1) && (obj->type != SDB_ATTRIBUTE)) {
-               /* new entry of a previous type or a new type on the same level;
-                * rewind to the right state */
-               while (f->current > 0) {
-                       if (f->context[f->current] == obj->type)
-                               break;
-                       sdb_strbuf_append(f->buf, "}]");
-                       --f->current;
-               }
-       }
-       if ((obj->type == f->type) && (f->type != SDB_HOST))
-               if (obj->parent != f->current_host)
-                       sdb_strbuf_append(f->buf, "}]");
+                       host->last_update,
+                       host->interval,
+                       host->backends_num,
+                       (const char * const *)host->backends,
+               };
 
-       if (obj->type == f->context[f->current]) {
-               /* new entry of the same type */
-               sdb_strbuf_append(f->buf, "},");
-       }
-       else if ((f->context[f->current] == SDB_HOST)
-                       || (obj->type == SDB_ATTRIBUTE)) {
-               assert(obj->type != SDB_HOST);
-               /* all object types may be children of a host;
-                * attributes may be children of any type */
-               sdb_strbuf_append(f->buf, ", \"%ss\": [",
-                               SDB_STORE_TYPE_TO_NAME(obj->type));
-               ++f->current;
+               return json_emit(f, &o);
        }
-       else {
-               sdb_log(SDB_LOG_ERR, "store: Unexpected object of type %s "
-                               "on level %zu during JSON serialization",
-                               SDB_STORE_TYPE_TO_NAME(obj->type), f->current);
+} /* emit_host */
+
+static int
+emit_service(sdb_store_service_t *service, sdb_object_t *user_data)
+{
+       sdb_store_json_formatter_t *f = F(user_data);
+
+       if ((! service) || (! user_data))
                return -1;
-       }
 
-       json_emit(f, obj);
-       assert(f->current < SDB_STATIC_ARRAY_LEN(f->context));
-       f->context[f->current] = obj->type;
-       return 0;
-} /* sdb_store_json_emit */
+       {
+               obj_t o = {
+                       SDB_SERVICE,
+                       service->name,
 
-int
-sdb_store_json_emit_full(sdb_store_json_formatter_t *f, sdb_store_obj_t *obj,
-               sdb_store_matcher_t *filter)
+                       /* value */ NULL,
+                       /* timeseries */ -1, NULL, 0,
+
+                       service->last_update,
+                       service->interval,
+                       service->backends_num,
+                       (const char * const *)service->backends,
+               };
+
+               return json_emit(f, &o);
+       }
+} /* emit_service */
+
+static int
+emit_metric(sdb_store_metric_t *metric, sdb_object_t *user_data)
 {
-       sdb_avltree_t *trees[] = { NULL, NULL, NULL };
+       sdb_store_json_formatter_t *f = F(user_data);
+       int status;
        size_t i;
 
-       if (sdb_store_json_emit(f, obj))
+       if ((! metric) || (! user_data))
                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;
+       {
+               obj_t o = {
+                       SDB_METRIC,
+                       metric->name,
 
-       for (i = 0; i < SDB_STATIC_ARRAY_LEN(trees); ++i) {
-               sdb_avltree_iter_t *iter;
+                       /* value */ NULL,
+                       /* timeseries */ metric->stores_num > 0, NULL, 0,
 
-               if (! trees[i])
-                       continue;
+                       metric->last_update,
+                       metric->interval,
+                       metric->backends_num,
+                       (const char * const *)metric->backends,
+               };
 
-               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));
+               for (i = 0; i < metric->stores_num; i++) {
+                       const sdb_metric_store_t *s = metric->stores + i;
+                       size_t j;
 
-                       if (filter && (! sdb_store_matcher_matches(filter, child, NULL)))
+                       if (! s->info)
                                continue;
 
-                       if (sdb_store_json_emit_full(f, child, filter)) {
-                               sdb_avltree_iter_destroy(iter);
-                               return -1;
+                       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]);
                }
-               sdb_avltree_iter_destroy(iter);
+
+               status = json_emit(f, &o);
+               stringv_free(&o.data_names, &o.data_names_len);
+               return status;
        }
-       return 0;
-} /* sdb_store_json_emit_full */
+} /* emit_metric */
+
+static int
+emit_attribute(sdb_store_attribute_t *attr, sdb_object_t *user_data)
+{
+       sdb_store_json_formatter_t *f = F(user_data);
+
+       if ((! attr) || (! user_data))
+               return -1;
+
+       {
+               obj_t o = {
+                       SDB_ATTRIBUTE,
+                       attr->key,
+
+                       /* value */ &attr->value,
+                       /* timeseries */ -1, NULL, 0,
+
+                       attr->last_update,
+                       attr->interval,
+                       attr->backends_num,
+                       (const char * const *)attr->backends,
+               };
+
+               return json_emit(f, &o);
+       }
+} /* emit_attribute */
+
+/*
+ * public API
+ */
+
+sdb_store_writer_t sdb_store_json_writer = {
+       emit_host, emit_service, emit_metric, emit_attribute,
+};
+
+sdb_store_json_formatter_t *
+sdb_store_json_formatter(sdb_strbuf_t *buf, int type, int flags)
+{
+       return F(sdb_object_create("json-formatter", formatter_type,
+                               buf, type, flags));
+} /* sdb_store_json_formatter */
 
 int
 sdb_store_json_finish(sdb_store_json_formatter_t *f)
@@ -339,12 +434,7 @@ sdb_store_json_finish(sdb_store_json_formatter_t *f)
                sdb_strbuf_append(f->buf, "}]");
                --f->current;
        }
-       if (f->context[0] != SDB_HOST) {
-               assert(f->type != SDB_HOST);
-               sdb_strbuf_append(f->buf, "}]}");
-       }
-       else
-               sdb_strbuf_append(f->buf, "}");
+       sdb_strbuf_append(f->buf, "}");
 
        if (f->flags & SDB_WANT_ARRAY)
                sdb_strbuf_append(f->buf, "]");