host.js 8.2 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. var validator = require('utils/validator');
  20. require('models/service');
  21. require('models/cluster');
  22. require('models/host');
  23. App.MainHostController = Em.ArrayController.extend(App.Pagination, {
  24. name:'mainHostController',
  25. content:[],
  26. fullContent:App.Host.find(),
  27. clusters:App.Cluster.find(),
  28. componentsForFilter:App.Component.find(),
  29. totalBinding:'fullContent.length',
  30. filters:{components:[]},
  31. pageSize:3,
  32. pageSizeRange:[1, 3, 5, 'all'],
  33. rangeStart:0,
  34. allChecked:false,
  35. selectedHostsIds:[],
  36. sortingAsc:true,
  37. isSort:false,
  38. sortClass:function () {
  39. return this.get('sortingAsc') ? 'icon-arrow-down' : 'icon-arrow-up';
  40. }.property('sortingAsc'),
  41. isDisabled:true,
  42. checkRemoved: function(host_id) {
  43. var hosts = this.get('content');
  44. var selectedHosts = hosts.filterProperty('id', host_id);
  45. this.get('fullContent').removeObjects(selectedHosts);
  46. },
  47. masterComponents:function () {
  48. var components = [];
  49. this.get('componentsForFilter').forEach(function (component) {
  50. if (component.get('isMaster')) {
  51. components.push(component);
  52. }
  53. });
  54. return components;
  55. }.property('componentsForFilter'),
  56. slaveComponents:function () {
  57. var components = [];
  58. this.get('componentsForFilter').forEach(function (component) {
  59. if (component.get('isSlave')) {
  60. components.push(component);
  61. }
  62. });
  63. return components;
  64. }.property('componentsForFilter'),
  65. backgroundOperationsCount:function () {
  66. return 5;
  67. }.property(),
  68. onAllChecked:function () {
  69. var hosts = this.get('content');
  70. hosts.setEach('isChecked', this.get('allChecked'));
  71. this.set('isDisabled', !this.get('allChecked'));
  72. var selectedHostsIds = this.get('allChecked') ? hosts.getEach('id') : [];
  73. this.set('selectedHostsIds', selectedHostsIds);
  74. }.observes('allChecked'),
  75. onHostChecked:function (host) {
  76. var selected = this.get('selectedHostsIds');
  77. host.set('isChecked', !host.get('isChecked'));
  78. if (host.get('isChecked')) selected.push(host.get('id'));
  79. else {
  80. var index = selected.indexOf(host.get('id'));
  81. if (index != -1) selected.splice(index, 1);
  82. }
  83. this.set('isDisabled', selected.length == 0);
  84. },
  85. changeSelectedHosts:function () {
  86. var visibleHosts = this.get('content');
  87. var selectedHosts = visibleHosts.filterProperty('isChecked', true);
  88. this.get('fullContent').forEach(function (item) {
  89. var index = visibleHosts.getEach('id').indexOf(item.get('id'));
  90. if (index == -1) item.set('isChecked', false);
  91. });
  92. this.set('isDisabled', selectedHosts.length == 0);
  93. this.set('selectedHostsIds', selectedHosts.getEach('id'));
  94. },
  95. checkedComponentsIds:function () {
  96. var checked = [];
  97. this.get('componentsForFilter').forEach(function (comp) {
  98. if (comp.get('checkedForHostFilter'))
  99. checked.push(comp.get('id'));
  100. });
  101. return checked;
  102. },
  103. filterByComponentsIds:function () {
  104. var componentsIds = this.checkedComponentsIds();
  105. this.set('filters.components', componentsIds);
  106. this.get('componentsForFilter').forEach(function(component) {
  107. if (componentsIds.indexOf(component.get('id')) != -1){
  108. component.set('isChecked', false);
  109. } else component.set('isChecked', true);
  110. });
  111. this.changeContent();
  112. },
  113. filterHostsBy:function (field, value) {
  114. this.set('hostFilter' + field, value);
  115. this.changeContent();
  116. },
  117. filterByComponent:function (component) {
  118. this.get('componentsForFilter').setEach('isChecked', false);
  119. component.set('isChecked', true);
  120. this.set('filters.components', [component.get('id')]);
  121. this.changeContent();
  122. },
  123. applyHostFilters:function (items) {
  124. var field = 'hostName'; // make this function universal
  125. var value = this.get('hostFilter' + field);
  126. var itemsToDelete = [];
  127. if (value) {
  128. items.forEach(function (host, index) {
  129. if (host) {
  130. var fieldValue = host.get(field);
  131. if (fieldValue) {
  132. if (fieldValue.indexOf(value) == -1) {
  133. itemsToDelete.push(host);
  134. }
  135. }
  136. }
  137. });
  138. }
  139. if (itemsToDelete.length) {
  140. itemsToDelete.forEach(function (hostToDelete) {
  141. var index = items.indexOf(hostToDelete);
  142. items.removeAt(index);
  143. })
  144. }
  145. return items;
  146. },
  147. changeContent:function () {
  148. var items = [];
  149. var filters = this.get('filters.components');
  150. this.get('fullContent').forEach(function (item) {
  151. if (filters.length) {
  152. var inFilters = false;
  153. item.get('components').forEach(function(component) {
  154. if (filters.indexOf(component.get('id')) == -1){
  155. inFilters = true;
  156. }
  157. });
  158. if (inFilters) {
  159. items.push(item);
  160. }
  161. } else {
  162. items.push(item);
  163. }
  164. });
  165. items = this.applyHostFilters(items);
  166. this.set('total', items.length);
  167. var content = items.slice(this.get('rangeStart'), this.get('rangeStop'));
  168. this.replace(0, this.get('length'), content);
  169. this.changeSelectedHosts();
  170. }.observes('rangeStart', 'rangeStop', 'total'),
  171. showNextPage:function () {
  172. this.nextPage();
  173. },
  174. showPreviousPage:function () {
  175. this.previousPage();
  176. },
  177. assignedToRackPopup:function (event) {
  178. var self = this;
  179. App.ModalPopup.show({
  180. header:Em.I18n.t('hosts.assignedToRack.popup.header'),
  181. body:Em.I18n.t('hosts.assignedToRack.popup.body'),
  182. primary:'Yes',
  183. secondary:'No',
  184. onPrimary:function () {
  185. self.assignedToRack(event.context);
  186. this.hide();
  187. },
  188. onSecondary:function () {
  189. this.hide();
  190. }
  191. });
  192. },
  193. assignedToRack:function (rack) {
  194. var hosts = this.get('content');
  195. var selectedHosts = hosts.filterProperty('isChecked', true);
  196. selectedHosts.setEach('cluster', rack);
  197. },
  198. decommissionButtonPopup:function () {
  199. var self = this;
  200. App.ModalPopup.show({
  201. header:Em.I18n.t('hosts.decommission.popup.header'),
  202. body:Em.I18n.t('hosts.decommission.popup.body'),
  203. primary:'Yes',
  204. secondary:'No',
  205. onPrimary:function () {
  206. alert('do');
  207. this.hide();
  208. },
  209. onSecondary:function () {
  210. this.hide();
  211. }
  212. });
  213. },
  214. deleteButtonPopup:function () {
  215. var self = this;
  216. App.ModalPopup.show({
  217. header:Em.I18n.t('hosts.delete.popup.header'),
  218. body:Em.I18n.t('hosts.delete.popup.body'),
  219. primary:'Yes',
  220. secondary:'No',
  221. onPrimary:function () {
  222. self.removeHosts();
  223. this.hide();
  224. },
  225. onSecondary:function () {
  226. this.hide();
  227. }
  228. });
  229. },
  230. removeHosts:function () {
  231. var hosts = this.get('content');
  232. var selectedHosts = hosts.filterProperty('isChecked', true);
  233. selectedHosts.forEach(function (_hostInfo) {
  234. console.log('Removing: ' + _hostInfo.hostName);
  235. });
  236. // App.db.removeHosts(selectedHosts);
  237. this.get('fullContent').removeObjects(selectedHosts);
  238. },
  239. sortByName:function () {
  240. var asc = this.get('sortingAsc');
  241. var objects = this.get('fullContent').toArray().sort(function (a, b) {
  242. var nA = a.get('hostName').toLowerCase();
  243. var nB = b.get('hostName').toLowerCase();
  244. if (nA < nB)
  245. return asc ? -1 : 1;
  246. else if (nA > nB)
  247. return asc ? 1 : -1;
  248. return 0;
  249. });
  250. this.set('fullContent', objects);
  251. this.set('isSort', true);
  252. this.set('sortingAsc', !this.get('sortingAsc'));
  253. this.changeContent();
  254. }
  255. });