datasource_srv.js 3.9 KB

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