X-Git-Url: https://git.tokkee.org/?p=sysdb.git;a=blobdiff_plain;f=src%2Fcore%2Fstore_json.c;h=878cc4fe265886653c754a60a83ef23889af1668;hp=8ec717515b69c7e480f0f46c289af689004c4a03;hb=b75718ea9fe4d6c90f1794e517a0712729553c0c;hpb=1fc4fcdedf87070a5a6d6a1dd24678403bbea478 diff --git a/src/core/store_json.c b/src/core/store_json.c index 8ec7175..878cc4f 100644 --- a/src/core/store_json.c +++ b/src/core/store_json.c @@ -48,6 +48,9 @@ */ 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 @@ -61,6 +64,35 @@ 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 @@ -119,8 +151,8 @@ json_emit(sdb_store_json_formatter_t *f, sdb_store_obj_t *obj) if (obj->type == SDB_ATTRIBUTE) { char tmp[sdb_data_strlen(&ATTR(obj)->value) + 1]; char val[2 * sizeof(tmp) + 3]; - if (sdb_data_format(&ATTR(obj)->value, tmp, sizeof(tmp), - SDB_DOUBLE_QUOTED) < 0) + if (! sdb_data_format(&ATTR(obj)->value, tmp, sizeof(tmp), + SDB_DOUBLE_QUOTED)) snprintf(tmp, sizeof(tmp), ""); if (tmp[0] == '"') { @@ -132,10 +164,15 @@ 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) + 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), ""); time_str[sizeof(time_str) - 1] = '\0'; @@ -164,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