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 } if (-not [Session]::getCurrent().canRead($source)){ Write-Host 'Background jobs are still active, try later.' -ForegroundColor Red return } $msgSource = (Read-Message -source $source -id $id -progressMessage 'Locating source message...') 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 'Background jobs are still active, try later.' -ForegroundColor Red return } if (-not $timestampSource) { Write-Host "" $approximate = $true $approxThreshold = 2000 Write-Host ('Message is not traceable, approximative messages within a range of +/- {0}' -f [DateTimeHelper]::FormatMilliseconds($approxThreshold)) $timestampSource = [DateTimeHelper]::ConvertFromTimestamp($msgSource.createdAt, $true) } else { if ($approxThreshold -ne 0){ $approximate = $true Write-Host ('Displaying approximative messages within a range of for +/- {0}' -f [DateTimeHelper]::FormatMilliseconds($approxThreshold)) $timestampSource = [DateTimeHelper]::ConvertToTimestamp($msgSource.createdAt.ToUniversalTime()) } } Write-Output $msgSource Read-Message -source $target -progressMessage "Loading $target messages..." | Where-Object { if ($approximate){ $timestampTarget = [DateTimeHelper]::ConvertToTimeStamp($_.createdAt.ToUniversalTime()) $diff = $timestampTarget - $timestampSource if ($diff -gt 0) { break } else { $continue = ([Math]::Abs($diff) -lt $approxThreshold) } } else { $continue = [bool]($timestampSource -eq $_.timestamp) if ($continue) { Write-Output $_ break } } return $continue } } |