datasource_srv.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. define([
  2. 'angular',
  3. 'lodash',
  4. '../core_module',
  5. 'app/core/config',
  6. ],
  7. function (angular, _, coreModule, config) {
  8. 'use strict';
  9. coreModule.default.service('datasourceSrv', function($q, $injector, $rootScope, templateSrv) {
  10. var self = this;
  11. this.init = function() {
  12. this.datasources = {};
  13. };
  14. this.get = function(name) {
  15. if (!name) {
  16. return this.get(config.defaultDatasource);
  17. }
  18. name = templateSrv.replace(name);
  19. if (this.datasources[name]) {
  20. return $q.when(this.datasources[name]);
  21. }
  22. return this.loadDatasource(name);
  23. };
  24. this.loadDatasource = function(name) {
  25. var dsConfig = config.datasources[name];
  26. if (!dsConfig) {
  27. return $q.reject({message: "Datasource named " + name + " was not found"});
  28. }
  29. var deferred = $q.defer();
  30. var pluginDef = dsConfig.meta;
  31. System.import(pluginDef.module).then(function(plugin) {
  32. // check if its in cache now
  33. if (self.datasources[name]) {
  34. deferred.resolve(self.datasources[name]);
  35. return;
  36. }
  37. // plugin module needs to export a constructor function named Datasource
  38. if (!plugin.Datasource) {
  39. throw "Plugin module is missing Datasource constructor";
  40. }
  41. var instance = $injector.instantiate(plugin.Datasource, {instanceSettings: dsConfig});
  42. instance.meta = pluginDef;
  43. instance.name = name;
  44. self.datasources[name] = instance;
  45. deferred.resolve(instance);
  46. }).catch(function(err) {
  47. $rootScope.appEvent('alert-error', [dsConfig.name + ' plugin failed', err.toString()]);
  48. });
  49. return deferred.promise;
  50. };
  51. this.getAll = function() {
  52. return config.datasources;
  53. };
  54. this.getAnnotationSources = function() {
  55. var sources = [];
  56. this.addDataSourceVariables(sources);
  57. _.each(config.datasources, function(value) {
  58. if (value.meta && value.meta.annotations) {
  59. sources.push(value);
  60. }
  61. });
  62. return sources;
  63. };
  64. this.getMetricSources = function(options) {
  65. var metricSources = [];
  66. _.each(config.datasources, function(value, key) {
  67. if (value.meta && value.meta.metrics) {
  68. metricSources.push({value: key, name: key, meta: value.meta});
  69. if (key === config.defaultDatasource) {
  70. metricSources.push({value: null, name: 'default', meta: value.meta});
  71. }
  72. }
  73. });
  74. if (!options || !options.skipVariables) {
  75. this.addDataSourceVariables(metricSources);
  76. }
  77. metricSources.sort(function(a, b) {
  78. if (a.meta.builtIn || a.name > b.name) {
  79. return 1;
  80. }
  81. if (a.name < b.name) {
  82. return -1;
  83. }
  84. return 0;
  85. });
  86. return metricSources;
  87. };
  88. this.addDataSourceVariables = function(list) {
  89. // look for data source variables
  90. for (var i = 0; i < templateSrv.variables.length; i++) {
  91. var variable = templateSrv.variables[i];
  92. if (variable.type !== 'datasource') {
  93. continue;
  94. }
  95. var first = variable.current.value;
  96. var ds = config.datasources[first];
  97. if (ds) {
  98. list.push({
  99. name: '$' + variable.name,
  100. value: '$' + variable.name,
  101. meta: ds.meta,
  102. });
  103. }
  104. }
  105. };
  106. this.init();
  107. });
  108. });