Commit 0de861a3 by Torkel Ödegaard

Minor progress on react query editor support, solving updating query persisted state

parent e4244d8b
import React from 'react'; import React from 'react';
import _ from 'lodash'; import _ from 'lodash';
import { Select } from '@grafana/ui'; import { Select, SelectOptionItem } from '@grafana/ui';
import { SelectOptionItem } from '@grafana/ui';
import { Variable } from 'app/types/templates'; import { Variable } from 'app/types/templates';
export interface Props { export interface Props {
......
...@@ -165,6 +165,11 @@ export class QueriesTab extends PureComponent<Props, State> { ...@@ -165,6 +165,11 @@ export class QueriesTab extends PureComponent<Props, State> {
this.setState({ isAddingMixed: false }); this.setState({ isAddingMixed: false });
}; };
onQueryChange = (query: DataQuery, index) => {
this.props.panel.changeQuery(query, index);
this.forceUpdate();
};
setScrollTop = (event: React.MouseEvent<HTMLElement>) => { setScrollTop = (event: React.MouseEvent<HTMLElement>) => {
const target = event.target as HTMLElement; const target = event.target as HTMLElement;
this.setState({ scrollTop: target.scrollTop }); this.setState({ scrollTop: target.scrollTop });
...@@ -201,6 +206,7 @@ export class QueriesTab extends PureComponent<Props, State> { ...@@ -201,6 +206,7 @@ export class QueriesTab extends PureComponent<Props, State> {
key={query.refId} key={query.refId}
panel={panel} panel={panel}
query={query} query={query}
onChange={query => this.onQueryChange(query, index)}
onRemoveQuery={this.onRemoveQuery} onRemoveQuery={this.onRemoveQuery}
onAddQuery={this.onAddQuery} onAddQuery={this.onAddQuery}
onMoveQuery={this.onMoveQuery} onMoveQuery={this.onMoveQuery}
......
...@@ -18,6 +18,7 @@ interface Props { ...@@ -18,6 +18,7 @@ interface Props {
onAddQuery: (query?: DataQuery) => void; onAddQuery: (query?: DataQuery) => void;
onRemoveQuery: (query: DataQuery) => void; onRemoveQuery: (query: DataQuery) => void;
onMoveQuery: (query: DataQuery, direction: number) => void; onMoveQuery: (query: DataQuery, direction: number) => void;
onChange: (query: DataQuery) => void;
dataSourceValue: string | null; dataSourceValue: string | null;
inMixedMode: boolean; inMixedMode: boolean;
} }
...@@ -105,16 +106,12 @@ export class QueryEditorRow extends PureComponent<Props, State> { ...@@ -105,16 +106,12 @@ export class QueryEditorRow extends PureComponent<Props, State> {
this.setState({ isCollapsed: !this.state.isCollapsed }); this.setState({ isCollapsed: !this.state.isCollapsed });
}; };
onQueryChange = (query: DataQuery) => {
Object.assign(this.props.query, query);
};
onRunQuery = () => { onRunQuery = () => {
this.props.panel.refresh(); this.props.panel.refresh();
}; };
renderPluginEditor() { renderPluginEditor() {
const { query } = this.props; const { query, onChange } = this.props;
const { datasource } = this.state; const { datasource } = this.state;
if (datasource.pluginExports.QueryCtrl) { if (datasource.pluginExports.QueryCtrl) {
...@@ -127,7 +124,7 @@ export class QueryEditorRow extends PureComponent<Props, State> { ...@@ -127,7 +124,7 @@ export class QueryEditorRow extends PureComponent<Props, State> {
<QueryEditor <QueryEditor
query={query} query={query}
datasource={datasource} datasource={datasource}
onChange={this.onQueryChange} onChange={onChange}
onRunQuery={this.onRunQuery} onRunQuery={this.onRunQuery}
/> />
); );
...@@ -165,7 +162,7 @@ export class QueryEditorRow extends PureComponent<Props, State> { ...@@ -165,7 +162,7 @@ export class QueryEditorRow extends PureComponent<Props, State> {
onDisableQuery = () => { onDisableQuery = () => {
this.props.query.hide = !this.props.query.hide; this.props.query.hide = !this.props.query.hide;
this.onExecuteQuery(); this.onRunQuery();
this.forceUpdate(); this.forceUpdate();
}; };
......
...@@ -268,6 +268,19 @@ export class PanelModel { ...@@ -268,6 +268,19 @@ export class PanelModel {
}); });
} }
changeQuery(query: DataQuery, index: number) {
// ensure refId is maintained
query.refId = this.targets[index].refId;
// update query in array
this.targets = this.targets.map((item, itemIndex) => {
if (itemIndex === index) {
return query;
}
return item;
});
}
destroy() { destroy() {
this.events.emit('panel-teardown'); this.events.emit('panel-teardown');
this.events.removeAllListeners(); this.events.removeAllListeners();
......
// Libraries // Libraries
import React, { PureComponent } from 'react'; import React, { PureComponent } from 'react';
// Components
import { Select, SelectOptionItem } from '@grafana/ui';
// Types // Types
import { QueryEditorProps } from '@grafana/ui/src/types'; import { QueryEditorProps } from '@grafana/ui/src/types';
import { LokiDatasource } from '../datasource'; import { LokiDatasource } from '../datasource';
...@@ -14,13 +17,12 @@ interface State { ...@@ -14,13 +17,12 @@ interface State {
} }
export class LokiQueryEditor extends PureComponent<Props> { export class LokiQueryEditor extends PureComponent<Props> {
state: State = { state: State = {
query: this.props.query query: this.props.query,
}; };
onRunQuery = () => { onRunQuery = () => {
const { query } = this.state; const { query } = this.state;
this.props.onChange(query); this.props.onChange(query);
this.props.onRunQuery(); this.props.onRunQuery();
...@@ -28,17 +30,44 @@ export class LokiQueryEditor extends PureComponent<Props> { ...@@ -28,17 +30,44 @@ export class LokiQueryEditor extends PureComponent<Props> {
onFieldChange = (query: LokiQuery, override?) => { onFieldChange = (query: LokiQuery, override?) => {
this.setState({ this.setState({
query: query query: {
...this.state.query,
expr: query.expr,
}
});
};
onFormatChanged = (option: SelectOptionItem) => {
this.props.onChange({
...this.state.query,
resultFormat: option.value,
}); });
}; };
render() { render() {
const { query } = this.state; const { query } = this.state;
const { datasource } = this.props; const { datasource } = this.props;
const formatOptions: SelectOptionItem[] = [
{ label: 'Time Series', value: 'time_series' },
{ label: 'Table', value: 'table' },
{ label: 'Logs', value: 'logs' },
];
query.resultFormat = query.resultFormat || 'time_series';
const currentFormat = formatOptions.find(item => item.value === query.resultFormat);
return ( return (
<div> <div>
<LokiQueryField datasource={datasource} initialQuery={query} onQueryChange={this.onFieldChange} onPressEnter={this.onRunQuery} /> <LokiQueryField
datasource={datasource}
initialQuery={query}
onQueryChange={this.onFieldChange}
onPressEnter={this.onRunQuery}
/>
<div className="gf-form">
<div className="gf-form-label">Format as</div>
<Select isSearchable={false} options={formatOptions} onChange={this.onFormatChanged} value={currentFormat} />
</div>
</div> </div>
); );
} }
......
...@@ -2,5 +2,8 @@ import { DataQuery } from '@grafana/ui/src/types'; ...@@ -2,5 +2,8 @@ import { DataQuery } from '@grafana/ui/src/types';
export interface LokiQuery extends DataQuery { export interface LokiQuery extends DataQuery {
expr: string; expr: string;
resultFormat?: LokiQueryResultFormats;
} }
export type LokiQueryResultFormats = 'time_series' | 'logs';
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment