datasourceSrv.js 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. define([
  2. 'angular',
  3. 'underscore',
  4. 'config',
  5. './graphite/graphiteDatasource',
  6. './influxdb/influxdbDatasource',
  7. './opentsdb/opentsdbDatasource',
  8. './elasticsearch/es-datasource',
  9. ],
  10. function (angular, _, config) {
  11. 'use strict';
  12. var module = angular.module('grafana.services');
  13. module.service('datasourceSrv', function($q, filterSrv, $http, $injector) {
  14. var datasources = {};
  15. var metricSources = [];
  16. var annotationSources = [];
  17. var grafanaDB = {};
  18. this.init = function() {
  19. _.each(config.datasources, function(value, key) {
  20. datasources[key] = this.datasourceFactory(value);
  21. if (value.default) {
  22. this.default = datasources[key];
  23. }
  24. }, this);
  25. if (!this.default) {
  26. this.default = datasources[_.keys(datasources)[0]];
  27. this.default.default = true;
  28. }
  29. // create list of different source types
  30. _.each(datasources, function(value, key) {
  31. if (value.supportMetrics) {
  32. metricSources.push({
  33. name: value.name,
  34. value: value.default ? null : key,
  35. });
  36. }
  37. if (value.supportAnnotations) {
  38. annotationSources.push({
  39. name: key,
  40. editorSrc: value.annotationEditorSrc,
  41. });
  42. }
  43. if (value.grafanaDB) {
  44. grafanaDB = value;
  45. }
  46. });
  47. };
  48. this.datasourceFactory = function(ds) {
  49. var Datasource = null;
  50. switch(ds.type) {
  51. case 'graphite':
  52. Datasource = $injector.get('GraphiteDatasource');
  53. break;
  54. case 'influxdb':
  55. Datasource = $injector.get('InfluxDatasource');
  56. break;
  57. case 'opentsdb':
  58. Datasource = $injector.get('OpenTSDBDatasource');
  59. break;
  60. case 'elasticsearch':
  61. Datasource = $injector.get('ElasticDatasource');
  62. break;
  63. }
  64. return new Datasource(ds);
  65. };
  66. this.get = function(name) {
  67. if (!name) { return this.default; }
  68. if (datasources[name]) { return datasources[name]; }
  69. throw "Unable to find datasource: " + name;
  70. };
  71. this.getAnnotationSources = function() {
  72. return annotationSources;
  73. };
  74. this.getMetricSources = function() {
  75. return metricSources;
  76. };
  77. this.getGrafanaDB = function() {
  78. return grafanaDB;
  79. };
  80. this.init();
  81. });
  82. });