main.js 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  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. /**
  109. * check server version and web client version
  110. */
  111. checkServerClientVersion: function () {
  112. var dfd = $.Deferred();
  113. var self = this;
  114. self.getServerVersion().done(function () {
  115. dfd.resolve();
  116. });
  117. return dfd.promise();
  118. },
  119. getServerVersion: function(){
  120. return App.ajax.send({
  121. name: 'ambari.service',
  122. sender: this,
  123. data: {
  124. fields: '?fields=RootServiceComponents/component_version,RootServiceComponents/properties/server.os_family&minimal_response=true'
  125. },
  126. success: 'getServerVersionSuccessCallback',
  127. error: 'getServerVersionErrorCallback'
  128. });
  129. },
  130. getServerVersionSuccessCallback: function (data) {
  131. var clientVersion = App.get('version');
  132. var serverVersion = (data.RootServiceComponents.component_version).toString();
  133. this.set('ambariServerVersion', serverVersion);
  134. if (clientVersion) {
  135. this.set('versionConflictAlertBody', Em.I18n.t('app.versionMismatchAlert.body').format(serverVersion, clientVersion));
  136. this.set('isServerClientVersionMismatch', clientVersion != serverVersion);
  137. } else {
  138. this.set('isServerClientVersionMismatch', false);
  139. }
  140. App.set('isManagedMySQLForHiveEnabled', App.config.isManagedMySQLForHiveAllowed(data.RootServiceComponents.properties['server.os_family']));
  141. },
  142. getServerVersionErrorCallback: function () {
  143. },
  144. monitorInactivity: function() {
  145. var timeout = Number(App.router.get('clusterController.ambariProperties')['user.inactivity.timeout.default']);
  146. var readonly_timeout = Number(App.router.get('clusterController.ambariProperties')['user.inactivity.timeout.role.readonly.default']);
  147. var isAdmin = App.get('isAdmin');
  148. if (isAdmin && timeout > 0) {
  149. this.set('userTimeOut', timeout * 1000);
  150. } else if (!isAdmin && readonly_timeout > 0) {
  151. this.set('userTimeOut', readonly_timeout * 1000);
  152. }
  153. if (this.get('userTimeOut') > 0) {
  154. this.startMonitorInactivity();
  155. }
  156. },
  157. startMonitorInactivity: function() {
  158. this.set('isUserActive', true);
  159. this.set('lastUserActiveTime', Date.now());
  160. this.rebindActivityEventMonitors();
  161. if (!this.get('checkActivenessInterval')) {
  162. this.set('checkActivenessInterval', window.setInterval(this.checkActiveness, 1000));
  163. }
  164. },
  165. /* this will be triggerred by user driven events: 'mousemove', 'keypress' and 'click' */
  166. keepActive: function() {
  167. var scope = App.router.get('mainController');
  168. if (scope.get('isUserActive')) {
  169. scope.set('lastUserActiveTime', Date.now());
  170. }
  171. },
  172. checkActiveness: function() {
  173. var scope = App.router.get('mainController');
  174. if (!scope.isOnWizard()) {
  175. var remainTime = scope.get('userTimeOut') - (Date.now() - scope.get('lastUserActiveTime'));
  176. if (remainTime < 0) {
  177. scope.set('isUserActive', false);
  178. scope.unbindActivityEventMonitors();
  179. clearInterval(scope.get('checkActivenessInterval'));
  180. App.router.logOff({});
  181. } else if (remainTime < App.inactivityRemainTime * 1000 && !scope.userTimeOutModal) {
  182. // show alert 60 seconds before logging user out
  183. scope.userTimeOutModal = App.ModalPopup.show({
  184. primary: Em.I18n.t('common.timeout.warning.popup.primary'),
  185. secondary: Em.I18n.t('common.timeout.warning.popup.secondary'),
  186. third: false,
  187. header: Em.I18n.t('common.timeout.warning.popup.header'),
  188. showCloseButton: false,
  189. bodyClass: Ember.View.extend({
  190. template: Ember.Handlebars.compile('<p>{{view.beforeMsg}}<b>{{view.remainTime}}</b>{{view.afterMsg}}</p>'),
  191. beforeMsg: Em.I18n.t('common.timeout.warning.popup.body.before'),
  192. afterMsg: Em.I18n.t('common.timeout.warning.popup.body.after'),
  193. remainTime: App.inactivityRemainTime,
  194. didInsertElement: function() {
  195. var self = this;
  196. setInterval(function(){self.countDown();}, 1000)
  197. },
  198. countDown: function() {
  199. if (this.get('remainTime') > 0) {
  200. this.set('remainTime', this.get('remainTime') - 1);
  201. }
  202. if (this.get('remainTime') == 0) {
  203. App.router.logOff({});
  204. }
  205. }
  206. }),
  207. onPrimary: function() {
  208. scope.keepActive();
  209. scope.userTimeOutModal.hide();
  210. delete scope.userTimeOutModal;
  211. },
  212. onSecondary: function() {
  213. scope.userTimeOutModal.hide();
  214. delete scope.userTimeOutModal;
  215. App.router.logOff({});
  216. }
  217. });
  218. }
  219. }
  220. },
  221. rebindActivityEventMonitors: function() {
  222. this.unbindActivityEventMonitors();
  223. this.bindActivityEventMonitors();
  224. },
  225. isOnWizard: function() {
  226. var isWizard = window.location.href.indexOf('/step') != -1;
  227. var isUpgrade = window.location.href.indexOf('/stack/upgrade') != -1;
  228. return isWizard || isUpgrade;
  229. },
  230. bindActivityEventMonitors: function() {
  231. $(window).bind('mousemove', this.keepActive);
  232. $(window).bind('keypress', this.keepActive);
  233. $(window).bind('click', this.keepActive);
  234. // iframes need to be monitored as well
  235. var iframes = $('iframe');
  236. if (iframes.length > 0) {
  237. for (var i = 0; i < iframes.length; i++) {
  238. var iframe = iframes[i];
  239. $(iframe.contentWindow).bind('mousemove', this.keepActive);
  240. $(iframe.contentWindow).bind('keypress', this.keepActive);
  241. $(iframe.contentWindow).bind('click', this.keepActive);
  242. }
  243. }
  244. },
  245. unbindActivityEventMonitors: function() {
  246. $(window).unbind('mousemove', this.keepActive);
  247. $(window).unbind('keypress', this.keepActive);
  248. $(window).unbind('click', this.keepActive);
  249. // iframes need to be monitored as well
  250. var iframes = $('iframe');
  251. if (iframes.length > 0) {
  252. for (var i = 0; i < iframes.length; i++) {
  253. var iframe = iframes[i];
  254. $(iframe.contentWindow).unbind('mousemove', this.keepActive);
  255. $(iframe.contentWindow).unbind('keypress', this.keepActive);
  256. $(iframe.contentWindow).unbind('click', this.keepActive);
  257. }
  258. }
  259. }
  260. });