| 1 | // SPDX-License-Identifier: GPL-3.0-or-later |
| 2 | |
| 3 | #include "api_v1_calls.h" |
| 4 | |
| 5 | int api_v1_single_chart_helper(RRDHOST *host, struct web_client *w, char *url, void callback(RRDSET *st, BUFFER *buf)) { |
| 6 | int ret = HTTP_RESP_BAD_REQUEST; |
| 7 | char *chart = NULL; |
| 8 | |
| 9 | buffer_flush(w->response.data); |
| 10 | |
| 11 | while(url) { |
| 12 | char *value = strsep_skip_consecutive_separators(&url, "&"); |
| 13 | if(!value || !*value) continue; |
| 14 | |
| 15 | char *name = strsep_skip_consecutive_separators(&value, "="); |
| 16 | if(!name || !*name) continue; |
| 17 | if(!value || !*value) continue; |
| 18 | |
| 19 | // name and value are now the parameters |
| 20 | // they are not null and not empty |
| 21 | |
| 22 | if(!strcmp(name, "chart")) chart = value; |
| 23 | //else { |
| 24 | /// buffer_sprintf(w->response.data, "Unknown parameter '%s' in request.", name); |
| 25 | // goto cleanup; |
| 26 | //} |
| 27 | } |
| 28 | |
| 29 | if(!chart || !*chart) { |
| 30 | buffer_sprintf(w->response.data, "No chart id is given at the request."); |
| 31 | goto cleanup; |
| 32 | } |
| 33 | |
| 34 | RRDSET *st = rrdset_find(host, chart, false); |
| 35 | if(!st) st = rrdset_find_byname(host, chart); |
| 36 | if(!st) { |
| 37 | buffer_strcat(w->response.data, "Chart is not found: "); |
| 38 | buffer_strcat_htmlescape(w->response.data, chart); |
| 39 | ret = HTTP_RESP_NOT_FOUND; |
| 40 | goto cleanup; |
| 41 | } |
| 42 | |
| 43 | w->response.data->content_type = CT_APPLICATION_JSON; |
| 44 | st->last_accessed_time_s = now_realtime_sec(); |
| 45 | callback(st, w->response.data); |
| 46 | return HTTP_RESP_OK; |
| 47 | |
| 48 | cleanup: |
| 49 | return ret; |
| 50 | } |
| 51 | |
| 52 | int api_v1_charts(RRDHOST *host, struct web_client *w, char *url) { |
| 53 | (void)url; |
| 54 | |
| 55 | buffer_flush(w->response.data); |
| 56 | w->response.data->content_type = CT_APPLICATION_JSON; |
| 57 | charts2json(host, w->response.data); |
| 58 | return HTTP_RESP_OK; |
| 59 | } |
| 60 | |
| 61 | int api_v1_chart(RRDHOST *host, struct web_client *w, char *url) { |
| 62 | return api_v1_single_chart_helper(host, w, url, rrd_stats_api_v1_chart); |
| 63 | } |
| 64 |