| 1 | // SPDX-License-Identifier: GPL-3.0-or-later |
| 2 | |
| 3 | #include "api_v1_calls.h" |
| 4 | |
| 5 | int api_v1_context(RRDHOST *host, struct web_client *w, char *url) { |
| 6 | char *context = NULL; |
| 7 | RRDCONTEXT_TO_JSON_OPTIONS options = RRDCONTEXT_OPTION_NONE; |
| 8 | time_t after = 0, before = 0; |
| 9 | const char *chart_label_key = NULL, *chart_labels_filter = NULL; |
| 10 | BUFFER *dimensions = NULL; |
| 11 | |
| 12 | buffer_flush(w->response.data); |
| 13 | |
| 14 | while(url) { |
| 15 | char *value = strsep_skip_consecutive_separators(&url, "&"); |
| 16 | if(!value || !*value) continue; |
| 17 | |
| 18 | char *name = strsep_skip_consecutive_separators(&value, "="); |
| 19 | if(!name || !*name) continue; |
| 20 | if(!value || !*value) continue; |
| 21 | |
| 22 | // name and value are now the parameters |
| 23 | // they are not null and not empty |
| 24 | |
| 25 | if(!strcmp(name, "context") || !strcmp(name, "ctx")) context = value; |
| 26 | else if(!strcmp(name, "after")) after = str2l(value); |
| 27 | else if(!strcmp(name, "before")) before = str2l(value); |
| 28 | else if(!strcmp(name, "options")) options = rrdcontext_to_json_parse_options(value); |
| 29 | else if(!strcmp(name, "chart_label_key")) chart_label_key = value; |
| 30 | else if(!strcmp(name, "chart_labels_filter")) chart_labels_filter = value; |
| 31 | else if(!strcmp(name, "dimension") || !strcmp(name, "dim") || !strcmp(name, "dimensions") || !strcmp(name, "dims")) { |
| 32 | if(!dimensions) dimensions = buffer_create(100, &netdata_buffers_statistics.buffers_api); |
| 33 | buffer_strcat(dimensions, "|"); |
| 34 | buffer_strcat(dimensions, value); |
| 35 | } |
| 36 | } |
| 37 | |
| 38 | if(!context || !*context) { |
| 39 | buffer_sprintf(w->response.data, "No context is given at the request."); |
| 40 | return HTTP_RESP_BAD_REQUEST; |
| 41 | } |
| 42 | |
| 43 | SIMPLE_PATTERN *chart_label_key_pattern = NULL; |
| 44 | SIMPLE_PATTERN *chart_labels_filter_pattern = NULL; |
| 45 | SIMPLE_PATTERN *chart_dimensions_pattern = NULL; |
| 46 | |
| 47 | if(chart_label_key) |
| 48 | chart_label_key_pattern = simple_pattern_create(chart_label_key, ",|\t\r\n\f\v", SIMPLE_PATTERN_EXACT, true); |
| 49 | |
| 50 | if(chart_labels_filter) |
| 51 | chart_labels_filter_pattern = simple_pattern_create(chart_labels_filter, ",|\t\r\n\f\v", SIMPLE_PATTERN_EXACT, |
| 52 | true); |
| 53 | |
| 54 | if(dimensions) { |
| 55 | chart_dimensions_pattern = simple_pattern_create(buffer_tostring(dimensions), ",|\t\r\n\f\v", |
| 56 | SIMPLE_PATTERN_EXACT, true); |
| 57 | buffer_free(dimensions); |
| 58 | } |
| 59 | |
| 60 | w->response.data->content_type = CT_APPLICATION_JSON; |
| 61 | int ret = rrdcontext_to_json(host, w->response.data, after, before, options, context, chart_label_key_pattern, chart_labels_filter_pattern, chart_dimensions_pattern); |
| 62 | |
| 63 | simple_pattern_free(chart_label_key_pattern); |
| 64 | simple_pattern_free(chart_labels_filter_pattern); |
| 65 | simple_pattern_free(chart_dimensions_pattern); |
| 66 | |
| 67 | return ret; |
| 68 | } |