datasourceEditCtrl.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. define([
  2. 'angular',
  3. 'config',
  4. ],
  5. function (angular, config) {
  6. 'use strict';
  7. var module = angular.module('grafana.controllers');
  8. var datasourceTypes = [];
  9. module.controller('DataSourceEditCtrl', function($scope, $q, backendSrv, $routeParams, $location, datasourceSrv) {
  10. $scope.httpConfigPartialSrc = 'app/features/org/partials/datasourceHttpConfig.html';
  11. var defaults = {name: '', type: 'graphite', url: '', access: 'proxy' };
  12. $scope.indexPatternTypes = [
  13. {name: 'No pattern', value: undefined},
  14. {name: 'Hourly', value: 'Hourly', example: '[logstash-]YYYY.MM.DD.HH'},
  15. {name: 'Daily', value: 'Daily', example: '[logstash-]YYYY.MM.DD'},
  16. {name: 'Weekly', value: 'Weekly', example: '[logstash-]GGGG.WW'},
  17. {name: 'Monthly', value: 'Monthly', example: '[logstash-]YYYY.MM'},
  18. {name: 'Yearly', value: 'Yearly', example: '[logstash-]YYYY'},
  19. ];
  20. $scope.init = function() {
  21. $scope.isNew = true;
  22. $scope.datasources = [];
  23. $scope.loadDatasourceTypes().then(function() {
  24. if ($routeParams.id) {
  25. $scope.getDatasourceById($routeParams.id);
  26. } else {
  27. $scope.current = angular.copy(defaults);
  28. $scope.typeChanged();
  29. }
  30. });
  31. };
  32. $scope.loadDatasourceTypes = function() {
  33. if (datasourceTypes.length > 0) {
  34. $scope.types = datasourceTypes;
  35. return $q.when(null);
  36. }
  37. return backendSrv.get('/api/datasources/plugins').then(function(plugins) {
  38. datasourceTypes = plugins;
  39. $scope.types = plugins;
  40. });
  41. };
  42. $scope.getDatasourceById = function(id) {
  43. backendSrv.get('/api/datasources/' + id).then(function(ds) {
  44. $scope.isNew = false;
  45. $scope.current = ds;
  46. $scope.typeChanged();
  47. });
  48. };
  49. $scope.typeChanged = function() {
  50. $scope.datasourceMeta = $scope.types[$scope.current.type];
  51. };
  52. $scope.updateFrontendSettings = function() {
  53. return backendSrv.get('/api/frontend/settings').then(function(settings) {
  54. config.datasources = settings.datasources;
  55. config.defaultDatasource = settings.defaultDatasource;
  56. datasourceSrv.init();
  57. });
  58. };
  59. $scope.testDatasource = function() {
  60. $scope.testing = { done: false };
  61. datasourceSrv.get($scope.current.name).then(function(datasource) {
  62. if (!datasource.testDatasource) {
  63. $scope.testing.message = 'Data source does not support test connection feature.';
  64. $scope.testing.status = 'warning';
  65. $scope.testing.title = 'Unknown';
  66. return;
  67. }
  68. return datasource.testDatasource().then(function(result) {
  69. $scope.testing.message = result.message;
  70. $scope.testing.status = result.status;
  71. $scope.testing.title = result.title;
  72. }, function(err) {
  73. if (err.statusText) {
  74. $scope.testing.message = err.statusText;
  75. $scope.testing.title = "HTTP Error";
  76. } else {
  77. $scope.testing.message = err.message;
  78. $scope.testing.title = "Unknown error";
  79. }
  80. });
  81. }).finally(function() {
  82. $scope.testing.done = true;
  83. });
  84. };
  85. $scope.saveChanges = function(test) {
  86. if (!$scope.editForm.$valid) {
  87. return;
  88. }
  89. if ($scope.current.id) {
  90. return backendSrv.put('/api/datasources/' + $scope.current.id, $scope.current).then(function() {
  91. $scope.updateFrontendSettings().then(function() {
  92. if (test) {
  93. $scope.testDatasource();
  94. } else {
  95. $location.path('datasources');
  96. }
  97. });
  98. });
  99. } else {
  100. return backendSrv.post('/api/datasources', $scope.current).then(function(result) {
  101. $scope.updateFrontendSettings();
  102. $location.path('datasources/edit/' + result.id);
  103. });
  104. }
  105. };
  106. $scope.indexPatternTypeChanged = function() {
  107. var def = _.findWhere($scope.indexPatternTypes, {value: $scope.current.jsonData.interval});
  108. $scope.current.database = def.example || 'es-index-name';
  109. };
  110. $scope.init();
  111. });
  112. });