host.js 9.7 KB

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