mainCtrl.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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. 'use strict';
  19. angular.module('ambariAdminConsole')
  20. .controller('MainCtrl',['$scope','$rootScope','$window','Auth', 'Alert', '$modal', 'Cluster', 'View', function($scope, $rootScope, $window, Auth, Alert, $modal, Cluster, View) {
  21. $scope.signOut = function() {
  22. var data = JSON.parse(localStorage.ambari);
  23. delete data.app.authenticated;
  24. delete data.app.loginName;
  25. delete data.app.user;
  26. localStorage.ambari = JSON.stringify(data);
  27. $scope.hello = "hello";
  28. Auth.signout().finally(function() {
  29. $window.location.pathname = '';
  30. });
  31. };
  32. $scope.ambariVersion = null;
  33. $scope.about = function() {
  34. var ambariVersion = $scope.ambariVersion;
  35. var modalInstance = $modal.open({
  36. templateUrl:'views/modals/AboutModal.html',
  37. controller: ['$scope', function($scope) {
  38. $scope.ok = function() {
  39. modalInstance.close();
  40. };
  41. $scope.ambariVersion = ambariVersion;
  42. }]
  43. });
  44. };
  45. $scope.currentUser = Auth.getCurrentUser();
  46. $scope.cluster = null;
  47. $scope.isLoaded = null;
  48. function loadAmbariVersion() {
  49. Cluster.getAmbariVersion().then(function(version){
  50. $scope.ambariVersion = version;
  51. });
  52. }
  53. function loadClusterData(){
  54. Cluster.getStatus().then(function(cluster) {
  55. $scope.cluster = cluster;
  56. $scope.isLoaded = true;
  57. if(cluster && cluster.Clusters.provisioning_state === 'INIT'){
  58. setTimeout(loadClusterData, 1000);
  59. }
  60. }).catch(function(data) {
  61. Alert.error('Cannot load cluster status', data.statusText);
  62. });
  63. }
  64. loadClusterData();
  65. loadAmbariVersion();
  66. $scope.viewInstances = [];
  67. $scope.updateInstances = function () {
  68. View.getAllVisibleInstance().then(function(instances) {
  69. $scope.viewInstances = instances;
  70. });
  71. };
  72. $scope.gotoViewsDashboard =function() {
  73. window.location = '/#/main/views';
  74. };
  75. $scope.$root.$on('instancesUpdate', function (event, data) {
  76. $scope.updateInstances();
  77. });
  78. $scope.startInactiveTimeoutMonitoring = function(timeout) {
  79. var TIME_OUT = timeout;
  80. var active = true;
  81. var lastActiveTime = Date.now();
  82. var keepActive = function() {
  83. //console.error('keepActive');
  84. if (active) {
  85. lastActiveTime = Date.now();
  86. }
  87. };
  88. $(window).bind('mousemove', keepActive);
  89. $(window).bind('keypress', keepActive);
  90. $(window).bind('click', keepActive);
  91. var checkActiveness = function() {
  92. //console.error("checkActiveness " + lastActiveTime + " : " + Date.now());
  93. if (Date.now() - lastActiveTime > TIME_OUT) {
  94. //console.error("LOGOUT!");
  95. active = false;
  96. $(window).unbind('mousemove', keepActive);
  97. $(window).unbind('keypress', keepActive);
  98. $(window).unbind('click', keepActive);
  99. clearInterval($rootScope.userActivityTimeoutInterval);
  100. $scope.signOut();
  101. }
  102. };
  103. $rootScope.userActivityTimeoutInterval = window.setInterval(checkActiveness, 1000);
  104. };
  105. if (!$rootScope.userActivityTimeoutInterval) {
  106. Cluster.getAmbariTimeout().then(function(timeout) {
  107. if (Number(timeout) > 0)
  108. $scope.startInactiveTimeoutMonitoring(timeout * 1000);
  109. });
  110. }
  111. $scope.updateInstances();
  112. }]);