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"`
}
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")

View File

@ -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':