Skip to content
This repository was archived by the owner on Apr 27, 2023. It is now read-only.

Commit ba0193e

Browse files
committed
fix first review bugs
1 parent 6cecf32 commit ba0193e

File tree

6 files changed

+101
-36
lines changed

6 files changed

+101
-36
lines changed

src/ConfigEditor.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,12 +54,12 @@ export class ConfigEditor extends PureComponent<Props, State> {
5454
<div className="gf-form-group">
5555
<div className="gf-form">
5656
<FormField
57-
label="Endpoint"
57+
label="Pyroscope instance"
5858
labelWidth={6}
5959
inputWidth={20}
6060
onChange={this.onPathChange}
6161
value={jsonData.path || ''}
62-
placeholder="url to your pyroscope flamegraph API endpoint"
62+
placeholder="url to your pyroscope instance"
6363
/>
6464
</div>
6565

src/QueryEditor.tsx

Lines changed: 37 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,57 @@
11
import defaults from 'lodash/defaults';
22

3-
import React, { ChangeEvent, PureComponent } from 'react';
4-
import { LegacyForms } from '@grafana/ui';
5-
import { QueryEditorProps } from '@grafana/data';
3+
import React, { useState, useEffect } from 'react';
4+
import { AsyncSelect, Label, Field } from '@grafana/ui';
5+
import { QueryEditorProps, SelectableValue } from '@grafana/data';
66
import { DataSource } from './datasource';
77
import { defaultQuery, MyDataSourceOptions, FlamegraphQuery } from './types';
88

9-
const { FormField } = LegacyForms;
109

1110
type Props = QueryEditorProps<DataSource, FlamegraphQuery, MyDataSourceOptions>;
1211

13-
export class QueryEditor extends PureComponent<Props> {
14-
onNameChange = (event: ChangeEvent<HTMLInputElement>) => {
15-
const { onChange, query, onRunQuery } = this.props;
16-
onChange({ ...query, name: event.target.value });
17-
onRunQuery();
18-
};
12+
export const QueryEditor = (props: Props) => {
13+
const query = defaults(props.query, defaultQuery);
14+
const [appName, setAppName] = useState<SelectableValue<string>>({ label: query.name, value: query.name });
15+
const [names, setNames] = useState<SelectableValue<string>[]>([]);
16+
const loadAppNames = (query: string) => {
17+
return props.datasource.loadAppNames().then(
18+
result => setNames(result.data.map((value: string) => ({ label: value, value }))),
19+
response => {
20+
throw new Error(response.statusText);
21+
}
22+
);
23+
}
24+
25+
useEffect(() => {
26+
loadAppNames('');
27+
}, [])
1928

20-
render() {
21-
const query = defaults(this.props.query, defaultQuery);
22-
const { name } = query;
29+
useEffect(() => {
30+
const { onChange, query, onRunQuery } = props;
31+
onChange({ ...query, name: appName.value });
32+
onRunQuery();
33+
}, [appName]);
2334

2435
return (
2536
<div className="gf-form">
2637
{/* <FormField
27-
width={4}
28-
value={constant}
29-
onChange={this.onConstantChange}
30-
label="Constant"
31-
type="number"
32-
step="0.1"
33-
/> */}
34-
<FormField
3538
labelWidth={8}
3639
value={name}
3740
onChange={this.onNameChange}
3841
label="Application name"
39-
/>
42+
/> */}
43+
<div style={{display: 'flex', flexDirection: 'row', marginTop: '10px'}}>
44+
<Label style={{marginTop: '8px', marginRight: '10px'}}>Application</Label>
45+
<Field>
46+
<AsyncSelect
47+
placeholder='Application name'
48+
value={appName}
49+
onChange={setAppName}
50+
defaultOptions={names}
51+
loadOptions={(query: string) => Promise.resolve(names.filter(name => name.value?.includes(query)))}
52+
/>
53+
</Field>
54+
</div>
4055
</div>
4156
);
42-
}
4357
}

src/datasource.ts

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,13 @@ export class DataSource extends DataSourceApi<FlamegraphQuery, MyDataSourceOptio
1818
this.instanceSettings = instanceSettings;
1919
this.backendSrv = getBackendSrv();
2020
this.url = instanceSettings.url || '';
21-
console.log(instanceSettings)
2221
}
2322

2423
instanceSettings: DataSourceInstanceSettings<MyDataSourceOptions>;
2524
backendSrv: BackendSrv;
2625
url: string;
2726

28-
async doRequest(query: FlamegraphQuery) {
27+
async getFlamegraph(query: FlamegraphQuery) {
2928
const result = await this.backendSrv.fetch({
3029
method: 'GET',
3130
url: this.url +'/render/render',
@@ -35,15 +34,25 @@ export class DataSource extends DataSourceApi<FlamegraphQuery, MyDataSourceOptio
3534
return result;
3635
}
3736

37+
async getNames() {
38+
const result = await this.backendSrv.fetch<Array<string>>({
39+
method: 'GET',
40+
url: this.url +'/render/label-values?label=__name__',
41+
}).toPromise();
42+
43+
return result;
44+
}
45+
3846
async query(options: DataQueryRequest<FlamegraphQuery>): Promise<DataQueryResponse> {
3947
const { range } = options;
4048
const from = range.raw.from.valueOf();
4149
const until = range.raw.to.valueOf();
4250

4351
const promises = options.targets.map((query) =>
44-
this.doRequest({ ...query, from, until }).then((response: any) => {
52+
this.getFlamegraph({ ...query, from, until }).then((response: any) => {
4553
const frame = new MutableDataFrame({
4654
refId: query.refId,
55+
name: query.name,
4756
fields: [
4857
{ name: "flamebearer", type: FieldType.other },
4958
],
@@ -58,11 +67,22 @@ export class DataSource extends DataSourceApi<FlamegraphQuery, MyDataSourceOptio
5867
return Promise.all(promises).then((data) => ({ data }));
5968
}
6069

70+
loadAppNames(): Promise<any> {
71+
return this.getNames();
72+
}
73+
6174
async testDatasource() {
62-
// Implement a health check for your data source.
63-
return {
64-
status: 'success',
65-
message: 'Success',
66-
};
75+
const names = await this.getNames();
76+
if(names.status === 200) {
77+
return {
78+
status: 'success',
79+
message: 'Success',
80+
};
81+
} else {
82+
return {
83+
status: 'error',
84+
message: 'Server is not reachable',
85+
};
86+
}
6787
}
6888
}

src/img/logo.svg

Lines changed: 32 additions & 1 deletion
Loading

src/plugin.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
"links": [
2222
{
2323
"name": "Website",
24-
"url": "https://github.com/grafana/grafana-starter-datasource"
24+
"url": "https://pyroscope.io"
2525
},
2626
{
2727
"name": "License",

src/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { DataQuery, DataSourceJsonData } from '@grafana/data';
22

33
export interface FlamegraphQuery extends DataQuery {
4-
name: string;
4+
name?: string;
55
format: string;
66
from?: number | string;
77
until?: number | string;

0 commit comments

Comments
 (0)