2017-12-20 05:33:33 -06:00
|
|
|
import _ from 'lodash';
|
|
|
|
import kbn from 'app/core/utils/kbn';
|
2019-10-31 04:59:30 -05:00
|
|
|
import {
|
|
|
|
assignModelProperties,
|
|
|
|
IntervalVariableModel,
|
|
|
|
VariableActions,
|
|
|
|
VariableHide,
|
|
|
|
VariableOption,
|
|
|
|
VariableRefresh,
|
|
|
|
VariableType,
|
|
|
|
variableTypes,
|
|
|
|
} from './variable';
|
2019-07-16 04:35:42 -05:00
|
|
|
import { TimeSrv } from '../dashboard/services/TimeSrv';
|
|
|
|
import { TemplateSrv } from './template_srv';
|
|
|
|
import { VariableSrv } from './variable_srv';
|
2016-09-16 03:48:03 -05:00
|
|
|
|
2019-10-31 04:59:30 -05:00
|
|
|
export class IntervalVariable implements IntervalVariableModel, VariableActions {
|
|
|
|
type: VariableType;
|
2017-10-24 07:39:10 -05:00
|
|
|
name: string;
|
2019-10-31 04:59:30 -05:00
|
|
|
label: string;
|
|
|
|
hide: VariableHide;
|
|
|
|
skipUrlSync: boolean;
|
2018-09-03 04:00:46 -05:00
|
|
|
auto_count: number; // tslint:disable-line variable-name
|
2019-10-31 04:59:30 -05:00
|
|
|
auto_min: string; // tslint:disable-line variable-name
|
|
|
|
options: VariableOption[];
|
2016-09-16 03:48:03 -05:00
|
|
|
auto: boolean;
|
|
|
|
query: string;
|
2019-10-31 04:59:30 -05:00
|
|
|
refresh: VariableRefresh;
|
|
|
|
current: VariableOption;
|
2016-09-16 03:48:03 -05:00
|
|
|
|
2019-10-31 04:59:30 -05:00
|
|
|
defaults: IntervalVariableModel = {
|
2017-12-20 05:33:33 -06:00
|
|
|
type: 'interval',
|
|
|
|
name: '',
|
|
|
|
label: '',
|
2019-10-31 04:59:30 -05:00
|
|
|
hide: VariableHide.dontHide,
|
|
|
|
skipUrlSync: false,
|
|
|
|
auto_count: 30,
|
|
|
|
auto_min: '10s',
|
2016-09-19 10:03:29 -05:00
|
|
|
options: [],
|
|
|
|
auto: false,
|
2019-10-31 04:59:30 -05:00
|
|
|
query: '1m,10m,30m,1h,6h,12h,1d,7d,14d,30d',
|
|
|
|
refresh: VariableRefresh.onTimeRangeChanged,
|
|
|
|
current: {} as VariableOption,
|
2016-09-19 10:03:29 -05:00
|
|
|
};
|
|
|
|
|
2018-08-31 09:40:43 -05:00
|
|
|
/** @ngInject */
|
2019-07-16 04:35:42 -05:00
|
|
|
constructor(
|
|
|
|
private model: any,
|
|
|
|
private timeSrv: TimeSrv,
|
|
|
|
private templateSrv: TemplateSrv,
|
|
|
|
private variableSrv: VariableSrv
|
|
|
|
) {
|
2016-09-19 10:03:29 -05:00
|
|
|
assignModelProperties(this, model, this.defaults);
|
2019-10-31 04:59:30 -05:00
|
|
|
this.refresh = VariableRefresh.onTimeRangeChanged;
|
2016-09-19 10:03:29 -05:00
|
|
|
}
|
|
|
|
|
2016-11-17 04:28:33 -06:00
|
|
|
getSaveModel() {
|
2016-09-19 10:03:29 -05:00
|
|
|
assignModelProperties(this.model, this, this.defaults);
|
|
|
|
return this.model;
|
2016-09-16 03:48:03 -05:00
|
|
|
}
|
|
|
|
|
2019-07-16 04:35:42 -05:00
|
|
|
setValue(option: any) {
|
2016-09-17 04:28:45 -05:00
|
|
|
this.updateAutoValue();
|
2016-09-19 11:06:36 -05:00
|
|
|
return this.variableSrv.setOptionAsCurrent(this, option);
|
2016-09-16 03:48:03 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
updateAutoValue() {
|
2016-09-17 04:28:45 -05:00
|
|
|
if (!this.auto) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-09-16 03:48:03 -05:00
|
|
|
// add auto option if missing
|
2017-12-20 05:33:33 -06:00
|
|
|
if (this.options.length && this.options[0].text !== 'auto') {
|
2017-12-19 09:06:54 -06:00
|
|
|
this.options.unshift({
|
2017-12-20 05:33:33 -06:00
|
|
|
text: 'auto',
|
|
|
|
value: '$__auto_interval_' + this.name,
|
2019-10-31 04:59:30 -05:00
|
|
|
selected: false,
|
2017-12-19 09:06:54 -06:00
|
|
|
});
|
2016-09-16 03:48:03 -05:00
|
|
|
}
|
|
|
|
|
2018-08-26 14:52:57 -05:00
|
|
|
const res = kbn.calculateInterval(this.timeSrv.timeRange(), this.auto_count, this.auto_min);
|
2017-12-21 01:39:31 -06:00
|
|
|
this.templateSrv.setGrafanaVariable('$__auto_interval_' + this.name, res.interval);
|
2017-10-24 07:39:10 -05:00
|
|
|
// for backward compatibility, to be removed eventually
|
2017-12-20 05:33:33 -06:00
|
|
|
this.templateSrv.setGrafanaVariable('$__auto_interval', res.interval);
|
2016-09-16 03:48:03 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
updateOptions() {
|
2017-03-06 01:37:49 -06:00
|
|
|
// extract options between quotes and/or comma
|
2018-09-05 00:47:30 -05:00
|
|
|
this.options = _.map(this.query.match(/(["'])(.*?)\1|\w+/g), text => {
|
2017-12-20 05:33:33 -06:00
|
|
|
text = text.replace(/["']+/g, '');
|
2019-10-31 04:59:30 -05:00
|
|
|
return { text: text.trim(), value: text.trim(), selected: false };
|
2016-09-16 03:48:03 -05:00
|
|
|
});
|
|
|
|
|
2016-09-19 04:34:08 -05:00
|
|
|
this.updateAutoValue();
|
2016-09-19 11:06:36 -05:00
|
|
|
return this.variableSrv.validateVariableSelectionState(this);
|
2016-09-16 03:48:03 -05:00
|
|
|
}
|
2016-09-16 09:50:30 -05:00
|
|
|
|
2019-07-16 04:35:42 -05:00
|
|
|
dependsOn(variable: any) {
|
2016-09-16 09:50:30 -05:00
|
|
|
return false;
|
|
|
|
}
|
2016-09-17 04:28:45 -05:00
|
|
|
|
2019-07-16 04:35:42 -05:00
|
|
|
setValueFromUrl(urlValue: string | string[]) {
|
2016-09-17 04:28:45 -05:00
|
|
|
this.updateAutoValue();
|
|
|
|
return this.variableSrv.setOptionFromUrl(this, urlValue);
|
|
|
|
}
|
2016-09-20 04:57:43 -05:00
|
|
|
|
|
|
|
getValueForUrl() {
|
|
|
|
return this.current.value;
|
|
|
|
}
|
2016-09-16 03:48:03 -05:00
|
|
|
}
|
|
|
|
|
2019-07-16 04:35:42 -05:00
|
|
|
// @ts-ignore
|
2017-12-20 05:33:33 -06:00
|
|
|
variableTypes['interval'] = {
|
|
|
|
name: 'Interval',
|
2016-09-19 11:32:09 -05:00
|
|
|
ctor: IntervalVariable,
|
2017-12-20 05:33:33 -06:00
|
|
|
description: 'Define a timespan interval (ex 1m, 1h, 1d)',
|
2016-09-19 11:32:09 -05:00
|
|
|
};
|