main.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  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. require('models/background_operation');
  20. App.MainController = Em.Controller.extend({
  21. name: 'mainController',
  22. isUserActive: true,
  23. checkActivenessInterval: null,
  24. lastUserActiveTime: null,
  25. userTimeOut: 0,
  26. userTimeOutModal: null,
  27. updateTitle: function(){
  28. var name = App.router.get('clusterController.clusterName');
  29. if(App.router.get('clusterInstallCompleted')) {
  30. if (name && App.router.get('clusterController').get('isLoaded')) {
  31. name = name.length > 13 ? name.substr(0, 10) + "..." : name;
  32. } else {
  33. name = Em.I18n.t('common.loading');
  34. }
  35. $('title').text(Em.I18n.t('app.name.subtitle').format(name));
  36. }
  37. }.observes('App.router.clusterController.clusterName, App.router.clusterInstallCompleted', 'App.router.clusterController.isLoaded'),
  38. isClusterDataLoaded: Em.computed.alias('App.router.clusterController.isLoaded'),
  39. clusterDataLoadedPercent: Em.computed.alias('App.router.clusterController.clusterDataLoadedPercent'),
  40. /**
  41. * run all processes and cluster's data loading
  42. */
  43. initialize: function(){
  44. App.router.get('clusterController').loadClusterData();
  45. },
  46. dataLoading: function () {
  47. var self = this;
  48. var dfd = $.Deferred();
  49. if (App.router.get('clusterController.isLoaded')) {
  50. dfd.resolve();
  51. } else {
  52. var interval = setInterval(function () {
  53. if (self.get('isClusterDataLoaded')) {
  54. dfd.resolve();
  55. clearInterval(interval);
  56. }
  57. }, 50);
  58. }
  59. return dfd.promise();
  60. },
  61. /**
  62. *
  63. * @param isLoaded {Boolean}
  64. * @param opts {Object}
  65. * {
  66. * period {Number}
  67. * }
  68. * @return {*|{then}}
  69. */
  70. isLoading: function(isLoaded, opts) {
  71. var dfd = $.Deferred();
  72. var self = this;
  73. opts = opts || {};
  74. var period = opts.period || 20;
  75. if (this.get(isLoaded)) {
  76. dfd.resolve();
  77. } else {
  78. var interval = setInterval(function () {
  79. if (self.get(isLoaded)) {
  80. dfd.resolve();
  81. clearInterval(interval);
  82. }
  83. }, period);
  84. }
  85. return dfd.promise();
  86. },
  87. startPolling: function () {
  88. if (App.router.get('applicationController.isExistingClusterDataLoaded')) {
  89. App.router.get('updateController').set('isWorking', true);
  90. App.router.get('backgroundOperationsController').set('isWorking', true);
  91. }
  92. }.observes('App.router.applicationController.isExistingClusterDataLoaded'),
  93. stopPolling: function(){
  94. App.router.get('updateController').set('isWorking', false);
  95. App.router.get('backgroundOperationsController').set('isWorking', false);
  96. },
  97. reloadTimeOut: null,
  98. pageReload: function () {
  99. clearTimeout(this.get("reloadTimeOut"));
  100. this.set('reloadTimeOut',
  101. setTimeout(function () {
  102. if (App.clusterStatus.get('isInstalled')) {
  103. location.reload();
  104. }
  105. }, App.pageReloadTime)
  106. );
  107. }.observes("App.router.location.lastSetURL", "App.clusterStatus.isInstalled"),
  108. scRequest: function(request) {
  109. return App.router.get('mainServiceController').get(request);
  110. },
  111. isAllServicesInstalled: function() {
  112. return this.scRequest('isAllServicesInstalled');
  113. }.property('App.router.mainServiceController.content.content.@each',
  114. 'App.router.mainServiceController.content.content.length'),
  115. isStartAllDisabled: function() {
  116. return this.scRequest('isStartAllDisabled');
  117. }.property('App.router.mainServiceController.isStartStopAllClicked',
  118. 'App.router.mainServiceController.content.@each.healthStatus'),
  119. isStopAllDisabled: function() {
  120. return this.scRequest('isStopAllDisabled');
  121. }.property('App.router.mainServiceController.isStartStopAllClicked',
  122. 'App.router.mainServiceController.content.@each.healthStatus'),
  123. gotoAddService: function() {
  124. App.router.get('mainServiceController').gotoAddService();
  125. },
  126. startAllService: function(event){
  127. App.router.get('mainServiceController').startAllService(event);
  128. },
  129. stopAllService: function(event){
  130. App.router.get('mainServiceController').stopAllService(event);
  131. },
  132. /**
  133. * check server version and web client version
  134. */
  135. checkServerClientVersion: function () {
  136. var dfd = $.Deferred();
  137. var self = this;
  138. self.getServerVersion().done(function () {
  139. dfd.resolve();
  140. });
  141. return dfd.promise();
  142. },
  143. getServerVersion: function(){
  144. return App.ajax.send({
  145. name: 'ambari.service',
  146. sender: this,
  147. data: {
  148. fields: '?fields=RootServiceComponents/component_version,RootServiceComponents/properties/server.os_family&minimal_response=true'
  149. },
  150. success: 'getServerVersionSuccessCallback',
  151. error: 'getServerVersionErrorCallback'
  152. });
  153. },
  154. getServerVersionSuccessCallback: function (data) {
  155. var clientVersion = App.get('version');
  156. var serverVersion = (data.RootServiceComponents.component_version).toString();
  157. this.set('ambariServerVersion', serverVersion);
  158. if (clientVersion) {
  159. this.set('versionConflictAlertBody', Em.I18n.t('app.versionMismatchAlert.body').format(serverVersion, clientVersion));
  160. this.set('isServerClientVersionMismatch', clientVersion != serverVersion);
  161. } else {
  162. this.set('isServerClientVersionMismatch', false);
  163. }
  164. App.set('isManagedMySQLForHiveEnabled', App.config.isManagedMySQLForHiveAllowed(data.RootServiceComponents.properties['server.os_family']));
  165. },
  166. getServerVersionErrorCallback: function () {
  167. },
  168. monitorInactivity: function() {
  169. var timeout = Number(App.router.get('clusterController.ambariProperties')['user.inactivity.timeout.default']);
  170. var readonly_timeout = Number(App.router.get('clusterController.ambariProperties')['user.inactivity.timeout.role.readonly.default']);
  171. var isAdmin = App.get('isAdmin');
  172. if (isAdmin && timeout > 0) {
  173. this.set('userTimeOut', timeout * 1000);
  174. } else if (!isAdmin && readonly_timeout > 0) {
  175. this.set('userTimeOut', readonly_timeout * 1000);
  176. }
  177. if (this.get('userTimeOut') > 0) {
  178. this.startMonitorInactivity();
  179. }
  180. },
  181. startMonitorInactivity: function() {
  182. this.set('isUserActive', true);
  183. this.set('lastUserActiveTime', Date.now());
  184. this.rebindActivityEventMonitors();
  185. if (!this.get('checkActivenessInterval')) {
  186. this.set('checkActivenessInterval', window.setInterval(this.checkActiveness, 1000));
  187. }
  188. },
  189. /* this will be triggerred by user driven events: 'mousemove', 'keypress' and 'click' */
  190. keepActive: function() {
  191. var scope = App.router.get('mainController');
  192. if (scope.get('isUserActive')) {
  193. scope.set('lastUserActiveTime', Date.now());
  194. }
  195. },
  196. checkActiveness: function() {
  197. var scope = App.router.get('mainController');
  198. if (!scope.isOnWizard()) {
  199. var remainTime = scope.get('userTimeOut') - (Date.now() - scope.get('lastUserActiveTime'));
  200. if (remainTime < 0) {
  201. scope.set('isUserActive', false);
  202. scope.unbindActivityEventMonitors();
  203. clearInterval(scope.get('checkActivenessInterval'));
  204. App.router.logOff({});
  205. } else if (remainTime < App.inactivityRemainTime * 1000 && !scope.userTimeOutModal) {
  206. // show alert 60 seconds before logging user out
  207. scope.userTimeOutModal = App.ModalPopup.show({
  208. primary: Em.I18n.t('common.timeout.warning.popup.primary'),
  209. secondary: Em.I18n.t('common.timeout.warning.popup.secondary'),
  210. third: false,
  211. header: Em.I18n.t('common.timeout.warning.popup.header'),
  212. showCloseButton: false,
  213. bodyClass: Ember.View.extend({
  214. template: Ember.Handlebars.compile('<p>{{view.beforeMsg}}<b>{{view.remainTime}}</b>{{view.afterMsg}}</p>'),
  215. beforeMsg: Em.I18n.t('common.timeout.warning.popup.body.before'),
  216. afterMsg: Em.I18n.t('common.timeout.warning.popup.body.after'),
  217. remainTime: App.inactivityRemainTime,
  218. didInsertElement: function() {
  219. var self = this;
  220. setInterval(function(){self.countDown();}, 1000)
  221. },
  222. countDown: function() {
  223. if (this.get('remainTime') > 0) {
  224. this.set('remainTime', this.get('remainTime') - 1);
  225. }
  226. if (this.get('remainTime') == 0) {
  227. App.router.logOff({});
  228. }
  229. }
  230. }),
  231. onPrimary: function() {
  232. scope.keepActive();
  233. scope.userTimeOutModal.hide();
  234. delete scope.userTimeOutModal;
  235. },
  236. onSecondary: function() {
  237. scope.userTimeOutModal.hide();
  238. delete scope.userTimeOutModal;
  239. App.router.logOff({});
  240. }
  241. });
  242. }
  243. }
  244. },
  245. rebindActivityEventMonitors: function() {
  246. this.unbindActivityEventMonitors();
  247. this.bindActivityEventMonitors();
  248. },
  249. isOnWizard: function() {
  250. var isWizard = window.location.href.indexOf('/step') != -1;
  251. var isUpgrade = window.location.href.indexOf('/stack/upgrade') != -1;
  252. return isWizard || isUpgrade;
  253. },
  254. bindActivityEventMonitors: function() {
  255. $(window).bind('mousemove', this.keepActive);
  256. $(window).bind('keypress', this.keepActive);
  257. $(window).bind('click', this.keepActive);
  258. // iframes need to be monitored as well
  259. var iframes = $('iframe');
  260. if (iframes.length > 0) {
  261. for (var i = 0; i < iframes.length; i++) {
  262. var iframe = iframes[i];
  263. $(iframe.contentWindow).bind('mousemove', this.keepActive);
  264. $(iframe.contentWindow).bind('keypress', this.keepActive);
  265. $(iframe.contentWindow).bind('click', this.keepActive);
  266. }
  267. }
  268. },
  269. unbindActivityEventMonitors: function() {
  270. $(window).unbind('mousemove', this.keepActive);
  271. $(window).unbind('keypress', this.keepActive);
  272. $(window).unbind('click', this.keepActive);
  273. // iframes need to be monitored as well
  274. var iframes = $('iframe');
  275. if (iframes.length > 0) {
  276. for (var i = 0; i < iframes.length; i++) {
  277. var iframe = iframes[i];
  278. $(iframe.contentWindow).unbind('mousemove', this.keepActive);
  279. $(iframe.contentWindow).unbind('keypress', this.keepActive);
  280. $(iframe.contentWindow).unbind('click', this.keepActive);
  281. }
  282. }
  283. }
  284. });