datasource_variable.ts 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. import { Variable, containsVariable, assignModelProperties, variableTypes } from './variable';
  2. import { stringToJsRegex } from '@grafana/data';
  3. import { VariableSrv } from './variable_srv';
  4. import { TemplateSrv } from './template_srv';
  5. import { DatasourceSrv } from '../plugins/datasource_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. skipUrlSync: boolean;
  15. defaults: any = {
  16. type: 'datasource',
  17. name: '',
  18. hide: 0,
  19. label: '',
  20. current: {},
  21. regex: '',
  22. options: [],
  23. query: '',
  24. multi: false,
  25. includeAll: false,
  26. refresh: 1,
  27. skipUrlSync: false,
  28. };
  29. /** @ngInject */
  30. constructor(
  31. private model: any,
  32. private datasourceSrv: DatasourceSrv,
  33. private variableSrv: VariableSrv,
  34. private templateSrv: TemplateSrv
  35. ) {
  36. assignModelProperties(this, model, this.defaults);
  37. this.refresh = 1;
  38. }
  39. getSaveModel() {
  40. assignModelProperties(this.model, this, this.defaults);
  41. // don't persist options
  42. this.model.options = [];
  43. return this.model;
  44. }
  45. setValue(option: any) {
  46. return this.variableSrv.setOptionAsCurrent(this, option);
  47. }
  48. updateOptions() {
  49. const options = [];
  50. const sources = this.datasourceSrv.getMetricSources({ skipVariables: true });
  51. let regex;
  52. if (this.regex) {
  53. regex = this.templateSrv.replace(this.regex, null, 'regex');
  54. regex = stringToJsRegex(regex);
  55. }
  56. for (let i = 0; i < sources.length; i++) {
  57. const source = sources[i];
  58. // must match on type
  59. if (source.meta.id !== this.query) {
  60. continue;
  61. }
  62. if (regex && !regex.exec(source.name)) {
  63. continue;
  64. }
  65. options.push({ text: source.name, value: source.name });
  66. }
  67. if (options.length === 0) {
  68. options.push({ text: 'No data sources found', value: '' });
  69. }
  70. this.options = options;
  71. if (this.includeAll) {
  72. this.addAllOption();
  73. }
  74. return this.variableSrv.validateVariableSelectionState(this);
  75. }
  76. addAllOption() {
  77. this.options.unshift({ text: 'All', value: '$__all' });
  78. }
  79. dependsOn(variable: any) {
  80. if (this.regex) {
  81. return containsVariable(this.regex, variable.name);
  82. }
  83. return false;
  84. }
  85. setValueFromUrl(urlValue: string | string[]) {
  86. return this.variableSrv.setOptionFromUrl(this, urlValue);
  87. }
  88. getValueForUrl() {
  89. if (this.current.text === 'All') {
  90. return 'All';
  91. }
  92. return this.current.value;
  93. }
  94. }
  95. variableTypes['datasource'] = {
  96. name: 'Datasource',
  97. ctor: DatasourceVariable,
  98. supportsMulti: true,
  99. description: 'Enabled you to dynamically switch the datasource for multiple panels',
  100. };