interval_variable.ts 2.2 KB

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