diff options
author | Romain Forlot <romain.forlot@iot.bzh> | 2018-04-14 01:57:17 +0200 |
---|---|---|
committer | Sebastien Douheret <sebastien.douheret@iot.bzh> | 2018-07-10 23:41:14 +0200 |
commit | 5d50f0426699a06b5720e10f1feaef35c8b59f57 (patch) | |
tree | 34641293f0eef907c89ffa1cb6d6b80b863106fc /src/plugins/influxdb.c | |
parent | be6447ca2038c84d2e946e27b897815e95c48e34 (diff) |
Improve writer/reader processing
- Handle indefinite number and kind of tags and fields for a metric
- Include only once header files
- Cleaning and ordering code
Change-Id: I14a4f0e6e1626971bff73ce7d9ac067bda69cfc4
Signed-off-by: Romain Forlot <romain.forlot@iot.bzh>
Diffstat (limited to 'src/plugins/influxdb.c')
-rw-r--r-- | src/plugins/influxdb.c | 91 |
1 files changed, 54 insertions, 37 deletions
diff --git a/src/plugins/influxdb.c b/src/plugins/influxdb.c index 8a8a112..b8a733b 100644 --- a/src/plugins/influxdb.c +++ b/src/plugins/influxdb.c @@ -1,5 +1,5 @@ /* - * Copyright (C) 2015, 2016, 2017, 2018 "IoT.bzh" + * Copyright (C) 2018 "IoT.bzh" * Author "Romain Forlot" <romain.forlot@iot.bzh> * * Licensed under the Apache License, Version 2.0 (the "License"); @@ -21,8 +21,35 @@ #include <unistd.h> #include <string.h> +#include "influxdb.h" #include "tsdb.h" #include "wrap-json.h" +#include "../utils/list.h" + +void concatenate(char* dest, const char* source, const char *sep) +{ + strncat(dest, sep, strlen(sep)); + strncat(dest, source, strlen(source)); +} + +size_t make_url(char *url, size_t l_url, const char *host, const char *port, const char *endpoint) +{ + bzero(url, l_url); + + /* Handle default host and port */ + host = host ? host : DEFAULT_DBHOST; + port = port ? port : DEFAULT_DBPORT; + + strncat(url, host, strlen(host)); + strncat(url, ":", 1); + strncat(url, port, strlen(port)); + strncat(url, "/", 1); + strncat(url, endpoint, strlen(endpoint)); + strncat(url, "?db="DEFAULT_DB, strlen("?db="DEFAULT_DB)); + + return strlen(url); +} + int create_database() { @@ -40,56 +67,46 @@ int create_database() if(curl_wrap_response_code_get(request) != 200) { AFB_ERROR("Can't create database."); - ret = -1; + ret = ERROR; } curl_easy_cleanup(request); if(ret == 0) - AFB_NOTICE("Database 'agl-collector' created"); + AFB_NOTICE("Database '"DEFAULT_DB"' created"); return ret; } -int unpack_metric_from_binding(json_object *m, const char **name, const char **source, const char **unit, const char **identity, json_object **jv, uint64_t *timestamp) +void unpacking_from_api(void *s, json_object *valueJ, const char *key) { - if (wrap_json_unpack(m, "{ss,s?s,s?s,s?s,so,sI!}", - "name", name, - "source", source, - "unit", unit, - "identity", identity, - "value", jv, - "timestamp", timestamp)) - return -1; - else if (!json_object_is_type(*jv, json_type_boolean) && - !json_object_is_type(*jv, json_type_double) && - !json_object_is_type(*jv, json_type_int) && - !json_object_is_type(*jv, json_type_string)) - return -1; - - return 0; + size_t key_length = strlen(key); + struct series_t *serie = (struct series_t*)s; + + /* Treat the 2 static key that could have been specified */ + if(strcasecmp("name", key) == 0) + serie->name = json_object_get_string(valueJ); + else if(strcasecmp("timestamp", key) == 0) + serie->timestamp = get_ts(); + /* Treat all key looking for tag and field object. Those ones could be find + with the last 2 character. '_t' for tag and '_f' that are the keys that + could be indefinite. Cf influxdb documentation: + https://docs.influxdata.com/influxdb/v1.5/write_protocols/line_protocol_reference/ */ + else if(strncasecmp(&key[key_length-2], "_t", 2) == 0) + add_elt(&serie->series_columns.tags, key, valueJ); + else if(strncasecmp(&key[key_length-2], "_f", 2) == 0) + add_elt(&serie->series_columns.fields, key, valueJ); } -void concatenate(char* dest, const char* source, const char *sep) +int unpack_metric_from_api(json_object *m, struct series_t **serie) { - strncat(dest, sep, strlen(sep)); - strncat(dest, source, strlen(source)); -} + *serie = malloc(sizeof(struct series_t)); + bzero(*serie, sizeof(struct series_t)); -size_t make_url(char *url, size_t l_url, const char *host, const char *port, const char *endpoint) -{ - bzero(url, l_url); + wrap_json_object_for_all(m, unpacking_from_api, *serie); - /* Handle default host and port */ - host = host ? host : DEFAULT_DBHOST; - port = port ? port : DEFAULT_DBPORT; + if(!(*serie)->timestamp) + (*serie)->timestamp = get_ts(); - strncat(url, host, strlen(host)); - strncat(url, ":", 1); - strncat(url, port, strlen(port)); - strncat(url, "/", 1); - strncat(url, endpoint, strlen(endpoint)); - strncat(url, "?db="DEFAULT_DB, strlen("?db="DEFAULT_DB)); - - return strlen(url); + return 0; } |