2017-12-20 12:33:33 +01:00
|
|
|
import _ from 'lodash';
|
|
|
|
|
import kbn from 'app/core/utils/kbn';
|
|
|
|
|
import { Variable, assignModelProperties, variableTypes } from './variable';
|
2016-09-16 10:48:03 +02:00
|
|
|
|
|
|
|
|
export class IntervalVariable implements Variable {
|
2017-10-24 14:39:10 +02:00
|
|
|
name: string;
|
2016-09-16 10:48:03 +02:00
|
|
|
auto_count: number;
|
|
|
|
|
auto_min: number;
|
|
|
|
|
options: any;
|
|
|
|
|
auto: boolean;
|
|
|
|
|
query: string;
|
2016-09-19 18:06:36 +02:00
|
|
|
refresh: number;
|
2016-09-20 11:57:43 +02:00
|
|
|
current: any;
|
2016-09-16 10:48:03 +02:00
|
|
|
|
2016-09-19 17:03:29 +02:00
|
|
|
defaults = {
|
2017-12-20 12:33:33 +01:00
|
|
|
type: 'interval',
|
|
|
|
|
name: '',
|
2016-09-19 17:03:29 +02:00
|
|
|
hide: 0,
|
2017-12-20 12:33:33 +01:00
|
|
|
label: '',
|
2016-09-19 18:06:36 +02:00
|
|
|
refresh: 2,
|
2016-09-19 17:03:29 +02:00
|
|
|
options: [],
|
2016-09-20 11:57:43 +02:00
|
|
|
current: {},
|
2017-12-20 12:33:33 +01:00
|
|
|
query: '1m,10m,30m,1h,6h,12h,1d,7d,14d,30d',
|
2016-09-19 17:03:29 +02:00
|
|
|
auto: false,
|
2017-12-20 12:33:33 +01:00
|
|
|
auto_min: '10s',
|
|
|
|
|
auto_count: 30,
|
2016-09-19 17:03:29 +02:00
|
|
|
};
|
|
|
|
|
|
2016-09-22 10:22:16 +02:00
|
|
|
/** @ngInject **/
|
2017-12-21 08:39:31 +01:00
|
|
|
constructor(private model, private timeSrv, private templateSrv, private variableSrv) {
|
2016-09-19 17:03:29 +02:00
|
|
|
assignModelProperties(this, model, this.defaults);
|
2016-09-19 18:06:36 +02:00
|
|
|
this.refresh = 2;
|
2016-09-19 17:03:29 +02:00
|
|
|
}
|
|
|
|
|
|
2016-11-17 11:28:33 +01:00
|
|
|
getSaveModel() {
|
2016-09-19 17:03:29 +02:00
|
|
|
assignModelProperties(this.model, this, this.defaults);
|
|
|
|
|
return this.model;
|
2016-09-16 10:48:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
setValue(option) {
|
2016-09-17 11:28:45 +02:00
|
|
|
this.updateAutoValue();
|
2016-09-19 18:06:36 +02:00
|
|
|
return this.variableSrv.setOptionAsCurrent(this, option);
|
2016-09-16 10:48:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
updateAutoValue() {
|
2016-09-17 11:28:45 +02:00
|
|
|
if (!this.auto) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2016-09-16 10:48:03 +02:00
|
|
|
// add auto option if missing
|
2017-12-20 12:33:33 +01:00
|
|
|
if (this.options.length && this.options[0].text !== 'auto') {
|
2017-12-19 16:06:54 +01:00
|
|
|
this.options.unshift({
|
2017-12-20 12:33:33 +01:00
|
|
|
text: 'auto',
|
|
|
|
|
value: '$__auto_interval_' + this.name,
|
2017-12-19 16:06:54 +01:00
|
|
|
});
|
2016-09-16 10:48:03 +02:00
|
|
|
}
|
|
|
|
|
|
2017-12-21 08:39:31 +01:00
|
|
|
var res = kbn.calculateInterval(this.timeSrv.timeRange(), this.auto_count, this.auto_min);
|
|
|
|
|
this.templateSrv.setGrafanaVariable('$__auto_interval_' + this.name, res.interval);
|
2017-10-24 14:39:10 +02:00
|
|
|
// for backward compatibility, to be removed eventually
|
2017-12-20 12:33:33 +01:00
|
|
|
this.templateSrv.setGrafanaVariable('$__auto_interval', res.interval);
|
2016-09-16 10:48:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
updateOptions() {
|
2017-03-06 08:37:49 +01:00
|
|
|
// extract options between quotes and/or comma
|
2017-12-21 08:39:31 +01:00
|
|
|
this.options = _.map(this.query.match(/(["'])(.*?)\1|\w+/g), function(text) {
|
2017-12-20 12:33:33 +01:00
|
|
|
text = text.replace(/["']+/g, '');
|
2017-12-19 16:06:54 +01:00
|
|
|
return { text: text.trim(), value: text.trim() };
|
2016-09-16 10:48:03 +02:00
|
|
|
});
|
|
|
|
|
|
2016-09-19 11:34:08 +02:00
|
|
|
this.updateAutoValue();
|
2016-09-19 18:06:36 +02:00
|
|
|
return this.variableSrv.validateVariableSelectionState(this);
|
2016-09-16 10:48:03 +02:00
|
|
|
}
|
2016-09-16 16:50:30 +02:00
|
|
|
|
2016-09-17 11:28:45 +02:00
|
|
|
dependsOn(variable) {
|
2016-09-16 16:50:30 +02:00
|
|
|
return false;
|
|
|
|
|
}
|
2016-09-17 11:28:45 +02:00
|
|
|
|
|
|
|
|
setValueFromUrl(urlValue) {
|
|
|
|
|
this.updateAutoValue();
|
|
|
|
|
return this.variableSrv.setOptionFromUrl(this, urlValue);
|
|
|
|
|
}
|
2016-09-20 11:57:43 +02:00
|
|
|
|
|
|
|
|
getValueForUrl() {
|
|
|
|
|
return this.current.value;
|
|
|
|
|
}
|
2016-09-16 10:48:03 +02:00
|
|
|
}
|
|
|
|
|
|
2017-12-20 12:33:33 +01:00
|
|
|
variableTypes['interval'] = {
|
|
|
|
|
name: 'Interval',
|
2016-09-19 18:32:09 +02:00
|
|
|
ctor: IntervalVariable,
|
2017-12-20 12:33:33 +01:00
|
|
|
description: 'Define a timespan interval (ex 1m, 1h, 1d)',
|
2016-09-19 18:32:09 +02:00
|
|
|
};
|