datasourceEditCtrl.js 4.1 KB

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