datasource_srv.js 3.7 KB

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