datasource_variable.ts 1.9 KB

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