github.com/hernad/nomad@v1.6.112/ui/app/components/lifecycle-chart.js (about)

     1  /**
     2   * Copyright (c) HashiCorp, Inc.
     3   * SPDX-License-Identifier: MPL-2.0
     4   */
     5  
     6  import Component from '@ember/component';
     7  import { computed } from '@ember/object';
     8  import { sort } from '@ember/object/computed';
     9  import { tagName } from '@ember-decorators/component';
    10  import classic from 'ember-classic-decorator';
    11  
    12  @classic
    13  @tagName('')
    14  export default class LifecycleChart extends Component {
    15    tasks = null;
    16    taskStates = null;
    17  
    18    @computed('tasks.@each.lifecycle', 'taskStates.@each.state')
    19    get lifecyclePhases() {
    20      const tasksOrStates = this.taskStates || this.tasks;
    21      const lifecycles = {
    22        'prestart-ephemerals': [],
    23        'prestart-sidecars': [],
    24        'poststart-ephemerals': [],
    25        'poststart-sidecars': [],
    26        poststops: [],
    27        mains: [],
    28      };
    29  
    30      tasksOrStates.forEach((taskOrState) => {
    31        const task = taskOrState.task || taskOrState;
    32  
    33        if (task.lifecycleName) {
    34          lifecycles[`${task.lifecycleName}s`].push(taskOrState);
    35        }
    36      });
    37  
    38      const phases = [];
    39      const stateActiveIterator = (state) => state.state === 'running';
    40  
    41      if (lifecycles.mains.length < tasksOrStates.length) {
    42        phases.push({
    43          name: 'Prestart',
    44          isActive: lifecycles['prestart-ephemerals'].some(stateActiveIterator),
    45        });
    46  
    47        phases.push({
    48          name: 'Main',
    49          isActive:
    50            lifecycles.mains.some(stateActiveIterator) ||
    51            lifecycles['poststart-ephemerals'].some(stateActiveIterator),
    52        });
    53  
    54        // Poststart is rendered as a subphase of main and therefore has no independent active state
    55        phases.push({
    56          name: 'Poststart',
    57        });
    58  
    59        phases.push({
    60          name: 'Poststop',
    61          isActive: lifecycles.poststops.some(stateActiveIterator),
    62        });
    63      }
    64  
    65      return phases;
    66    }
    67  
    68    @sort('taskStates', function (a, b) {
    69      return getTaskSortPrefix(a.task).localeCompare(getTaskSortPrefix(b.task));
    70    })
    71    sortedLifecycleTaskStates;
    72  
    73    @sort('tasks', function (a, b) {
    74      return getTaskSortPrefix(a).localeCompare(getTaskSortPrefix(b));
    75    })
    76    sortedLifecycleTasks;
    77  }
    78  
    79  const lifecycleNameSortPrefix = {
    80    'prestart-ephemeral': 0,
    81    'prestart-sidecar': 1,
    82    main: 2,
    83    'poststart-sidecar': 3,
    84    'poststart-ephemeral': 4,
    85    poststop: 5,
    86  };
    87  
    88  function getTaskSortPrefix(task) {
    89    return `${lifecycleNameSortPrefix[task.lifecycleName]}-${task.name}`;
    90  }