2018-11-22 11:22:15 -06:00
|
|
|
import React, { PureComponent } from 'react';
|
|
|
|
import { getAngularLoader, AngularComponent } from 'app/core/services/AngularLoader';
|
2018-11-23 07:32:56 -06:00
|
|
|
import { Emitter } from 'app/core/utils/emitter';
|
2018-11-27 07:45:39 -06:00
|
|
|
import { getIntervals } from 'app/core/utils/explore';
|
2018-11-22 11:22:15 -06:00
|
|
|
import { DataQuery } from 'app/types';
|
2018-11-27 07:45:39 -06:00
|
|
|
import { RawTimeRange } from 'app/types/series';
|
2018-11-23 07:32:56 -06:00
|
|
|
import 'app/features/plugins/plugin_loader';
|
2018-11-27 07:45:39 -06:00
|
|
|
import { getTimeSrv } from 'app/features/dashboard/time_srv';
|
2018-11-22 11:22:15 -06:00
|
|
|
|
|
|
|
interface QueryEditorProps {
|
|
|
|
datasource: any;
|
|
|
|
error?: string | JSX.Element;
|
|
|
|
onExecuteQuery?: () => void;
|
|
|
|
onQueryChange?: (value: DataQuery, override?: boolean) => void;
|
2018-11-23 07:32:56 -06:00
|
|
|
initialQuery: DataQuery;
|
|
|
|
exploreEvents: Emitter;
|
2018-11-27 07:45:39 -06:00
|
|
|
range: RawTimeRange;
|
2018-11-22 11:22:15 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
export default class QueryEditor extends PureComponent<QueryEditorProps, any> {
|
|
|
|
element: any;
|
|
|
|
component: AngularComponent;
|
|
|
|
|
|
|
|
async componentDidMount() {
|
|
|
|
if (!this.element) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-11-27 07:45:39 -06:00
|
|
|
const { datasource, initialQuery, exploreEvents, range } = this.props;
|
|
|
|
this.initTimeSrv(range);
|
|
|
|
|
2018-11-22 11:22:15 -06:00
|
|
|
const loader = getAngularLoader();
|
2018-11-23 07:32:56 -06:00
|
|
|
const template = '<plugin-component type="query-ctrl"> </plugin-component>';
|
2018-11-27 06:13:26 -06:00
|
|
|
const target = { datasource: datasource.name, ...initialQuery };
|
2018-11-22 11:22:15 -06:00
|
|
|
const scopeProps = {
|
2018-11-23 07:32:56 -06:00
|
|
|
target,
|
2018-11-22 11:22:15 -06:00
|
|
|
ctrl: {
|
|
|
|
refresh: () => {
|
2018-11-23 07:32:56 -06:00
|
|
|
this.props.onQueryChange({ refId: initialQuery.refId, ...target }, false);
|
2018-11-22 11:22:15 -06:00
|
|
|
this.props.onExecuteQuery();
|
|
|
|
},
|
2018-11-23 08:12:20 -06:00
|
|
|
events: exploreEvents,
|
2018-11-22 11:22:15 -06:00
|
|
|
panel: {
|
|
|
|
datasource,
|
2018-11-23 07:32:56 -06:00
|
|
|
targets: [{}],
|
2018-11-22 11:22:15 -06:00
|
|
|
},
|
|
|
|
dashboard: {
|
2018-11-23 07:32:56 -06:00
|
|
|
getNextQueryLetter: x => '',
|
2018-11-22 11:22:15 -06:00
|
|
|
},
|
2018-11-23 08:15:41 -06:00
|
|
|
hideEditorRowActions: true,
|
2018-11-27 07:45:39 -06:00
|
|
|
...getIntervals(range, datasource, null), // Possible to get resolution?
|
2018-11-22 11:22:15 -06:00
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
this.component = loader.load(this.element, scopeProps, template);
|
|
|
|
}
|
|
|
|
|
|
|
|
componentWillUnmount() {
|
|
|
|
if (this.component) {
|
|
|
|
this.component.destroy();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-27 07:45:39 -06:00
|
|
|
initTimeSrv(range) {
|
|
|
|
const timeSrv = getTimeSrv();
|
|
|
|
timeSrv.init({
|
|
|
|
time: range,
|
|
|
|
refresh: false,
|
|
|
|
getTimezone: () => 'utc',
|
|
|
|
timeRangeUpdated: () => console.log('refreshDashboard!'),
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-11-22 11:22:15 -06:00
|
|
|
render() {
|
|
|
|
return <div ref={element => (this.element = element)} style={{ width: '100%' }} />;
|
|
|
|
}
|
|
|
|
}
|