|
|
import { ReportImpl } from './report'; |
|
|
import { send } from './transports/logstash'; |
|
|
import type { Collector, Report, ReportData, ReportPayload } from './types'; |
|
|
|
|
|
const inFlightReporters: Map< string, Promise< Report > > = new Map(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export const start = async ( |
|
|
id: string, |
|
|
{ |
|
|
fullPageLoad = true, |
|
|
collectors = [], |
|
|
}: { fullPageLoad?: boolean; collectors?: Collector[] } = {} |
|
|
): Promise< void > => { |
|
|
|
|
|
if ( inFlightReporters.has( id ) ) { |
|
|
return; |
|
|
} |
|
|
|
|
|
const report = fullPageLoad |
|
|
? ReportImpl.fromPageStart( id, collectors ) |
|
|
: ReportImpl.fromNow( id, collectors ); |
|
|
inFlightReporters.set( id, report ); |
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export const stop = async ( |
|
|
id: string, |
|
|
{ collectors = [] }: { collectors?: Collector[] } = {} |
|
|
): Promise< boolean > => { |
|
|
const existingReport = inFlightReporters.get( id ); |
|
|
|
|
|
|
|
|
if ( ! existingReport ) { |
|
|
return false; |
|
|
} |
|
|
|
|
|
inFlightReporters.delete( id ); |
|
|
|
|
|
|
|
|
const startedReport = await existingReport; |
|
|
|
|
|
const payload = await startedReport.stop( collectors ); |
|
|
|
|
|
return send( payload ); |
|
|
}; |
|
|
|
|
|
export type { Collector, Report, ReportData, ReportPayload }; |
|
|
|