From 0de861a3a8ca9cc5e91f17a9ca235d3b1ff20120 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Torkel=20=C3=96degaard?= Date: Tue, 29 Jan 2019 09:39:23 +0100 Subject: [PATCH] Minor progress on react query editor support, solving updating query persisted state --- .../core/components/Select/MetricSelect.tsx | 3 +- .../dashboard/panel_editor/QueriesTab.tsx | 6 +++ .../dashboard/panel_editor/QueryEditorRow.tsx | 11 ++--- public/app/features/dashboard/panel_model.ts | 13 ++++++ .../loki/components/LokiQueryEditor.tsx | 41 ++++++++++++++++--- public/app/plugins/datasource/loki/types.ts | 3 ++ 6 files changed, 62 insertions(+), 15 deletions(-) diff --git a/public/app/core/components/Select/MetricSelect.tsx b/public/app/core/components/Select/MetricSelect.tsx index c9247198052..62045662c64 100644 --- a/public/app/core/components/Select/MetricSelect.tsx +++ b/public/app/core/components/Select/MetricSelect.tsx @@ -1,8 +1,7 @@ import React from 'react'; import _ from 'lodash'; -import { Select } from '@grafana/ui'; -import { SelectOptionItem } from '@grafana/ui'; +import { Select, SelectOptionItem } from '@grafana/ui'; import { Variable } from 'app/types/templates'; export interface Props { diff --git a/public/app/features/dashboard/panel_editor/QueriesTab.tsx b/public/app/features/dashboard/panel_editor/QueriesTab.tsx index 28d822e3ad5..1937bebc95c 100644 --- a/public/app/features/dashboard/panel_editor/QueriesTab.tsx +++ b/public/app/features/dashboard/panel_editor/QueriesTab.tsx @@ -165,6 +165,11 @@ export class QueriesTab extends PureComponent { this.setState({ isAddingMixed: false }); }; + onQueryChange = (query: DataQuery, index) => { + this.props.panel.changeQuery(query, index); + this.forceUpdate(); + }; + setScrollTop = (event: React.MouseEvent) => { const target = event.target as HTMLElement; this.setState({ scrollTop: target.scrollTop }); @@ -201,6 +206,7 @@ export class QueriesTab extends PureComponent { key={query.refId} panel={panel} query={query} + onChange={query => this.onQueryChange(query, index)} onRemoveQuery={this.onRemoveQuery} onAddQuery={this.onAddQuery} onMoveQuery={this.onMoveQuery} diff --git a/public/app/features/dashboard/panel_editor/QueryEditorRow.tsx b/public/app/features/dashboard/panel_editor/QueryEditorRow.tsx index 720e972031a..abc33e9ca25 100644 --- a/public/app/features/dashboard/panel_editor/QueryEditorRow.tsx +++ b/public/app/features/dashboard/panel_editor/QueryEditorRow.tsx @@ -18,6 +18,7 @@ interface Props { onAddQuery: (query?: DataQuery) => void; onRemoveQuery: (query: DataQuery) => void; onMoveQuery: (query: DataQuery, direction: number) => void; + onChange: (query: DataQuery) => void; dataSourceValue: string | null; inMixedMode: boolean; } @@ -105,16 +106,12 @@ export class QueryEditorRow extends PureComponent { this.setState({ isCollapsed: !this.state.isCollapsed }); }; - onQueryChange = (query: DataQuery) => { - Object.assign(this.props.query, query); - }; - onRunQuery = () => { this.props.panel.refresh(); }; renderPluginEditor() { - const { query } = this.props; + const { query, onChange } = this.props; const { datasource } = this.state; if (datasource.pluginExports.QueryCtrl) { @@ -127,7 +124,7 @@ export class QueryEditorRow extends PureComponent { ); @@ -165,7 +162,7 @@ export class QueryEditorRow extends PureComponent { onDisableQuery = () => { this.props.query.hide = !this.props.query.hide; - this.onExecuteQuery(); + this.onRunQuery(); this.forceUpdate(); }; diff --git a/public/app/features/dashboard/panel_model.ts b/public/app/features/dashboard/panel_model.ts index 6aded0da1d7..2a5af602662 100644 --- a/public/app/features/dashboard/panel_model.ts +++ b/public/app/features/dashboard/panel_model.ts @@ -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() { this.events.emit('panel-teardown'); this.events.removeAllListeners(); diff --git a/public/app/plugins/datasource/loki/components/LokiQueryEditor.tsx b/public/app/plugins/datasource/loki/components/LokiQueryEditor.tsx index d2cd2fa988e..a72bc585a65 100644 --- a/public/app/plugins/datasource/loki/components/LokiQueryEditor.tsx +++ b/public/app/plugins/datasource/loki/components/LokiQueryEditor.tsx @@ -1,6 +1,9 @@ // Libraries import React, { PureComponent } from 'react'; +// Components +import { Select, SelectOptionItem } from '@grafana/ui'; + // Types import { QueryEditorProps } from '@grafana/ui/src/types'; import { LokiDatasource } from '../datasource'; @@ -14,13 +17,12 @@ interface State { } export class LokiQueryEditor extends PureComponent { - state: State = { - query: this.props.query + query: this.props.query, }; onRunQuery = () => { - const { query } = this.state; + const { query } = this.state; this.props.onChange(query); this.props.onRunQuery(); @@ -28,17 +30,44 @@ export class LokiQueryEditor extends PureComponent { onFieldChange = (query: LokiQuery, override?) => { this.setState({ - query: query + query: { + ...this.state.query, + expr: query.expr, + } + }); + }; + + onFormatChanged = (option: SelectOptionItem) => { + this.props.onChange({ + ...this.state.query, + resultFormat: option.value, }); }; render() { - const { query } = this.state; + const { query } = this.state; 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 (
- + +
+
Format as
+