github.com/anth0d/nomad@v0.0.0-20221214183521-ae3a0a2cad06/ui/app/controllers/jobs/job/allocations.js (about)

     1  /* eslint-disable ember/no-incorrect-calls-with-inline-anonymous-functions */
     2  import { alias } from '@ember/object/computed';
     3  import Controller from '@ember/controller';
     4  import { action, computed } from '@ember/object';
     5  import { scheduleOnce } from '@ember/runloop';
     6  import intersection from 'lodash.intersection';
     7  import Sortable from 'nomad-ui/mixins/sortable';
     8  import Searchable from 'nomad-ui/mixins/searchable';
     9  import WithNamespaceResetting from 'nomad-ui/mixins/with-namespace-resetting';
    10  import {
    11    serialize,
    12    deserializedQueryParam as selection,
    13  } from 'nomad-ui/utils/qp-serialize';
    14  import classic from 'ember-classic-decorator';
    15  
    16  @classic
    17  export default class AllocationsController extends Controller.extend(
    18    Sortable,
    19    Searchable,
    20    WithNamespaceResetting
    21  ) {
    22    queryParams = [
    23      {
    24        currentPage: 'page',
    25      },
    26      {
    27        searchTerm: 'search',
    28      },
    29      {
    30        sortProperty: 'sort',
    31      },
    32      {
    33        sortDescending: 'desc',
    34      },
    35      {
    36        qpStatus: 'status',
    37      },
    38      {
    39        qpClient: 'client',
    40      },
    41      {
    42        qpTaskGroup: 'taskGroup',
    43      },
    44      'activeTask',
    45    ];
    46  
    47    qpStatus = '';
    48    qpClient = '';
    49    qpTaskGroup = '';
    50    currentPage = 1;
    51    pageSize = 25;
    52    activeTask = null;
    53  
    54    sortProperty = 'modifyIndex';
    55    sortDescending = true;
    56  
    57    @alias('model') job;
    58  
    59    @computed
    60    get searchProps() {
    61      return ['shortId', 'name', 'taskGroupName'];
    62    }
    63  
    64    @computed('model.allocations.[]')
    65    get allocations() {
    66      return this.get('model.allocations') || [];
    67    }
    68  
    69    @computed(
    70      'allocations.[]',
    71      'selectionStatus',
    72      'selectionClient',
    73      'selectionTaskGroup'
    74    )
    75    get filteredAllocations() {
    76      const { selectionStatus, selectionClient, selectionTaskGroup } = this;
    77  
    78      return this.allocations.filter((alloc) => {
    79        if (
    80          selectionStatus.length &&
    81          !selectionStatus.includes(alloc.clientStatus)
    82        ) {
    83          return false;
    84        }
    85        if (
    86          selectionClient.length &&
    87          !selectionClient.includes(alloc.get('node.shortId'))
    88        ) {
    89          return false;
    90        }
    91        if (
    92          selectionTaskGroup.length &&
    93          !selectionTaskGroup.includes(alloc.taskGroupName)
    94        ) {
    95          return false;
    96        }
    97        return true;
    98      });
    99    }
   100  
   101    @alias('filteredAllocations') listToSort;
   102    @alias('listSorted') listToSearch;
   103    @alias('listSearched') sortedAllocations;
   104  
   105    @selection('qpStatus') selectionStatus;
   106    @selection('qpClient') selectionClient;
   107    @selection('qpTaskGroup') selectionTaskGroup;
   108  
   109    @action
   110    gotoAllocation(allocation) {
   111      this.transitionToRoute('allocations.allocation', allocation);
   112    }
   113  
   114    get optionsAllocationStatus() {
   115      return [
   116        { key: 'pending', label: 'Pending' },
   117        { key: 'running', label: 'Running' },
   118        { key: 'complete', label: 'Complete' },
   119        { key: 'failed', label: 'Failed' },
   120        { key: 'lost', label: 'Lost' },
   121        { key: 'unknown', label: 'Unknown' },
   122      ];
   123    }
   124  
   125    @computed('model.allocations.[]', 'selectionClient')
   126    get optionsClients() {
   127      const clients = Array.from(
   128        new Set(this.model.allocations.mapBy('node.shortId'))
   129      ).compact();
   130  
   131      // Update query param when the list of clients changes.
   132      scheduleOnce('actions', () => {
   133        // eslint-disable-next-line ember/no-side-effects
   134        this.set(
   135          'qpClient',
   136          serialize(intersection(clients, this.selectionClient))
   137        );
   138      });
   139  
   140      return clients.sort().map((c) => ({ key: c, label: c }));
   141    }
   142  
   143    @computed('model.allocations.[]', 'selectionTaskGroup')
   144    get optionsTaskGroups() {
   145      const taskGroups = Array.from(
   146        new Set(this.model.allocations.mapBy('taskGroupName'))
   147      ).compact();
   148  
   149      // Update query param when the list of task groups changes.
   150      scheduleOnce('actions', () => {
   151        // eslint-disable-next-line ember/no-side-effects
   152        this.set(
   153          'qpTaskGroup',
   154          serialize(intersection(taskGroups, this.selectionTaskGroup))
   155        );
   156      });
   157  
   158      return taskGroups.sort().map((tg) => ({ key: tg, label: tg }));
   159    }
   160  
   161    setFacetQueryParam(queryParam, selection) {
   162      this.set(queryParam, serialize(selection));
   163    }
   164  
   165    @action
   166    setActiveTaskQueryParam(task) {
   167      if (task) {
   168        this.set('activeTask', `${task.allocation.id}-${task.name}`);
   169      } else {
   170        this.set('activeTask', null);
   171      }
   172    }
   173  }