main.js 9.5 KB

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