Change /api/query return type

This commit is contained in:
Lou Knauer 2022-02-02 11:26:05 +01:00
parent 407e650745
commit 15d28af12d
2 changed files with 38 additions and 24 deletions

17
api.go
View File

@ -154,6 +154,11 @@ type ApiQueryRequest struct {
ForAllNodes []string `json:"for-all-nodes"` ForAllNodes []string `json:"for-all-nodes"`
} }
type ApiQueryResponse struct {
Queries []ApiQuery `json:"queries,omitempty"`
Results [][]ApiMetricData `json:"results"`
}
type ApiQuery struct { type ApiQuery struct {
Metric string `json:"metric"` Metric string `json:"metric"`
Hostname string `json:"host"` Hostname string `json:"host"`
@ -172,19 +177,23 @@ func handleQuery(rw http.ResponseWriter, r *http.Request) {
return return
} }
response := ApiQueryResponse{
Results: make([][]ApiMetricData, 0, len(req.Queries)),
}
if req.ForAllNodes != nil { if req.ForAllNodes != nil {
nodes := memoryStore.ListChildren([]string{req.Cluster}) nodes := memoryStore.ListChildren([]string{req.Cluster})
for _, node := range nodes { for _, node := range nodes {
for _, metric := range req.ForAllNodes { for _, metric := range req.ForAllNodes {
req.Queries = append(req.Queries, ApiQuery{ q := ApiQuery{
Metric: metric, Metric: metric,
Hostname: node, 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 { for _, query := range req.Queries {
sels := make([]Selector, 0, 1) sels := make([]Selector, 0, 1)
if query.Aggregate || query.Type == nil { if query.Aggregate || query.Type == nil {
@ -252,7 +261,7 @@ func handleQuery(rw http.ResponseWriter, r *http.Request) {
} }
res = append(res, data) res = append(res, data)
} }
response = append(response, res) response.Results = append(response.Results, res)
} }
rw.Header().Set("Content-Type", "application/json") rw.Header().Set("Content-Type", "application/json")

View File

@ -60,8 +60,13 @@ paths:
content: content:
'application/json': 'application/json':
schema: schema:
description: 'Array where each element is a response to the query at that same index in the request' type: object
properties:
queries:
description: 'Only if for-all-nodes was used, this property exists.'
results:
type: array type: array
description: 'Array where each element is a response to the query at that same index in the request'
items: items:
description: 'If `aggreg` is true, only ever has one element.' description: 'If `aggreg` is true, only ever has one element.'
type: array type: array