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 $LocalizedData.NoActiveSessionMessage
        return
    }
    if (-not [Session]::getCurrent().canRead($source)){
        Write-Host $LocalizedData.BackgroundJobsStillActiveMessage -ForegroundColor Red
        return
    }

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

    $timestampSource = $msgSource.timestamp

    #determine target
    $isChanged = $false
    switch ($msgSource.sourceType) {
        ([SOURCE_TYPE]::backend) { $target = [SOURCE]::core; $isChanged = $true }
        ([SOURCE_TYPE]::feedService) {
            if ($null -eq $target){
                $target = [ProcessorFactory]::getSourceByProducer($msgSource.producer)
                if ($target -eq [SOURCE]::core){ $target = [SOURCE]::sb; $isChanged = $true  }
            }
        }
        ([SOURCE_TYPE]::adapter) { $target = [SOURCE]::core; $isChanged = $true }
    }
    if ($isChanged) { Write-Host "Setting $target as target..." }

    if (-not [Session]::getCurrent().canRead($target)){
        Write-Host $LocalizedData.BackgroundJobsStillActiveMessage -ForegroundColor Red
        return
    }

    if (-not $timestampSource) {
        Write-Host ""

        $approximate = $true
        $approxThreshold = 2000
        Write-Host ($LocalizedData.NoTracableMessage -f [DateTimeHelper]::FormatMilliseconds($approxThreshold))
        $timestampSource = [DateTimeHelper]::ConvertFromTimestamp($msgSource.createdAt, $true)
    }
    else {
        if ($approxThreshold -ne 0){
            $approximate = $true
            Write-Host ($LocalizedData.ApproximativeDisplayMessage -f [DateTimeHelper]::FormatMilliseconds($approxThreshold))
            $timestampSource = [DateTimeHelper]::ConvertToTimestamp($msgSource.createdAt.ToUniversalTime())
        }
    }

    Write-Output $msgSource

    $shouldBreak = $false
    Read-Message -source $target -progressMessage "Loading $target messages..." -surpressOutToHost | Where-Object {
        if ($shouldBreak){ break }
        if ($approximate){
            $timestampTarget = [DateTimeHelper]::ConvertToTimeStamp($_.createdAt.ToUniversalTime())

            $diff = $timestampTarget - $timestampSource
            if ($diff -gt 0) {
                break
            }
            else {
                $continue = ([Math]::Abs($diff) -lt $approxThreshold)
            }
        }
        else {
            $continue = ($timestampSource -eq $_.timestamp)
            if ($continue) { $shouldBreak = $true }
        }

        return $continue
    }
}