grafana/public/app/features/explore/QueryEditor.tsx
Hugo Häggmark 6dbaa704bc
Explore: Align Explore with Dashboards and Panels (#16823)
* Wip: Removes queryTransactions from state

* Refactor: Adds back query failures

* Refactor: Moves error parsing to datasources

* Refactor: Adds back hinting for Prometheus

* Refactor: removed commented out code

* Refactor: Adds back QueryStatus

* Refactor: Adds scanning back to Explore

* Fix: Fixes prettier error

* Fix: Makes sure there is an error

* Merge: Merges with master

* Fix: Adds safeStringifyValue to error parsing

* Fix: Fixes table result calculations

* Refactor: Adds ErrorContainer and generic error handling in Explore

* Fix: Fixes so refIds remain consistent

* Refactor: Makes it possible to return result even when there are errors

* Fix: Fixes digest issue with Angular editors

* Refactor: Adds tests for explore utils

* Refactor: Breakes current behaviour of always returning a result even if Query fails

* Fix: Fixes Prettier error

* Fix: Adds back console.log for erroneous querys

* Refactor: Changes console.log to console.error
2019-05-10 14:00:39 +02:00

91 lines
2.5 KiB
TypeScript

// Libraries
import React, { PureComponent } from 'react';
// Services
import { getAngularLoader, AngularComponent } from 'app/core/services/AngularLoader';
import { getTimeSrv } from 'app/features/dashboard/services/TimeSrv';
// Types
import { Emitter } from 'app/core/utils/emitter';
import { DataQuery, TimeRange } from '@grafana/ui';
import 'app/features/plugins/plugin_loader';
import { dateTime } from '@grafana/ui/src/utils/moment_wrapper';
interface QueryEditorProps {
error?: any;
datasource: any;
onExecuteQuery?: () => void;
onQueryChange?: (value: DataQuery) => void;
initialQuery: DataQuery;
exploreEvents: Emitter;
range: TimeRange;
}
export default class QueryEditor extends PureComponent<QueryEditorProps, any> {
element: any;
component: AngularComponent;
async componentDidMount() {
if (!this.element) {
return;
}
const { datasource, initialQuery, exploreEvents, range } = this.props;
this.initTimeSrv(range);
const loader = getAngularLoader();
const template = '<plugin-component type="query-ctrl"> </plugin-component>';
const target = { datasource: datasource.name, ...initialQuery };
const scopeProps = {
ctrl: {
datasource,
target,
refresh: () => {
this.props.onQueryChange(target);
this.props.onExecuteQuery();
},
onQueryChange: () => {
this.props.onQueryChange(target);
},
events: exploreEvents,
panel: { datasource, targets: [target] },
dashboard: {},
},
};
this.component = loader.load(this.element, scopeProps, template);
this.props.onQueryChange(target);
}
componentDidUpdate(prevProps: QueryEditorProps) {
if (prevProps.error !== this.props.error && this.component) {
// Some query controllers listen to data error events and need a digest
// for some reason this needs to be done in next tick
setTimeout(this.component.digest);
}
}
componentWillUnmount() {
if (this.component) {
this.component.destroy();
}
}
initTimeSrv(range: TimeRange) {
const timeSrv = getTimeSrv();
timeSrv.init({
time: {
from: dateTime(range.from),
to: dateTime(range.to),
},
refresh: false,
getTimezone: () => 'utc',
timeRangeUpdated: () => console.log('refreshDashboard!'),
});
}
render() {
return <div className="gf-form-query" ref={element => (this.element = element)} style={{ width: '100%' }} />;
}
}