From 15d28af12df1f3b586f3259852a6d4d7c79da09e Mon Sep 17 00:00:00 2001 From: Lou Knauer Date: Wed, 2 Feb 2022 11:26:05 +0100 Subject: [PATCH] Change /api/query return type --- api.go | 17 +++++++++++++---- openapi.yaml | 45 +++++++++++++++++++++++++-------------------- 2 files changed, 38 insertions(+), 24 deletions(-) diff --git a/api.go b/api.go index 38ff278..ba58837 100644 --- a/api.go +++ b/api.go @@ -154,6 +154,11 @@ type ApiQueryRequest struct { ForAllNodes []string `json:"for-all-nodes"` } +type ApiQueryResponse struct { + Queries []ApiQuery `json:"queries,omitempty"` + Results [][]ApiMetricData `json:"results"` +} + type ApiQuery struct { Metric string `json:"metric"` Hostname string `json:"host"` @@ -172,19 +177,23 @@ func handleQuery(rw http.ResponseWriter, r *http.Request) { return } + response := ApiQueryResponse{ + Results: make([][]ApiMetricData, 0, len(req.Queries)), + } if req.ForAllNodes != nil { nodes := memoryStore.ListChildren([]string{req.Cluster}) for _, node := range nodes { for _, metric := range req.ForAllNodes { - req.Queries = append(req.Queries, ApiQuery{ + q := ApiQuery{ Metric: metric, Hostname: node, - }) + } + req.Queries = append(req.Queries, q) + response.Queries = append(response.Queries, q) } } } - response := make([][]ApiMetricData, 0, len(req.Queries)) for _, query := range req.Queries { sels := make([]Selector, 0, 1) if query.Aggregate || query.Type == nil { @@ -252,7 +261,7 @@ func handleQuery(rw http.ResponseWriter, r *http.Request) { } res = append(res, data) } - response = append(response, res) + response.Results = append(response.Results, res) } rw.Header().Set("Content-Type", "application/json") diff --git a/openapi.yaml b/openapi.yaml index f33e01d..e3d31f6 100644 --- a/openapi.yaml +++ b/openapi.yaml @@ -60,26 +60,31 @@ paths: content: 'application/json': schema: - description: 'Array where each element is a response to the query at that same index in the request' - type: array - items: - description: 'If `aggreg` is true, only ever has one element.' - type: array - items: - type: object - properties: - error: - description: 'If not null or undefined, an error happend processing that query' - type: string - nullable: true - data: - type: array - items: - type: number - nullable: true - avg: { type: number } - min: { type: number } - max: { type: number } + type: object + properties: + queries: + description: 'Only if for-all-nodes was used, this property exists.' + results: + type: array + description: 'Array where each element is a response to the query at that same index in the request' + items: + description: 'If `aggreg` is true, only ever has one element.' + type: array + items: + type: object + properties: + error: + description: 'If not null or undefined, an error happend processing that query' + type: string + nullable: true + data: + type: array + items: + type: number + nullable: true + avg: { type: number } + min: { type: number } + max: { type: number } 400: description: 'Bad Request' '/api/free':