github.com/manicqin/nomad@v0.9.5/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 SortableFactory from 'nomad-ui/mixins/sortable-factory';
     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(
    11    SortableFactory(['id', 'name', 'compositeStatus', 'datacenter']),
    12    Searchable,
    13    {
    14      clientsController: controller('clients'),
    15  
    16      nodes: alias('model.nodes'),
    17      agents: alias('model.agents'),
    18  
    19      queryParams: {
    20        currentPage: 'page',
    21        searchTerm: 'search',
    22        sortProperty: 'sort',
    23        sortDescending: 'desc',
    24        qpClass: 'class',
    25        qpState: 'state',
    26        qpDatacenter: 'dc',
    27      },
    28  
    29      currentPage: 1,
    30      pageSize: 8,
    31  
    32      sortProperty: 'modifyIndex',
    33      sortDescending: true,
    34  
    35      searchProps: computed(() => ['id', 'name', 'datacenter']),
    36  
    37      qpClass: '',
    38      qpState: '',
    39      qpDatacenter: '',
    40  
    41      selectionClass: selection('qpClass'),
    42      selectionState: selection('qpState'),
    43      selectionDatacenter: selection('qpDatacenter'),
    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      optionsState: computed(() => [
    57        { key: 'initializing', label: 'Initializing' },
    58        { key: 'ready', label: 'Ready' },
    59        { key: 'down', label: 'Down' },
    60        { key: 'ineligible', label: 'Ineligible' },
    61        { key: 'draining', label: 'Draining' },
    62      ]),
    63  
    64      optionsDatacenter: computed('nodes.[]', function() {
    65        const datacenters = Array.from(new Set(this.nodes.mapBy('datacenter'))).compact();
    66  
    67        // Remove any invalid datacenters from the query param/selection
    68        scheduleOnce('actions', () => {
    69          this.set('qpDatacenter', serialize(intersection(datacenters, this.selectionDatacenter)));
    70        });
    71  
    72        return datacenters.sort().map(dc => ({ key: dc, label: dc }));
    73      }),
    74  
    75      filteredNodes: computed(
    76        'nodes.[]',
    77        'selectionClass',
    78        'selectionState',
    79        'selectionDatacenter',
    80        function() {
    81          const {
    82            selectionClass: classes,
    83            selectionState: states,
    84            selectionDatacenter: datacenters,
    85          } = this;
    86  
    87          const onlyIneligible = states.includes('ineligible');
    88          const onlyDraining = states.includes('draining');
    89  
    90          // states is a composite of node status and other node states
    91          const statuses = states.without('ineligible').without('draining');
    92  
    93          return this.nodes.filter(node => {
    94            if (classes.length && !classes.includes(node.get('nodeClass'))) return false;
    95            if (statuses.length && !statuses.includes(node.get('status'))) return false;
    96            if (datacenters.length && !datacenters.includes(node.get('datacenter'))) return false;
    97  
    98            if (onlyIneligible && node.get('isEligible')) return false;
    99            if (onlyDraining && !node.get('isDraining')) return false;
   100  
   101            return true;
   102          });
   103        }
   104      ),
   105  
   106      listToSort: alias('filteredNodes'),
   107      listToSearch: alias('listSorted'),
   108      sortedNodes: alias('listSearched'),
   109  
   110      isForbidden: alias('clientsController.isForbidden'),
   111  
   112      setFacetQueryParam(queryParam, selection) {
   113        this.set(queryParam, serialize(selection));
   114      },
   115  
   116      actions: {
   117        gotoNode(node) {
   118          this.transitionToRoute('clients.client', node);
   119        },
   120      },
   121    }
   122  );