import ServiceElectron from '../service.electron' ; import Logger from '../../tools/env.logger' ; import ServiceStreams from "../service.streams" ; import MergeDiscover from '../../controllers/features/merge/merge.discover' ; import MergeFormat from '../../controllers/features/merge/merge.format' ; import TimestampExtract from '../../controllers/features/timestamp/timestamp.extract' ; import ServiceTimestampFormatRecent from './service.timestamp.recent' ; import { IPCMessages } from '../service.electron' ; import { Subscription } from '../../tools/index' ; import { IService } from '../../interfaces/interface.service' ; import { dialog , SaveDialogReturnValue } from 'electron' ; import * as fs from 'fs' ; class description class ServiceTimestamp implements IService { private _logger : Logger = new Logger ( 'ServiceTimestamp' ) ; private _subscriptions : { [ key ] : Subscription ; } = { } ; private _tasks : Map < string , Map < string , MergeDiscover > > = new Map < string , Map < string , MergeDiscover > > ( ) ; returns public init ( ) { return new Promise ( ( resolve , reject ) => { Promise . all ( [ ServiceElectron . IPC . subscribe ( IPCMessages . TimestampDiscoverRequest , this . _onTimestampDiscoverRequest . bind ( this ) ) . then ( ( subscription ) => { this . _subscriptions . TimestampDiscoverRequest = subscription ; } ) , ServiceElectron . IPC . subscribe ( IPCMessages . TimestampTestRequest , this . _onTimestampTestRequest . bind ( this ) ) . then ( ( subscription ) => { this . _subscriptions . TimestampTestRequest = subscription ; } ) , ServiceElectron . IPC . subscribe ( IPCMessages . TimestampExtractRequest , this . _onTimestampExtractRequest . bind ( this ) ) . then ( ( subscription ) => { this . _subscriptions . TimestampExtractRequest = subscription ; } ) , ServiceElectron . IPC . subscribe ( IPCMessages . TimestampExportCSVRequest , this . _onTimestampExportCSVRequest . bind ( this ) ) . then ( ( subscription ) => { this . _subscriptions . TimestampExportCSVRequest = subscription ; } ) , ] ) . then ( ( ) => { this . _subscriptions . onSessionClosed = ServiceStreams . getSubjects ( ) . onSessionClosed . subscribe ( this . _onSessionClosed . bind ( this ) ) ; 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 . _subscriptions ) . forEach ( ( key ) => { this . _subscriptions [ key ] . destroy ( ) ; } ) ; resolve ( ) ; } ) ; } public getName ( ) { return 'ServiceTimestamp' ; } private _onTimestampDiscoverRequest ( request , response ) { const req = request as IPCMessages . TimestampDiscoverRequest ; const filedata = ServiceStreams . getStreamFile ( req . session ) ; if ( filedata instanceof Error ) { return response ( new IPCMessages . TimestampDiscoverResponse ( { id : req . id , error : filedata . message , } ) ) ; } fs . stat ( filedata . file , ( err , stats ) => { if ( err ) { return response ( new IPCMessages . TimestampDiscoverResponse ( { id : req . id , error : `Fail to get access to session file due error: ${ err . message }` , } ) ) ; } if ( stats . size === 0 ) { return response ( new IPCMessages . TimestampDiscoverResponse ( { id : req . id , error : `Session is empty` , } ) ) ; } const controller = new MergeDiscover ( [ { file : filedata . file } ] ) ; controller . discover ( undefined , true ) . then ( ( processed ) => { if ( processed [ 0 ] . format === undefined || typeof processed [ 0 ] . minTime !== 'number' || typeof processed [ 0 ] . maxTime !== 'number' ) { return response ( new IPCMessages . TimestampDiscoverResponse ( { id : req . id , error : typeof processed [ 0 ] . error !== 'string' ? `Fail to detect datetime format` : processed [ 0 ] . error , } ) ) ; } response ( new IPCMessages . TimestampDiscoverResponse ( { id : req . id , format : processed [ 0 ] . format , error : processed [ 0 ] . error , minTime : processed [ 0 ] . minTime , maxTime : processed [ 0 ] . maxTime , } ) ) ; } ) . catch ( ( error ) => { response ( new IPCMessages . TimestampDiscoverResponse ( { id : req . id , error : error . message , } ) ) ; } ) . finally ( ( ) => { this . _removeTask ( req . signature , req . id ) ; } ) ; this . _addTask ( req . signature , req . id , controller ) ; } ) ; } private _onTimestampTestRequest ( request , response ) { const req = request as IPCMessages . TimestampTestRequest ; const controller = new MergeFormat ( req . format ) ; controller . validate ( req . flags ) . then ( ( regexp ) => { if ( regexp === undefined ) { response ( new IPCMessages . TimestampTestResponse ( { id : req . id , error : `Fail to generate regexp based on "${ req . format }" format string.` , } ) ) ; } else { response ( new IPCMessages . TimestampTestResponse ( { id : req . id , format : { regex : regexp , flags : [ ] , format : req . format , } , } ) ) ; } } ) . catch ( ( error ) => { response ( new IPCMessages . TimestampTestResponse ( { id : req . id , error : error . message , } ) ) ; } ) ; } private _onTimestampExtractRequest ( request , response ) { const req = request as IPCMessages . TimestampExtractRequest ; const controller = new TimestampExtract ( req . str , req . format ) ; controller . extract ( req . replacements ) . then ( ( timestamp ) => { if ( timestamp === undefined ) { response ( new IPCMessages . TimestampExtractResponse ( { id : req . id , error : `Fail to extract timestamp based on "${ req . format }" format string.` , } ) ) ; } else { response ( new IPCMessages . TimestampExtractResponse ( { id : req . id , timestamp : timestamp , } ) ) ; } } ) . catch ( ( error ) => { response ( new IPCMessages . TimestampExtractResponse ( { id : req . id , error : error . message , } ) ) ; } ) ; } private _onTimestampExportCSVRequest ( request , response ) { const req = request as IPCMessages . TimestampExportCSVRequest ; dialog . showSaveDialog ( { title : 'Saving filters' , filters : [ { name : 'CSV Files' , extensions : [ 'csv' ] } ] , } ) . then ( ( results ) => { if ( typeof results . filePath !== 'string' || results . filePath . trim ( ) === '' ) { return response ( new IPCMessages . TimestampExportCSVResponse ( { id : req . id , } ) ) ; } fs . writeFile ( results . filePath , req . csv , ( error ) => { if ( error ) { response ( new IPCMessages . TimestampExportCSVResponse ( { id : req . id , error : error . message , } ) ) ; } else { response ( new IPCMessages . TimestampExportCSVResponse ( { id : req . id , } ) ) ; } } ) ; } ) ; } private _addTask ( session , taskId , controller ) { const storage = this . _tasks . get ( session ) ; if ( storage === undefined ) { this . _tasks . set ( session , new Map ( [ [ taskId , controller ] ] ) ) ; } else { storage . set ( taskId , controller ) ; this . _tasks . set ( session , storage ) ; } } private _removeTask ( session , taskId ) { const storage = this . _tasks . get ( session ) ; if ( storage !== undefined ) { storage . delete ( taskId ) ; this . _tasks . set ( session , storage ) ; } } private _onSessionClosed ( guid ) { const storage = this . _tasks . get ( guid ) ; if ( storage !== undefined ) { storage . forEach ( ( controller , id ) => { controller . abort ( ) . then ( ( ) => { this . _logger . debug ( `Task "${ id }" is aborted` ) ; } ) ; } ) ; this . _tasks . delete ( guid ) ; } } } export default ( new ServiceTimestamp ( ) ) ;