service.js 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. /**
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. */
  18. var App = require('app');
  19. App.MainServiceController = Em.ArrayController.extend({
  20. name: 'mainServiceController',
  21. /**
  22. * @type {Ember.Object[]}
  23. */
  24. content: function () {
  25. if (!App.router.get('clusterController.isLoaded')) {
  26. return [];
  27. }
  28. return App.Service.find();
  29. }.property('App.router.clusterController.isLoaded').volatile(),
  30. /**
  31. * Current cluster
  32. * @type {Ember.Object}
  33. */
  34. cluster: function () {
  35. if (!App.router.get('clusterController.isLoaded')) {
  36. return null;
  37. }
  38. return App.Cluster.find().objectAt(0);
  39. }.property('App.router.clusterController.isLoaded'),
  40. /**
  41. * Check if all services are installed
  42. * true - all installed, false - not all
  43. * @type {bool}
  44. */
  45. isAllServicesInstalled: function () {
  46. if (!this.get('content.content')) return false;
  47. var availableServices = App.StackService.find().mapProperty('serviceName');
  48. return this.get('content.content').length == availableServices.length;
  49. }.property('content.content.@each', 'content.content.length'),
  50. /**
  51. * Should "Start All"-button be disabled
  52. * @type {bool}
  53. */
  54. isStartAllDisabled: function () {
  55. if (this.get('isStartStopAllClicked') == true) {
  56. return true;
  57. }
  58. var stoppedServices = this.get('content').filter(function (_service) {
  59. return (_service.get('healthStatus') === 'red' && !App.get('services.clientOnly').contains(_service.get('serviceName')));
  60. });
  61. return (stoppedServices.length === 0); // all green status
  62. }.property('isStartStopAllClicked', 'content.@each.healthStatus'),
  63. /**
  64. * Should "Stop All"-button be disabled
  65. * @type {bool}
  66. */
  67. isStopAllDisabled: function () {
  68. if (this.get('isStartStopAllClicked') == true) {
  69. return true;
  70. }
  71. var startedServiceLength = this.get('content').filterProperty('healthStatus', 'green').length;
  72. return (startedServiceLength === 0);
  73. }.property('isStartStopAllClicked', 'content.@each.healthStatus'),
  74. /**
  75. * @type {bool}
  76. */
  77. isStartStopAllClicked: function () {
  78. return (App.router.get('backgroundOperationsController').get('allOperationsCount') !== 0);
  79. }.property('App.router.backgroundOperationsController.allOperationsCount'),
  80. /**
  81. * Callback for <code>start all service</code> button
  82. * @return {App.ModalPopup|null}
  83. * @method startAllService
  84. */
  85. startAllService: function (event) {
  86. return this.startStopAllService(event, 'STARTED');
  87. },
  88. /**
  89. * Callback for <code>stop all service</code> button
  90. * @return {App.ModalPopup|null}
  91. * @method stopAllService
  92. */
  93. stopAllService: function (event) {
  94. return this.startStopAllService(event, 'INSTALLED');
  95. },
  96. /**
  97. * Common method for "start-all", "stop-all" calls
  98. * @param {object} event
  99. * @param {string} state 'STARTED|INSTALLED'
  100. * @returns {App.ModalPopup|null}
  101. * @method startStopAllService
  102. */
  103. startStopAllService: function(event, state) {
  104. if ($(event.target).hasClass('disabled') || $(event.target.parentElement).hasClass('disabled')) {
  105. return null;
  106. }
  107. var self = this;
  108. var bodyMessage = Em.Object.create({
  109. confirmMsg: state == 'INSTALLED' ? Em.I18n.t('services.service.stopAll.confirmMsg') : Em.I18n.t('services.service.startAll.confirmMsg'),
  110. confirmButton: state == 'INSTALLED' ? Em.I18n.t('services.service.stop.confirmButton') : Em.I18n.t('services.service.start.confirmButton')
  111. });
  112. return App.showConfirmationFeedBackPopup(function (query) {
  113. self.allServicesCall(state, query);
  114. }, bodyMessage);
  115. },
  116. /**
  117. * Do request to server for "start|stop" all services
  118. * @param {string} state "STARTED|INSTALLED"
  119. * @param {object} query
  120. * @method allServicesCall
  121. * @return {$.ajax}
  122. */
  123. allServicesCall: function (state, query) {
  124. var context = (state == 'INSTALLED') ? App.BackgroundOperationsController.CommandContexts.STOP_ALL_SERVICES :
  125. App.BackgroundOperationsController.CommandContexts.START_ALL_SERVICES;
  126. return App.ajax.send({
  127. name: 'common.services.update',
  128. sender: this,
  129. data: {
  130. context: context,
  131. ServiceInfo: {
  132. state: state
  133. },
  134. query: query
  135. },
  136. success: 'allServicesCallSuccessCallback',
  137. error: 'allServicesCallErrorCallback'
  138. });
  139. },
  140. /**
  141. * Restart all services - stops all services, then starts them back
  142. */
  143. restartAllServices: function () {
  144. this.silentStopAllServices();
  145. },
  146. /**
  147. * Silent stop all services - without user confirmation
  148. * @returns {$.ajax}
  149. */
  150. silentStopAllServices: function () {
  151. return App.ajax.send({
  152. name: 'common.services.update',
  153. sender: this,
  154. data: {
  155. context: App.BackgroundOperationsController.CommandContexts.STOP_ALL_SERVICES,
  156. ServiceInfo: {
  157. state: 'INSTALLED'
  158. }
  159. },
  160. success: 'silentStopSuccess'
  161. });
  162. },
  163. /**
  164. * Success callback for silent stop
  165. */
  166. silentStopSuccess: function () {
  167. var self = this;
  168. App.router.get('applicationController').dataLoading().done(function (initValue) {
  169. if (initValue) {
  170. App.router.get('backgroundOperationsController').showPopup();
  171. }
  172. Ember.run.later(function () {
  173. self.set('shouldStart', true);
  174. }, App.bgOperationsUpdateInterval);
  175. });
  176. },
  177. /**
  178. * Silent start all services - without user confirmation
  179. */
  180. silentStartAllServices: function () {
  181. if (!App.router.get('backgroundOperationsController').get('allOperationsCount')) {
  182. if (this.get('shouldStart')) {
  183. this.set('shouldStart', false);
  184. return App.ajax.send({
  185. name: 'common.services.update',
  186. sender: this,
  187. data: {
  188. context: App.BackgroundOperationsController.CommandContexts.START_ALL_SERVICES,
  189. ServiceInfo: {
  190. state: 'STARTED'
  191. }
  192. },
  193. success: 'silentCallSuccessCallback'
  194. });
  195. }
  196. }
  197. }.observes('shouldStart', 'controllers.backgroundOperationsController.allOperationsCount'),
  198. /**
  199. * Success callback for silent start
  200. */
  201. silentCallSuccessCallback: function () {
  202. // load data (if we need to show this background operations popup) from persist
  203. App.router.get('applicationController').dataLoading().done(function (initValue) {
  204. if (initValue) {
  205. App.router.get('backgroundOperationsController').showPopup();
  206. }
  207. });
  208. },
  209. /**
  210. * Success-callback for all-services request
  211. * @param {object} data
  212. * @param {object} xhr
  213. * @param {object} params
  214. * @method allServicesCallSuccessCallback
  215. */
  216. allServicesCallSuccessCallback: function (data, xhr, params) {
  217. params.query.set('status', 'SUCCESS');
  218. // load data (if we need to show this background operations popup) from persist
  219. App.router.get('applicationController').dataLoading().done(function (initValue) {
  220. if (initValue) {
  221. App.router.get('backgroundOperationsController').showPopup();
  222. }
  223. });
  224. },
  225. /**
  226. * Error-callback for all-services request
  227. * @param {object} request
  228. * @param {object} ajaxOptions
  229. * @param {string} error
  230. * @param {object} opt
  231. * @param {object} params
  232. * @method allServicesCallErrorCallback
  233. */
  234. allServicesCallErrorCallback: function (request, ajaxOptions, error, opt, params) {
  235. params.query.set('status', 'FAIL');
  236. },
  237. /**
  238. * "Add-service"-click handler
  239. * @method gotoAddService
  240. */
  241. gotoAddService: function () {
  242. if (this.get('isAllServicesInstalled')) {
  243. return;
  244. }
  245. App.router.get('addServiceController').setDBProperty('onClosePath', 'main.services.index');
  246. App.router.transitionTo('main.serviceAdd');
  247. }
  248. });