2017-12-20 05:33:33 -06:00
|
|
|
import _ from 'lodash';
|
2018-05-30 08:24:47 -05:00
|
|
|
|
2018-11-13 09:01:52 -06:00
|
|
|
import kbn from 'app/core/utils/kbn';
|
|
|
|
|
2017-12-20 05:33:33 -06:00
|
|
|
import { PanelCtrl } from 'app/features/panel/panel_ctrl';
|
2018-09-24 10:47:43 -05:00
|
|
|
import { getExploreUrl } from 'app/core/utils/explore';
|
2018-11-13 09:01:52 -06:00
|
|
|
import { applyPanelTimeOverrides, getResolution } from 'app/features/dashboard/utils/panel';
|
2019-01-21 03:50:34 -06:00
|
|
|
import { ContextSrv } from 'app/core/services/context_srv';
|
2019-10-31 04:48:05 -05:00
|
|
|
import {
|
|
|
|
DataFrame,
|
2019-11-07 06:49:45 -06:00
|
|
|
DataQueryResponse,
|
2019-10-31 04:48:05 -05:00
|
|
|
DataSourceApi,
|
2019-11-07 06:49:45 -06:00
|
|
|
LegacyResponseData,
|
|
|
|
LoadingState,
|
2019-10-31 04:48:05 -05:00
|
|
|
PanelData,
|
|
|
|
PanelEvents,
|
2019-11-07 06:49:45 -06:00
|
|
|
TimeRange,
|
|
|
|
toDataFrameDTO,
|
|
|
|
toLegacyResponseData,
|
2019-10-31 04:48:05 -05:00
|
|
|
} from '@grafana/data';
|
2019-04-11 00:57:24 -05:00
|
|
|
import { Unsubscribable } from 'rxjs';
|
2019-05-10 00:09:48 -05:00
|
|
|
import { PanelModel } from 'app/features/dashboard/state';
|
2019-10-14 03:27:47 -05:00
|
|
|
import { CoreEvents } from 'app/types';
|
2016-01-25 16:51:18 -06:00
|
|
|
|
2016-01-24 17:44:21 -06:00
|
|
|
class MetricsPanelCtrl extends PanelCtrl {
|
2017-02-04 08:10:40 -06:00
|
|
|
scope: any;
|
2019-05-10 00:09:48 -05:00
|
|
|
datasource: DataSourceApi;
|
2016-01-25 16:51:18 -06:00
|
|
|
$timeout: any;
|
2019-01-21 03:50:34 -06:00
|
|
|
contextSrv: ContextSrv;
|
2016-01-25 14:58:24 -06:00
|
|
|
datasourceSrv: any;
|
2016-01-25 16:51:18 -06:00
|
|
|
timeSrv: any;
|
2016-02-29 02:42:38 -06:00
|
|
|
templateSrv: any;
|
2019-04-11 00:57:24 -05:00
|
|
|
range: TimeRange;
|
2016-01-25 16:51:18 -06:00
|
|
|
interval: any;
|
2016-09-27 07:39:51 -05:00
|
|
|
intervalMs: any;
|
2016-01-25 16:51:18 -06:00
|
|
|
resolution: any;
|
2019-04-11 00:57:24 -05:00
|
|
|
timeInfo?: string;
|
2016-01-25 16:51:18 -06:00
|
|
|
skipDataOnInit: boolean;
|
2019-04-11 00:57:24 -05:00
|
|
|
dataList: LegacyResponseData[];
|
2019-05-10 00:09:48 -05:00
|
|
|
querySubscription?: Unsubscribable;
|
2019-09-12 10:28:46 -05:00
|
|
|
useDataFrames = false;
|
2016-01-25 14:09:37 -06:00
|
|
|
|
2019-05-10 00:09:48 -05:00
|
|
|
constructor($scope: any, $injector: any) {
|
2016-01-25 14:58:24 -06:00
|
|
|
super($scope, $injector);
|
2016-01-25 16:51:18 -06:00
|
|
|
|
2018-05-30 06:13:29 -05:00
|
|
|
this.contextSrv = $injector.get('contextSrv');
|
2017-12-20 05:33:33 -06:00
|
|
|
this.datasourceSrv = $injector.get('datasourceSrv');
|
|
|
|
this.timeSrv = $injector.get('timeSrv');
|
|
|
|
this.templateSrv = $injector.get('templateSrv');
|
2017-02-04 08:10:40 -06:00
|
|
|
this.scope = $scope;
|
2017-10-03 05:18:22 -05:00
|
|
|
this.panel.datasource = this.panel.datasource || null;
|
2016-01-25 14:09:37 -06:00
|
|
|
|
2019-10-14 03:27:47 -05:00
|
|
|
this.events.on(PanelEvents.refresh, this.onMetricsPanelRefresh.bind(this));
|
|
|
|
this.events.on(PanelEvents.panelTeardown, this.onPanelTearDown.bind(this));
|
2016-12-20 09:09:04 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
private onPanelTearDown() {
|
2019-05-10 00:09:48 -05:00
|
|
|
if (this.querySubscription) {
|
|
|
|
this.querySubscription.unsubscribe();
|
|
|
|
this.querySubscription = null;
|
2016-12-20 09:09:04 -06:00
|
|
|
}
|
2016-01-24 17:44:21 -06:00
|
|
|
}
|
|
|
|
|
2016-03-23 06:50:56 -05:00
|
|
|
private onMetricsPanelRefresh() {
|
2016-01-25 16:51:18 -06:00
|
|
|
// ignore fetching data if another panel is in fullscreen
|
2017-12-19 09:06:54 -06:00
|
|
|
if (this.otherPanelInFullscreenMode()) {
|
|
|
|
return;
|
|
|
|
}
|
2016-01-25 14:09:37 -06:00
|
|
|
|
2016-01-25 16:51:18 -06:00
|
|
|
// if we have snapshot data use that
|
2016-01-25 14:09:37 -06:00
|
|
|
if (this.panel.snapshotData) {
|
2016-03-22 15:27:53 -05:00
|
|
|
this.updateTimeRange();
|
2018-08-30 02:03:11 -05:00
|
|
|
let data = this.panel.snapshotData;
|
2018-04-13 12:48:37 -05:00
|
|
|
// backward compatibility
|
2016-03-23 10:37:58 -05:00
|
|
|
if (!_.isArray(data)) {
|
2016-04-14 10:06:48 -05:00
|
|
|
data = data.data;
|
2016-03-23 10:37:58 -05:00
|
|
|
}
|
|
|
|
|
2018-03-20 13:33:54 -05:00
|
|
|
// Defer panel rendering till the next digest cycle.
|
|
|
|
// For some reason snapshot panels don't init at this time, so this helps to avoid rendering issues.
|
|
|
|
return this.$timeout(() => {
|
2019-10-14 03:27:47 -05:00
|
|
|
this.events.emit(PanelEvents.dataSnapshotLoad, data);
|
2018-03-20 13:33:54 -05:00
|
|
|
});
|
2016-01-25 14:09:37 -06:00
|
|
|
}
|
|
|
|
|
2016-01-25 16:51:18 -06:00
|
|
|
// clear loading/error state
|
2016-01-25 14:09:37 -06:00
|
|
|
delete this.error;
|
|
|
|
this.loading = true;
|
|
|
|
|
2016-01-25 16:51:18 -06:00
|
|
|
// load datasource service
|
2019-07-03 09:58:38 -05:00
|
|
|
return this.datasourceSrv
|
|
|
|
.get(this.panel.datasource, this.panel.scopedVars)
|
|
|
|
.then(this.updateTimeRange.bind(this))
|
|
|
|
.then(this.issueQueries.bind(this))
|
|
|
|
.catch((err: any) => {
|
|
|
|
this.processDataError(err);
|
|
|
|
});
|
2019-05-10 00:09:48 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
processDataError(err: any) {
|
|
|
|
// if canceled keep loading set to true
|
|
|
|
if (err.cancelled) {
|
|
|
|
console.log('Panel request cancelled', err);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.error = err.message || 'Request Error';
|
2017-12-19 09:06:54 -06:00
|
|
|
|
2019-05-10 00:09:48 -05:00
|
|
|
if (err.data) {
|
|
|
|
if (err.data.message) {
|
|
|
|
this.error = err.data.message;
|
2019-11-05 17:20:04 -06:00
|
|
|
} else if (err.data.error) {
|
2019-05-10 00:09:48 -05:00
|
|
|
this.error = err.data.error;
|
|
|
|
}
|
|
|
|
}
|
2019-12-05 01:59:07 -06:00
|
|
|
|
|
|
|
this.angularDirtyCheck();
|
|
|
|
}
|
|
|
|
|
|
|
|
angularDirtyCheck() {
|
2019-12-06 02:03:14 -06:00
|
|
|
if (!this.$scope.$root.$$phase) {
|
2019-12-05 01:59:07 -06:00
|
|
|
this.$scope.$digest();
|
|
|
|
}
|
2019-05-10 00:09:48 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Updates the response with information from the stream
|
|
|
|
panelDataObserver = {
|
|
|
|
next: (data: PanelData) => {
|
|
|
|
if (data.state === LoadingState.Error) {
|
|
|
|
this.loading = false;
|
|
|
|
this.processDataError(data.error);
|
2019-05-14 10:47:41 -05:00
|
|
|
}
|
2019-05-10 00:09:48 -05:00
|
|
|
|
2019-07-03 09:58:38 -05:00
|
|
|
// Ignore data in loading state
|
|
|
|
if (data.state === LoadingState.Loading) {
|
|
|
|
this.loading = true;
|
2019-12-05 01:59:07 -06:00
|
|
|
this.angularDirtyCheck();
|
2019-07-03 09:58:38 -05:00
|
|
|
return;
|
|
|
|
}
|
2017-04-20 10:10:09 -05:00
|
|
|
|
2019-05-14 10:47:41 -05:00
|
|
|
if (data.request) {
|
2019-09-25 04:19:17 -05:00
|
|
|
const { timeInfo } = data.request;
|
2019-05-14 10:47:41 -05:00
|
|
|
if (timeInfo) {
|
|
|
|
this.timeInfo = timeInfo;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-25 04:19:17 -05:00
|
|
|
if (data.timeRange) {
|
|
|
|
this.range = data.timeRange;
|
|
|
|
}
|
|
|
|
|
2019-09-12 10:28:46 -05:00
|
|
|
if (this.useDataFrames) {
|
2019-08-02 01:16:29 -05:00
|
|
|
this.handleDataFrames(data.series);
|
2019-09-12 10:28:46 -05:00
|
|
|
} else {
|
|
|
|
// Make the results look as if they came directly from a <6.2 datasource request
|
|
|
|
const legacy = data.series.map(v => toLegacyResponseData(v));
|
|
|
|
this.handleQueryResult({ data: legacy });
|
2019-05-14 11:43:26 -05:00
|
|
|
}
|
2019-12-05 01:59:07 -06:00
|
|
|
|
|
|
|
this.angularDirtyCheck();
|
2019-05-10 00:09:48 -05:00
|
|
|
},
|
|
|
|
};
|
2016-01-25 16:51:18 -06:00
|
|
|
|
2019-05-10 00:09:48 -05:00
|
|
|
updateTimeRange(datasource?: DataSourceApi) {
|
2017-12-19 09:06:54 -06:00
|
|
|
this.datasource = datasource || this.datasource;
|
2016-01-25 16:51:18 -06:00
|
|
|
this.range = this.timeSrv.timeRange();
|
2018-11-12 02:32:55 -06:00
|
|
|
this.resolution = getResolution(this.panel);
|
2016-01-25 16:51:18 -06:00
|
|
|
|
2018-11-13 09:01:52 -06:00
|
|
|
const newTimeData = applyPanelTimeOverrides(this.panel, this.range);
|
|
|
|
this.timeInfo = newTimeData.timeInfo;
|
|
|
|
this.range = newTimeData.timeRange;
|
|
|
|
|
2016-09-27 07:39:51 -05:00
|
|
|
this.calculateInterval();
|
2017-03-16 06:20:23 -05:00
|
|
|
|
|
|
|
return this.datasource;
|
2017-04-20 04:16:37 -05:00
|
|
|
}
|
2016-02-05 08:10:55 -06:00
|
|
|
|
2016-09-27 07:39:51 -05:00
|
|
|
calculateInterval() {
|
2018-11-13 09:01:52 -06:00
|
|
|
let intervalOverride = this.panel.interval;
|
2016-09-27 07:39:51 -05:00
|
|
|
|
2018-11-13 09:01:52 -06:00
|
|
|
// if no panel interval check datasource
|
|
|
|
if (intervalOverride) {
|
|
|
|
intervalOverride = this.templateSrv.replace(intervalOverride, this.panel.scopedVars);
|
|
|
|
} else if (this.datasource && this.datasource.interval) {
|
|
|
|
intervalOverride = this.datasource.interval;
|
|
|
|
}
|
|
|
|
|
|
|
|
const res = kbn.calculateInterval(this.range, this.resolution, intervalOverride);
|
|
|
|
this.interval = res.interval;
|
|
|
|
this.intervalMs = res.intervalMs;
|
2017-04-20 04:16:37 -05:00
|
|
|
}
|
2016-01-25 16:51:18 -06:00
|
|
|
|
2019-05-10 00:09:48 -05:00
|
|
|
issueQueries(datasource: DataSourceApi) {
|
2016-04-03 15:19:14 -05:00
|
|
|
this.datasource = datasource;
|
2016-02-02 09:32:36 -06:00
|
|
|
|
2019-05-10 00:09:48 -05:00
|
|
|
const panel = this.panel as PanelModel;
|
|
|
|
const queryRunner = panel.getQueryRunner();
|
2016-01-25 16:51:18 -06:00
|
|
|
|
2019-05-10 00:09:48 -05:00
|
|
|
if (!this.querySubscription) {
|
2019-09-12 10:28:46 -05:00
|
|
|
this.querySubscription = queryRunner.getData().subscribe(this.panelDataObserver);
|
2019-05-10 00:09:48 -05:00
|
|
|
}
|
2017-01-09 08:31:19 -06:00
|
|
|
|
2019-05-10 00:09:48 -05:00
|
|
|
return queryRunner.run({
|
|
|
|
datasource: panel.datasource,
|
|
|
|
queries: panel.targets,
|
|
|
|
panelId: panel.id,
|
2018-03-20 16:21:24 -05:00
|
|
|
dashboardId: this.dashboard.id,
|
2019-05-10 00:09:48 -05:00
|
|
|
timezone: this.dashboard.timezone,
|
|
|
|
timeRange: this.range,
|
|
|
|
widthPixels: this.resolution, // The pixel width
|
|
|
|
maxDataPoints: panel.maxDataPoints,
|
|
|
|
minInterval: panel.interval,
|
|
|
|
scopedVars: panel.scopedVars,
|
|
|
|
cacheTimeout: panel.cacheTimeout,
|
2019-09-09 01:58:57 -05:00
|
|
|
transformations: panel.transformations,
|
2019-05-10 00:09:48 -05:00
|
|
|
});
|
2016-03-22 15:27:53 -05:00
|
|
|
}
|
2016-03-16 08:19:30 -05:00
|
|
|
|
2019-08-02 01:16:29 -05:00
|
|
|
handleDataFrames(data: DataFrame[]) {
|
2019-08-29 12:04:33 -05:00
|
|
|
this.loading = false;
|
|
|
|
|
2019-05-14 11:43:26 -05:00
|
|
|
if (this.dashboard && this.dashboard.snapshot) {
|
2019-08-29 12:04:33 -05:00
|
|
|
this.panel.snapshotData = data.map(frame => toDataFrameDTO(frame));
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
2019-10-14 03:27:47 -05:00
|
|
|
this.events.emit(CoreEvents.dataFramesReceived, data);
|
2019-08-29 12:04:33 -05:00
|
|
|
} catch (err) {
|
|
|
|
this.processDataError(err);
|
2019-05-14 11:43:26 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-10 00:09:48 -05:00
|
|
|
handleQueryResult(result: DataQueryResponse) {
|
2016-03-22 15:27:53 -05:00
|
|
|
this.loading = false;
|
2016-01-25 16:51:18 -06:00
|
|
|
|
2016-03-22 15:27:53 -05:00
|
|
|
if (this.dashboard.snapshot) {
|
2016-03-23 10:37:58 -05:00
|
|
|
this.panel.snapshotData = result.data;
|
2016-02-02 15:58:37 -06:00
|
|
|
}
|
2016-01-26 17:08:08 -06:00
|
|
|
|
2016-06-07 22:45:13 -05:00
|
|
|
if (!result || !result.data) {
|
2017-12-21 01:39:31 -06:00
|
|
|
console.log('Data source query result invalid, missing data field:', result);
|
2017-12-19 09:06:54 -06:00
|
|
|
result = { data: [] };
|
2016-06-07 22:45:13 -05:00
|
|
|
}
|
|
|
|
|
2019-08-29 12:04:33 -05:00
|
|
|
try {
|
2019-10-14 03:27:47 -05:00
|
|
|
this.events.emit(PanelEvents.dataReceived, result.data);
|
2019-08-29 12:04:33 -05:00
|
|
|
} catch (err) {
|
|
|
|
this.processDataError(err);
|
|
|
|
}
|
2016-03-16 08:19:30 -05:00
|
|
|
}
|
|
|
|
|
2019-09-02 05:47:33 -05:00
|
|
|
async getAdditionalMenuItems() {
|
2018-04-29 07:02:32 -05:00
|
|
|
const items = [];
|
2019-01-21 03:50:34 -06:00
|
|
|
if (this.contextSrv.hasAccessToExplore() && this.datasource) {
|
2018-04-29 07:02:32 -05:00
|
|
|
items.push({
|
|
|
|
text: 'Explore',
|
2019-03-06 01:00:43 -06:00
|
|
|
icon: 'gicon gicon-explore',
|
2018-04-29 07:02:32 -05:00
|
|
|
shortcut: 'x',
|
2019-11-07 06:49:45 -06:00
|
|
|
href: await getExploreUrl({
|
|
|
|
panel: this.panel,
|
|
|
|
panelTargets: this.panel.targets,
|
|
|
|
panelDatasource: this.datasource,
|
|
|
|
datasourceSrv: this.datasourceSrv,
|
|
|
|
timeSrv: this.timeSrv,
|
|
|
|
}),
|
2018-04-29 07:02:32 -05:00
|
|
|
});
|
|
|
|
}
|
|
|
|
return items;
|
|
|
|
}
|
2016-01-24 17:44:21 -06:00
|
|
|
}
|
|
|
|
|
2017-12-19 09:06:54 -06:00
|
|
|
export { MetricsPanelCtrl };
|