github.com/ferranbt/nomad@v0.9.3-0.20190607002617-85c449b7667c/ui/app/controllers/clients/index.js (about) 1 import { alias } from '@ember/object/computed'; 2 import Controller, { inject as controller } from '@ember/controller'; 3 import { computed } from '@ember/object'; 4 import { scheduleOnce } from '@ember/runloop'; 5 import intersection from 'lodash.intersection'; 6 import Sortable from 'nomad-ui/mixins/sortable'; 7 import Searchable from 'nomad-ui/mixins/searchable'; 8 import { serialize, deserializedQueryParam as selection } from 'nomad-ui/utils/qp-serialize'; 9 10 export default Controller.extend(Sortable, Searchable, { 11 clientsController: controller('clients'), 12 13 nodes: alias('model.nodes'), 14 agents: alias('model.agents'), 15 16 queryParams: { 17 currentPage: 'page', 18 searchTerm: 'search', 19 sortProperty: 'sort', 20 sortDescending: 'desc', 21 qpClass: 'class', 22 qpStatus: 'status', 23 qpDatacenter: 'dc', 24 qpFlags: 'flags', 25 }, 26 27 currentPage: 1, 28 pageSize: 8, 29 30 sortProperty: 'modifyIndex', 31 sortDescending: true, 32 33 searchProps: computed(() => ['id', 'name', 'datacenter']), 34 35 qpClass: '', 36 qpStatus: '', 37 qpDatacenter: '', 38 qpFlags: '', 39 40 selectionClass: selection('qpClass'), 41 selectionStatus: selection('qpStatus'), 42 selectionDatacenter: selection('qpDatacenter'), 43 selectionFlags: selection('qpFlags'), 44 45 optionsClass: computed('nodes.[]', function() { 46 const classes = Array.from(new Set(this.nodes.mapBy('nodeClass'))).compact(); 47 48 // Remove any invalid node classes from the query param/selection 49 scheduleOnce('actions', () => { 50 this.set('qpClass', serialize(intersection(classes, this.selectionClass))); 51 }); 52 53 return classes.sort().map(dc => ({ key: dc, label: dc })); 54 }), 55 56 optionsStatus: computed(() => [ 57 { key: 'initializing', label: 'Initializing' }, 58 { key: 'ready', label: 'Ready' }, 59 { key: 'down', label: 'Down' }, 60 ]), 61 62 optionsDatacenter: computed('nodes.[]', function() { 63 const datacenters = Array.from(new Set(this.nodes.mapBy('datacenter'))).compact(); 64 65 // Remove any invalid datacenters from the query param/selection 66 scheduleOnce('actions', () => { 67 this.set( 68 'qpDatacenter', 69 serialize(intersection(datacenters, this.selectionDatacenter)) 70 ); 71 }); 72 73 return datacenters.sort().map(dc => ({ key: dc, label: dc })); 74 }), 75 76 optionsFlags: computed(() => [ 77 { key: 'ineligible', label: 'Ineligible' }, 78 { key: 'draining', label: 'Draining' }, 79 ]), 80 81 filteredNodes: computed( 82 'nodes.[]', 83 'selectionClass', 84 'selectionStatus', 85 'selectionDatacenter', 86 'selectionFlags', 87 function() { 88 const { 89 selectionClass: classes, 90 selectionStatus: statuses, 91 selectionDatacenter: datacenters, 92 selectionFlags: flags, 93 } = this; 94 95 const onlyIneligible = flags.includes('ineligible'); 96 const onlyDraining = flags.includes('draining'); 97 98 return this.nodes.filter(node => { 99 if (classes.length && !classes.includes(node.get('nodeClass'))) return false; 100 if (statuses.length && !statuses.includes(node.get('status'))) return false; 101 if (datacenters.length && !datacenters.includes(node.get('datacenter'))) return false; 102 103 if (onlyIneligible && node.get('isEligible')) return false; 104 if (onlyDraining && !node.get('isDraining')) return false; 105 106 return true; 107 }); 108 } 109 ), 110 111 listToSort: alias('filteredNodes'), 112 listToSearch: alias('listSorted'), 113 sortedNodes: alias('listSearched'), 114 115 isForbidden: alias('clientsController.isForbidden'), 116 117 setFacetQueryParam(queryParam, selection) { 118 this.set(queryParam, serialize(selection)); 119 }, 120 121 actions: { 122 gotoNode(node) { 123 this.transitionToRoute('clients.client', node); 124 }, 125 }, 126 });