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