interval_variable.ts 2.1 KB

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