private/Read-RawLog.ps1

function Read-RawLog{
    param(
        # Selecting which date type to be analyzed Event or Market.
        [DATATYPE] $datatype,
        [Parameter(Mandatory=$true)]
        # Audit log source file from where the messages will be loaded.
        [SOURCE] $source,
        # Where the appointed marketId should be searched as:
        # * internalId - regular marketId used in the appointed source
        # * externalId - reference for the externalId, the one participated in this log from lower level
        # * typeId - used at some adapters when there is generalization of the marketId's typically as certain blueprint type.
        #
        # Default value is id.
        [SEARCH_SCOPE] $searchScope,
        # Id of the searched scope.
        [string[]] $id = '*',
        # Referent message time taken as baseline from where the messages will be start analyzing.
        [string] $referentTime,
        # Timeframe in which the messages will be analyzed after the referent time.
        [string] $timeSpan,
        # If the tracing is requested for Outcomes, this could filter down the wanted outcomeId.
        [string] $oid = '*',
        # Aggregate state of the item
        [switch] $aggregate,
        # Displayed progress message
        [string] $progressMessage="Read $source audit log file...",
        # provide the result as native object rather than display one
        [switch] $asObject
    )

    #region Init

    if ([Session]::activeSessions.Count -eq 0){
        Write-Host $LocalizedData.NoActiveSessionMessage -ForegroundColor Red
        return
    }
    if (-not [Session]::getCurrent().canRead($source)){
        Write-Host $LocalizedData.BackgroundJobsStillActiveMessage -ForegroundColor Red
        return
    }

    $afterDateTime, $beforeDateTime = [DateTimeHelper]::getAfterBefore($referentTime, $timeSpan)
    if ($timeSpan -and -not $referentTime){ $referentTime = (now)}

    $hasReturn = $false

    $logFile = [Session]::getCurrent().auditLogFile
    $processor = [ProcessorFactory]::getBySource($source)

    $totalLines = $logFile.AssignFile($source)
    if($referentTime) {
        $count = $logFile.Seek($afterDateTime)
    }
    else {
        $count = 0
    }

    [Message] $prevEventMsg=$null; [Message] $prevMarketMsg=$null; [Message] $prevMsg=$null;
    [Message]::SetDisplay($dataType)

    #endregion

    while (-not $logFile.EOF()){
        $line = $logFile.ReadLine()

        if ($line -and $line.created_at -lt $beforeDateTime){
            $processor.ProcessMessage($line)

            if ($processor.isEventRelated($line)){
                $processor.ProcessEvent($line, $false)

                if ($processor.shouldReprocess -and ($aggregate -and $prevMsg)) {
                    $processor.message.event.Aggregate($prevMsg.event)
                    $processor.Resolve()
                }
                elseif (-not $processor.shouldReprocess -and ($aggregate -and $prevEventMsg)) {
                    $processor.message.event.Aggregate($prevEventMsg.event)
                    $processor.Resolve()
                }

                if ($processor.message) {

                    $prevEventMsg = $processor.message.Copy()
                    $prevMsg = $processor.message.Copy()

                    if ($asObject){
                        Write-Output $prevEventMsg
                    }
                    else {
                        Write-Output $prevEventMsg.ToOutput()
                    }

                    $hasReturn = $true
                }
            }

            if ($dataType -in ([DATATYPE]::Market, [DATATYPE]::Outcome)){ #-and $processor.isMarketRelated($line, $searchScope, $id)
                $processor.ProcessMarket($line, $searchScope, $id, $oid)

                if ($processor.shouldReprocess -and ($aggregate -and $prevMsg)) {
                    $processor.message.event.Aggregate($prevMsg.event)
                    $processor.Resolve()
                }
                elseif (-not $processor.shouldReprocess -and ($aggregate -and $prevMarketMsg)) {
                    $processor.message.event.Aggregate($prevMarketMsg.event)
                    $processor.Resolve()
                }

                if ($processor.message.event.markets.Count -gt 0) {
                    $prevMarketMsg = $processor.message.Copy()
                    $prevMsg = $processor.message.Copy()

                    if ($asObject){
                        Write-Output $prevMarketMsg
                    }
                    else {
                        foreach ($market in $prevMarketMsg.event.markets) {
                            Write-Output $prevMarketMsg.ToOutput($market)

                            if ($datatype -eq [DATATYPE]::Outcome){
                                foreach ($outcome in $market.outcomes) {
                                    Write-Output $prevMarketMsg.ToOutput($market, $outcome)
                                }
                            }

                            $hasReturn = $true
                        }
                    }
                }
            }
        }
        else { break }

        $count++
        $percentage = 100 * $logFile.getFilePosition() / $totalLines
        Write-Progress -Activity $progressMessage -PercentComplete $percentage
    }
    Write-Progress -Activity $progressMessage -Completed

    if (-not $hasReturn -and -not $asObject){
        Write-Host $LocalizedData.NoOutputMessage -ForegroundColor Red
    }
}