From 130c971dd9197cd1e1724ea80b25c44d2cdd9b01 Mon Sep 17 00:00:00 2001 From: Sebastian Harl Date: Tue, 1 Jul 2014 21:59:29 +0200 Subject: [PATCH] store: Include backend information in the JSON serialization. --- src/core/store.c | 11 ++++++++++- t/unit/core/store_test.c | 36 ++++++++++++++++++------------------ 2 files changed, 28 insertions(+), 19 deletions(-) diff --git a/src/core/store.c b/src/core/store.c index 125009c..d185431 100644 --- a/src/core/store.c +++ b/src/core/store.c @@ -379,6 +379,7 @@ store_common_tojson(sdb_store_obj_t *obj, sdb_strbuf_t *buf) { char time_str[64]; char interval_str[64]; + size_t i; if (! sdb_strftime(time_str, sizeof(time_str), "%F %T %z", obj->last_update)) @@ -391,7 +392,15 @@ store_common_tojson(sdb_store_obj_t *obj, sdb_strbuf_t *buf) interval_str[sizeof(interval_str) - 1] = '\0'; sdb_strbuf_append(buf, "\"last_update\": \"%s\", " - "\"update_interval\": \"%s\"", time_str, interval_str); + "\"update_interval\": \"%s\", \"backends\": [", + time_str, interval_str); + + for (i = 0; i < obj->backends_num; ++i) { + sdb_strbuf_append(buf, "\"%s\"", obj->backends[i]); + if (i < obj->backends_num - 1) + sdb_strbuf_append(buf, ","); + } + sdb_strbuf_append(buf, "]"); } /* store_common_tojson */ /* diff --git a/t/unit/core/store_test.c b/t/unit/core/store_test.c index cca3968..4e40743 100644 --- a/t/unit/core/store_test.c +++ b/t/unit/core/store_test.c @@ -278,72 +278,72 @@ START_TEST(test_store_tojson) } golden_data[] = { { 0, "{\"hosts\":[" "{\"name\": \"h1\", \"last_update\": \"1970-01-01 00:00:00 +0000\", " - "\"update_interval\": \"0s\", " + "\"update_interval\": \"0s\", \"backends\": [], " "\"attributes\": [" "{\"name\": \"k1\", \"value\": \"v1\", " "\"last_update\": \"1970-01-01 00:00:00 +0000\", " - "\"update_interval\": \"0s\"}," + "\"update_interval\": \"0s\", \"backends\": []}," "{\"name\": \"k2\", \"value\": \"v2\", " "\"last_update\": \"1970-01-01 00:00:00 +0000\", " - "\"update_interval\": \"0s\"}," + "\"update_interval\": \"0s\", \"backends\": []}," "{\"name\": \"k3\", \"value\": \"v3\", " "\"last_update\": \"1970-01-01 00:00:00 +0000\", " - "\"update_interval\": \"0s\"}" + "\"update_interval\": \"0s\", \"backends\": []}" "], " "\"services\": []}," "{\"name\": \"h2\", \"last_update\": \"1970-01-01 00:00:00 +0000\", " - "\"update_interval\": \"0s\", " + "\"update_interval\": \"0s\", \"backends\": [], " "\"attributes\": [], " "\"services\": [" "{\"name\": \"s1\", " "\"last_update\": \"1970-01-01 00:00:00 +0000\", " - "\"update_interval\": \"0s\"}," + "\"update_interval\": \"0s\", \"backends\": []}," "{\"name\": \"s2\", " "\"last_update\": \"1970-01-01 00:00:00 +0000\", " - "\"update_interval\": \"0s\"}" + "\"update_interval\": \"0s\", \"backends\": []}" "]}" "]}" }, { SDB_SKIP_SERVICES, "{\"hosts\":[" "{\"name\": \"h1\", \"last_update\": \"1970-01-01 00:00:00 +0000\", " - "\"update_interval\": \"0s\", " + "\"update_interval\": \"0s\", \"backends\": [], " "\"attributes\": [" "{\"name\": \"k1\", \"value\": \"v1\", " "\"last_update\": \"1970-01-01 00:00:00 +0000\", " - "\"update_interval\": \"0s\"}," + "\"update_interval\": \"0s\", \"backends\": []}," "{\"name\": \"k2\", \"value\": \"v2\", " "\"last_update\": \"1970-01-01 00:00:00 +0000\", " - "\"update_interval\": \"0s\"}," + "\"update_interval\": \"0s\", \"backends\": []}," "{\"name\": \"k3\", \"value\": \"v3\", " "\"last_update\": \"1970-01-01 00:00:00 +0000\", " - "\"update_interval\": \"0s\"}" + "\"update_interval\": \"0s\", \"backends\": []}" "]}," "{\"name\": \"h2\", \"last_update\": \"1970-01-01 00:00:00 +0000\", " - "\"update_interval\": \"0s\", " + "\"update_interval\": \"0s\", \"backends\": [], " "\"attributes\": []}" "]}" }, { SDB_SKIP_ATTRIBUTES, "{\"hosts\":[" "{\"name\": \"h1\", \"last_update\": \"1970-01-01 00:00:00 +0000\", " - "\"update_interval\": \"0s\", " + "\"update_interval\": \"0s\", \"backends\": [], " "\"services\": []}," "{\"name\": \"h2\", \"last_update\": \"1970-01-01 00:00:00 +0000\", " - "\"update_interval\": \"0s\", " + "\"update_interval\": \"0s\", \"backends\": [], " "\"services\": [" "{\"name\": \"s1\", " "\"last_update\": \"1970-01-01 00:00:00 +0000\", " - "\"update_interval\": \"0s\"}," + "\"update_interval\": \"0s\", \"backends\": []}," "{\"name\": \"s2\", " "\"last_update\": \"1970-01-01 00:00:00 +0000\", " - "\"update_interval\": \"0s\"}" + "\"update_interval\": \"0s\", \"backends\": []}" "]}" "]}" }, { SDB_SKIP_SERVICES | SDB_SKIP_ATTRIBUTES, "{\"hosts\":[" "{\"name\": \"h1\", \"last_update\": \"1970-01-01 00:00:00 +0000\", " - "\"update_interval\": \"0s\"}," + "\"update_interval\": \"0s\", \"backends\": []}," "{\"name\": \"h2\", \"last_update\": \"1970-01-01 00:00:00 +0000\", " - "\"update_interval\": \"0s\"}" + "\"update_interval\": \"0s\", \"backends\": []}" "]}" }, }; -- 2.30.2