Code

store: Include update interval in the JSON serialization.
authorSebastian Harl <sh@tokkee.org>
Thu, 3 Apr 2014 13:28:00 +0000 (15:28 +0200)
committerSebastian Harl <sh@tokkee.org>
Thu, 3 Apr 2014 13:28:00 +0000 (15:28 +0200)
src/core/store.c
t/core/store_test.c

index 0dec68db0ba9eea881c97bc00a6a178f4356c640..fd08de9101272dfafa7e7b0e6f8e4dbe1d626675 100644 (file)
@@ -353,6 +353,7 @@ store_obj_tojson(sdb_llist_t *list, int type, sdb_strbuf_t *buf)
 {
        sdb_llist_iter_t *iter;
        char time_str[64];
+       char interval_str[64];
 
        assert((type == SDB_ATTRIBUTE) || (type == SDB_SERVICE));
 
@@ -378,16 +379,23 @@ store_obj_tojson(sdb_llist_t *list, int type, sdb_strbuf_t *buf)
                        snprintf(time_str, sizeof(time_str), "<error>");
                time_str[sizeof(time_str) - 1] = '\0';
 
+               if (! sdb_strfinterval(interval_str, sizeof(interval_str),
+                                       sobj->interval))
+                       snprintf(interval_str, sizeof(interval_str), "<error>");
+               interval_str[sizeof(interval_str) - 1] = '\0';
+
                sdb_strbuf_append(buf, "{\"name\": \"%s\", ", SDB_OBJ(sobj)->name);
                if (type == SDB_ATTRIBUTE) {
                        char tmp[sdb_data_strlen(&SDB_ATTR(sobj)->value) + 1];
                        sdb_data_format(&SDB_ATTR(sobj)->value, tmp, sizeof(tmp),
                                        SDB_DOUBLE_QUOTED);
-                       sdb_strbuf_append(buf, "\"value\": %s, \"last_update\": \"%s\"}",
-                                       tmp, time_str);
+                       sdb_strbuf_append(buf, "\"value\": %s, \"last_update\": \"%s\", "
+                                       "\"update_interval\": \"%s\"}", tmp, time_str,
+                                       interval_str);
                }
                else
-                       sdb_strbuf_append(buf, "\"last_update\": \"%s\"}", time_str);
+                       sdb_strbuf_append(buf, "\"last_update\": \"%s\", "
+                                       "\"update_interval\": \"%s\"}", time_str, interval_str);
 
                if (sdb_llist_iter_has_next(iter))
                        sdb_strbuf_append(buf, ",");
@@ -503,6 +511,7 @@ sdb_store_host_tojson(sdb_store_base_t *h, sdb_strbuf_t *buf, int flags)
 {
        sdb_store_obj_t *host;
        char time_str[64];
+       char interval_str[64];
 
        if ((! h) || (h->type != SDB_HOST) || (! buf))
                return -1;
@@ -514,9 +523,14 @@ sdb_store_host_tojson(sdb_store_base_t *h, sdb_strbuf_t *buf, int flags)
                snprintf(time_str, sizeof(time_str), "<error>");
        time_str[sizeof(time_str) - 1] = '\0';
 
+       if (! sdb_strfinterval(interval_str, sizeof(interval_str),
+                               host->_interval))
+               snprintf(interval_str, sizeof(interval_str), "<error>");
+       interval_str[sizeof(interval_str) - 1] = '\0';
+
        sdb_strbuf_append(buf, "{\"name\": \"%s\", "
-                       "\"last_update\": \"%s\"",
-                       SDB_OBJ(host)->name, time_str);
+                       "\"last_update\": \"%s\", \"update_interval\": \"%s\"",
+                       SDB_OBJ(host)->name, time_str, interval_str);
 
        if (! (flags & SDB_SKIP_ATTRIBUTES)) {
                sdb_strbuf_append(buf, ", \"attributes\": ");
index d0ef5a910cbc7e5c15c32fb93ff58d8b85ee5bce..bc514027d5d9bbfb34719be5c7b375731b68a11a 100644 (file)
@@ -277,44 +277,72 @@ START_TEST(test_store_tojson)
        } golden_data[] = {
                { 0, "{\"hosts\":["
                                "{\"name\": \"h1\", \"last_update\": \"1970-01-01 00:00:00 +0000\", "
+                                       "\"update_interval\": \"0s\", "
                                        "\"attributes\": ["
-                                               "{\"name\": \"k1\", \"value\": \"v1\", \"last_update\": \"1970-01-01 00:00:00 +0000\"},"
-                                               "{\"name\": \"k2\", \"value\": \"v2\", \"last_update\": \"1970-01-01 00:00:00 +0000\"},"
-                                               "{\"name\": \"k3\", \"value\": \"v3\", \"last_update\": \"1970-01-01 00:00:00 +0000\"}"
+                                               "{\"name\": \"k1\", \"value\": \"v1\", "
+                                                       "\"last_update\": \"1970-01-01 00:00:00 +0000\", "
+                                                       "\"update_interval\": \"0s\"},"
+                                               "{\"name\": \"k2\", \"value\": \"v2\", "
+                                                       "\"last_update\": \"1970-01-01 00:00:00 +0000\", "
+                                                       "\"update_interval\": \"0s\"},"
+                                               "{\"name\": \"k3\", \"value\": \"v3\", "
+                                                       "\"last_update\": \"1970-01-01 00:00:00 +0000\", "
+                                                       "\"update_interval\": \"0s\"}"
                                        "], "
                                        "\"services\": []},"
                                "{\"name\": \"h2\", \"last_update\": \"1970-01-01 00:00:00 +0000\", "
+                                       "\"update_interval\": \"0s\", "
                                        "\"attributes\": [], "
                                        "\"services\": ["
-                                               "{\"name\": \"s1\", \"last_update\": \"1970-01-01 00:00:00 +0000\"},"
-                                               "{\"name\": \"s2\", \"last_update\": \"1970-01-01 00:00:00 +0000\"}"
+                                               "{\"name\": \"s1\", "
+                                                       "\"last_update\": \"1970-01-01 00:00:00 +0000\", "
+                                                       "\"update_interval\": \"0s\"},"
+                                               "{\"name\": \"s2\", "
+                                                       "\"last_update\": \"1970-01-01 00:00:00 +0000\", "
+                                                       "\"update_interval\": \"0s\"}"
                                        "]}"
                        "]}" },
                { SDB_SKIP_SERVICES,
                        "{\"hosts\":["
                                "{\"name\": \"h1\", \"last_update\": \"1970-01-01 00:00:00 +0000\", "
+                                       "\"update_interval\": \"0s\", "
                                        "\"attributes\": ["
-                                               "{\"name\": \"k1\", \"value\": \"v1\", \"last_update\": \"1970-01-01 00:00:00 +0000\"},"
-                                               "{\"name\": \"k2\", \"value\": \"v2\", \"last_update\": \"1970-01-01 00:00:00 +0000\"},"
-                                               "{\"name\": \"k3\", \"value\": \"v3\", \"last_update\": \"1970-01-01 00:00:00 +0000\"}"
+                                               "{\"name\": \"k1\", \"value\": \"v1\", "
+                                                       "\"last_update\": \"1970-01-01 00:00:00 +0000\", "
+                                                       "\"update_interval\": \"0s\"},"
+                                               "{\"name\": \"k2\", \"value\": \"v2\", "
+                                                       "\"last_update\": \"1970-01-01 00:00:00 +0000\", "
+                                                       "\"update_interval\": \"0s\"},"
+                                               "{\"name\": \"k3\", \"value\": \"v3\", "
+                                                       "\"last_update\": \"1970-01-01 00:00:00 +0000\", "
+                                                       "\"update_interval\": \"0s\"}"
                                        "]},"
                                "{\"name\": \"h2\", \"last_update\": \"1970-01-01 00:00:00 +0000\", "
+                                       "\"update_interval\": \"0s\", "
                                        "\"attributes\": []}"
                        "]}" },
                { SDB_SKIP_ATTRIBUTES,
                        "{\"hosts\":["
                                "{\"name\": \"h1\", \"last_update\": \"1970-01-01 00:00:00 +0000\", "
+                                       "\"update_interval\": \"0s\", "
                                        "\"services\": []},"
                                "{\"name\": \"h2\", \"last_update\": \"1970-01-01 00:00:00 +0000\", "
+                                       "\"update_interval\": \"0s\", "
                                        "\"services\": ["
-                                               "{\"name\": \"s1\", \"last_update\": \"1970-01-01 00:00:00 +0000\"},"
-                                               "{\"name\": \"s2\", \"last_update\": \"1970-01-01 00:00:00 +0000\"}"
+                                               "{\"name\": \"s1\", "
+                                                       "\"last_update\": \"1970-01-01 00:00:00 +0000\", "
+                                                       "\"update_interval\": \"0s\"},"
+                                               "{\"name\": \"s2\", "
+                                                       "\"last_update\": \"1970-01-01 00:00:00 +0000\", "
+                                                       "\"update_interval\": \"0s\"}"
                                        "]}"
                        "]}" },
                { SDB_SKIP_SERVICES | SDB_SKIP_ATTRIBUTES,
                        "{\"hosts\":["
-                               "{\"name\": \"h1\", \"last_update\": \"1970-01-01 00:00:00 +0000\"},"
-                               "{\"name\": \"h2\", \"last_update\": \"1970-01-01 00:00:00 +0000\"}"
+                               "{\"name\": \"h1\", \"last_update\": \"1970-01-01 00:00:00 +0000\", "
+                                       "\"update_interval\": \"0s\"},"
+                               "{\"name\": \"h2\", \"last_update\": \"1970-01-01 00:00:00 +0000\", "
+                                       "\"update_interval\": \"0s\"}"
                        "]}" },
        };