datasourceSrv.js 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. define([
  2. 'angular',
  3. 'underscore',
  4. 'config',
  5. './graphite/graphiteDatasource',
  6. './influxdb/influxdbDatasource',
  7. './opentsdb/opentsdbDatasource',
  8. ],
  9. function (angular, _, config) {
  10. 'use strict';
  11. var module = angular.module('kibana.services');
  12. module.service('datasourceSrv', function($q, filterSrv, $http, GraphiteDatasource, InfluxDatasource, OpenTSDBDatasource) {
  13. this.init = function() {
  14. var defaultDatasource = _.findWhere(_.values(config.datasources), { default: true } );
  15. this.default = this.datasourceFactory(defaultDatasource);
  16. };
  17. this.datasourceFactory = function(ds) {
  18. switch(ds.type) {
  19. case 'graphite':
  20. return new GraphiteDatasource(ds);
  21. case 'influxdb':
  22. return new InfluxDatasource(ds);
  23. case 'opentsdb':
  24. return new OpenTSDBDatasource(ds);
  25. }
  26. };
  27. this.get = function(name) {
  28. if (!name) { return this.default; }
  29. var ds = config.datasources[name];
  30. if (!ds) {
  31. return null;
  32. }
  33. return this.datasourceFactory(ds);
  34. };
  35. this.listOptions = function() {
  36. return _.map(config.datasources, function(value, key) {
  37. return {
  38. name: value.default ? key + ' (default)' : key,
  39. value: value.default ? null : key
  40. };
  41. });
  42. };
  43. this.init();
  44. });
  45. });