Code

store_json: Base the memstore emitter on the store-writer API.
[sysdb.git] / src / core / store_json.c
index 26a0854eeb4ef09957d7cd400bc5e699496cd7bc..a069d8543872ea79e20edd8f31c0355f372488dd 100644 (file)
@@ -260,10 +260,118 @@ json_emit(sdb_store_json_formatter_t *f, obj_t *obj)
        return 0;
 } /* json_emit */
 
+static int
+emit_host(sdb_store_host_t *host, sdb_object_t *user_data)
+{
+       sdb_store_json_formatter_t *f = F(user_data);
+
+       if ((! host) || (! user_data))
+               return -1;
+
+       {
+               obj_t o = {
+                       SDB_HOST,
+                       host->name,
+
+                       /* value */ NULL,
+                       /* timeseries */ -1,
+
+                       host->last_update,
+                       host->interval,
+                       host->backends_num,
+                       (const char * const *)host->backends,
+               };
+
+               return json_emit(f, &o);
+       }
+} /* 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;
+
+       {
+               obj_t o = {
+                       SDB_SERVICE,
+                       service->name,
+
+                       /* value */ NULL,
+                       /* timeseries */ -1,
+
+                       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_store_json_formatter_t *f = F(user_data);
+
+       if ((! metric) || (! user_data))
+               return -1;
+
+       {
+               obj_t o = {
+                       SDB_METRIC,
+                       metric->name,
+
+                       /* value */ NULL,
+                       /* timeseries */ metric->store.type != NULL,
+
+                       metric->last_update,
+                       metric->interval,
+                       metric->backends_num,
+                       (const char * const *)metric->backends,
+               };
+
+               return json_emit(f, &o);
+       }
+} /* 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,
+
+                       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)
 {
@@ -277,27 +385,67 @@ sdb_store_json_emit(sdb_store_json_formatter_t *f, sdb_store_obj_t *obj)
        if ((! f) || (! obj))
                return -1;
 
-       {
-               obj_t o = {
-                       obj->type,
-                       obj->_name,
-
-                       /* value */ NULL,
-                       /* timeseries */ -1,
-
-                       obj->last_update,
-                       obj->interval,
-                       obj->backends_num,
-                       (const char * const *)obj->backends,
-               };
-
-               if (obj->type == SDB_ATTRIBUTE)
-                       o.value = &ATTR(obj)->value;
-               if (obj->type == SDB_METRIC)
-                       o.timeseries = METRIC(obj)->store.type != NULL;
-
-               return json_emit(f, &o);
+       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,
+                       };
+                       return sdb_store_json_writer.store_host(&host, SDB_OBJ(f));
+               }
+       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,
+                       };
+                       return sdb_store_json_writer.store_service(&service, SDB_OBJ(f));
+               }
+       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,
+                       };
+                       return sdb_store_json_writer.store_metric(&metric, SDB_OBJ(f));
+               }
+       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;
+                       return sdb_store_json_writer.store_attribute(&attr, SDB_OBJ(f));
+               }
        }
+
+       return -1;
 } /* sdb_store_json_emit */
 
 int