datasource_variable.ts 2.1 KB

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