2019-04-12 12:13:36 -05:00
|
|
|
// Libraries
|
|
|
|
import React, { PureComponent } from 'react';
|
|
|
|
|
|
|
|
// Types
|
2019-07-01 14:00:29 -05:00
|
|
|
import { InputDatasource, describeDataFrame } from './InputDatasource';
|
2019-05-09 04:48:13 -05:00
|
|
|
import { InputQuery, InputOptions } from './types';
|
2019-04-12 12:13:36 -05:00
|
|
|
|
2019-07-01 14:00:29 -05:00
|
|
|
import { FormLabel, Select, QueryEditorProps, SelectOptionItem, DataFrame, TableInputCSV, toCSV } from '@grafana/ui';
|
2019-04-12 12:13:36 -05:00
|
|
|
|
2019-05-09 04:48:13 -05:00
|
|
|
type Props = QueryEditorProps<InputDatasource, InputQuery, InputOptions>;
|
2019-04-12 12:13:36 -05:00
|
|
|
|
|
|
|
const options = [
|
|
|
|
{ value: 'panel', label: 'Panel', description: 'Save data in the panel configuration.' },
|
|
|
|
{ value: 'shared', label: 'Shared', description: 'Save data in the shared datasource object.' },
|
|
|
|
];
|
|
|
|
|
|
|
|
interface State {
|
|
|
|
text: string;
|
|
|
|
}
|
|
|
|
|
|
|
|
export class InputQueryEditor extends PureComponent<Props, State> {
|
|
|
|
state = {
|
|
|
|
text: '',
|
|
|
|
};
|
|
|
|
|
|
|
|
onComponentDidMount() {
|
|
|
|
const { query } = this.props;
|
|
|
|
const text = query.data ? toCSV(query.data) : '';
|
|
|
|
this.setState({ text });
|
|
|
|
}
|
|
|
|
|
2019-04-23 13:44:16 -05:00
|
|
|
onSourceChange = (item: SelectOptionItem<string>) => {
|
2019-04-12 12:13:36 -05:00
|
|
|
const { datasource, query, onChange, onRunQuery } = this.props;
|
2019-07-01 14:00:29 -05:00
|
|
|
let data: DataFrame[] | undefined = undefined;
|
2019-04-12 12:13:36 -05:00
|
|
|
if (item.value === 'panel') {
|
|
|
|
if (query.data) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
data = [...datasource.data];
|
|
|
|
if (!data) {
|
|
|
|
data = [
|
|
|
|
{
|
|
|
|
fields: [],
|
|
|
|
rows: [],
|
|
|
|
},
|
|
|
|
];
|
|
|
|
}
|
|
|
|
this.setState({ text: toCSV(data) });
|
|
|
|
}
|
|
|
|
onChange({ ...query, data });
|
|
|
|
onRunQuery();
|
|
|
|
};
|
|
|
|
|
2019-07-01 14:00:29 -05:00
|
|
|
onSeriesParsed = (data: DataFrame[], text: string) => {
|
2019-04-12 12:13:36 -05:00
|
|
|
const { query, onChange, onRunQuery } = this.props;
|
|
|
|
this.setState({ text });
|
|
|
|
if (!data) {
|
|
|
|
data = [
|
|
|
|
{
|
|
|
|
fields: [],
|
|
|
|
rows: [],
|
|
|
|
},
|
|
|
|
];
|
|
|
|
}
|
|
|
|
onChange({ ...query, data });
|
|
|
|
onRunQuery();
|
|
|
|
};
|
|
|
|
|
|
|
|
render() {
|
|
|
|
const { datasource, query } = this.props;
|
|
|
|
const { id, name } = datasource;
|
|
|
|
const { text } = this.state;
|
|
|
|
|
|
|
|
const selected = query.data ? options[0] : options[1];
|
|
|
|
return (
|
|
|
|
<div>
|
|
|
|
<div className="gf-form">
|
|
|
|
<FormLabel width={4}>Data</FormLabel>
|
|
|
|
<Select width={6} options={options} value={selected} onChange={this.onSourceChange} />
|
|
|
|
|
|
|
|
<div className="btn btn-link">
|
|
|
|
{query.data ? (
|
2019-07-01 14:00:29 -05:00
|
|
|
describeDataFrame(query.data)
|
2019-04-12 12:13:36 -05:00
|
|
|
) : (
|
|
|
|
<a href={`datasources/edit/${id}/`}>
|
2019-07-01 14:00:29 -05:00
|
|
|
{name}: {describeDataFrame(datasource.data)}
|
2019-04-12 12:13:36 -05:00
|
|
|
<i className="fa fa-pencil-square-o" />
|
|
|
|
</a>
|
|
|
|
)}
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
{query.data && <TableInputCSV text={text} onSeriesParsed={this.onSeriesParsed} width={'100%'} height={200} />}
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|