| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101 |
- define([
- 'angular',
- 'lodash',
- '../core_module',
- 'app/core/config',
- ],
- function (angular, _, coreModule, config) {
- 'use strict';
- coreModule.default.service('datasourceSrv', function($q, $injector, $rootScope) {
- var self = this;
- this.init = function() {
- this.datasources = {};
- this.metricSources = [];
- this.annotationSources = [];
- _.each(config.datasources, function(value, key) {
- if (value.meta && value.meta.metrics) {
- self.metricSources.push({
- value: key === config.defaultDatasource ? null : key,
- name: key,
- meta: value.meta,
- });
- }
- if (value.meta && value.meta.annotations) {
- self.annotationSources.push(value);
- }
- });
- this.metricSources.sort(function(a, b) {
- if (a.meta.builtIn || a.name > b.name) {
- return 1;
- }
- if (a.name < b.name) {
- return -1;
- }
- return 0;
- });
- };
- this.get = function(name) {
- if (!name) {
- return this.get(config.defaultDatasource);
- }
- if (this.datasources[name]) {
- return $q.when(this.datasources[name]);
- }
- return this.loadDatasource(name);
- };
- this.loadDatasource = function(name) {
- var dsConfig = config.datasources[name];
- if (!dsConfig) {
- return $q.reject({message: "Datasource named " + name + " was not found"});
- }
- var deferred = $q.defer();
- var pluginDef = dsConfig.meta;
- System.import(pluginDef.module).then(function(plugin) {
- // check if its in cache now
- if (self.datasources[name]) {
- deferred.resolve(self.datasources[name]);
- return;
- }
- // plugin module needs to export a constructor function named Datasource
- if (!plugin.Datasource) {
- throw "Plugin module is missing Datasource constructor";
- }
- var instance = $injector.instantiate(plugin.Datasource, {instanceSettings: dsConfig});
- instance.meta = pluginDef;
- instance.name = name;
- self.datasources[name] = instance;
- deferred.resolve(instance);
- }).catch(function(err) {
- $rootScope.appEvent('alert-error', [dsConfig.name + ' plugin failed', err.toString()]);
- });
- return deferred.promise;
- };
- this.getAll = function() {
- return config.datasources;
- };
- this.getAnnotationSources = function() {
- return this.annotationSources;
- };
- this.getMetricSources = function() {
- return this.metricSources;
- };
- this.init();
- });
- });
|