summaryrefslogtreecommitdiffstats
path: root/src/plugins/influxdb-writer.c
diff options
context:
space:
mode:
authorSebastien Douheret <sebastien.douheret@iot.bzh>2019-07-05 09:32:26 +0200
committerSebastien Douheret <sebastien.douheret@iot.bzh>2019-08-01 17:45:11 +0200
commit132d8131ae287bb0ab148aef545b9337e9703db6 (patch)
tree557c41a338b210eae081843e7d9d1dd9e7ee54ec /src/plugins/influxdb-writer.c
parent4bcea9804e13134d2e0f45ae158c17825cff1b76 (diff)
Add comments and re-ident code
Change-Id: Ibb4636bd968d6e4fd564ab40d5cf59e5a6ec6210 Signed-off-by: Sebastien Douheret <sebastien.douheret@iot.bzh>
Diffstat (limited to 'src/plugins/influxdb-writer.c')
-rw-r--r--src/plugins/influxdb-writer.c79
1 files changed, 40 insertions, 39 deletions
diff --git a/src/plugins/influxdb-writer.c b/src/plugins/influxdb-writer.c
index c4929f8..a560cb7 100644
--- a/src/plugins/influxdb-writer.c
+++ b/src/plugins/influxdb-writer.c
@@ -16,16 +16,16 @@
*/
#define _GNU_SOURCE
-#include <string.h>
#include <stdio.h>
+#include <string.h>
#include "influxdb.h"
-void influxdb_write_curl_cb(void *closure, int status, CURL *curl, const char *result, size_t size)
+void influxdb_write_curl_cb(void* closure, int status, CURL* curl, const char* result, size_t size)
{
afb_req_t request = (afb_req_t)closure;
long rep_code = curl_wrap_response_code_get(curl);
- switch(rep_code) {
+ switch (rep_code) {
case 204:
AFB_REQ_DEBUG(request, "Request correctly written");
afb_req_success(request, NULL, "Request has been successfully written");
@@ -38,14 +38,14 @@ void influxdb_write_curl_cb(void *closure, int status, CURL *curl, const char *r
break;
case 404:
afb_req_fail(request, "Not found", result);
- AFB_REQ_NOTICE(request, "Attempt to create the DB '"DEFAULT_DB"'");
+ AFB_REQ_NOTICE(request, "Attempt to create the DB '" DEFAULT_DB "'");
create_database(request);
break;
case 500:
afb_req_fail_f(request, "Timeout", "Overloaded server: %s", result);
break;
default:
- afb_req_fail(request, "Failure", "Unexpected behavior.");
+ afb_req_fail_f(request, "Failure", "Unexpected behavior (code %ld).", rep_code);
break;
}
}
@@ -57,8 +57,8 @@ void influxdb_write_curl_cb(void *closure, int status, CURL *curl, const char *r
static void serialize_list_to_query(char *query, struct list *node, const char *list_sep, const char *item_sep) {
bool first = true;
- while(node != NULL) {
- if(first) {
+ while (node != NULL) {
+ if (first) {
concatenate(query, node->key, list_sep);
first = false;
} else {
@@ -73,10 +73,9 @@ static void serialize_list_to_query(char *query, struct list *node, const char *
}
}
-
-static size_t format_write_args(char *query, struct series_t *serie)
+static size_t format_write_args(char* query, struct series_t* serie)
{
- char *ts;
+ char* ts;
strncat(query, serie->name, strlen(serie->name));
@@ -91,20 +90,20 @@ static size_t format_write_args(char *query, struct series_t *serie)
return -1;
}
-CURL *make_curl_write_post(afb_api_t apiHandle, const char *url, json_object *metricsJ)
+CURL* make_curl_write_post(afb_api_t apiHandle, const char* url, json_object* metricsJ)
{
- CURL *curl = NULL;
+ CURL* curl = NULL;
size_t lpd = 0, len_write = 0, i = 0;
- char **post_data;
- char write[URL_MAXIMUM_LENGTH] = "";
- struct series_t *serie = NULL;
- json_object *metricsArrayJ = NULL;
+ char** post_data;
+ char write[URL_MAXIMUM_LENGTH] = "";
+ struct series_t* serie = NULL;
+ json_object* metricsArrayJ = NULL;
+
- if(json_object_is_type(metricsJ, json_type_array)) {
+ if (json_object_is_type(metricsJ, json_type_array)) {
lpd = json_object_array_length(metricsJ);
metricsArrayJ = metricsJ;
- }
- else {
+ } else {
metricsArrayJ = json_object_new_array();
json_object_array_add(metricsArrayJ, metricsJ);
lpd = 1;
@@ -113,20 +112,18 @@ CURL *make_curl_write_post(afb_api_t apiHandle, const char *url, json_object *me
serie = malloc(sizeof(struct series_t));
post_data = calloc(lpd + 1, sizeof(void*));
- for(i = 0; i < lpd; i++) {
+ for (i = 0; i < lpd; i++) {
memset(serie, 0, sizeof(struct series_t));
- if(unpack_metric_from_api(json_object_array_get_idx(metricsArrayJ, i), serie)) {
+ if (unpack_metric_from_api(json_object_array_get_idx(metricsArrayJ, i), serie)) {
AFB_API_ERROR(apiHandle, "ERROR unpacking metric. %s", json_object_to_json_string(metricsArrayJ));
break;
- }
- else {
- if(! serie->name) {
+ } else {
+ if (!serie->name) {
post_data[i] = NULL;
- }
- else {
+ } else {
len_write = format_write_args(write, serie);
- if(len_write > 0) {
+ if (len_write > 0) {
post_data[i] = malloc(len_write + 1);
strcpy(post_data[i], write);
memset(write, 0, len_write);
@@ -135,19 +132,23 @@ CURL *make_curl_write_post(afb_api_t apiHandle, const char *url, json_object *me
}
}
+ AFB_API_DEBUG(apiHandle, "curl POST '%s' '%s' ", url, *post_data);
+
/* Check that we just do not broke the for loop before trying preparing CURL
request object */
- curl = i == lpd ?
- curl_wrap_prepare_post_unescaped(url, NULL, "\n", (const char * const*)post_data) : NULL;
+ curl = i == lpd ? curl_wrap_prepare_post_unescaped(url, NULL, "\n", (const char* const*)post_data) : NULL;
free(serie);
- for(i = 0; i < lpd; i++)
- if (post_data[i]) free(post_data[i]);
+ for (i = 0; i < lpd; i++) {
+ if (post_data[i]) {
+ free(post_data[i]);
+ }
+ }
free(post_data);
return curl;
}
-CURL *influxdb_write(afb_api_t apiHandle, const char* host, const char *port, json_object *metricJ)
+CURL* influxdb_write(afb_api_t apiHandle, const char* host, const char* port, json_object* metricJ)
{
char url[URL_MAXIMUM_LENGTH]; /* Safe limit for most popular web browser */
make_url(url, sizeof(url), host, port, "write");
@@ -157,25 +158,25 @@ CURL *influxdb_write(afb_api_t apiHandle, const char* host, const char *port, js
CTLP_CAPI(write_to_influxdb, source, argsJ, eventJ)
{
afb_req_t request = source->request;
- const char *port = NULL;
- const char *host = NULL;
- CURL *curl_request;
+ const char* port = NULL;
+ const char* host = NULL;
+ CURL* curl_request;
int rc = -1;
json_object *req_args = afb_req_json(request),
*portJ = NULL,
*metric = NULL;
- if(wrap_json_unpack(req_args, "{s?s,s?o,so!}",
+ if (wrap_json_unpack(req_args, "{s?s,s?o,so!}",
"host", &host,
"port", &portJ,
- "metric", &metric) || ! metric) {
+ "metric", &metric)
+ || !metric) {
afb_req_fail(request, "Failed", "Error processing arguments. Miss metric\
JSON object or malformed");
rc = -1;
} else {
- port = json_object_is_type(portJ, json_type_null) ?
- NULL : json_object_to_json_string(portJ);
+ port = json_object_is_type(portJ, json_type_null) ? NULL : json_object_to_json_string(portJ);
curl_request = influxdb_write(source->api, host, port, metric);
curl_wrap_do(curl_request, influxdb_write_curl_cb, request);
rc = 0;