datasource_variable.ts 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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. 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) {
  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. if (this.refresh !== 0) {
  32. this.model.options = [];
  33. }
  34. return this.model;
  35. }
  36. setValue(option) {
  37. return this.variableSrv.setOptionAsCurrent(this, option);
  38. }
  39. updateOptions() {
  40. var options = [];
  41. var sources = this.datasourceSrv.getMetricSources({skipVariables: true});
  42. var regex;
  43. if (this.regex) {
  44. regex = kbn.stringToJsRegex(this.regex);
  45. }
  46. for (var i = 0; i < sources.length; i++) {
  47. var source = sources[i];
  48. // must match on type
  49. if (source.meta.id !== this.query) {
  50. continue;
  51. }
  52. if (regex && !regex.exec(source.name)) {
  53. continue;
  54. }
  55. options.push({text: source.name, value: source.name});
  56. }
  57. if (options.length === 0) {
  58. options.push({text: 'No data sources found', value: ''});
  59. }
  60. this.options = options;
  61. return this.variableSrv.validateVariableSelectionState(this);
  62. }
  63. dependsOn(variable) {
  64. return false;
  65. }
  66. setValueFromUrl(urlValue) {
  67. return this.variableSrv.setOptionFromUrl(this, urlValue);
  68. }
  69. getValueForUrl() {
  70. return this.current.value;
  71. }
  72. }
  73. variableTypes['datasource'] = {
  74. name: 'Datasource',
  75. ctor: DatasourceVariable,
  76. description: 'Enabled you to dynamically switch the datasource for multiple panels',
  77. };