datasource_srv.js 3.9 KB

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