github.com/hernad/nomad@v1.6.112/ui/app/components/job-status/deployment-history.js (about) 1 /** 2 * Copyright (c) HashiCorp, Inc. 3 * SPDX-License-Identifier: MPL-2.0 4 */ 5 6 // @ts-check 7 import Component from '@glimmer/component'; 8 import { alias } from '@ember/object/computed'; 9 import { tracked } from '@glimmer/tracking'; 10 import { inject as service } from '@ember/service'; 11 import { action } from '@ember/object'; 12 13 const MAX_NUMBER_OF_EVENTS = 500; 14 15 export default class JobStatusDeploymentHistoryComponent extends Component { 16 @service notifications; 17 18 @tracked isHidden = this.args.isHidden; 19 20 /** 21 * @type { Error } 22 */ 23 @tracked errorState = null; 24 25 /** 26 * @type { import('../../models/job').default } 27 */ 28 @alias('args.deployment.job') job; 29 30 /** 31 * @type { number } 32 */ 33 @alias('args.deployment.versionNumber') deploymentVersion; 34 35 /** 36 * Get all allocations for the job 37 * @type { import('../../models/allocation').default[] } 38 */ 39 get jobAllocations() { 40 return this.job.get('allocations'); 41 } 42 43 /** 44 * Filter the job's allocations to only those that are part of the deployment 45 * @type { import('../../models/allocation').default[] } 46 */ 47 get deploymentAllocations() { 48 return ( 49 this.args.allocations || 50 this.jobAllocations.filter( 51 (alloc) => alloc.jobVersion === this.deploymentVersion 52 ) 53 ); 54 } 55 56 /** 57 * Map the deployment's allocations to their task events, in reverse-chronological order 58 * @type { import('../../models/task-event').default[] } 59 */ 60 get history() { 61 try { 62 return this.deploymentAllocations 63 .map((a) => 64 a 65 .get('states') 66 .map((s) => s.events.content) 67 .flat() 68 ) 69 .flat() 70 .filter((a) => this.containsSearchTerm(a)) 71 .sort((a, b) => a.get('time') - b.get('time')) 72 .reverse() 73 .slice(0, MAX_NUMBER_OF_EVENTS); 74 } catch (e) { 75 this.triggerError(e); 76 return []; 77 } 78 } 79 80 @action triggerError(error) { 81 this.errorState = error; 82 this.notifications.add({ 83 title: 'Could not fetch deployment history', 84 message: error, 85 color: 'critical', 86 }); 87 } 88 89 // #region search 90 91 /** 92 * @type { string } 93 */ 94 @tracked searchTerm = ''; 95 96 /** 97 * @param { import('../../models/task-event').default } taskEvent 98 * @returns { boolean } 99 */ 100 containsSearchTerm(taskEvent) { 101 return ( 102 taskEvent.message.toLowerCase().includes(this.searchTerm.toLowerCase()) || 103 taskEvent.type.toLowerCase().includes(this.searchTerm.toLowerCase()) || 104 taskEvent.state.allocation.shortId.includes(this.searchTerm.toLowerCase()) 105 ); 106 } 107 108 // #endregion search 109 }