interval_variable.ts 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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. defaults = {
  14. type: 'interval',
  15. name: '',
  16. hide: 0,
  17. label: '',
  18. refresh: 2,
  19. options: [],
  20. current: {text: '', value: ''},
  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. getModel() {
  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 interval = kbn.calculateInterval(this.timeSrv.timeRange(), this.auto_count, (this.auto_min ? ">"+this.auto_min : null));
  48. this.templateSrv.setGrafanaVariable('$__auto_interval', interval);
  49. }
  50. updateOptions() {
  51. // extract options in comma separated string
  52. this.options = _.map(this.query.split(/[,]+/), function(text) {
  53. return {text: text.trim(), value: text.trim()};
  54. });
  55. this.updateAutoValue();
  56. return this.variableSrv.validateVariableSelectionState(this);
  57. }
  58. dependsOn(variable) {
  59. return false;
  60. }
  61. setValueFromUrl(urlValue) {
  62. this.updateAutoValue();
  63. return this.variableSrv.setOptionFromUrl(this, urlValue);
  64. }
  65. }
  66. variableTypes['interval'] = {
  67. name: 'Interval',
  68. ctor: IntervalVariable,
  69. description: 'Define a timespan interval (ex 1m, 1h, 1d)',
  70. };