import type { TaskSummary } from 'api-client' ; import { TaskSummary as RmfTaskSummary } from 'rmf-models' ; export const getActorFromStatus = ( status ) : RegExpMatchArray | null => { return status . match ( /\[[A-Za-z]([a-zA-Z0-9\/]){3,}\]+/gi ) ; } ; export const formatStatus = ( status ) : string [ ] => { return status . split ( '|' ) ; } ; export const getStateLabel = ( state ) : string => { switch ( state ) { case RmfTaskSummary . STATE_QUEUED : return 'QUEUED' ; case RmfTaskSummary . STATE_ACTIVE : return 'ACTIVE' ; case RmfTaskSummary . STATE_COMPLETED : return 'COMPLETED' ; case RmfTaskSummary . STATE_FAILED : return 'FAILED' ; default : return 'UNKNOWN' ; } } ; export const sortTasksBySubmissionTime = ( tasks ) : TaskSummary [ ] => { if ( tasks . length === 0 ) return [ ] ; return tasks . sort ( ( a , b ) => ( a . submission_time . nanosec < b . submission_time . nanosec ? 1 : - 1 ) ) ; } ; /**
 * Classifies and stores each task by its state.
 */ export const separateTasksByState = ( tasks , states ) : Record < string , TaskSummary [ ] > => { const stateTasks = { } ; states . forEach ( ( state ) => { stateTasks [ state ] = [ ] ; } ) ; Object . keys ( tasks ) . forEach ( ( key ) => { switch ( tasks [ key ] . state ) { case RmfTaskSummary . STATE_QUEUED : stateTasks . queued . push ( tasks [ key ] ) ; break ; case RmfTaskSummary . STATE_ACTIVE : stateTasks . active . push ( tasks [ key ] ) ; break ; case RmfTaskSummary . STATE_COMPLETED : stateTasks . completed . push ( tasks [ key ] ) ; break ; case RmfTaskSummary . STATE_FAILED : stateTasks . failed . push ( tasks [ key ] ) ; break ; default : stateTasks . unknown . push ( tasks [ key ] ) ; break ; } } ) ; return stateTasks ; } ; /**
 * Sort tasks by state and by submission time, so what is active is always at the top of the list.
 */ export const sortTasks = ( tasks ) : TaskSummary [ ] => { const states = [ 'active' , 'queued' , 'failed' , 'completed' , 'unknown' ] ; const stateTasks = separateTasksByState ( tasks , states ) ; const sortedTasks = [ ] ; states . forEach ( ( state ) => { sortedTasks . push ( ... sortTasksBySubmissionTime ( stateTasks [ state ] ) ) ; } ) ; return sortedTasks ; } ;