From 92642269f19f585b04d439a3bbe4e60d33918974 Mon Sep 17 00:00:00 2001 From: Valentin Rakush <52716954+alpes214@users.noreply.github.com> Date: Tue, 20 Aug 2019 12:11:43 +0300 Subject: Add alarm variables to the response of chart and data (#6615) ##### Summary Implements feature #6054 Now requests like http://localhost:19999/api/v1/chart?chart=example.random http://localhost:19999/api/v1/data?chart=example.random&options=jsonwrap&options=showcustomvars - return chart variables in their responses. Chart variables include only those with options set to RRDVAR_OPTION_CUSTOM_CHART_VAR - for /api/v1/data requests chart variables are returned when parameter options=jsonwrap and options=showcustomvars ##### Component Name [/database](https://github.com/netdata/netdata/tree/master/database/) [/web/api/formatters](https://github.com/netdata/netdata/tree/master/web/api/formatters) --- web/api/formatters/json_wrapper.c | 13 ++++++++----- web/api/formatters/rrdset2json.c | 5 ++++- web/api/netdata-swagger.json | 41 +++++++++++++++++++++++++++++++-------- web/api/netdata-swagger.yaml | 29 ++++++++++++++++++++------- web/api/queries/rrdr.h | 1 + web/api/web_api_v1.c | 1 + 6 files changed, 69 insertions(+), 21 deletions(-) (limited to 'web') diff --git a/web/api/formatters/json_wrapper.c b/web/api/formatters/json_wrapper.c index 4911d2f0ea..5ef2caf101 100644 --- a/web/api/formatters/json_wrapper.c +++ b/web/api/formatters/json_wrapper.c @@ -182,11 +182,14 @@ void rrdr_json_wrapper_begin(RRDR *r, BUFFER *wb, uint32_t format, RRDR_OPTIONS rrdr_buffer_print_format(wb, format); - buffer_sprintf(wb, "%s,\n" - " %sresult%s: " - , sq - , kq, kq - ); + if((options & RRDR_OPTION_CUSTOM_VARS) && (options & RRDR_OPTION_JSON_WRAP)) { + buffer_sprintf(wb, "%s,\n %schart_variables%s: ", sq, kq, kq); + health_api_v1_chart_custom_variables2json(r->st, wb); + } + else + buffer_sprintf(wb, "%s", sq); + + buffer_sprintf(wb, ",\n %sresult%s: ", kq, kq); if(string_value) buffer_strcat(wb, sq); //info("JSONWRAPPER(): %s: END", r->st->id); diff --git a/web/api/formatters/rrdset2json.c b/web/api/formatters/rrdset2json.c index 2bbd2a70f3..4d91399270 100644 --- a/web/api/formatters/rrdset2json.c +++ b/web/api/formatters/rrdset2json.c @@ -73,7 +73,10 @@ void rrdset2json(RRDSET *st, BUFFER *wb, size_t *dimensions_count, size_t *memor if(dimensions_count) *dimensions_count += dimensions; if(memory_used) *memory_used += memory; - buffer_strcat(wb, "\n\t\t\t},\n\t\t\t\"green\": "); + buffer_sprintf(wb, "\n\t\t\t},\n\t\t\t\"chart_variables\": "); + health_api_v1_chart_custom_variables2json(st, wb); + + buffer_strcat(wb, ",\n\t\t\t\"green\": "); buffer_rrd_value(wb, st->green); buffer_strcat(wb, ",\n\t\t\t\"red\": "); buffer_rrd_value(wb, st->red); diff --git a/web/api/netdata-swagger.json b/web/api/netdata-swagger.json index 63bc5638de..44e862d174 100644 --- a/web/api/netdata-swagger.json +++ b/web/api/netdata-swagger.json @@ -243,7 +243,8 @@ "percentage", "unaligned", "match-ids", - "match-names" + "match-names", + "showcustomvars" ], "collectionFormat": "pipes" }, @@ -975,6 +976,14 @@ } } }, + "chart_variables": { + "type": "object", + "properties": { + "key": { + "$ref": "#/definitions/chart_variables" + } + } + }, "green": { "type": "number", "description": "Chart health green threshold" @@ -1011,13 +1020,8 @@ "chart_variables": { "type": "object", "properties": { - "varname1": { - "type": "number", - "format": "float" - }, - "varname2": { - "type": "number", - "format": "float" + "key": { + "$ref": "#/definitions/chart_variables" } } }, @@ -1049,6 +1053,19 @@ } } }, + "chart_variables": { + "type": "object", + "properties": { + "varname1": { + "type": "number", + "format": "float" + }, + "varname2": { + "type": "number", + "format": "float" + } + } + }, "dimension": { "type": "object", "properties": { @@ -1145,6 +1162,14 @@ "type": "string", "description": "The format of the result returned." }, + "chart_variables": { + "type": "object", + "properties": { + "key": { + "$ref": "#/definitions/chart_variables" + } + } + }, "result": { "description": "The result requested, in the format requested." } diff --git a/web/api/netdata-swagger.yaml b/web/api/netdata-swagger.yaml index 3386e01a7e..0052e646c2 100644 --- a/web/api/netdata-swagger.yaml +++ b/web/api/netdata-swagger.yaml @@ -163,7 +163,7 @@ paths: type: array items: type: string - enum: [ 'nonzero', 'flip', 'jsonwrap', 'min2max', 'seconds', 'milliseconds', 'abs', 'absolute', 'absolute-sum', 'null2zero', 'objectrows', 'google_json', 'percentage', 'unaligned', 'match-ids', 'match-names' ] + enum: [ 'nonzero', 'flip', 'jsonwrap', 'min2max', 'seconds', 'milliseconds', 'abs', 'absolute', 'absolute-sum', 'null2zero', 'objectrows', 'google_json', 'percentage', 'unaligned', 'match-ids', 'match-names', 'showcustomvars' ] collectionFormat: pipes default: [seconds, jsonwrap] allowEmptyValue: false @@ -654,6 +654,11 @@ definitions: properties: key: $ref: '#/definitions/dimension' + chart_variables: + type: object + properties: + key: + $ref: '#/definitions/chart_variables' green: type: number description: 'Chart health green threshold' @@ -681,12 +686,8 @@ definitions: chart_variables: type: object properties: - varname1: - type: number - format: float - varname2: - type: number - format: float + key: + $ref: '#/definitions/chart_variables' family_variables: type: object properties: @@ -705,6 +706,15 @@ definitions: varname2: type: number format: float + chart_variables: + type: object + properties: + varname1: + type: number + format: float + varname2: + type: number + format: float dimension: type: object properties: @@ -776,6 +786,11 @@ definitions: format: type: string description: 'The format of the result returned.' + chart_variables: + type: object + properties: + key: + $ref: '#/definitions/chart_variables' result: description: 'The result requested, in the format requested.' alarms: diff --git a/web/api/queries/rrdr.h b/web/api/queries/rrdr.h index 4f6350389b..6473ae7457 100644 --- a/web/api/queries/rrdr.h +++ b/web/api/queries/rrdr.h @@ -22,6 +22,7 @@ typedef enum rrdr_options { RRDR_OPTION_DISPLAY_ABS = 0x00002000, // for badges, display the absolute value, but calculate colors with sign RRDR_OPTION_MATCH_IDS = 0x00004000, // when filtering dimensions, match only IDs RRDR_OPTION_MATCH_NAMES = 0x00008000, // when filtering dimensions, match only names + RRDR_OPTION_CUSTOM_VARS = 0x00010000, // when wraping response in a JSON, return custom variables in response } RRDR_OPTIONS; typedef enum rrdr_value_flag { diff --git a/web/api/web_api_v1.c b/web/api/web_api_v1.c index 2273224bb1..2d12049632 100644 --- a/web/api/web_api_v1.c +++ b/web/api/web_api_v1.c @@ -32,6 +32,7 @@ static struct { , {"match-ids" , 0 , RRDR_OPTION_MATCH_IDS} , {"match_names" , 0 , RRDR_OPTION_MATCH_NAMES} , {"match-names" , 0 , RRDR_OPTION_MATCH_NAMES} + , {"showcustomvars" , 0 , RRDR_OPTION_CUSTOM_VARS} , { NULL, 0, 0} }; -- cgit v1.2.3