ds_edit_ctrl.ts 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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.getDatasourceNav(0);
  39. this.isNew = true;
  40. this.datasources = [];
  41. this.tabIndex = 0;
  42. this.loadDatasourceTypes().then(() => {
  43. if (this.$routeParams.id) {
  44. this.getDatasourceById(this.$routeParams.id);
  45. } else {
  46. this.initNewDatasourceModel();
  47. }
  48. });
  49. }
  50. initNewDatasourceModel() {
  51. this.current = angular.copy(defaults);
  52. // We are coming from getting started
  53. if (this.$location.search().gettingstarted) {
  54. this.gettingStarted = true;
  55. this.current.isDefault = true;
  56. }
  57. this.typeChanged();
  58. }
  59. loadDatasourceTypes() {
  60. if (datasourceTypes.length > 0) {
  61. this.types = datasourceTypes;
  62. return this.$q.when(null);
  63. }
  64. return this.backendSrv.get('/api/plugins', {enabled: 1, type: 'datasource'}).then(plugins => {
  65. datasourceTypes = plugins;
  66. this.types = plugins;
  67. });
  68. }
  69. getDatasourceById(id) {
  70. this.backendSrv.get('/api/datasources/' + id).then(ds => {
  71. this.isNew = false;
  72. this.current = ds;
  73. if (datasourceCreated) {
  74. datasourceCreated = false;
  75. this.testDatasource();
  76. }
  77. return this.typeChanged();
  78. });
  79. }
  80. typeChanged() {
  81. this.hasDashboards = false;
  82. return this.backendSrv.get('/api/plugins/' + this.current.type + '/settings').then(pluginInfo => {
  83. this.datasourceMeta = pluginInfo;
  84. console.log(this.datasourceMeta) ;
  85. this.hasDashboards = _.find(pluginInfo.includes, {type: 'dashboard'});
  86. });
  87. }
  88. updateFrontendSettings() {
  89. return this.backendSrv.get('/api/frontend/settings').then(settings => {
  90. config.datasources = settings.datasources;
  91. config.defaultDatasource = settings.defaultDatasource;
  92. this.datasourceSrv.init();
  93. });
  94. }
  95. testDatasource() {
  96. this.testing = { done: false };
  97. this.datasourceSrv.get(this.current.name).then(datasource => {
  98. if (!datasource.testDatasource) {
  99. delete this.testing;
  100. return;
  101. }
  102. return datasource.testDatasource().then(result => {
  103. this.testing.message = result.message;
  104. this.testing.status = result.status;
  105. this.testing.title = result.title;
  106. }).catch(err => {
  107. if (err.statusText) {
  108. this.testing.message = err.statusText;
  109. this.testing.title = "HTTP Error";
  110. } else {
  111. this.testing.message = err.message;
  112. this.testing.title = "Unknown error";
  113. }
  114. });
  115. }).finally(() => {
  116. if (this.testing) {
  117. this.testing.done = true;
  118. }
  119. });
  120. }
  121. saveChanges() {
  122. if (!this.editForm.$valid) {
  123. return;
  124. }
  125. if (this.current.id) {
  126. return this.backendSrv.put('/api/datasources/' + this.current.id, this.current).then(() => {
  127. this.updateFrontendSettings().then(() => {
  128. this.testDatasource();
  129. });
  130. });
  131. } else {
  132. return this.backendSrv.post('/api/datasources', this.current).then(result => {
  133. this.updateFrontendSettings();
  134. datasourceCreated = true;
  135. this.$location.path('datasources/edit/' + result.id);
  136. });
  137. }
  138. }
  139. confirmDelete() {
  140. this.backendSrv.delete('/api/datasources/' + this.current.id).then(() => {
  141. this.$location.path('datasources');
  142. });
  143. }
  144. delete(s) {
  145. appEvents.emit('confirm-modal', {
  146. title: 'Delete',
  147. text: 'Are you sure you want to delete this datasource?',
  148. yesText: "Delete",
  149. icon: "fa-trash",
  150. onConfirm: () => {
  151. this.confirmDelete();
  152. }
  153. });
  154. }
  155. }
  156. coreModule.controller('DataSourceEditCtrl', DataSourceEditCtrl);
  157. coreModule.directive('datasourceHttpSettings', function() {
  158. return {
  159. scope: {
  160. current: "=",
  161. suggestUrl: "@",
  162. },
  163. templateUrl: 'public/app/features/plugins/partials/ds_http_settings.html',
  164. link: {
  165. pre: function($scope, elem, attrs) {
  166. $scope.getSuggestUrls = function() {
  167. return [$scope.suggestUrl];
  168. };
  169. }
  170. }
  171. };
  172. });