datasource_variable.ts 2.2 KB

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