interval_variable.ts 2.3 KB

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