datasourceSrv.js 1.2 KB

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