Files
grafana/public/app/features/panel/metrics_panel_ctrl.ts

333 lines
9.3 KiB
TypeScript
Raw Normal View History

2017-12-20 12:33:33 +01:00
import config from 'app/core/config';
import $ from 'jquery';
import _ from 'lodash';
import kbn from 'app/core/utils/kbn';
import { PanelCtrl } from 'app/features/panel/panel_ctrl';
2016-01-24 18:44:21 -05:00
2017-12-20 12:33:33 +01:00
import * as rangeUtil from 'app/core/utils/rangeutil';
import * as dateMath from 'app/core/utils/datemath';
2017-12-20 12:33:33 +01:00
import { metricsTabDirective } from './metrics_tab';
2016-01-24 18:44:21 -05:00
class MetricsPanelCtrl extends PanelCtrl {
2017-02-04 14:10:40 +00:00
scope: any;
datasource: any;
datasourceName: any;
2016-01-25 15:58:24 -05:00
$q: any;
$timeout: any;
2016-01-25 15:58:24 -05:00
datasourceSrv: any;
timeSrv: any;
2016-02-29 17:42:38 +09:00
templateSrv: any;
timing: any;
range: any;
interval: any;
2016-09-27 14:39:51 +02:00
intervalMs: any;
resolution: any;
timeInfo: any;
skipDataOnInit: boolean;
dataStream: any;
dataSubscription: any;
dataList: any;
nextRefId: string;
2016-01-25 15:58:24 -05:00
constructor($scope, $injector) {
super($scope, $injector);
// make metrics tab the default
this.editorTabIndex = 1;
2017-12-20 12:33:33 +01:00
this.$q = $injector.get('$q');
this.datasourceSrv = $injector.get('datasourceSrv');
this.timeSrv = $injector.get('timeSrv');
this.templateSrv = $injector.get('templateSrv');
2017-02-04 14:10:40 +00:00
this.scope = $scope;
this.panel.datasource = this.panel.datasource || null;
if (!this.panel.targets) {
this.panel.targets = [{}];
}
2017-12-20 12:33:33 +01:00
this.events.on('refresh', this.onMetricsPanelRefresh.bind(this));
this.events.on('init-edit-mode', this.onInitMetricsPanelEditMode.bind(this));
2017-12-20 12:33:33 +01:00
this.events.on('panel-teardown', this.onPanelTearDown.bind(this));
}
private onPanelTearDown() {
if (this.dataSubscription) {
this.dataSubscription.unsubscribe();
this.dataSubscription = null;
}
2016-01-24 18:44:21 -05:00
}
private onInitMetricsPanelEditMode() {
2017-12-20 12:33:33 +01:00
this.addEditorTab('Metrics', metricsTabDirective);
this.addEditorTab('Time range', 'public/app/features/panel/partials/panelTime.html');
}
private onMetricsPanelRefresh() {
// ignore fetching data if another panel is in fullscreen
if (this.otherPanelInFullscreenMode()) {
return;
}
// if we have snapshot data use that
if (this.panel.snapshotData) {
2016-03-22 21:27:53 +01:00
this.updateTimeRange();
var data = this.panel.snapshotData;
// backward compatability
if (!_.isArray(data)) {
data = data.data;
}
2018-03-20 21:33:54 +03: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(() => {
this.events.emit('data-snapshot-load', data);
});
}
// // ignore if we have data stream
if (this.dataStream) {
return;
}
// clear loading/error state
delete this.error;
this.loading = true;
// load datasource service
this.setTimeQueryStart();
this.datasourceSrv
.get(this.panel.datasource)
.then(this.updateTimeRange.bind(this))
.then(this.issueQueries.bind(this))
.then(this.handleQueryResult.bind(this))
.catch(err => {
// if cancelled keep loading set to true
if (err.cancelled) {
2017-12-20 12:33:33 +01:00
console.log('Panel request cancelled', err);
return;
2017-04-20 17:10:09 +02:00
}
this.loading = false;
2017-12-20 12:33:33 +01:00
this.error = err.message || 'Request Error';
this.inspector = { error: err };
if (err.data) {
if (err.data.message) {
this.error = err.data.message;
}
if (err.data.error) {
this.error = err.data.error;
}
2017-04-20 17:10:09 +02:00
}
2017-12-20 12:33:33 +01:00
this.events.emit('data-error', err);
console.log('Panel data error:', err);
});
2016-01-24 18:44:21 -05:00
}
setTimeQueryStart() {
this.timing.queryStart = new Date().getTime();
}
setTimeQueryEnd() {
this.timing.queryEnd = new Date().getTime();
}
updateTimeRange(datasource?) {
this.datasource = datasource || this.datasource;
this.range = this.timeSrv.timeRange();
this.applyPanelTimeOverrides();
if (this.panel.maxDataPoints) {
this.resolution = this.panel.maxDataPoints;
} else {
this.resolution = Math.ceil($(window).width() * (this.panel.gridPos.w / 24));
}
2016-09-27 14:39:51 +02:00
this.calculateInterval();
return this.datasource;
}
2016-02-05 15:10:55 +01:00
2016-09-27 14:39:51 +02:00
calculateInterval() {
var intervalOverride = this.panel.interval;
// if no panel interval check datasource
if (intervalOverride) {
intervalOverride = this.templateSrv.replace(intervalOverride, this.panel.scopedVars);
} else if (this.datasource && this.datasource.interval) {
2016-09-27 14:39:51 +02:00
intervalOverride = this.datasource.interval;
}
var res = kbn.calculateInterval(this.range, this.resolution, intervalOverride);
2016-09-27 14:39:51 +02:00
this.interval = res.interval;
this.intervalMs = res.intervalMs;
}
2016-02-05 15:10:55 +01:00
applyPanelTimeOverrides() {
2017-12-20 12:33:33 +01:00
this.timeInfo = '';
2016-02-05 15:10:55 +01:00
// check panel time overrrides
if (this.panel.timeFrom) {
var timeFromInterpolated = this.templateSrv.replace(this.panel.timeFrom, this.panel.scopedVars);
2016-02-29 17:42:38 +09:00
var timeFromInfo = rangeUtil.describeTextRange(timeFromInterpolated);
2016-02-05 15:10:55 +01:00
if (timeFromInfo.invalid) {
2017-12-20 12:33:33 +01:00
this.timeInfo = 'invalid time override';
2016-02-05 15:10:55 +01:00
return;
}
if (_.isString(this.range.raw.from)) {
2016-02-05 15:10:55 +01:00
var timeFromDate = dateMath.parse(timeFromInfo.from);
this.timeInfo = timeFromInfo.display;
this.range.from = timeFromDate;
this.range.to = dateMath.parse(timeFromInfo.to);
this.range.raw.from = timeFromInfo.from;
this.range.raw.to = timeFromInfo.to;
}
2016-02-05 15:10:55 +01:00
}
2016-02-05 15:10:55 +01:00
if (this.panel.timeShift) {
var timeShiftInterpolated = this.templateSrv.replace(this.panel.timeShift, this.panel.scopedVars);
2016-02-29 17:42:38 +09:00
var timeShiftInfo = rangeUtil.describeTextRange(timeShiftInterpolated);
2016-02-05 15:10:55 +01:00
if (timeShiftInfo.invalid) {
2017-12-20 12:33:33 +01:00
this.timeInfo = 'invalid timeshift';
2016-02-05 15:10:55 +01:00
return;
}
2017-12-20 12:33:33 +01:00
var timeShift = '-' + timeShiftInterpolated;
this.timeInfo += ' timeshift ' + timeShift;
this.range.from = dateMath.parseDateMath(timeShift, this.range.from, false);
2016-02-05 15:10:55 +01:00
this.range.to = dateMath.parseDateMath(timeShift, this.range.to, true);
this.range.raw = { from: this.range.from, to: this.range.to };
2016-02-05 15:10:55 +01:00
}
2016-02-05 15:10:55 +01:00
if (this.panel.hideTimeOverride) {
2017-12-20 12:33:33 +01:00
this.timeInfo = '';
2016-02-05 15:10:55 +01:00
}
}
2016-01-27 12:51:01 -05:00
issueQueries(datasource) {
this.datasource = datasource;
if (!this.panel.targets || this.panel.targets.length === 0) {
return this.$q.when([]);
}
// make shallow copy of scoped vars,
// and add built in variables interval and interval_ms
var scopedVars = Object.assign({}, this.panel.scopedVars, {
__interval: { text: this.interval, value: this.interval },
2017-12-20 12:33:33 +01:00
__interval_ms: { text: this.intervalMs, value: this.intervalMs },
});
var metricsQuery = {
timezone: this.dashboard.getTimezone(),
panelId: this.panel.id,
dashboardId: this.dashboard.id,
range: this.range,
rangeRaw: this.range.raw,
interval: this.interval,
2016-09-27 14:39:51 +02:00
intervalMs: this.intervalMs,
targets: this.panel.targets,
maxDataPoints: this.resolution,
scopedVars: scopedVars,
2017-12-20 12:33:33 +01:00
cacheTimeout: this.panel.cacheTimeout,
};
2016-03-22 21:27:53 +01:00
return datasource.query(metricsQuery);
}
2016-03-22 21:27:53 +01:00
handleQueryResult(result) {
this.setTimeQueryEnd();
this.loading = false;
2016-03-22 21:27:53 +01:00
// check for if data source returns subject
if (result && result.subscribe) {
this.handleDataStream(result);
return;
}
if (this.dashboard.snapshot) {
this.panel.snapshotData = result.data;
}
2016-01-26 18:08:08 -05:00
if (!result || !result.data) {
console.log('Data source query result invalid, missing data field:', result);
result = { data: [] };
}
2017-12-20 12:33:33 +01:00
this.events.emit('data-received', result.data);
}
handleDataStream(stream) {
// if we already have a connection
if (this.dataStream) {
2017-12-20 12:33:33 +01:00
console.log('two stream observables!');
return;
}
this.dataStream = stream;
this.dataSubscription = stream.subscribe({
next: data => {
2017-12-20 12:33:33 +01:00
console.log('dataSubject next!');
if (data.range) {
this.range = data.range;
}
2017-12-20 12:33:33 +01:00
this.events.emit('data-received', data.data);
},
error: error => {
2017-12-20 12:33:33 +01:00
this.events.emit('data-error', error);
console.log('panel: observer got error');
},
complete: () => {
2017-12-20 12:33:33 +01:00
console.log('panel: observer got complete');
this.dataStream = null;
2017-12-20 12:33:33 +01:00
},
});
}
2016-01-26 18:08:08 -05:00
setDatasource(datasource) {
// switching to mixed
if (datasource.meta.mixed) {
_.each(this.panel.targets, target => {
target.datasource = this.panel.datasource;
if (!target.datasource) {
2016-01-26 18:08:08 -05:00
target.datasource = config.defaultDatasource;
}
});
} else if (this.datasource && this.datasource.meta.mixed) {
_.each(this.panel.targets, target => {
delete target.datasource;
});
}
this.panel.datasource = datasource.value;
this.datasourceName = datasource.name;
2016-01-26 18:08:08 -05:00
this.datasource = null;
this.refresh();
}
addQuery(target) {
target.refId = this.dashboard.getNextQueryLetter(this.panel);
this.panel.targets.push(target);
this.nextRefId = this.dashboard.getNextQueryLetter(this.panel);
}
removeQuery(target) {
var index = _.indexOf(this.panel.targets, target);
this.panel.targets.splice(index, 1);
this.nextRefId = this.dashboard.getNextQueryLetter(this.panel);
this.refresh();
}
moveQuery(target, direction) {
var index = _.indexOf(this.panel.targets, target);
_.move(this.panel.targets, index, index + direction);
}
2016-01-24 18:44:21 -05:00
}
export { MetricsPanelCtrl };