interval_variable.ts 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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({
  47. text: 'auto',
  48. value: '$__auto_interval_' + this.name,
  49. });
  50. }
  51. var res = kbn.calculateInterval(this.timeSrv.timeRange(), this.auto_count, this.auto_min);
  52. this.templateSrv.setGrafanaVariable('$__auto_interval_' + this.name, res.interval);
  53. // for backward compatibility, to be removed eventually
  54. this.templateSrv.setGrafanaVariable('$__auto_interval', res.interval);
  55. }
  56. updateOptions() {
  57. // extract options between quotes and/or comma
  58. this.options = _.map(this.query.match(/(["'])(.*?)\1|\w+/g), function(text) {
  59. text = text.replace(/["']+/g, '');
  60. return { text: text.trim(), value: text.trim() };
  61. });
  62. this.updateAutoValue();
  63. return this.variableSrv.validateVariableSelectionState(this);
  64. }
  65. dependsOn(variable) {
  66. return false;
  67. }
  68. setValueFromUrl(urlValue) {
  69. this.updateAutoValue();
  70. return this.variableSrv.setOptionFromUrl(this, urlValue);
  71. }
  72. getValueForUrl() {
  73. return this.current.value;
  74. }
  75. }
  76. variableTypes['interval'] = {
  77. name: 'Interval',
  78. ctor: IntervalVariable,
  79. description: 'Define a timespan interval (ex 1m, 1h, 1d)',
  80. };