github.com/hernad/nomad@v1.6.112/ui/app/controllers/jobs/job/allocations.js (about)

     1  /**
     2   * Copyright (c) HashiCorp, Inc.
     3   * SPDX-License-Identifier: MPL-2.0
     4   */
     5  
     6  /* eslint-disable ember/no-incorrect-calls-with-inline-anonymous-functions */
     7  import { alias } from '@ember/object/computed';
     8  import Controller from '@ember/controller';
     9  import { action, computed } from '@ember/object';
    10  import { scheduleOnce } from '@ember/runloop';
    11  import intersection from 'lodash.intersection';
    12  import Sortable from 'nomad-ui/mixins/sortable';
    13  import Searchable from 'nomad-ui/mixins/searchable';
    14  import WithNamespaceResetting from 'nomad-ui/mixins/with-namespace-resetting';
    15  import {
    16    serialize,
    17    deserializedQueryParam as selection,
    18  } from 'nomad-ui/utils/qp-serialize';
    19  import classic from 'ember-classic-decorator';
    20  
    21  @classic
    22  export default class AllocationsController extends Controller.extend(
    23    Sortable,
    24    Searchable,
    25    WithNamespaceResetting
    26  ) {
    27    queryParams = [
    28      {
    29        currentPage: 'page',
    30      },
    31      {
    32        searchTerm: 'search',
    33      },
    34      {
    35        sortProperty: 'sort',
    36      },
    37      {
    38        sortDescending: 'desc',
    39      },
    40      {
    41        qpStatus: 'status',
    42      },
    43      {
    44        qpClient: 'client',
    45      },
    46      {
    47        qpTaskGroup: 'taskGroup',
    48      },
    49      {
    50        qpVersion: 'version',
    51      },
    52      {
    53        qpScheduling: 'scheduling',
    54      },
    55      'activeTask',
    56    ];
    57  
    58    qpStatus = '';
    59    qpClient = '';
    60    qpTaskGroup = '';
    61    qpVersion = '';
    62    qpScheduling = '';
    63    currentPage = 1;
    64    pageSize = 25;
    65    activeTask = null;
    66  
    67    sortProperty = 'modifyIndex';
    68    sortDescending = true;
    69  
    70    @alias('model') job;
    71  
    72    @computed
    73    get searchProps() {
    74      return ['shortId', 'name', 'taskGroupName'];
    75    }
    76  
    77    @computed('model.allocations.[]')
    78    get allocations() {
    79      return this.get('model.allocations') || [];
    80    }
    81  
    82    @computed(
    83      'allocations.[]',
    84      'selectionStatus',
    85      'selectionClient',
    86      'selectionTaskGroup',
    87      'selectionVersion',
    88      'selectionScheduling'
    89    )
    90    get filteredAllocations() {
    91      const {
    92        selectionStatus,
    93        selectionClient,
    94        selectionTaskGroup,
    95        selectionVersion,
    96        selectionScheduling,
    97      } = this;
    98  
    99      return this.allocations.filter((alloc) => {
   100        if (
   101          selectionStatus.length &&
   102          !selectionStatus.includes(alloc.clientStatus)
   103        ) {
   104          return false;
   105        }
   106        if (
   107          selectionClient.length &&
   108          !selectionClient.includes(alloc.get('node.shortId'))
   109        ) {
   110          return false;
   111        }
   112        if (
   113          selectionTaskGroup.length &&
   114          !selectionTaskGroup.includes(alloc.taskGroupName)
   115        ) {
   116          return false;
   117        }
   118        if (
   119          selectionVersion.length &&
   120          !selectionVersion.includes(alloc.jobVersion)
   121        ) {
   122          return false;
   123        }
   124  
   125        if (selectionScheduling.length) {
   126          if (
   127            selectionScheduling.includes('will-not-reschedule') &&
   128            !alloc.willNotReschedule
   129          ) {
   130            return false;
   131          }
   132          if (
   133            selectionScheduling.includes('will-not-restart') &&
   134            !alloc.willNotRestart
   135          ) {
   136            return false;
   137          }
   138          if (
   139            selectionScheduling.includes('has-been-rescheduled') &&
   140            !alloc.hasBeenRescheduled
   141          ) {
   142            return false;
   143          }
   144          if (
   145            selectionScheduling.includes('has-been-restarted') &&
   146            !alloc.hasBeenRestarted
   147          ) {
   148            return false;
   149          }
   150          return true;
   151        }
   152  
   153        return true;
   154      });
   155    }
   156  
   157    @alias('filteredAllocations') listToSort;
   158    @alias('listSorted') listToSearch;
   159    @alias('listSearched') sortedAllocations;
   160  
   161    @selection('qpStatus') selectionStatus;
   162    @selection('qpClient') selectionClient;
   163    @selection('qpTaskGroup') selectionTaskGroup;
   164    @selection('qpVersion') selectionVersion;
   165    @selection('qpScheduling') selectionScheduling;
   166  
   167    @action
   168    gotoAllocation(allocation) {
   169      this.transitionToRoute('allocations.allocation', allocation.id);
   170    }
   171  
   172    get optionsAllocationStatus() {
   173      return [
   174        { key: 'pending', label: 'Pending' },
   175        { key: 'running', label: 'Running' },
   176        { key: 'complete', label: 'Complete' },
   177        { key: 'failed', label: 'Failed' },
   178        { key: 'lost', label: 'Lost' },
   179        { key: 'unknown', label: 'Unknown' },
   180      ];
   181    }
   182  
   183    @computed('model.allocations.[]', 'selectionClient')
   184    get optionsClients() {
   185      const clients = Array.from(
   186        new Set(this.model.allocations.mapBy('node.shortId'))
   187      ).compact();
   188  
   189      // Update query param when the list of clients changes.
   190      scheduleOnce('actions', () => {
   191        // eslint-disable-next-line ember/no-side-effects
   192        this.set(
   193          'qpClient',
   194          serialize(intersection(clients, this.selectionClient))
   195        );
   196      });
   197  
   198      return clients.sort().map((c) => ({ key: c, label: c }));
   199    }
   200  
   201    @computed('model.allocations.[]', 'selectionTaskGroup')
   202    get optionsTaskGroups() {
   203      const taskGroups = Array.from(
   204        new Set(this.model.allocations.mapBy('taskGroupName'))
   205      ).compact();
   206  
   207      // Update query param when the list of task groups changes.
   208      scheduleOnce('actions', () => {
   209        // eslint-disable-next-line ember/no-side-effects
   210        this.set(
   211          'qpTaskGroup',
   212          serialize(intersection(taskGroups, this.selectionTaskGroup))
   213        );
   214      });
   215  
   216      return taskGroups.sort().map((tg) => ({ key: tg, label: tg }));
   217    }
   218  
   219    @computed('model.allocations.[]', 'selectionVersion')
   220    get optionsVersions() {
   221      const versions = Array.from(
   222        new Set(this.model.allocations.mapBy('jobVersion'))
   223      ).compact();
   224  
   225      // Update query param when the list of versions changes.
   226      scheduleOnce('actions', () => {
   227        // eslint-disable-next-line ember/no-side-effects
   228        this.set(
   229          'qpVersion',
   230          serialize(intersection(versions, this.selectionVersion))
   231        );
   232      });
   233  
   234      return versions.sort((a, b) => a - b).map((v) => ({ key: v, label: v }));
   235    }
   236  
   237    @computed('model.allocations.[]', 'selectionScheduling')
   238    get optionsScheduling() {
   239      return [
   240        {
   241          key: 'has-been-rescheduled',
   242          label: 'Failed & Has Been Rescheduled',
   243        },
   244        {
   245          key: 'will-not-reschedule',
   246          label: "Failed & Won't Reschedule",
   247        },
   248        {
   249          key: 'has-been-restarted',
   250          label: 'Has Been Restarted',
   251        },
   252        {
   253          key: 'will-not-restart',
   254          label: "Won't Restart",
   255        },
   256      ];
   257    }
   258  
   259    setFacetQueryParam(queryParam, selection) {
   260      this.set(queryParam, serialize(selection));
   261    }
   262  
   263    @action
   264    setActiveTaskQueryParam(task) {
   265      if (task) {
   266        this.set('activeTask', `${task.allocation.id}-${task.name}`);
   267      } else {
   268        this.set('activeTask', null);
   269      }
   270    }
   271  }