datasourceEditCtrl.js 4.2 KB

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