datasource_variable.ts 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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 DatasourceVariable implements Variable {
  7. regex: any;
  8. query: string;
  9. options: any;
  10. /** @ngInject */
  11. constructor(private model, private datasourceSrv, private variableSrv) {
  12. _.extend(this, model);
  13. }
  14. setValue(option) {
  15. this.variableSrv.setOptionAsCurrent(this, option);
  16. }
  17. updateOptions() {
  18. var options = [];
  19. var sources = this.datasourceSrv.getMetricSources({skipVariables: true});
  20. var regex;
  21. if (this.regex) {
  22. regex = kbn.stringToJsRegex(this.regex);
  23. }
  24. for (var i = 0; i < sources.length; i++) {
  25. var source = sources[i];
  26. // must match on type
  27. if (source.meta.id !== this.query) {
  28. continue;
  29. }
  30. if (regex && !regex.exec(source.name)) {
  31. continue;
  32. }
  33. options.push({text: source.name, value: source.name});
  34. }
  35. if (options.length === 0) {
  36. options.push({text: 'No data sources found', value: ''});
  37. }
  38. this.options = options;
  39. }
  40. dependsOn(variable) {
  41. return false;
  42. }
  43. setValueFromUrl(urlValue) {
  44. return this.variableSrv.setOptionFromUrl(this, urlValue);
  45. }
  46. }
  47. variableConstructorMap['datasource'] = DatasourceVariable;