interval_variable.ts 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. ///<reference path="../../headers/common.d.ts" />
  2. import _ from 'lodash';
  3. import kbn from 'app/core/utils/kbn';
  4. import {Variable} from './variable';
  5. import {VariableSrv, variableConstructorMap} 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. /** @ngInject */
  13. constructor(private model, private timeSrv, private templateSrv, private variableSrv) {
  14. _.extend(this, model);
  15. }
  16. setValue(option) {
  17. this.updateAutoValue();
  18. this.variableSrv.setOptionAsCurrent(this, option);
  19. }
  20. updateAutoValue() {
  21. if (!this.auto) {
  22. return;
  23. }
  24. // add auto option if missing
  25. if (this.options.length && this.options[0].text !== 'auto') {
  26. this.options.unshift({ text: 'auto', value: '$__auto_interval' });
  27. }
  28. var interval = kbn.calculateInterval(this.timeSrv.timeRange(), this.auto_count, (this.auto_min ? ">"+this.auto_min : null));
  29. this.templateSrv.setGrafanaVariable('$__auto_interval', interval);
  30. }
  31. updateOptions() {
  32. // extract options in comma separated string
  33. this.options = _.map(this.query.split(/[,]+/), function(text) {
  34. return {text: text.trim(), value: text.trim()};
  35. });
  36. if (this.auto) {
  37. this.updateAutoValue();
  38. }
  39. }
  40. dependsOn(variable) {
  41. return false;
  42. }
  43. setValueFromUrl(urlValue) {
  44. this.updateAutoValue();
  45. return this.variableSrv.setOptionFromUrl(this, urlValue);
  46. }
  47. }
  48. variableConstructorMap['interval'] = IntervalVariable;