Skip to content

Commit 6235015

Browse files
authored
Pass target type to /search request (#55)
1 parent 195e308 commit 6235015

File tree

5 files changed

+43
-59
lines changed

5 files changed

+43
-59
lines changed

README.md

Lines changed: 34 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,23 @@
22

33
The JSON Datasource executes JSON requests against arbitrary backends.
44
_JSON Datasource is built on top of the [Simple JSON Datasource](https://github.com/grafana/simple-json-datasource)._ It has refactored code, additional features and active development.
5-
6-
## Contents
7-
- [Installation](#installation)
8-
- [Setup](#setup)
9-
- [API](#api)
10-
- [/search](#search)
11-
- [/query](#tquery)
12-
- [/annotations](#annotations)
13-
- [/tag-keys](#tag-keys)
14-
- [/tag-values](#tag-values)
5+
6+
## Contents
7+
8+
- [Installation](#installation)
9+
- [Setup](#setup)
10+
- [API](#api)
11+
- [/search](#search)
12+
- [/query](#tquery)
13+
- [/annotations](#annotations)
14+
- [/tag-keys](#tag-keys)
15+
- [/tag-values](#tag-values)
1516
- [Development Setup](#development-setup)
1617

1718
## Installation
1819

1920
To install this plugin using the `grafana-cli` tool:
21+
2022
```sh
2123
grafana-cli plugins install simpod-json-datasource
2224
```
@@ -35,38 +37,42 @@ When adding datasource add your API endpoint to the `URL` field. That's where da
3537

3638
To work with this datasource the backend needs to implement 4 urls:
3739

38-
* `/` should return 200 ok. Used for "Test connection" on the datasource config page.
39-
* `/search` used by the find metric options on the query tab in panels.
40-
* `/query` should return metrics based on input.
41-
* `/annotations` should return annotations.
40+
- `/` should return 200 ok. Used for "Test connection" on the datasource config page.
41+
- `/search` should return available metrics when invoked by the find metric options on the query tab in panels.
42+
- `/query` should return metrics based on input.
43+
- `/annotations` should return annotations.
4244

4345
Those two urls are optional:
4446

45-
* `/tag-keys` should return tag keys for ad hoc filters.
46-
* `/tag-values` should return tag values for ad hoc filters.
47-
47+
- `/tag-keys` should return tag keys for ad hoc filters.
48+
- `/tag-values` should return tag values for ad hoc filters.
49+
4850
### /search
4951

5052
Example request
53+
5154
``` json
52-
{ "target": "upper_50" }
55+
{ "type": "timeseries", "target": "upper_50" }
5356
```
5457

5558
The search api can either return an array or map.
5659

5760
Example array response
61+
5862
``` json
5963
["upper_25","upper_50","upper_75","upper_90","upper_95"]
6064
```
6165

6266
Example map response
67+
6368
``` json
64-
[ { "text" :"upper_25", "value": 1}, { "text" :"upper_75", "value": 2} ]
69+
[ { "text": "upper_25", "value": 1}, { "text": "upper_75", "value": 2} ]
6570
```
6671

6772
### /query
6873

6974
Example `timeseries` request
75+
7076
``` json
7177
{
7278
"panelId": 1,
@@ -97,7 +103,6 @@ Example `timeseries` request
97103
}
98104
```
99105

100-
101106
Example `timeseries` response
102107

103108
``` javascript
@@ -135,7 +140,8 @@ Example `timeseries` response
135140

136141
_The relation between `target` in request and response is 1:n. You can return multiple targets in response for one requested `target`._
137142

138-
If the metric selected is `"type": "table"`, an example `table` response:
143+
Example `table` response to be returned if the metric selected is `"type": "table"`:
144+
139145
``` json
140146
{
141147
"columns":[
@@ -162,7 +168,7 @@ For example when `{ "additional": "optional json" }` is entered into Additional
162168

163169
```json
164170
{ "target": "upper_50", "refId": "A", "type": "timeseries", "data": { "additional": "optional json" } }
165-
```
171+
```
166172

167173
You can also enter variables:
168174

@@ -172,6 +178,7 @@ You can also enter variables:
172178

173179
The annotation request from the Simple JSON Datasource is a POST request to
174180
the `/annotations` endpoint in your datasource. The JSON request body looks like this:
181+
175182
``` json
176183
{
177184
"range": {
@@ -222,11 +229,13 @@ Access-Control-Allow-Origin:*
222229
### /tag-keys
223230

224231
Example request
232+
225233
``` json
226234
{ }
227235
```
228236

229237
The tag keys api returns:
238+
230239
``` json
231240
[
232241
{"type":"string","text":"City"},
@@ -237,11 +246,13 @@ The tag keys api returns:
237246
### /tag-values
238247

239248
Example request
249+
240250
``` json
241251
{"key": "City"}
242252
```
243253

244254
The tag values api returns:
255+
245256
``` json
246257
[
247258
{"text": "Eins!"},
@@ -254,7 +265,7 @@ The tag values api returns:
254265

255266
This plugin requires node 6.10.0. Use of [Yarn](https://yarnpkg.com/lang/en/docs/install/) is encouraged to build.
256267

257-
```
268+
```sh
258269
yarn install
259270
yarn run build
260271
```

spec/datasource.jest.ts

Lines changed: 4 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -48,34 +48,6 @@ describe('GenericDatasource', () => {
4848
});
4949
});
5050

51-
it('should return the metric results when a target is null', (done) => {
52-
ctx.backendSrv.datasourceRequest = function (request) {
53-
return ctx.$q.when({
54-
_request: request,
55-
data: [
56-
'metric_0',
57-
'metric_1',
58-
'metric_2',
59-
],
60-
});
61-
};
62-
63-
ctx.templateSrv.replace = function (data) {
64-
return data;
65-
};
66-
67-
ctx.ds.metricFindQuery({ target: null }).then((result) => {
68-
expect(result).toHaveLength(3);
69-
expect(result[0].text).toBe('metric_0');
70-
expect(result[0].value).toBe('metric_0');
71-
expect(result[1].text).toBe('metric_1');
72-
expect(result[1].value).toBe('metric_1');
73-
expect(result[2].text).toBe('metric_2');
74-
expect(result[2].value).toBe('metric_2');
75-
done();
76-
});
77-
});
78-
7951
it('should return the metric target results when a target is set', (done) => {
8052
ctx.backendSrv.datasourceRequest = function (request) {
8153
const target = request.data.target;
@@ -91,7 +63,7 @@ describe('GenericDatasource', () => {
9163
return data;
9264
};
9365

94-
ctx.ds.metricFindQuery('search').then((result) => {
66+
ctx.ds.findMetricsQuery('search', 'timeseries').then((result) => {
9567
expect(result).toHaveLength(3);
9668
expect(result[0].text).toBe('search_0');
9769
expect(result[0].value).toBe('search_0');
@@ -119,7 +91,7 @@ describe('GenericDatasource', () => {
11991
return data;
12092
};
12193

122-
ctx.ds.metricFindQuery('').then((result) => {
94+
ctx.ds.findMetricsQuery('').then((result) => {
12395
expect(result).toHaveLength(3);
12496
expect(result[0].text).toBe('metric_0');
12597
expect(result[0].value).toBe('metric_0');
@@ -147,7 +119,7 @@ describe('GenericDatasource', () => {
147119
return data;
148120
};
149121

150-
ctx.ds.metricFindQuery().then((result) => {
122+
ctx.ds.findMetricsQuery().then((result) => {
151123
expect(result).toHaveLength(3);
152124
expect(result[0].text).toBe('metric_0');
153125
expect(result[0].value).toBe('metric_0');
@@ -174,7 +146,7 @@ describe('GenericDatasource', () => {
174146
return data;
175147
};
176148

177-
ctx.ds.metricFindQuery('search').then((result) => {
149+
ctx.ds.findMetricsQuery('search', 'timeseries').then((result) => {
178150
expect(result).toHaveLength(3);
179151
expect(result[0].text).toBe('search_0');
180152
expect(result[0].value).toBe('search_0');

src/datasource.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,8 +92,9 @@ export class GenericDatasource {
9292
});
9393
}
9494

95-
metricFindQuery(query) {
95+
findMetricsQuery(query: string, type: string) {
9696
const interpolated = {
97+
type,
9798
target: this.templateSrv.replace(query, null, 'regex'),
9899
};
99100

src/partials/query.editor.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
<gf-form-dropdown model="ctrl.target.target"
2424
allow-custom="true"
2525
lookup-text="true"
26-
get-options="ctrl.getOptions($query)"
26+
get-options="ctrl.findMetrics($query)"
2727
on-change="ctrl.onChangeInternal()">
2828
</gf-form-dropdown>
2929
</div>

src/query_ctrl.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ export class GenericDatasourceQueryCtrl extends QueryCtrl {
2424
this.showJSON = false;
2525
}
2626

27-
getOptions(query) {
28-
return this.datasource.metricFindQuery(query || '');
27+
findMetrics(query: string) {
28+
return this.datasource.findMetricsQuery(query, this.target.type);
2929
}
3030

3131
// not used

0 commit comments

Comments
 (0)