interval_variable.ts 2.6 KB

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