datasourceEditCtrl.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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.elasticsearchVersions = [
  22. {name: '0.x', value: 0},
  23. {name: '1.x', value: 1},
  24. {name: '2.x', value: 2},
  25. ];
  26. $scope.init = function() {
  27. $scope.isNew = true;
  28. $scope.datasources = [];
  29. $scope.loadDatasourceTypes().then(function() {
  30. if ($routeParams.id) {
  31. $scope.getDatasourceById($routeParams.id);
  32. } else {
  33. $scope.current = angular.copy(defaults);
  34. $scope.typeChanged();
  35. }
  36. });
  37. };
  38. $scope.loadDatasourceTypes = function() {
  39. if (datasourceTypes.length > 0) {
  40. $scope.types = datasourceTypes;
  41. return $q.when(null);
  42. }
  43. return backendSrv.get('/api/datasources/plugins').then(function(plugins) {
  44. datasourceTypes = plugins;
  45. $scope.types = plugins;
  46. });
  47. };
  48. $scope.getDatasourceById = function(id) {
  49. backendSrv.get('/api/datasources/' + id).then(function(ds) {
  50. $scope.isNew = false;
  51. $scope.current = ds;
  52. $scope.typeChanged();
  53. });
  54. };
  55. $scope.typeChanged = function() {
  56. $scope.datasourceMeta = $scope.types[$scope.current.type];
  57. };
  58. $scope.updateFrontendSettings = function() {
  59. return backendSrv.get('/api/frontend/settings').then(function(settings) {
  60. config.datasources = settings.datasources;
  61. config.defaultDatasource = settings.defaultDatasource;
  62. datasourceSrv.init();
  63. });
  64. };
  65. $scope.testDatasource = function() {
  66. $scope.testing = { done: false };
  67. datasourceSrv.get($scope.current.name).then(function(datasource) {
  68. if (!datasource.testDatasource) {
  69. $scope.testing.message = 'Data source does not support test connection feature.';
  70. $scope.testing.status = 'warning';
  71. $scope.testing.title = 'Unknown';
  72. return;
  73. }
  74. return datasource.testDatasource().then(function(result) {
  75. $scope.testing.message = result.message;
  76. $scope.testing.status = result.status;
  77. $scope.testing.title = result.title;
  78. }, function(err) {
  79. if (err.statusText) {
  80. $scope.testing.message = err.statusText;
  81. $scope.testing.title = "HTTP Error";
  82. } else {
  83. $scope.testing.message = err.message;
  84. $scope.testing.title = "Unknown error";
  85. }
  86. });
  87. }).finally(function() {
  88. $scope.testing.done = true;
  89. });
  90. };
  91. $scope.saveChanges = function(test) {
  92. if (!$scope.editForm.$valid) {
  93. return;
  94. }
  95. if ($scope.current.id) {
  96. return backendSrv.put('/api/datasources/' + $scope.current.id, $scope.current).then(function() {
  97. $scope.updateFrontendSettings().then(function() {
  98. if (test) {
  99. $scope.testDatasource();
  100. } else {
  101. $location.path('datasources');
  102. }
  103. });
  104. });
  105. } else {
  106. return backendSrv.post('/api/datasources', $scope.current).then(function(result) {
  107. $scope.updateFrontendSettings();
  108. $location.path('datasources/edit/' + result.id);
  109. });
  110. }
  111. };
  112. $scope.indexPatternTypeChanged = function() {
  113. var def = _.findWhere($scope.indexPatternTypes, {value: $scope.current.jsonData.interval});
  114. $scope.current.database = def.example || 'es-index-name';
  115. };
  116. $scope.init();
  117. });
  118. });