diff options
author | Romain Forlot <romain.forlot@iot.bzh> | 2018-04-18 20:07:19 +0200 |
---|---|---|
committer | Sebastien Douheret <sebastien.douheret@iot.bzh> | 2018-07-10 23:41:14 +0200 |
commit | 0697533c9692ec55f711b9eed35ea6dbb4e69326 (patch) | |
tree | 17f4ffa23cc6e1c3217055731da7c30eb9e01556 | |
parent | 6a8f3a37db28ff8a601e5999499c20049c1453d5 (diff) |
Fix wrong handling of metrics arguments
Always handle arguments as an array even if we receive
a simple JSON object. This imply using an intermediary
variable
Change-Id: I0af8fdcd87e4d3f94d88d64ff7195f6c78002635
Signed-off-by: Romain Forlot <romain.forlot@iot.bzh>
-rw-r--r-- | src/plugins/influxdb-writer.c | 28 |
1 files changed, 21 insertions, 7 deletions
diff --git a/src/plugins/influxdb-writer.c b/src/plugins/influxdb-writer.c index a3763d5..a236fe3 100644 --- a/src/plugins/influxdb-writer.c +++ b/src/plugins/influxdb-writer.c @@ -96,9 +96,17 @@ CURL *make_curl_write_post(const char *url, json_object *metricsJ) char **post_data; char write[URL_MAXIMUM_LENGTH]; struct series_t *serie = NULL; + json_object *metricsArrayJ = NULL; - lpd = json_object_is_type(metricsJ, json_type_array) ? - json_object_array_length(metricsJ) + 1 : 2; + if(json_object_is_type(metricsJ, json_type_array)) { + lpd = json_object_array_length(metricsJ); + metricsArrayJ = metricsJ; + } + else { + metricsArrayJ = json_object_new_array(); + json_object_array_add(metricsArrayJ, metricsJ); + lpd = 1; + } serie = malloc(sizeof(struct series_t)); post_data = malloc(lpd); @@ -106,21 +114,27 @@ CURL *make_curl_write_post(const char *url, json_object *metricsJ) for(i = 0; i < lpd; i++) { bzero(serie, sizeof(struct series_t)); - if(unpack_metric_from_api(json_object_array_get_idx(metricsJ, i), serie)) { - AFB_ERROR("ERROR unpacking metric. %s", json_object_to_json_string(metricsJ)); + if(unpack_metric_from_api(json_object_array_get_idx(metricsArrayJ, i), serie)) { + AFB_ERROR("ERROR unpacking metric. %s", json_object_to_json_string(metricsArrayJ)); break; } else { bzero(write, URL_MAXIMUM_LENGTH); - format_write_args(write, serie); - post_data[i] = i == lpd - 1 ? NULL : write; + if(! serie->name) { + post_data[i] = NULL; + } + else { + format_write_args(write, serie); + strcpy(post_data[i], write); + } } } + post_data[i] = NULL; /* Check that we just do not broke the for loop before trying preparing CURL request object */ curl = i == lpd ? - curl_wrap_prepare_post(url, NULL, 1, (const char * const*)post_data):NULL; + curl_wrap_prepare_post(url, NULL, 1, " ", (const char * const*)post_data) : NULL; free(serie); free(post_data); |