import * as Tools from '../../tools/index' ; import ServiceElectron from '../service.electron' ; import Logger from '../../tools/env.logger' ; import indexer from 'indexer-neon' ; import ServiceStreams from '../../services/service.streams' ; import ServiceStorage from '../../services/service.storage' ; import { IPCMessages } from '../service.electron' ; import { Subscription } from '../../tools/index' ; import { IService } from '../../interfaces/interface.service' ; import { CommonInterfaces } from '../../interfaces/interface.common' ; import { Progress , DLT , CancelablePromise } from 'indexer-neon' ; import { IStorageScheme } from '../../services/service.storage' ; class description class ServiceDLTFiles implements IService { private _logger : Logger = new Logger ( 'ServiceDLTFiles' ) ; private _subscription : { [ key ] : Subscription ; } = { } ; private _tasks : Map < string , CancelablePromise < void , void , DLT . TDltStatsEvents , DLT . TDltStatsEventObject > > = new Map ( ) ; private _stats : Map < string , CommonInterfaces . DLT . StatisticInfo > = new Map ( ) ; returns public init ( ) { return new Promise ( ( resolve , reject ) => { Promise . all ( [ ServiceElectron . IPC . subscribe ( IPCMessages . DLTStatsRequest , this . _onDLTStatsRequest . bind ( this ) ) . then ( ( subscription ) => { this . _subscription . DLTStatsRequest = subscription ; } ) , ServiceElectron . IPC . subscribe ( IPCMessages . DLTStatsCancelRequest , this . _onDLTStatsCancelRequest . bind ( this ) ) . then ( ( subscription ) => { this . _subscription . DLTStatsCancelRequest = subscription ; } ) , ServiceElectron . IPC . subscribe ( IPCMessages . DLTRecentTimeZoneRequest , this . _onDLTRecentTimeZoneRequest . bind ( this ) ) . then ( ( subscription ) => { this . _subscription . DLTRecentTimeZoneRequest = subscription ; } ) , ] ) . then ( ( ) => { resolve ( ) ; } ) . catch ( ( error ) => { this . _logger . error ( `Fail to init module due error: ${ error . message }` ) ; reject ( error ) ; } ) ; } ) ; } public destroy ( ) { return new Promise ( ( resolve ) => { Object . keys ( this . _subscription ) . forEach ( ( key ) => { this . _subscription [ key ] . destroy ( ) ; } ) ; resolve ( ) ; } ) ; } public getName ( ) { return 'ServiceDLTFiles' ; } public getStats ( session ) { return this . _stats . get ( session ) ; } private _onDLTStatsRequest ( request , response ) { const req = request as IPCMessages . DLTStatsRequest ; const taskId = req . id ; ServiceStreams . addProgressSession ( taskId , 'Getting DLT stats' , req . session ) ; const hrstart = process . hrtime ( ) ; this . _logger . debug ( 'calling _onDLTStatsRequest with params: ' + JSON . stringify ( req ) ) ; let stats ; const task = indexer . dltStatsAsync ( req . file ) . then ( ( ) => { this . _logger . debug ( 'dltStatsAsync task finished' ) ; if ( stats !== undefined ) { this . _stats . set ( req . file , stats ) ; } response ( new IPCMessages . DLTStatsResponse ( { stats : stats , id : taskId , session : req . session , logs : undefined , } ) ) ; } ) . canceled ( ( ) => { response ( new IPCMessages . DLTStatsResponse ( { stats : undefined , id : taskId , session : req . session , logs : undefined , error : 'Task is canceled' , } ) ) ; } ) . catch ( ( error ) => { this . _logger . warn ( `Exception in dltStatsAsync: ${ error . message }` ) ; response ( new IPCMessages . DLTStatsResponse ( { stats : undefined , id : taskId , session : req . session , logs : undefined , error : `Fail to finish task due error: ${ error . message }` , } ) ) ; } ) . finally ( ( ) => { const hrend = process . hrtime ( hrstart ) ; const ms = Math . round ( hrend [ 0 ] * 1000 + hrend [ 1 ] / 1000000 ) ; this . _logger . debug ( 'Execution time for indexing : ' + ms + 'ms' ) ; ServiceStreams . removeProgressSession ( taskId , req . session ) ; this . _tasks . delete ( taskId ) ; } ) . on ( 'config' , ( event ) => { stats = event ; this . _logger . debug ( `_onDLTStatsRequest: received config, contained non-verbose logs: ${ event . contained_non_verbose }` ) ; } ) . on ( 'progress' , ( event ) => { const value = event . ellapsed / event . total ; ServiceStreams . updateProgressSession ( taskId , value , req . session ) ; } ) ; this . _tasks . set ( taskId , task ) ; } private _onDLTStatsCancelRequest ( request , response ) { const req = request as IPCMessages . DLTStatsCancelRequest ; const task = this . _tasks . get ( req . id ) ; if ( task === undefined ) { return response ( new IPCMessages . DLTStatsCancelResponse ( { id : req . id , session : req . session , error : `No task found with id "${ req . id }". Probably task is already other.` , } ) ) ; } task . canceled ( ( ) => { this . _tasks . delete ( req . id ) ; response ( new IPCMessages . DLTStatsCancelResponse ( { id : req . id , session : req . session , } ) ) ; } ) . abort ( ) ; } private _onDLTRecentTimeZoneRequest ( request , response ) { const stored = ServiceStorage . get ( ) . get ( ) ; response ( new IPCMessages . DLTRecentTimeZoneResponse ( { timezone : stored . recentDLTTimeZone === undefined ? '' : stored . recentDLTTimeZone , } ) ) ; } } export default new ServiceDLTFiles ( ) ;