public/Message/Trace-Message.ps1

using module '..\..\modules\Enums.psm1'
using module '..\..\modules\Helper\DateTimeHelper.psm1'
using module '..\..\modules\Session.psd1'

function Trace-Message{
    [Alias('trms')]
    param (
        [Parameter(Mandatory=$true)]
        [SOURCE]$source,
        [Parameter(Mandatory=$true)]
        [int32]$id,
        [SOURCE]$target,
        [int32]$approxThreshold
    )

    if ([Session]::activeSessions.Count -eq 0){
        Write-Error 'There is no active session. Import audit logs before they are analyzed.'
        return
    }

    $msgObjSource = (Select-Message -source $source -id $id -progressMessage 'Locating source message...')
    if (-not $msgObjSource){
        Write-Error "There is no message with id: $id from $source"
        return
    }

    $processor = [ProcessorFactory]::getBySource($source)
    $processor.ProcessMessage($msgObjSource)
    $timestampSource = $processor.message.timestamp

    #determine target
    switch ($processor.sourceType) {
        ([SOURCE_TYPE]::backend) { $target = [SOURCE]::core }
        ([SOURCE_TYPE]::feedService) {
            if ($null -eq $target){
                $target = [ProcessorFactory]::getSourceByProducer($processor.message.producer)
                if ($target -eq [SOURCE]::core){
                    $target = [SOURCE]::sb
                    Write-Host "No feedId in the source message, setting $target as target..."
                }
            }
        }
        ([SOURCE_TYPE]::adapter) { $target = [SOURCE]::core }
    }

    if (-not $timestampSource) {
        Write-Host "Message is not traceable, approximative messages will be displayed!"

        $approximate = $true
        $timestampSource = [DateTimeHelper]::ConvertFromTimestamp($msgObjSource.createdAt, $true)
    }
    else {
        if ($approxThreshold -ne 0){
            $approximate = $true
            Write-Host ('Displaying approximative messages with threshold for +/- {0}' -f [DateTimeHelper]::FormatMilliseconds($approxThreshold))
            $timestampSource = [DateTimeHelper]::ConvertFromTimestamp($msgObjSource.createdAt, $true)
        }
    }

    $lineTarget = Select-Message -source $target -progressMessage "Loading $target messages..."

    Write-Output $processor.message
    $i=0
    while($i -lt $lineTarget.Length) {
        $message = $lineTarget[$i]

        if ($approximate){
            $timestampTarget = [DateTimeHelper]::ConvertToTimeStamp($message.createdAt.ToUniversalTime())
            $diff = [Math]::Abs($timestampTarget-$timestampSource)
            $cont = [bool]([Math]::Abs($diff) -le $approxThreshold)
        }
        else {
            $processor = [ProcessorFactory]::getBySource($target)
            $processor.ProcessMessage($message.content)

            $timestampTarget = $processor.message.timestamp
            $cont = [bool]($timestampSource -eq $timestampTarget)
        }

        if ($cont){
            Write-Output $processor.message
        }

        $i+=1
    }
}