host.js 8.8 KB

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