Skip to content

Commit 6ef3934

Browse files
authored
implement testDatasource for ott-vis-datasource and add configurable baseUrl (#1399)
1 parent dd48ffb commit 6ef3934

File tree

6 files changed

+29
-8
lines changed

6 files changed

+29
-8
lines changed

packages/ott-vis-common/provisioning/datasources/datasources.yml

+2
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,5 @@ datasources:
88
orgId: 1
99
version: 1
1010
editable: true
11+
jsonData:
12+
baseUrl: "http://localhost:8000"

packages/ott-vis-datasource/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@
6767
"ott-vis-common": "*",
6868
"react": "18.2.0",
6969
"react-dom": "18.2.0",
70+
"rxjs": "7.8.0",
7071
"tslib": "2.5.3"
7172
}
7273
}

packages/ott-vis-datasource/provisioning/datasources/datasources.yml

+2
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,5 @@ datasources:
88
orgId: 1
99
version: 1
1010
editable: true
11+
jsonData:
12+
baseUrl: "http://localhost:8000"

packages/ott-vis-datasource/src/components/ConfigEditor.tsx

+6-6
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@ interface Props extends DataSourcePluginOptionsEditorProps<MyDataSourceOptions>
77

88
export function ConfigEditor(props: Props) {
99
const { onOptionsChange, options } = props;
10-
const onPathChange = (event: ChangeEvent<HTMLInputElement>) => {
10+
const onBaseUrlChange = (event: ChangeEvent<HTMLInputElement>) => {
1111
const jsonData = {
1212
...options.jsonData,
13-
path: event.target.value,
13+
baseUrl: event.target.value,
1414
};
1515
onOptionsChange({ ...options, jsonData });
1616
};
@@ -44,11 +44,11 @@ export function ConfigEditor(props: Props) {
4444

4545
return (
4646
<div className="gf-form-group">
47-
<InlineField label="Path" labelWidth={12}>
47+
<InlineField label="Base URL" labelWidth={12}>
4848
<Input
49-
onChange={onPathChange}
50-
value={jsonData.path || ""}
51-
placeholder="json field returned to frontend"
49+
onChange={onBaseUrlChange}
50+
value={jsonData.baseUrl || ""}
51+
placeholder="URL to ott-collector"
5252
width={40}
5353
/>
5454
</InlineField>

packages/ott-vis-datasource/src/datasource.ts

+17-1
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,15 @@ import {
99

1010
import { MyQuery, MyDataSourceOptions } from "./types";
1111
import type { SystemState } from "ott-vis-common";
12+
import { getBackendSrv } from "@grafana/runtime";
13+
import { lastValueFrom } from "rxjs";
1214

1315
export class DataSource extends DataSourceApi<MyQuery, MyDataSourceOptions> {
16+
baseUrl: string;
17+
1418
constructor(instanceSettings: DataSourceInstanceSettings<MyDataSourceOptions>) {
1519
super(instanceSettings);
20+
this.baseUrl = instanceSettings.jsonData.baseUrl;
1621
}
1722

1823
async query(options: DataQueryRequest<MyQuery>): Promise<DataQueryResponse> {
@@ -32,7 +37,18 @@ export class DataSource extends DataSourceApi<MyQuery, MyDataSourceOptions> {
3237
}
3338

3439
async testDatasource() {
35-
// Implement a health check for your data source.
40+
const obs = await getBackendSrv().fetch({
41+
url: `${this.baseUrl}/status`,
42+
});
43+
const resp = await lastValueFrom(obs);
44+
45+
if (resp.status !== 200) {
46+
return {
47+
status: "error",
48+
message: `Got HTTP status ${resp.status} from server: ${resp.statusText}`,
49+
};
50+
}
51+
3652
return {
3753
status: "success",
3854
message: "Success",

packages/ott-vis-datasource/src/types.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ export const DEFAULT_QUERY: Partial<MyQuery> = {
1414
* These are options configured for each DataSource instance
1515
*/
1616
export interface MyDataSourceOptions extends DataSourceJsonData {
17-
path?: string;
17+
baseUrl: string;
1818
}
1919

2020
/**

0 commit comments

Comments
 (0)