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. import {VariableSrv} from './variable_srv';
  6. export class IntervalVariable implements Variable {
  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. getModel() {
  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({ text: 'auto', value: '$__auto_interval' });
  47. }
  48. var interval = kbn.calculateInterval(this.timeSrv.timeRange(), this.auto_count, (this.auto_min ? ">"+this.auto_min : null));
  49. this.templateSrv.setGrafanaVariable('$__auto_interval', interval);
  50. }
  51. updateOptions() {
  52. // extract options in comma separated string
  53. this.options = _.map(this.query.split(/[,]+/), function(text) {
  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. };