import * as telemetry from '../telemetry' ; import * as util from '../common' ; import * as vscode from 'vscode' ; interface TimeStampSequence { firstFile ? : number ; didOpen : number ; setup : number ; updateRange : number ; } export class TimeTelemetryCollector { private cachedTimeStamps : Map < string , any > = new Map < string , any > ( ) ; public setFirstFile ( uri ) { if ( util . fileIsCOrCppSource ( uri . path ) ) { const curTimeStamps = this . getTimeStamp ( uri . path ) ; curTimeStamps . firstFile = new Date ( ) . getTime ( ) ; this . cachedTimeStamps . set ( uri . path , curTimeStamps ) ; } } public setDidOpenTime ( uri ) { const curTimeStamps = this . getTimeStamp ( uri . path ) ; curTimeStamps . didOpen = new Date ( ) . getTime ( ) ; this . cachedTimeStamps . set ( uri . path , curTimeStamps ) ; } public setSetupTime ( uri ) { const curTimeStamps = this . getTimeStamp ( uri . path ) ; curTimeStamps . setup = new Date ( ) . getTime ( ) ; this . cachedTimeStamps . set ( uri . path , curTimeStamps ) ; if ( curTimeStamps . didOpen && curTimeStamps . updateRange ) { this . logTelemetry ( uri . path , curTimeStamps ) ; } } public setUpdateRangeTime ( uri ) { const curTimeStamps = this . getTimeStamp ( uri . path ) ; if ( ! curTimeStamps . updateRange ) { curTimeStamps . updateRange = new Date ( ) . getTime ( ) ; this . cachedTimeStamps . set ( uri . path , curTimeStamps ) ; } if ( curTimeStamps . didOpen && curTimeStamps . setup ) { this . logTelemetry ( uri . path , curTimeStamps ) ; } } public clear ( ) { console . log ( "clearing timestamp log" ) ; this . cachedTimeStamps . clear ( ) ; } private getTimeStamp ( uri ) { return this . cachedTimeStamps . get ( uri ) ? this . cachedTimeStamps . get ( uri ) : { firstFile : 0 , didOpen : 0 , setup : 0 , updateRange : 0 } ; } private removeTimeStamp ( uri ) { this . cachedTimeStamps . delete ( uri ) ; } private logTelemetry ( uri , timeStamps ) { const startTime = timeStamps . firstFile ? timeStamps . firstFile : timeStamps . didOpen ; let properties = { } ; let metrics = { "setupTime" : ( timeStamps . setup - timeStamps . didOpen ) , "updateRangeTime" : ( timeStamps . updateRange - timeStamps . setup ) , "totalTime" : ( timeStamps . updateRange - startTime ) } ; if ( timeStamps . firstFile ) { properties = { "coldstart" : "true" } ; metrics = { "activationTime" : ( timeStamps . didOpen - startTime ) , ... metrics } ; } telemetry . logLanguageServerEvent ( "timeStamps" , properties , metrics ) ; this . removeTimeStamp ( uri ) ; } }