2014-02-15 13:46:36 +01:00
|
|
|
define([
|
|
|
|
|
'angular',
|
2014-08-07 14:35:19 +02:00
|
|
|
'lodash',
|
2015-10-30 15:58:20 +01:00
|
|
|
'../core_module',
|
2015-10-30 14:19:02 +01:00
|
|
|
'app/core/config',
|
2014-02-15 13:46:36 +01:00
|
|
|
],
|
2015-10-30 15:58:20 +01:00
|
|
|
function (angular, _, coreModule, config) {
|
2014-02-15 13:46:36 +01:00
|
|
|
'use strict';
|
|
|
|
|
|
2016-04-16 13:43:16 -04:00
|
|
|
coreModule.default.service('datasourceSrv', function($q, $injector, $rootScope, templateSrv) {
|
2015-02-27 13:45:00 +01:00
|
|
|
var self = this;
|
2015-02-18 14:06:44 +01:00
|
|
|
|
2015-03-31 14:31:47 +02:00
|
|
|
this.init = function() {
|
2015-02-28 08:25:13 +01:00
|
|
|
this.datasources = {};
|
2014-03-04 22:01:25 +01:00
|
|
|
};
|
2014-02-15 13:46:36 +01:00
|
|
|
|
|
|
|
|
this.get = function(name) {
|
2015-02-27 13:45:00 +01:00
|
|
|
if (!name) {
|
|
|
|
|
return this.get(config.defaultDatasource);
|
|
|
|
|
}
|
|
|
|
|
|
2016-04-16 13:43:16 -04:00
|
|
|
name = templateSrv.replace(name);
|
|
|
|
|
|
2017-02-21 17:27:21 +01:00
|
|
|
if (name === 'default') {
|
|
|
|
|
return this.get(config.defaultDatasource);
|
|
|
|
|
}
|
|
|
|
|
|
2015-02-27 13:45:00 +01:00
|
|
|
if (this.datasources[name]) {
|
|
|
|
|
return $q.when(this.datasources[name]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return this.loadDatasource(name);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
this.loadDatasource = function(name) {
|
2015-02-27 22:29:00 +01:00
|
|
|
var dsConfig = config.datasources[name];
|
2015-02-28 09:46:37 +01:00
|
|
|
if (!dsConfig) {
|
|
|
|
|
return $q.reject({message: "Datasource named " + name + " was not found"});
|
|
|
|
|
}
|
|
|
|
|
|
2015-02-27 13:45:00 +01:00
|
|
|
var deferred = $q.defer();
|
2015-02-27 22:29:00 +01:00
|
|
|
var pluginDef = dsConfig.meta;
|
|
|
|
|
|
2016-01-09 13:21:16 +01:00
|
|
|
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) {
|
2016-01-09 19:14:59 +01:00
|
|
|
throw "Plugin module is missing Datasource constructor";
|
2016-01-09 13:21:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var instance = $injector.instantiate(plugin.Datasource, {instanceSettings: dsConfig});
|
2015-02-27 22:29:00 +01:00
|
|
|
instance.meta = pluginDef;
|
|
|
|
|
instance.name = name;
|
2015-02-27 13:45:00 +01:00
|
|
|
self.datasources[name] = instance;
|
|
|
|
|
deferred.resolve(instance);
|
2015-12-15 17:54:16 +01:00
|
|
|
}).catch(function(err) {
|
2016-01-09 19:14:59 +01:00
|
|
|
$rootScope.appEvent('alert-error', [dsConfig.name + ' plugin failed', err.toString()]);
|
2015-02-27 13:45:00 +01:00
|
|
|
});
|
2014-02-27 21:46:06 +01:00
|
|
|
|
2015-02-27 13:45:00 +01:00
|
|
|
return deferred.promise;
|
2014-02-15 13:46:36 +01:00
|
|
|
};
|
|
|
|
|
|
2015-01-17 10:39:01 +01:00
|
|
|
this.getAll = function() {
|
2015-02-28 09:46:37 +01:00
|
|
|
return config.datasources;
|
2015-01-17 10:39:01 +01:00
|
|
|
};
|
|
|
|
|
|
2014-07-13 15:01:20 +02:00
|
|
|
this.getAnnotationSources = function() {
|
2016-05-23 09:28:14 +02:00
|
|
|
var sources = [];
|
2016-04-16 13:43:16 -04:00
|
|
|
|
2016-05-23 09:28:14 +02:00
|
|
|
this.addDataSourceVariables(sources);
|
|
|
|
|
|
|
|
|
|
_.each(config.datasources, function(value) {
|
2016-04-16 13:43:16 -04:00
|
|
|
if (value.meta && value.meta.annotations) {
|
2016-05-23 09:28:14 +02:00
|
|
|
sources.push(value);
|
2016-04-16 13:43:16 -04:00
|
|
|
}
|
2016-05-23 09:28:14 +02:00
|
|
|
});
|
2016-04-16 13:43:16 -04:00
|
|
|
|
2016-05-23 09:28:14 +02:00
|
|
|
return sources;
|
2014-07-13 15:15:10 +02:00
|
|
|
};
|
2014-07-13 15:01:20 +02:00
|
|
|
|
2016-04-16 13:43:16 -04:00
|
|
|
this.getMetricSources = function(options) {
|
|
|
|
|
var metricSources = [];
|
|
|
|
|
|
|
|
|
|
_.each(config.datasources, function(value, key) {
|
|
|
|
|
if (value.meta && value.meta.metrics) {
|
2016-05-14 10:00:43 +02:00
|
|
|
metricSources.push({value: key, name: key, meta: value.meta});
|
|
|
|
|
|
|
|
|
|
if (key === config.defaultDatasource) {
|
|
|
|
|
metricSources.push({value: null, name: 'default', meta: value.meta});
|
|
|
|
|
}
|
2016-04-16 13:43:16 -04:00
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (!options || !options.skipVariables) {
|
2016-05-23 09:28:14 +02:00
|
|
|
this.addDataSourceVariables(metricSources);
|
2016-04-16 13:43:16 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
metricSources.sort(function(a, b) {
|
2017-04-12 08:23:44 +02:00
|
|
|
// these two should always be at the bottom
|
|
|
|
|
if (a.meta.id === "mixed" || a.meta.id === "grafana") {
|
2016-04-16 13:43:16 -04:00
|
|
|
return 1;
|
|
|
|
|
}
|
2017-04-12 08:23:44 +02:00
|
|
|
if (b.meta.id === "mixed" || b.meta.id === "grafana") {
|
2017-02-08 00:01:42 +01:00
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
if (a.name.toLowerCase() > b.name.toLowerCase()) {
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
if (a.name.toLowerCase() < b.name.toLowerCase()) {
|
2016-04-16 13:43:16 -04:00
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
return 0;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return metricSources;
|
2014-02-15 13:46:36 +01:00
|
|
|
};
|
2014-03-04 22:01:25 +01:00
|
|
|
|
2016-05-23 09:28:14 +02:00
|
|
|
this.addDataSourceVariables = function(list) {
|
|
|
|
|
// look for data source variables
|
|
|
|
|
for (var i = 0; i < templateSrv.variables.length; i++) {
|
|
|
|
|
var variable = templateSrv.variables[i];
|
|
|
|
|
if (variable.type !== 'datasource') {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var first = variable.current.value;
|
2017-08-08 17:08:35 +02:00
|
|
|
if (first === 'default') {
|
|
|
|
|
first = config.defaultDatasource;
|
|
|
|
|
}
|
|
|
|
|
|
2016-05-23 09:28:14 +02:00
|
|
|
var ds = config.datasources[first];
|
|
|
|
|
|
|
|
|
|
if (ds) {
|
|
|
|
|
list.push({
|
|
|
|
|
name: '$' + variable.name,
|
|
|
|
|
value: '$' + variable.name,
|
|
|
|
|
meta: ds.meta,
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2015-03-31 14:31:47 +02:00
|
|
|
this.init();
|
2014-02-15 13:46:36 +01:00
|
|
|
});
|
2014-05-19 15:31:30 +02:00
|
|
|
});
|