Skip to content

Commit a0d4f74

Browse files
author
abhishek1305
committed
added type support to listStream, listSchema
added type support to loadSchemaOption changes in the hook[]
1 parent f5e41e8 commit a0d4f74

File tree

5 files changed

+101
-66
lines changed

5 files changed

+101
-66
lines changed

dist/module.js

Lines changed: 13 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/plugin.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
"links": [],
1818
"screenshots": [],
1919
"version": "1.0.0",
20-
"updated": "2022-12-27"
20+
"updated": "2023-01-10"
2121
},
2222
"routes": [
2323
{

src/components/QueryEditor.tsx

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import React, { ComponentType, ChangeEvent, useState } from 'react';
22
import { LegacyForms, AsyncSelect, Label, InlineField, InlineFieldRow } from '@grafana/ui';
33
import { QueryEditorProps, SelectableValue } from '@grafana/data';
44
import { DataSource } from '../datasource';
5-
import { MyDataSourceOptions, MyQuery } from '../types';
5+
import { Fields, MyDataSourceOptions, MyQuery } from '../types';
66

77
const { FormField } = LegacyForms;
88

@@ -13,40 +13,44 @@ interface Props extends QueryEditorProps<DataSource, MyQuery, MyDataSourceOption
1313
export const QueryEditor: ComponentType<Props> = ({ datasource, onChange, onRunQuery, query }) => {
1414

1515
const { queryText } = query;
16-
const [stream, setStream] = React.useState<SelectableValue<string | number>>();
16+
//const [stream, setStream] = React.useState<SelectableValue<string | number>>();
1717

1818
const loadAsyncOptions = React.useCallback(() => {
19-
return datasource.listMetrics().then(
19+
return datasource.listStreams().then(
2020
(result) => {
2121
const stream = result.map((data) => ({ label: data.name, value: data.name }));
22-
setStream(stream);
2322
return stream;
2423
},
2524
(response) => {
26-
setStream({ label: '', value: '' });
25+
//setStream({ label: '', value: '' });
2726
throw new Error(response.statusText);
2827
}
2928
);
3029
}, [datasource]);
3130

3231
const [value, setValue] = useState<SelectableValue<string>>();
33-
const [schema, setSchema] = React.useState<string | number>();
32+
const [schema = '', setSchema] = React.useState<string | number>();
33+
//const [fielder, setFielder] = React.useState<string | number>();
3434

3535
const loadSchemaOptions = React.useCallback((value) => {
3636
if (value) {
3737
return datasource.listSchema(value).then(
3838
(result) => {
39-
const schema = result.map((data) => (data.name));
40-
const schemaToText = schema.join(", ")
41-
setSchema(schemaToText);
39+
if (result.fields) {
40+
const schema = result.fields.map((data: Fields) => (data.name));
41+
const schemaToText = schema.join(", ")
42+
setSchema(schemaToText);
43+
return schema;
44+
}
4245
return schema;
4346
},
4447
(response) => {
4548
throw new Error(response.statusText);
4649
}
4750
);
4851
}
49-
}, [value]);
52+
return '';
53+
}, [datasource, schema]);
5054

5155
const onQueryTextChange = (event: ChangeEvent<HTMLInputElement>) => {
5256
onChange({ ...query, queryText: event.target.value });
@@ -58,11 +62,11 @@ export const QueryEditor: ComponentType<Props> = ({ datasource, onChange, onRunQ
5862
},
5963
2000)
6064
return () => clearTimeout(getData)
61-
}, [queryText])
65+
}, [onRunQuery, queryText])
6266

6367
React.useEffect(() => {
6468
loadSchemaOptions(value)
65-
}, [value]);
69+
}, [loadSchemaOptions, value]);
6670

6771
return (
6872
<>
@@ -102,4 +106,4 @@ export const QueryEditor: ComponentType<Props> = ({ datasource, onChange, onRunQ
102106
/>
103107
</>
104108
);
105-
};
109+
};

src/datasource.ts

