interval_variable.ts 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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(
  29. private model,
  30. private timeSrv,
  31. private templateSrv,
  32. private variableSrv
  33. ) {
  34. assignModelProperties(this, model, this.defaults);
  35. this.refresh = 2;
  36. }
  37. getSaveModel() {
  38. assignModelProperties(this.model, this, this.defaults);
  39. return this.model;
  40. }
  41. setValue(option) {
  42. this.updateAutoValue();
  43. return this.variableSrv.setOptionAsCurrent(this, option);
  44. }
  45. updateAutoValue() {
  46. if (!this.auto) {
  47. return;
  48. }
  49. // add auto option if missing
  50. if (this.options.length && this.options[0].text !== 'auto') {
  51. this.options.unshift({
  52. text: 'auto',
  53. value: '$__auto_interval_' + this.name,
  54. });
  55. }
  56. var res = kbn.calculateInterval(
  57. this.timeSrv.timeRange(),
  58. this.auto_count,
  59. this.auto_min
  60. );
  61. this.templateSrv.setGrafanaVariable(
  62. '$__auto_interval_' + this.name,
  63. res.interval
  64. );
  65. // for backward compatibility, to be removed eventually
  66. this.templateSrv.setGrafanaVariable('$__auto_interval', res.interval);
  67. }
  68. updateOptions() {
  69. // extract options between quotes and/or comma
  70. this.options = _.map(this.query.match(/(["'])(.*?)\1|\w+/g), function(
  71. text
  72. ) {
  73. text = text.replace(/["']+/g, '');
  74. return { text: text.trim(), value: text.trim() };
  75. });
  76. this.updateAutoValue();
  77. return this.variableSrv.validateVariableSelectionState(this);
  78. }
  79. dependsOn(variable) {
  80. return false;
  81. }
  82. setValueFromUrl(urlValue) {
  83. this.updateAutoValue();
  84. return this.variableSrv.setOptionFromUrl(this, urlValue);
  85. }
  86. getValueForUrl() {
  87. return this.current.value;
  88. }
  89. }
  90. variableTypes['interval'] = {
  91. name: 'Interval',
  92. ctor: IntervalVariable,
  93. description: 'Define a timespan interval (ex 1m, 1h, 1d)',
  94. };