interval_variable.ts 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. ///<reference path="../../headers/common.d.ts" />
  2. import _ from 'lodash';
  3. import kbn from 'app/core/utils/kbn';
  4. import {Variable, assignModelProperties, variableTypes} from './variable';
  5. export class IntervalVariable implements Variable {
  6. name: string;
  7. auto_count: number;
  8. auto_min: number;
  9. options: any;
  10. auto: boolean;
  11. query: string;
  12. refresh: number;
  13. current: any;
  14. defaults = {
  15. type: 'interval',
  16. name: '',
  17. hide: 0,
  18. label: '',
  19. refresh: 2,
  20. options: [],
  21. current: {},
  22. query: '1m,10m,30m,1h,6h,12h,1d,7d,14d,30d',
  23. auto: false,
  24. auto_min: '10s',
  25. auto_count: 30,
  26. };
  27. /** @ngInject **/
  28. constructor(private model, private timeSrv, private templateSrv, private variableSrv) {
  29. assignModelProperties(this, model, this.defaults);
  30. this.refresh = 2;
  31. }
  32. getSaveModel() {
  33. assignModelProperties(this.model, this, this.defaults);
  34. return this.model;
  35. }
  36. setValue(option) {
  37. this.updateAutoValue();
  38. return this.variableSrv.setOptionAsCurrent(this, option);
  39. }
  40. updateAutoValue() {
  41. if (!this.auto) {
  42. return;
  43. }
  44. // add auto option if missing
  45. if (this.options.length && this.options[0].text !== 'auto') {
  46. this.options.unshift({ text: 'auto', value: '$__auto_interval_' + this.name });
  47. }
  48. var res = kbn.calculateInterval(this.timeSrv.timeRange(), this.auto_count, this.auto_min);
  49. this.templateSrv.setGrafanaVariable('$__auto_interval_' + this.name, res.interval);
  50. // for backward compatibility, to be removed eventually
  51. this.templateSrv.setGrafanaVariable('$__auto_interval', res.interval);
  52. }
  53. updateOptions() {
  54. // extract options between quotes and/or comma
  55. this.options = _.map(this.query.match(/(["'])(.*?)\1|\w+/g), function(text) {
  56. text = text.replace(/["']+/g, '');
  57. return {text: text.trim(), value: text.trim()};
  58. });
  59. this.updateAutoValue();
  60. return this.variableSrv.validateVariableSelectionState(this);
  61. }
  62. dependsOn(variable) {
  63. return false;
  64. }
  65. setValueFromUrl(urlValue) {
  66. this.updateAutoValue();
  67. return this.variableSrv.setOptionFromUrl(this, urlValue);
  68. }
  69. getValueForUrl() {
  70. return this.current.value;
  71. }
  72. }
  73. variableTypes['interval'] = {
  74. name: 'Interval',
  75. ctor: IntervalVariable,
  76. description: 'Define a timespan interval (ex 1m, 1h, 1d)',
  77. };