From ec2b4f584ca8bbd8ca23af8aa7753bbb3065e7ac Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Torkel=20=C3=96degaard?= Date: Fri, 15 Aug 2014 19:06:04 +0200 Subject: [PATCH] some initial work on making it easy to add a custom datasource without modifing the original source, #701, #553 --- src/app/app.js | 20 +++++++---- src/app/components/settings.js | 6 +++- src/app/services/datasourceSrv.js | 2 ++ src/config.sample.js | 6 +++- src/plugins/datasource.example.js | 55 +++++++++++++++++++++++++++++++ 5 files changed, 80 insertions(+), 9 deletions(-) create mode 100644 src/plugins/datasource.example.js diff --git a/src/app/app.js b/src/app/app.js index 9acb4d03f02..ab5c8fd6355 100644 --- a/src/app/app.js +++ b/src/app/app.js @@ -78,14 +78,20 @@ function (angular, $, _, appLevelRequire, config) { apps_deps.push(module_name); }); + var preBootRequires = [ + 'controllers/all', + 'directives/all', + 'filters/all', + 'components/partials', + 'routes/all', + ]; + + _.each(config.plugins.dependencies, function(dep) { + preBootRequires.push('../plugins/' + dep); + }); + app.boot = function() { - require([ - 'controllers/all', - 'directives/all', - 'filters/all', - 'components/partials', - 'routes/all', - ], function () { + require(preBootRequires, function () { // disable tool tip animation $.fn.tooltip.defaults.animation = false; diff --git a/src/app/components/settings.js b/src/app/components/settings.js index 165bbc6c476..3edafc5a407 100644 --- a/src/app/components/settings.js +++ b/src/app/components/settings.js @@ -70,7 +70,7 @@ function (_, crypto) { _.each(settings.datasources, function(datasource, key) { datasource.name = key; - parseBasicAuth(datasource); + if (datasource.url) { parseBasicAuth(datasource); } if (datasource.type === 'influxdb') { parseMultipleHosts(datasource); } }); @@ -78,6 +78,10 @@ function (_, crypto) { settings.panels = _.union(settings.panels, settings.plugins.panels); } + if (!settings.plugins.dependencies) { + settings.plugins.dependencies = []; + } + return settings; }; }); diff --git a/src/app/services/datasourceSrv.js b/src/app/services/datasourceSrv.js index a3af1ae9590..9c25b249d1b 100644 --- a/src/app/services/datasourceSrv.js +++ b/src/app/services/datasourceSrv.js @@ -67,6 +67,8 @@ function (angular, _, config) { case 'elasticsearch': Datasource = $injector.get('ElasticDatasource'); break; + default: + Datasource = $injector.get(ds.type); } return new Datasource(ds); }; diff --git a/src/config.sample.js b/src/config.sample.js index 9bce1540fa4..c813ac824fe 100644 --- a/src/config.sample.js +++ b/src/config.sample.js @@ -96,7 +96,11 @@ function (Settings) { // Add your own custom pannels plugins: { - panels: [] + // list of plugin panels + panels: [], + // requirejs modules in plugins folder that should be loaded + // for example custom datasources + dependencies: [], } }); diff --git a/src/plugins/datasource.example.js b/src/plugins/datasource.example.js new file mode 100644 index 00000000000..d85409cf2a5 --- /dev/null +++ b/src/plugins/datasource.example.js @@ -0,0 +1,55 @@ +define([ + 'angular', + 'lodash', + 'kbn', + 'moment' +], +function (angular, _, kbn) { + 'use strict'; + + var module = angular.module('grafana.services'); + + module.factory('CustomDatasource', function($q) { + + // the datasource object passed to constructor + // is the same defined in config.js + function CustomDatasource(datasource) { + this.name = datasource.name; + this.supportMetrics = true; + this.url = datasource.url; + } + + CustomDatasource.prototype.query = function(filterSrv, options) { + // get from & to in seconds + var from = kbn.parseDate(options.range.from).getTime() / 1000; + var to = kbn.parseDate(options.range.to).getTime() / 1000; + + var series = []; + var stepInSeconds = (to - from) / options.maxDataPoints; + + for (var i = 0; i < 3; i++) { + var walker = Math.random() * 100; + var time = from; + var timeSeries = { + target: "Series " + i, + datapoints: [] + }; + + for (var j = 0; j < options.maxDataPoints; j++) { + timeSeries.datapoints[j] = [walker, time]; + walker += Math.random() - 0.5; + time += stepInSeconds; + } + + series.push(timeSeries); + } + + return $q.when({data: series }); + + }; + + return CustomDatasource; + + }); + +});