step9_controller.js 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  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.InstallerStep9Controller = Em.ArrayController.extend({
  20. name: 'installerStep9Controller',
  21. content: [],
  22. progress: '0',
  23. // result: 'pending', // val = pending or success or failed
  24. isStepCompleted: false,
  25. isSubmitDisabled: function() {
  26. return !this.get('isStepCompleted');
  27. }.property('isStepCompleted'),
  28. status: 'info',
  29. mockHostData: require('data/mock/step9_hosts'),
  30. pollData_1: require('data/mock/step9_pollData_1'),
  31. pollData_2: require('data/mock/step9_pollData_2'),
  32. pollDataCounter: 0,
  33. loadHosts: function () {
  34. var hostInfo = [];
  35. hostInfo = App.db.getHosts();
  36. var hosts = new Ember.Set();
  37. for (var index in hostInfo) {
  38. hostInfo[index].status = "pending";
  39. hosts.add(hostInfo[index]);
  40. console.log("TRACE: host name is: " + hostInfo[index].name);
  41. }
  42. return hosts;
  43. },
  44. renderHosts: function (hostsInfo) {
  45. var self = this;
  46. hostsInfo.forEach(function (_hostInfo) {
  47. var hostInfo = App.HostInfo.create({
  48. name: _hostInfo.name,
  49. message: _hostInfo.message,
  50. progress: '0'
  51. });
  52. console.log('pushing ' + hostInfo.name);
  53. self.content.pushObject(hostInfo);
  54. });
  55. },
  56. onSuccessPerHost: function (actions, contentHost) {
  57. if (actions.everyProperty('status', 'completed')) {
  58. contentHost.set('status', 'success');
  59. }
  60. },
  61. onWarningPerHost: function (actions, contentHost) {
  62. if (actions.findProperty('status', 'failed') || actions.findProperty('status', 'aborted')) {
  63. contentHost.set('status', 'warning');
  64. this.set('status', 'warning');
  65. }
  66. },
  67. onInProgressPerHost: function (actions, contentHost) {
  68. var runningAction = actions.findProperty('status', 'inprogress');
  69. if (runningAction !== null && runningAction !== undefined) {
  70. contentHost.set('message', runningAction.message);
  71. }
  72. },
  73. progressPerHost: function (actions, contentHost) {
  74. var totalProgress = 0;
  75. var actionsPerHost = actions.length;
  76. var completedActions = actions.filterProperty('status', 'completed').length
  77. + actions.filterProperty('status', 'failed').length +
  78. actions.filterProperty('status', 'aborted').length;
  79. var progress = (completedActions / actionsPerHost) * 100;
  80. console.log('INFO: progressPerHost is: ' + progress);
  81. contentHost.set('progress', progress.toString());
  82. return progress;
  83. },
  84. isSuccess: function (polledData) {
  85. return polledData.everyProperty('status', 'success');
  86. },
  87. isStepFailed: function (polledData) {
  88. var self = this;
  89. var result = false;
  90. polledData.forEach(function (_polledData) {
  91. var successFactor = _polledData.sf;
  92. var actionsPerRole = polledData.filterProperty('role', _polledData.role);
  93. var actionsFailed = actionsPerRole.filterProperty('status', 'failed');
  94. var actionsAborted = actionsPerRole.filterProperty('status', 'aborted');
  95. if ((((actionsFailed.length + actionsAborted.length) / actionsPerRole.length) * 100) <= successFactor) {
  96. console.log('TRACE: Entering success factor and result is failed');
  97. result = true;
  98. }
  99. });
  100. return result;
  101. },
  102. getFailedHostsForFailedRoles: function(polledData) {
  103. var hostArr = new Ember.Set();
  104. polledData.forEach(function (_polledData) {
  105. var successFactor = _polledData.sf;
  106. var actionsPerRole = polledData.filterProperty('role', _polledData.role);
  107. var actionsFailed = actionsPerRole.filterProperty('status', 'failed');
  108. var actionsAborted = actionsPerRole.filterProperty('status', 'aborted');
  109. if ((((actionsFailed.length + actionsAborted.length) / actionsPerRole.length) * 100) <= successFactor) {
  110. actionsFailed.forEach(function(_actionFailed) {
  111. hostArr.add(_actionFailed.name);
  112. });
  113. actionsAborted.forEach(function(_actionFailed) {
  114. hostArr.add(_actionFailed.name);
  115. });
  116. }
  117. });
  118. return hostArr;
  119. },
  120. setHostsStatus: function(hosts,status) {
  121. var self = this;
  122. hosts.forEach(function(_host){
  123. var host = self.findProperty('name',_host);
  124. host.set('status',status);
  125. });
  126. },
  127. // polling from ui stops only when no action has 'pending', 'queued' or 'inprogress' status
  128. finishStep: function (polledData) {
  129. var self = this;
  130. if (!polledData.someProperty('status', 'pending') && !polledData.someProperty('status', 'queued') && !polledData.someProperty('status', 'inprogress')) {
  131. this.set('progress', '100');
  132. if (this.isSuccess(polledData)) {
  133. this.set('status', 'success');
  134. } else {
  135. if (this.isStepFailed(polledData)) {
  136. self.set('status', 'failed');
  137. this.setHostsStatus(this.getFailedHostsForFailedRoles(polledData),'failed');
  138. }
  139. }
  140. this.set('isStepCompleted',true);
  141. }
  142. },
  143. parseHostInfo: function (polledData) {
  144. console.log('TRACE: Entering host info function');
  145. var self = this;
  146. var result = false;
  147. var totalProgress = 0;
  148. this.forEach(function (_content) {
  149. var actions = polledData.filterProperty('name', _content.name);
  150. if(actions.length === 0) {
  151. alert('For testing with mockData follow the sequence: hit referesh,"mockData btn", "pollData btn", again "pollData btn"');
  152. //exit();
  153. }
  154. if (actions !== null && actions !== undefined && actions.length !== 0) {
  155. self.onSuccessPerHost(actions, _content); // every action should be a success
  156. self.onWarningPerHost(actions, _content); // any action should be a faliure
  157. self.onInProgressPerHost(actions, _content); // current running action for a host
  158. totalProgress = totalProgress + self.progressPerHost(actions, _content);
  159. }
  160. });
  161. totalProgress = totalProgress / this.content.length;
  162. this.set('progress', totalProgress.toString());
  163. console.log("INFO: right now the progress is: " + this.get('progress'));
  164. this.finishStep(polledData);
  165. return this.get('isStepCompleted');
  166. },
  167. retry: function () {
  168. if (this.get('isSubmitDisabled')) {
  169. return;
  170. }
  171. this.clear();
  172. this.renderHosts(this.loadHosts());
  173. //this.startPolling();
  174. },
  175. startPolling: function () {
  176. this.set('isSubmitDisabled', true);
  177. this.doPolling();
  178. },
  179. doPolling: function () {
  180. var self = this;
  181. $.ajax({
  182. type: 'GET',
  183. url: '/ambari_server/api/polling',
  184. async: false,
  185. timeout: 5000,
  186. success: function (data) {
  187. console.log("TRACE: In success function for the GET bootstrap call");
  188. var result = self.parseHostInfo(data);
  189. if (result !== true) {
  190. window.setTimeout(self.doPolling, 3000);
  191. } else {
  192. self.stopPolling();
  193. }
  194. },
  195. error: function () {
  196. console.log("ERROR");
  197. self.stopPolling();
  198. },
  199. statusCode: {
  200. 404: function () {
  201. console.log("URI not found.");
  202. }
  203. },
  204. dataType: 'application/json'
  205. });
  206. },
  207. stopPolling: function () {
  208. //TODO: uncomment following line after the hook up with the API call
  209. // this.set('isStepCompleted',true);
  210. },
  211. submit: function () {
  212. if (!this.get('isSubmitDisabled')) {
  213. //var erroneousHosts = hostResult.filterProperty('status', 'error');
  214. App.get('router').transitionTo('step4');
  215. }
  216. },
  217. hostLogPopup: function (event) {
  218. App.ModalPopup.show({
  219. header: Em.I18n.t('installer.step3.hostLog.popup.header'),
  220. onPrimary: function () {
  221. this.hide();
  222. },
  223. bodyClass: Ember.View.extend({
  224. templateName: require('templates/installer/step3HostLogPopup')
  225. })
  226. });
  227. },
  228. mockBtn: function () {
  229. this.set('isSubmitDisabled', false);
  230. this.clear();
  231. var hostInfo = this.mockHostData;
  232. this.renderHosts(hostInfo);
  233. },
  234. pollBtn: function () {
  235. this.set('isSubmitDisabled', false);
  236. var data1 = this.pollData_1;
  237. var data2 = this.pollData_2;
  238. if ((this.get('pollDataCounter') / 2) === 0) {
  239. console.log("TRACE: In pollBtn function data1");
  240. var counter = parseInt(this.get('pollDataCounter')) + 1;
  241. this.set('pollDataCounter', counter.toString());
  242. this.parseHostInfo(data1);
  243. } else {
  244. console.log("TRACE: In pollBtn function data2");
  245. var counter = parseInt(this.get('pollDataCounter')) + 1;
  246. this.set('pollDataCounter', counter.toString());
  247. this.parseHostInfo(data2);
  248. }
  249. }
  250. });