import { CalendarManager } from '../calendar' ; import { GLOBAL_CALENDAR } from '../../../common/constants/calendars' ; import { MlJobsResponse } from '../../../common/types/job_service' ; import type { MlClient } from '../../lib/ml_client' ; export interface Group { id : string ; jobIds : string [ ] ; calendarIds : string [ ] ; } export interface Results { [ id ] : { success : boolean ; error ? : any ; } ; } export function groupsProvider ( mlClient ) { const calMngr = new CalendarManager ( mlClient ) ; async function getAllGroups ( ) { const groups = { } ; const jobIds = { } ; const [ { body } , calendars ] = await Promise . all ( [ mlClient . getJobs < MlJobsResponse > ( ) , calMngr . getAllCalendars ( ) , ] ) ; const { jobs } = body ; if ( jobs ) { jobs . forEach ( ( job ) => { jobIds [ job . job_id ] = null ; if ( job . groups !== undefined ) { job . groups . forEach ( ( g ) => { if ( groups [ g ] === undefined ) { groups [ g ] = { id : g , jobIds : [ job . job_id ] , calendarIds : [ ] , } ; } else { groups [ g ] . jobIds . push ( job . job_id ) ; } } ) ; } } ) ; } if ( calendars ) { calendars . forEach ( ( cal ) => { cal . job_ids . forEach ( ( jId ) => { if ( jId !== GLOBAL_CALENDAR && jobIds [ jId ] === undefined ) { if ( groups [ jId ] === undefined ) { groups [ jId ] = { id : jId , jobIds : [ ] , calendarIds : [ cal . calendar_id ] , } ; } else { groups [ jId ] . calendarIds . push ( cal . calendar_id ) ; } } } ) ; } ) ; } return Object . keys ( groups ) . sort ( ) . map ( ( g ) => groups [ g ] ) ; } async function updateGroups ( jobs ) { const results = { } ; for ( const job of jobs ) { const { jobId , groups } = job ; try { await mlClient . updateJob ( { job_id : jobId , body : { groups } } ) ; results [ jobId ] = { success : true } ; } catch ( { body } ) { results [ jobId ] = { success : false , error : body } ; } } return results ; } return { getAllGroups , updateGroups , } ; }