Lines changed: 44 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
import { getBackendSrv, BackendSrvRequest, FetchResponse, getTemplateSrv } from "@grafana/runtime";
2-
1+
import { getBackendSrv, BackendSrvRequest, FetchResponse } from "@grafana/runtime";
32
import {
43
DataQueryRequest,
54
DataQueryResponse,
@@ -9,19 +8,20 @@ import {
98
DataFrame,
109
FieldType,
1110
guessFieldTypeFromValue,
12-
ScopedVars
1311
} from '@grafana/data';
1412
import { lastValueFrom, of } from 'rxjs';
15-
import { isArray } from 'lodash';
13+
import { catchError, map } from 'rxjs/operators';
14+
import { isArray, isNull } from "lodash";
15+
1616
import {
1717
MyQuery,
1818
MyDataSourceOptions,
19-
StreamConfig,
20-
StreamPayloadConfig,
2119
QueryEditorMode,
20+
StreamName,
21+
StreamList,
22+
//Fields,
23+
ListSchemaResponse
2224
} from './types';
23-
import { catchError, map } from 'rxjs/operators';
24-
2525
export class DataSource extends DataSourceApi<MyQuery, MyDataSourceOptions> {
2626
url: string;
2727
withCredentials: boolean;
@@ -118,48 +118,50 @@ export class DataSource extends DataSourceApi<MyQuery, MyDataSourceOptions> {
118118
return dataFrame;
119119
}
120120

121-
async listMetrics() {
122-
const errorMessageBase = 'Parseable server is not reachable';
123-
try {
124-
const response = await lastValueFrom(
125-
this.doFetch({
126-
url: this.url + '/api/v1/logstream',
127-
method: 'GET',
128-
}).pipe(map((response) => response))
129-
);
130-
return response.data;
131-
} catch (err) {
132-
if (typeof err === 'string') {
133-
return {
134-
status: 'error',
135-
message: err,
136-
};
137-
}
138-
}
121+
async listStreams(): Promise<StreamList[]> {
122+
return lastValueFrom(
123+
this.doFetch({
124+
url: this.url + '/api/v1/logstream',
125+
method: 'GET',
126+
}).pipe(
127+
map((response) =>
128+
isArray(response.data)
129+
? response.data
130+
: []
131+
),
132+
catchError((err) => {
133+
console.error(err);
134+
135+
return of([]);
136+
}))
137+
);
139138
}
140139

141-
async listSchema(streamname) {
142-
const errorMessageBase = 'Parseable server is not reachable';
143-
try {
144-
const response = await lastValueFrom(
140+
async listSchema(streamname: StreamName): Promise<ListSchemaResponse> {
141+
if (streamname) {
142+
return lastValueFrom(
145143
this.doFetch({
146144
url: this.url + '/api/v1/logstream/' + streamname.value + '/schema',
147145
method: 'GET',
148-
}).pipe(map((response) => response))
149-
);
150-
return response.data.fields;
151-
} catch (err) {
152-
if (typeof err === 'string') {
153-
return {
154-
status: 'error',
155-
message: err,
156-
};
157-
}
146+
}).pipe(
147+
map((response) =>
148+
(typeof response.data === 'object' && !isNull(response.data))
149+
? response.data
150+
: {}
151+
),
152+
catchError((err) => {
153+
console.error(err);
154+
return of({
155+
status: 'error',
156+
message: err.statusText
157+
})
158+
159+
}))
160+
)
158161
}
162+
return { fields: [] }
159163
}
160164

161-
162-
163165
async testDatasource() {
164166
const errorMessageBase = 'Parseable server is not reachable';
165167
try {

src/types.ts

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,6 @@ export interface MyDataSourceOptions extends DataSourceJsonData {
3131
export interface MySecureJsonData {
3232
password?: string;
3333
}
34-
35-
declare module 'react' {
36-
interface DOMAttributes<T> {
37-
css?: InterpolationWithTheme<any>;
38-
}
39-
}
40-
4134
export interface StreamPayloadConfig {
4235
width?: number;
4336
placeholder?: string;
@@ -55,4 +48,30 @@ export interface StreamConfig {
5548
payloads?: StreamPayloadConfig[];
5649
}
5750

51+
export interface StreamName {
52+
label: String | Number;
53+
value: String | Number;
54+
}
55+
56+
export interface ListSchemaResponse {
57+
fields?: [] | undefined;
58+
status?: string;
59+
message?: string;
60+
}
61+
62+
export interface Fields {
63+
name?: string;
64+
data_type?: "Utf8";
65+
nullable?: boolean;
66+
dict_id?: number;
67+
dict_is_ordered?: boolean;
68+
}
69+
70+
export interface Schema {
71+
schema?: string[];
72+
}
73+
export interface StreamList {
74+
name?: string;
75+
}
76+
5877
export type QueryEditorMode = "code" | "builder";

0 commit comments

Comments
 (0)