Skip to content

Commit

Permalink
refactor: rename /options to /metric-payload-options
Browse files Browse the repository at this point in the history
  • Loading branch information
simPod committed Dec 26, 2022
1 parent 14242a2 commit b402d5d
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 17 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ The JSON Datasource executes requests against arbitrary backends and parses JSON
- [Setup](#setup)
- [API](#api)
- [/metrics](#metrics)
- [/options](#options)
- [/metric-payload-options](#metric-payload-options)
- [/query](#query)
- [/variable](#variable)
- [/tag-keys](#tag-keys)
Expand Down Expand Up @@ -44,7 +44,7 @@ To work with this datasource the backend needs to implement 3 endpoints:

- `GET /` with 200 status code response. Used for "Test connection" on the datasource config page.
- `POST /metrics` to return available metrics.
- `POST /options` to return a list of metric options.
- `POST /metric-payload-options` to return a list of metric payload options.
- `POST /query` to return panel data or annotations.

Those 3 endpoints are optional:
Expand Down Expand Up @@ -116,9 +116,9 @@ Example response:
The display is as follows:
![Metrics in builder mode](./docs/images/builder-metrics.png)

### /options
### /metric-payload-options

`POST /options`
`POST /metric-payload-options`

When the payload `type` is `select` or `multi-select` and the payload `options` configuration is empty, expanding the drop-down menu will trigger this API. The request body will carry the current metric and payload.

Expand Down
18 changes: 9 additions & 9 deletions examples/server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ func newHandler() http.Handler {
mux := http.NewServeMux()
mux.HandleFunc("/api/grafana/json", hello)
mux.HandleFunc("/api/grafana/json/metrics", getMetrics)
mux.HandleFunc("/api/grafana/json/options", getOptions)
mux.HandleFunc("/api/grafana/json/metric-payload-options", getMetricPayloadOptions)
return mux
}

Expand All @@ -30,8 +30,8 @@ var defaultMetrics = `
"type": "select",
"defaultValue": "acs_mongodb",
"placeholder": "Please select namespace",
"reloadMetric": true,
"options": [{
"reloadMetric": true,
"options": [{
"label": "acs_mongodb",
"value": "acs_mongodb"
},{
Expand Down Expand Up @@ -66,8 +66,8 @@ var rdsMetrics = `
"type": "select",
"defaultValue": "acs_mongodb",
"placeholder": "Please select namespace",
"reloadMetric": true,
"options": [{
"reloadMetric": true,
"options": [{
"label": "acs_mongodb",
"value": "acs_mongodb"
},{
Expand Down Expand Up @@ -122,7 +122,7 @@ type OptionsRequest struct {
Payload map[string]interface{} `json:"payload"`
}

func getOptions(writer http.ResponseWriter, request *http.Request) {
func getMetricPayloadOptions(writer http.ResponseWriter, request *http.Request) {
var req OptionsRequest
err := json.NewDecoder(request.Body).Decode(&req)
if err != nil {
Expand All @@ -132,7 +132,7 @@ func getOptions(writer http.ResponseWriter, request *http.Request) {
writer.Header().Set("content-type", "application/json")
switch req.Name {
case "instanceId":
writer.Write([]byte(`[{
writer.Write([]byte(`[{
"label": "My Database 1",
"value": "sadbip2kasdmnlo"
},{
Expand All @@ -144,7 +144,7 @@ func getOptions(writer http.ResponseWriter, request *http.Request) {
}]`))

case "metric":
writer.Write([]byte(`[{
writer.Write([]byte(`[{
"label": "CPUUtilization",
"value": "CPUUtilization"
},{
Expand All @@ -155,7 +155,7 @@ func getOptions(writer http.ResponseWriter, request *http.Request) {
"value": "memory_freeutilization"
}]`))
case "namespace":
writer.Write([]byte(`[{
writer.Write([]byte(`[{
"label": "MongoDB",
"value": "acs_mongodb"
},{
Expand Down
2 changes: 1 addition & 1 deletion examples/server/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func Test_Options(t *testing.T) {
mux := newHandler()
server := httptest.NewServer(mux)
payload := `{"metric":"DescribeMetricList","payload":{"namespace":"acs_rds"},"name":"instanceId"}`
resp, err := http.Post(server.URL+"/api/grafana/json/options", "application/json", bytes.NewBuffer([]byte(payload)))
resp, err := http.Post(server.URL+"/api/grafana/json/metric-payload-options", "application/json", bytes.NewBuffer([]byte(payload)))
require.NoError(t, err)
require.Equal(t, resp.StatusCode, 200)
rawBody, err := ioutil.ReadAll(resp.Body)
Expand Down
4 changes: 2 additions & 2 deletions openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -154,15 +154,15 @@ paths:
type: select
- name: instanceId
type: multi-select
/options:
/metric-payload-options:
post:
summary: List the available payload options.
description: >-
When the payload `type` is `select` or `multi-select` and the payload
`options` configuration is empty, expanding the drop-down menu will
trigger this API. The request body will carry the current metric and
payload.
operationId: api.endpoints.list_options
operationId: api.endpoints.list_metric_payload_options
tags:
- Visualization
parameters: []
Expand Down
2 changes: 1 addition & 1 deletion src/DataSource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ export class DataSource extends DataSourceApi<GrafanaQuery, GenericOptions> {
): Promise<Array<SelectableValue<string | number>>> {
return lastValueFrom<Array<SelectableValue<string | number>>>(
this.doFetch({
url: `${this.url}/options`,
url: `${this.url}/metric-payload-options`,
data: {
metric,
payload: this.processPayload(payload, 'builder', undefined),
Expand Down

0 comments on commit b402d5d

Please sign in to comment.