Code

store, plugin: Let the plugin module determine an objects backends.
[sysdb.git] / src / core / store_json.c
index 493ac9f377c9072d54fdf76488ab242e76cc6ec6..878cc4fe265886653c754a60a83ef23889af1668 100644 (file)
 
 #include <assert.h>
 
+#include <ctype.h>
 #include <stdlib.h>
+#include <string.h>
 
 /*
  * private data types
  */
 
 struct sdb_store_json_formatter {
+       sdb_object_t super;
+
+       /* The string buffer to write to */
        sdb_strbuf_t *buf;
 
        /* The context describes the state of the formatter through
@@ -59,16 +64,73 @@ struct sdb_store_json_formatter {
        int type;
        int flags;
 };
+#define F(obj) ((sdb_store_json_formatter_t *)(obj))
+
+static int
+formatter_init(sdb_object_t *obj, va_list ap)
+{
+       sdb_store_json_formatter_t *f = F(obj);
+
+       f->buf = va_arg(ap, sdb_strbuf_t *);
+       if (! f->buf)
+               return -1;
+
+       f->type = va_arg(ap, int);
+       if ((f->type != SDB_HOST) && (f->type != SDB_SERVICE) && (f->type != SDB_METRIC))
+               return -1;
+
+       f->flags = va_arg(ap, int);
+
+       f->context[0] = 0;
+       f->current = 0;
+
+       f->current_host = NULL;
+       return 0;
+} /* formatter_init */
+
+static sdb_type_t formatter_type = {
+       /* size = */ sizeof(sdb_store_json_formatter_t),
+       /* init = */ formatter_init,
+       /* destroy = */ NULL,
+};
 
 /*
  * private helper functions
  */
 
+static void
+escape_string(const char *src, char *dest)
+{
+       size_t i = 1;
+       dest[0] = '"';
+       for ( ; *src; ++src) {
+               char c = *src;
+               if ((c == '"') || (c == '\\') || iscntrl((int)c)) {
+                       dest[i] = '\\';
+                       ++i;
+               }
+               switch (c) {
+                       case '\a': dest[i] = 'a'; break;
+                       case '\b': dest[i] = 'b'; break;
+                       case '\t': dest[i] = 't'; break;
+                       case '\n': dest[i] = 'n'; break;
+                       case '\v': dest[i] = 'v'; break;
+                       case '\f': dest[i] = 'f'; break;
+                       case '\r': dest[i] = 'r'; break;
+                       default: dest[i] = c; break;
+               }
+               ++i;
+       }
+       dest[i] = '"';
+       dest[i + 1] = '\0';
+} /* escape_string */
+
 static int
 json_emit(sdb_store_json_formatter_t *f, sdb_store_obj_t *obj)
 {
        char time_str[64];
        char interval_str[64];
+       char name[2 * strlen(SDB_OBJ(obj)->name) + 3];
        size_t i;
 
        assert(f && obj);
@@ -84,18 +146,33 @@ json_emit(sdb_store_json_formatter_t *f, sdb_store_obj_t *obj)
                }
        }
 
-       sdb_strbuf_append(f->buf, "{\"name\": \"%s\", ", SDB_OBJ(obj)->name);
+       escape_string(SDB_OBJ(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 (sdb_data_format(&ATTR(obj)->value, tmp, sizeof(tmp),
-                                       SDB_DOUBLE_QUOTED) < 0)
+               char val[2 * sizeof(tmp) + 3];
+               if (! sdb_data_format(&ATTR(obj)->value, tmp, sizeof(tmp),
+                                       SDB_DOUBLE_QUOTED))
                        snprintf(tmp, sizeof(tmp), "<error>");
-               sdb_strbuf_append(f->buf, "\"value\": %s, ", tmp);
+
+               if (tmp[0] == '"') {
+                       /* a string; escape_string handles quoting */
+                       tmp[strlen(tmp) - 1] = '\0';
+                       escape_string(tmp + 1, val);
+                       sdb_strbuf_append(f->buf, "\"value\": %s, ", val);
+               }
+               else
+                       sdb_strbuf_append(f->buf, "\"value\": %s, ", tmp);
+       }
+       else if (obj->type == SDB_METRIC) {
+               if (METRIC(obj)->store.type != NULL)
+                       sdb_strbuf_append(f->buf, "\"timeseries\": true, ");
+               else
+                       sdb_strbuf_append(f->buf, "\"timeseries\": false, ");
        }
 
        /* TODO: make time and interval formats configurable */
-       if (! sdb_strftime(time_str, sizeof(time_str),
-                               "%F %T %z", obj->last_update))
+       if (! sdb_strftime(time_str, sizeof(time_str), obj->last_update))
                snprintf(time_str, sizeof(time_str), "<error>");
        time_str[sizeof(time_str) - 1] = '\0';
 
@@ -124,27 +201,8 @@ json_emit(sdb_store_json_formatter_t *f, sdb_store_obj_t *obj)
 sdb_store_json_formatter_t *
 sdb_store_json_formatter(sdb_strbuf_t *buf, int type, int flags)
 {
-       sdb_store_json_formatter_t *f;
-
-       if (! buf)
-               return NULL;
-
-       if ((type != SDB_HOST) && (type != SDB_SERVICE) && (type != SDB_METRIC))
-               return NULL;
-
-       f = calloc(1, sizeof(*f));
-       if (! f)
-               return NULL;
-
-       f->buf = buf;
-       f->context[0] = 0;
-       f->current = 0;
-
-       f->current_host = NULL;
-
-       f->type = type;
-       f->flags = flags;
-       return f;
+       return F(sdb_object_create("json-formatter", formatter_type,
+                               buf, type, flags));
 } /* sdb_store_json_formatter */
 
 int