datasource_variable.ts 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. ///<reference path="../../headers/common.d.ts" />
  2. import _ from 'lodash';
  3. import kbn from 'app/core/utils/kbn';
  4. import {Variable, containsVariable, assignModelProperties, variableTypes} from './variable';
  5. import {VariableSrv} from './variable_srv';
  6. export class DatasourceVariable implements Variable {
  7. regex: any;
  8. query: string;
  9. options: any;
  10. current: any;
  11. multi: boolean;
  12. includeAll: boolean;
  13. refresh: any;
  14. defaults = {
  15. type: 'datasource',
  16. name: '',
  17. hide: 0,
  18. label: '',
  19. current: {},
  20. regex: '',
  21. options: [],
  22. query: '',
  23. multi: false,
  24. includeAll: false,
  25. refresh: 1,
  26. };
  27. /** @ngInject **/
  28. constructor(private model, private datasourceSrv, private variableSrv, private templateSrv) {
  29. assignModelProperties(this, model, this.defaults);
  30. this.refresh = 1;
  31. }
  32. getSaveModel() {
  33. assignModelProperties(this.model, this, this.defaults);
  34. // dont persist options
  35. this.model.options = [];
  36. return this.model;
  37. }
  38. setValue(option) {
  39. return this.variableSrv.setOptionAsCurrent(this, option);
  40. }
  41. updateOptions() {
  42. var options = [];
  43. var sources = this.datasourceSrv.getMetricSources({skipVariables: true});
  44. var regex;
  45. if (this.regex) {
  46. regex = this.templateSrv.replace(this.regex, null, 'regex');
  47. regex = kbn.stringToJsRegex(regex);
  48. }
  49. for (var i = 0; i < sources.length; i++) {
  50. var source = sources[i];
  51. // must match on type
  52. if (source.meta.id !== this.query) {
  53. continue;
  54. }
  55. if (regex && !regex.exec(source.name)) {
  56. continue;
  57. }
  58. options.push({text: source.name, value: source.name});
  59. }
  60. if (options.length === 0) {
  61. options.push({text: 'No data sources found', value: ''});
  62. }
  63. this.options = options;
  64. if (this.includeAll) {
  65. this.addAllOption();
  66. }
  67. return this.variableSrv.validateVariableSelectionState(this);
  68. }
  69. addAllOption() {
  70. this.options.unshift({text: 'All', value: "$__all"});
  71. }
  72. dependsOn(variable) {
  73. if (this.regex) {
  74. return containsVariable(this.regex, variable.name);
  75. }
  76. return false;
  77. }
  78. setValueFromUrl(urlValue) {
  79. return this.variableSrv.setOptionFromUrl(this, urlValue);
  80. }
  81. getValueForUrl() {
  82. if (this.current.text === 'All') {
  83. return 'All';
  84. }
  85. return this.current.value;
  86. }
  87. }
  88. variableTypes['datasource'] = {
  89. name: 'Datasource',
  90. ctor: DatasourceVariable,
  91. supportsMulti: true,
  92. description: 'Enabled you to dynamically switch the datasource for multiple panels',
  93. };