datasource_variable.ts 2.2 KB

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