datasource_variable.ts 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. ///<reference path="../../headers/common.d.ts" />
  2. import _ from 'lodash';
  3. import kbn from 'app/core/utils/kbn';
  4. import {Variable, assignModelProperties} 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. defaults = {
  11. type: 'datasource',
  12. name: '',
  13. hide: 0,
  14. label: '',
  15. current: {text: '', value: ''}
  16. regex: '',
  17. options: [],
  18. query: '',
  19. };
  20. /** @ngInject */
  21. constructor(private model, private datasourceSrv, private variableSrv) {
  22. assignModelProperties(this, model, this.defaults);
  23. }
  24. getModel() {
  25. assignModelProperties(this.model, this, this.defaults);
  26. return this.model;
  27. }
  28. setValue(option) {
  29. this.variableSrv.setOptionAsCurrent(this, option);
  30. }
  31. updateOptions() {
  32. var options = [];
  33. var sources = this.datasourceSrv.getMetricSources({skipVariables: true});
  34. var regex;
  35. if (this.regex) {
  36. regex = kbn.stringToJsRegex(this.regex);
  37. }
  38. for (var i = 0; i < sources.length; i++) {
  39. var source = sources[i];
  40. // must match on type
  41. if (source.meta.id !== this.query) {
  42. continue;
  43. }
  44. if (regex && !regex.exec(source.name)) {
  45. continue;
  46. }
  47. options.push({text: source.name, value: source.name});
  48. }
  49. if (options.length === 0) {
  50. options.push({text: 'No data sources found', value: ''});
  51. }
  52. this.options = options;
  53. }
  54. dependsOn(variable) {
  55. return false;
  56. }
  57. setValueFromUrl(urlValue) {
  58. return this.variableSrv.setOptionFromUrl(this, urlValue);
  59. }
  60. }
  61. variableConstructorMap['datasource'] = DatasourceVariable;