datasource_variable.ts 2.0 KB

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