grafana 268 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. commit ad91093902bdfc0d2a87bb362a76a9057aef4361
  2. Author: Torkel Ödegaard <torkel.odegaard@gmail.com>
  3. Date: Wed Dec 17 17:31:57 2014 +0100
  4. Update datasource now works, #8
  5. diff --git a/src/app/controllers/pro/datasourcesCtrl.js b/src/app/controllers/pro/datasourcesCtrl.js
  6. index 6750015a6..431dd4873 100644
  7. --- a/src/app/controllers/pro/datasourcesCtrl.js
  8. +++ b/src/app/controllers/pro/datasourcesCtrl.js
  9. @@ -17,17 +17,62 @@ function (angular) {
  10. };
  11. $scope.init = function() {
  12. + $scope.reset();
  13. + $scope.editor = {index: 0};
  14. + $scope.datasources = [];
  15. + $scope.getDatasources();
  16. +
  17. + $scope.$watch('editor.index', function(newVal) {
  18. + if (newVal !== 2) {
  19. + $scope.reset();
  20. + }
  21. + });
  22. + };
  23. +
  24. + $scope.reset = function() {
  25. $scope.current = angular.copy(defaults);
  26. + $scope.currentIsNew = true;
  27. + };
  28. +
  29. + $scope.edit = function(ds) {
  30. + $scope.current = ds;
  31. + $scope.currentIsNew = false;
  32. + $scope.editor.index = 2;
  33. + };
  34. +
  35. + $scope.cancel = function() {
  36. + $scope.reset();
  37. + $scope.editor.index = 0;
  38. + };
  39. +
  40. + $scope.getDatasources = function() {
  41. + backendSrv.get('/api/admin/datasources/list').then(function(results) {
  42. + $scope.datasources = results;
  43. + });
  44. + };
  45. +
  46. + $scope.remove = function(ds) {
  47. + backendSrv.delete('/api/admin/datasources/' + ds.id).then(function() {
  48. + $scope.getDatasources();
  49. + });
  50. + };
  51. +
  52. + $scope.update = function() {
  53. + backendSrv.post('/api/admin/datasources', $scope.current).then(function() {
  54. + $scope.editor.index = 0;
  55. + $scope.getDatasources();
  56. + });
  57. };
  58. - $scope.addDatasource = function() {
  59. + $scope.add = function() {
  60. if (!$scope.editForm.$valid) {
  61. return;
  62. }
  63. - backendSrv.post('/api/admin/datasources/add', $scope.current)
  64. - .then(function(result) {
  65. - console.log('add datasource result', result);
  66. + backendSrv.put('/api/admin/datasources', $scope.current)
  67. + .then(function() {
  68. + $scope.editor.index = 0;
  69. + $scope.getDatasources();
  70. });
  71. };
  72. diff --git a/src/app/partials/pro/datasources.html b/src/app/partials/pro/datasources.html
  73. index 71aa1c536..2aff611db 100644
  74. --- a/src/app/partials/pro/datasources.html
  75. +++ b/src/app/partials/pro/datasources.html
  76. @@ -1,4 +1,4 @@
  77. -<div ng-include="'app/partials/pro/navbar.html'" ng-init="pageTitle='Data sources'"></div>
  78. +<div ng-include="'app/partials/pro/navbar.html'" ng-init="pageTitle='Admin'"></div>
  79. <div class="dashboard-edit-view" style="min-height: 500px">
  80. <div class="editor-row">
  81. @@ -7,43 +7,81 @@
  82. <div class="dashboard-editor-header">
  83. <div class="dashboard-editor-title">
  84. <i class="icon icon-sitemap"></i>
  85. - Add data source
  86. + Data sources
  87. </div>
  88. +
  89. + <div ng-model="editor.index" bs-tabs style="text-transform:capitalize;">
  90. + <div ng-repeat="tab in ['Overview', 'Add', 'Edit']" data-title="{{tab}}">
  91. + </div>
  92. + </div>
  93. +
  94. </div>
  95. <form name="editForm">
  96. -
  97. <div class="dashboard-editor-body">
  98. - <div class="editor-row">
  99. - <div class="editor-option">
  100. - <label class="small">Data source name</label>
  101. - <input type="text" class="input-large" required ng-model='current.name' placeholder="name" required></input>
  102. - </div>
  103. - <div class="editor-option">
  104. - <label class="small">Type</label>
  105. - <select class="input-medium" ng-model="current.type" ng-options="f for f in ['graphite', 'influxdb', 'opentsdb']" ng-change="typeChanged()"></select>
  106. + <div class="editor-row row" ng-if="editor.index == 0">
  107. + <div class="span8">
  108. + <div ng-if="datasources.length === 0">
  109. + <em>No datasources defined</em>
  110. + </div>
  111. + <table class="grafana-options-table">
  112. + <tr ng-repeat="ds in datasources">
  113. + <td style="width:1%">
  114. + <i class="icon-hdd"></i> &nbsp;
  115. + {{ds.name}}
  116. + </td>
  117. + <td style="width:90%">
  118. + {{ds.url}}
  119. + </td>
  120. + <td style="width: 1%">
  121. + <a ng-click="edit(ds)" class="btn btn-success btn-mini">
  122. + <i class="icon-edit"></i>
  123. + Edit
  124. + </a>
  125. + </td>
  126. + <td style="width: 1%">
  127. + <a ng-click="remove(ds)" class="btn btn-danger btn-mini">
  128. + <i class="icon-remove"></i>
  129. + </a>
  130. + </td>
  131. + </tr>
  132. + </table>
  133. </div>
  134. </div>
  135. - <div class="editor-row">
  136. - <div class="editor-option">
  137. - <label class="small">Url</label>
  138. - <input type="text" class="input-xxlarge" required ng-model='current.url' placeholder="http://my.graphite.com:8080" required></input>
  139. + <div ng-if="editor.index == 1 || (editor.index == 2 && !currentIsNew)">
  140. + <div class="editor-row">
  141. + <div class="editor-option">
  142. + <label class="small">Data source name</label>
  143. + <input type="text" class="input-large" required ng-model='current.name' placeholder="production" required></input>
  144. + </div>
  145. + <div class="editor-option">
  146. + <label class="small">Type</label>
  147. + <select class="input-medium" ng-model="current.type" ng-options="f for f in ['graphite', 'influxdb', 'opentsdb']" ng-change="typeChanged()"></select>
  148. + </div>
  149. </div>
  150. - <div class="editor-option">
  151. - <label class="small">Access method <tip>Direct = url is used directly from browser, Proxy = Grafana backend will proxy the request</label>
  152. - <select class="input-medium" ng-model="current.access" ng-options="f for f in ['direct', 'proxy']"></select>
  153. +
  154. + <div class="editor-row">
  155. + <div class="editor-option">
  156. + <label class="small">Url</label>
  157. + <input type="text" class="input-xxlarge" required ng-model='current.url' placeholder="http://my.graphite.com:8080" required></input>
  158. + </div>
  159. + <div class="editor-option">
  160. + <label class="small">Access method <tip>Direct = url is used directly from browser, Proxy = Grafana backend will proxy the request</label>
  161. + <select class="input-medium" ng-model="current.access" ng-options="f for f in ['direct', 'proxy']"></select>
  162. + </div>
  163. </div>
  164. </div>
  165. - </div>
  166. -
  167. - <div class="dashboard-editor-footer">
  168. - <button type="submit" class="btn btn-success" ng-click="addDatasource()">Add</button>
  169. - </div>
  170. + <div class="dashboard-editor-footer" style="margin-top: 20px">
  171. + <button type="submit" class="btn btn-success" ng-show="editor.index === 1" ng-click="add()">Add</button>
  172. + <button type="submit" class="btn btn-success" ng-show="editor.index === 2 && !currentIsNew" ng-click="update()">Update</button>
  173. + <button type="submit" class="btn btn-info" ng-show="editor.index === 2 && !currentIsNew" ng-click="cancel()">Cancel</button>
  174. + </div>
  175. - </form>
  176. + </form>
  177. + </div>
  178. </div>
  179. </div>
  180. </div>
  181. diff --git a/src/app/services/pro/backendSrv.js b/src/app/services/pro/backendSrv.js
  182. index b2a38de5e..28d323177 100644
  183. --- a/src/app/services/pro/backendSrv.js
  184. +++ b/src/app/services/pro/backendSrv.js
  185. @@ -13,6 +13,10 @@ function (angular, _) {
  186. return this.request({ method: 'GET', url: url });
  187. };
  188. + this.delete = function(url) {
  189. + return this.request({ method: 'DELETE', url: url });
  190. + };
  191. +
  192. this.post = function(url, data) {
  193. return this.request({ method: 'POST', url: url, data: data });
  194. };