import _ from 'lodash' ; import log from '../util/Logger' ; import NI from '../util/NetworkInterface' ; import * as queries from '../scripts/phaseQueries' ; import { IPhase , IPhaseData , IPhaseDataFull , IPhaseSeedData , IPhaseEntrantData , IPhaseAttendeeData , IPhasePaginatedData } from '../interfaces/IPhase' ; import { IPhaseGroup , IPhaseGroupData , IPhaseGroupEventData , } from '../interfaces/IPhaseGroup' ; import { IGGSet , IGGSetOptions , IGGSetData } from '../interfaces/IGGSet' ; import { IAttendee , IAttendeeData , IAttendeeOptions } from '../interfaces/IAttendee' ; import { IEntrant , IEntrantData , IEntrantOptions } from '../interfaces/IEntrant' ; import { ISeed , ISeedData , ISeedOptions } from '../interfaces/ISeed' ; import { Seed } from './Seed' ; import { GGSet } from './GGSet' ; import { Attendee } from './Attendee' ; import { Entrant } from './Entrant' ; import { PhaseGroup } from './PhaseGroup' ; export class Phase implements IPhase { public static parse ( data , eventId = - 1 ) { return new Phase ( data . id , eventId || - 1 , data . name , data . numSeeds , data . groupCount ) ; } public static async get ( theId , eventId ) { log . info ( 'Getting Phase with id %s and event id %s' , theId , eventId ) ; const data = await NI . query ( queries . phase , { id : theId } ) ; return Phase . parse ( data . phase , eventId ) ; } private id : number ; private eventId : number ; private name : string ; private numSeeds : number ; private groupCount : number ; constructor ( id , eventId , name , numSeeds , groupCount ) { this . id = id ; this . eventId = eventId ; this . name = name ; this . numSeeds = numSeeds ; this . groupCount = groupCount ; } public getId ( ) { return this . id ; } public getEventId ( ) { return this . eventId ; } public getName ( ) { return this . name ; } public getNumSeeds ( ) { return this . numSeeds ; } public getGroupCount ( ) { return this . groupCount ; } public async getPhaseGroups2 ( ) { log . info ( 'Getting phase groups for phase %s' , this . id ) ; const data = await NI . query ( queries . phasePhaseGroups2 , { eventId : this . eventId } ) ; const phaseGroupData = data . event . phaseGroups . filter ( pgData => pgData . phaseId === this . id ) ; const phaseGroups = phaseGroupData . map ( pg => PhaseGroup . parse ( pg ) ) ; return phaseGroups ; } public async getPhaseGroups ( ) { log . info ( 'Getting phase groups for phase %s' , this . id ) ; const data = await NI . query ( queries . phasePhaseGroups , { id : this . id } ) ; const phaseGroupData = data . phase . phaseGroups . nodes . filter ( pgData => pgData . phaseId === this . id ) ; const phaseGroups = phaseGroupData . map ( pg => PhaseGroup . parse ( pg ) ) ; return phaseGroups ; } public async getSeeds ( options ) { log . info ( 'Getting seeds for phase %s' , this . id ) ; log . verbose ( 'Query variables: %s' , JSON . stringify ( options ) ) ; const pgs = await this . getPhaseGroups ( ) ; const seeds = await NI . clusterQuery ( pgs , 'getSeeds' , options ) ; return _ . uniqBy ( _ . flatten ( seeds ) , 'id' ) ; } public async getEntrants ( options = Entrant . getDefaultEntrantOptions ( ) ) { log . info ( 'Getting entrants for phase %s' , this . id ) ; log . verbose ( 'Query variables: %s' , JSON . stringify ( options ) ) ; const pgs = await this . getPhaseGroups ( ) ; const entrants = await NI . clusterQuery ( pgs , 'getEntrants' , options ) ; return _ . uniqBy ( _ . flatten ( entrants ) , 'id' ) ; } public async getAttendees ( options = Attendee . getDefaultAttendeeOptions ( ) ) { log . info ( 'Getting attendees for phase %s' , this . id ) ; log . verbose ( 'Query variables: %s' , JSON . stringify ( options ) ) ; const pgs = await this . getPhaseGroups ( ) ; const attendees = await NI . clusterQuery ( pgs , 'getAttendees' , options ) ; return _ . uniqBy ( _ . flatten ( attendees ) , 'id' ) ; } public async getSets ( options = GGSet . getDefaultSetOptions ( ) ) { log . info ( 'Getting sets for phase %s' , this . id ) ; const pgs = await this . getPhaseGroups ( ) ; const pgSets = await NI . clusterQuery ( pgs , 'getSets' , options ) ; return _ . uniqBy ( _ . flatten ( pgSets ) , 'id' ) ; } public async getSeeds2 ( options ) { log . info ( 'Getting seeds for phase %s' , this . id ) ; log . verbose ( 'Query variables: %s' , JSON . stringify ( options ) ) ; const data = await NI . paginatedQuery ( `Phase Seeds [${ this . id }]` , queries . phaseSeeds , { id : this . id } , options , { } , 2 ) ; const seedData = _ . flatten ( data . map ( results => results . phase . paginatedSeeds . nodes ) ) . filter ( seed => seed != null ) ; const seeds = seedData . map ( ( sData ) => Seed . parse ( sData ) ) ; return _ . uniqBy ( seeds , 'id' ) ; } public async getEntrants2 ( options = Entrant . getDefaultEntrantOptions ( ) ) { log . info ( 'Getting entrants for phase %s' , this . id ) ; log . verbose ( 'Query variables: %s' , JSON . stringify ( options ) ) ; const data = await NI . paginatedQuery ( `Phase Entrants [${ this . id }]` , queries . phaseEntrants , { id : this . id } , options , { } , 2 ) ; const entrantData = _ . flatten ( data . map ( eData => eData . phase . paginatedSeeds . nodes ) ) . filter ( entrant => entrant != null ) ; const entrants = entrantData . map ( ( e ) => Entrant . parse ( e ) ) as IEntrant [ ] ; return _ . uniqBy ( entrants , 'id' ) ; } public async getAttendees2 ( options = Attendee . getDefaultAttendeeOptions ( ) ) { log . info ( 'Getting attendees for phase %s' , this . id ) ; log . verbose ( 'Query variables: %s' , JSON . stringify ( options ) ) ; const data = await NI . paginatedQuery ( `Phase Attendees [${ this . id }]` , queries . phaseAttendees , { id : this . id } , options , { } , 3 ) ; const seeds = _ . flatten ( data . map ( seed => seed . phase . paginatedSeeds ) ) ; const nodes = _ . flatten ( seeds . map ( seed => seed . nodes ) ) ; const entrants = nodes . map ( node => node . entrant ) ; const participants = _ . flatten ( entrants . map ( entrant => entrant . participants ) ) . filter ( participant => participant != null ) ; const attendees = participants . map ( participant => Attendee . parse ( participant ) ) ; return _ . uniqBy ( attendees , 'id' ) ; } public async getIncompleteSets ( options = GGSet . getDefaultSetOptions ( ) ) { return GGSet . filterForIncompleteSets ( await this . getSets ( options ) ) ; } public async getCompleteSets ( options = GGSet . getDefaultSetOptions ( ) ) { return GGSet . filterForCompleteSets ( await this . getSets ( options ) ) ; } public async getSetsXMinutesBack ( minutesBack , options = GGSet . getDefaultSetOptions ( ) ) { return GGSet . filterForXMinutesBack ( await this . getSets ( options ) , minutesBack ) ; } }