2014-02-15 06:46:36 -06:00
|
|
|
define([
|
|
|
|
'angular',
|
2014-08-07 07:35:19 -05:00
|
|
|
'lodash',
|
2014-02-27 14:46:06 -06:00
|
|
|
'config',
|
2014-02-15 06:46:36 -06:00
|
|
|
],
|
2014-02-25 10:48:01 -06:00
|
|
|
function (angular, _, config) {
|
2014-02-15 06:46:36 -06:00
|
|
|
'use strict';
|
|
|
|
|
2014-07-28 11:11:52 -05:00
|
|
|
var module = angular.module('grafana.services');
|
2014-02-25 10:48:01 -06:00
|
|
|
|
2015-02-27 06:45:00 -06:00
|
|
|
module.service('datasourceSrv', function($q, $injector, $rootScope) {
|
|
|
|
var self = this;
|
2015-02-18 07:06:44 -06:00
|
|
|
|
2015-03-31 07:31:47 -05:00
|
|
|
this.init = function() {
|
2015-02-28 01:25:13 -06:00
|
|
|
this.datasources = {};
|
|
|
|
this.metricSources = [];
|
|
|
|
this.annotationSources = [];
|
|
|
|
|
2015-02-27 15:29:00 -06:00
|
|
|
_.each(config.datasources, function(value, key) {
|
|
|
|
if (value.meta && value.meta.metrics) {
|
2015-02-28 03:52:25 -06:00
|
|
|
self.metricSources.push({
|
|
|
|
value: key === config.defaultDatasource ? null : key,
|
|
|
|
name: key
|
|
|
|
});
|
|
|
|
}
|
|
|
|
if (value.meta && value.meta.annotations) {
|
|
|
|
self.annotationSources.push(value);
|
2015-02-27 15:29:00 -06:00
|
|
|
}
|
|
|
|
});
|
2014-03-04 15:01:25 -06:00
|
|
|
};
|
2014-02-15 06:46:36 -06:00
|
|
|
|
|
|
|
this.get = function(name) {
|
2015-02-27 06:45:00 -06:00
|
|
|
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) {
|
2015-02-27 15:29:00 -06:00
|
|
|
var dsConfig = config.datasources[name];
|
2015-02-28 02:46:37 -06:00
|
|
|
if (!dsConfig) {
|
|
|
|
return $q.reject({message: "Datasource named " + name + " was not found"});
|
|
|
|
}
|
|
|
|
|
2015-02-27 06:45:00 -06:00
|
|
|
var deferred = $q.defer();
|
|
|
|
|
2015-02-27 15:29:00 -06:00
|
|
|
var pluginDef = dsConfig.meta;
|
|
|
|
|
2015-02-27 06:45:00 -06:00
|
|
|
$rootScope.require([pluginDef.module], function() {
|
|
|
|
var AngularService = $injector.get(pluginDef.serviceName);
|
2015-02-27 15:29:00 -06:00
|
|
|
var instance = new AngularService(dsConfig, pluginDef);
|
|
|
|
instance.meta = pluginDef;
|
|
|
|
instance.name = name;
|
2015-02-27 06:45:00 -06:00
|
|
|
self.datasources[name] = instance;
|
|
|
|
deferred.resolve(instance);
|
|
|
|
});
|
2014-02-27 14:46:06 -06:00
|
|
|
|
2015-02-27 06:45:00 -06:00
|
|
|
return deferred.promise;
|
2014-02-15 06:46:36 -06:00
|
|
|
};
|
|
|
|
|
2015-01-17 03:39:01 -06:00
|
|
|
this.getAll = function() {
|
2015-02-28 02:46:37 -06:00
|
|
|
return config.datasources;
|
2015-01-17 03:39:01 -06:00
|
|
|
};
|
|
|
|
|
2014-07-13 08:01:20 -05:00
|
|
|
this.getAnnotationSources = function() {
|
2015-02-18 07:06:44 -06:00
|
|
|
return this.annotationSources;
|
2014-07-13 08:15:10 -05:00
|
|
|
};
|
2014-07-13 08:01:20 -05:00
|
|
|
|
2014-07-30 01:34:58 -05:00
|
|
|
this.getMetricSources = function() {
|
2015-02-18 07:06:44 -06:00
|
|
|
return this.metricSources;
|
2014-02-15 06:46:36 -06:00
|
|
|
};
|
2014-03-04 15:01:25 -06:00
|
|
|
|
2015-03-31 07:31:47 -05:00
|
|
|
this.init();
|
2014-02-15 06:46:36 -06:00
|
|
|
});
|
2014-05-19 08:31:30 -05:00
|
|
|
});
|