ds_edit_ctrl.ts 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. ///<reference path="../../headers/common.d.ts" />
  2. import angular from 'angular';
  3. import _ from 'lodash';
  4. import config from 'app/core/config';
  5. import {coreModule, appEvents} from 'app/core/core';
  6. var datasourceTypes = [];
  7. var defaults = {
  8. name: '',
  9. type: 'graphite',
  10. url: '',
  11. access: 'proxy',
  12. jsonData: {},
  13. secureJsonFields: {},
  14. };
  15. var datasourceCreated = false;
  16. export class DataSourceEditCtrl {
  17. isNew: boolean;
  18. datasources: any[];
  19. current: any;
  20. types: any;
  21. testing: any;
  22. datasourceMeta: any;
  23. tabIndex: number;
  24. hasDashboards: boolean;
  25. editForm: any;
  26. gettingStarted: boolean;
  27. navModel: any;
  28. /** @ngInject */
  29. constructor(
  30. private $scope,
  31. private $q,
  32. private backendSrv,
  33. private $routeParams,
  34. private $location,
  35. private datasourceSrv,
  36. private navModelSrv,
  37. ) {
  38. this.navModel = navModelSrv.getNav('cfg', 'datasources');
  39. this.datasources = [];
  40. this.tabIndex = 0;
  41. this.loadDatasourceTypes().then(() => {
  42. if (this.$routeParams.id) {
  43. this.getDatasourceById(this.$routeParams.id);
  44. } else {
  45. this.initNewDatasourceModel();
  46. }
  47. });
  48. }
  49. initNewDatasourceModel() {
  50. this.isNew = true;
  51. this.current = angular.copy(defaults);
  52. this.navModel.breadcrumbs.push({text: 'New data source'});
  53. // We are coming from getting started
  54. if (this.$location.search().gettingstarted) {
  55. this.gettingStarted = true;
  56. this.current.isDefault = true;
  57. }
  58. this.typeChanged();
  59. }
  60. loadDatasourceTypes() {
  61. if (datasourceTypes.length > 0) {
  62. this.types = datasourceTypes;
  63. return this.$q.when(null);
  64. }
  65. return this.backendSrv.get('/api/plugins', {enabled: 1, type: 'datasource'}).then(plugins => {
  66. datasourceTypes = plugins;
  67. this.types = plugins;
  68. });
  69. }
  70. getDatasourceById(id) {
  71. this.backendSrv.get('/api/datasources/' + id).then(ds => {
  72. this.isNew = false;
  73. this.current = ds;
  74. this.navModel.breadcrumbs.push({text: ds.name});
  75. if (datasourceCreated) {
  76. datasourceCreated = false;
  77. this.testDatasource();
  78. }
  79. return this.typeChanged();
  80. });
  81. }
  82. typeChanged() {
  83. this.hasDashboards = false;
  84. return this.backendSrv.get('/api/plugins/' + this.current.type + '/settings').then(pluginInfo => {
  85. this.datasourceMeta = pluginInfo;
  86. console.log(this.datasourceMeta) ;
  87. this.hasDashboards = _.find(pluginInfo.includes, {type: 'dashboard'});
  88. });
  89. }
  90. updateFrontendSettings() {
  91. return this.backendSrv.get('/api/frontend/settings').then(settings => {
  92. config.datasources = settings.datasources;
  93. config.defaultDatasource = settings.defaultDatasource;
  94. this.datasourceSrv.init();
  95. });
  96. }
  97. testDatasource() {
  98. this.testing = { done: false };
  99. this.datasourceSrv.get(this.current.name).then(datasource => {
  100. if (!datasource.testDatasource) {
  101. delete this.testing;
  102. return;
  103. }
  104. return datasource.testDatasource().then(result => {
  105. this.testing.message = result.message;
  106. this.testing.status = result.status;
  107. this.testing.title = result.title;
  108. }).catch(err => {
  109. if (err.statusText) {
  110. this.testing.message = err.statusText;
  111. this.testing.title = "HTTP Error";
  112. } else {
  113. this.testing.message = err.message;
  114. this.testing.title = "Unknown error";
  115. }
  116. });
  117. }).finally(() => {
  118. if (this.testing) {
  119. this.testing.done = true;
  120. }
  121. });
  122. }
  123. saveChanges() {
  124. if (!this.editForm.$valid) {
  125. return;
  126. }
  127. if (this.current.id) {
  128. return this.backendSrv.put('/api/datasources/' + this.current.id, this.current).then(() => {
  129. this.updateFrontendSettings().then(() => {
  130. this.testDatasource();
  131. });
  132. });
  133. } else {
  134. return this.backendSrv.post('/api/datasources', this.current).then(result => {
  135. this.updateFrontendSettings();
  136. datasourceCreated = true;
  137. this.$location.path('datasources/edit/' + result.id);
  138. });
  139. }
  140. }
  141. confirmDelete() {
  142. this.backendSrv.delete('/api/datasources/' + this.current.id).then(() => {
  143. this.$location.path('datasources');
  144. });
  145. }
  146. delete(s) {
  147. appEvents.emit('confirm-modal', {
  148. title: 'Delete',
  149. text: 'Are you sure you want to delete this datasource?',
  150. yesText: "Delete",
  151. icon: "fa-trash",
  152. onConfirm: () => {
  153. this.confirmDelete();
  154. }
  155. });
  156. }
  157. }
  158. coreModule.controller('DataSourceEditCtrl', DataSourceEditCtrl);
  159. coreModule.directive('datasourceHttpSettings', function() {
  160. return {
  161. scope: {
  162. current: "=",
  163. suggestUrl: "@",
  164. },
  165. templateUrl: 'public/app/features/plugins/partials/ds_http_settings.html',
  166. link: {
  167. pre: function($scope, elem, attrs) {
  168. $scope.getSuggestUrls = function() {
  169. return [$scope.suggestUrl];
  170. };
  171. }
  172. }
  173. };
  174. });