From 6cc7fedd176358fddf4a77095da86f1fcf857088 Mon Sep 17 00:00:00 2001 From: Sebastian Harl Date: Thu, 3 Apr 2014 15:28:00 +0200 Subject: [PATCH] store: Include update interval in the JSON serialization. --- src/core/store.c | 24 ++++++++++++++++----- t/core/store_test.c | 52 ++++++++++++++++++++++++++++++++++----------- 2 files changed, 59 insertions(+), 17 deletions(-) diff --git a/src/core/store.c b/src/core/store.c index 0dec68d..fd08de9 100644 --- a/src/core/store.c +++ b/src/core/store.c @@ -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), ""); time_str[sizeof(time_str) - 1] = '\0'; + if (! sdb_strfinterval(interval_str, sizeof(interval_str), + sobj->interval)) + snprintf(interval_str, sizeof(interval_str), ""); + 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), ""); time_str[sizeof(time_str) - 1] = '\0'; + if (! sdb_strfinterval(interval_str, sizeof(interval_str), + host->_interval)) + snprintf(interval_str, sizeof(interval_str), ""); + 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\": "); diff --git a/t/core/store_test.c b/t/core/store_test.c index d0ef5a9..bc51402 100644 --- a/t/core/store_test.c +++ b/t/core/store_test.c @@ -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\"}" "]}" }, }; -- 2.30.2