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.
        [timespan] $timeSpan,
        # Additional tracing details which could be used. For markets all values are allowed: Competitors, Producers, Outcomes and Competitors.
        [TRACE[]] $trace = @(),
        # If the tracing is requested for Outcomes, this could filter down the wanted outcomeId.
        [string] $outcomeId = '*',
        # 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 'There is no active session. Import audit logs before they are analyzed.' -ForegroundColor Red
        return
    }
    if (-not [Session]::getCurrent().canRead($source)){
        Write-Host 'Background jobs are still active, try later.' -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;

    #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.EventToOutput($dataType)
                    }

                    $hasReturn = $true
                }
            }

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

                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.MarketToOutput($dataType, $market)

                            if ($datatype -eq [DATATYPE]::Outcome){
                                foreach ($outcome in $market.outcomes) {
                                    Write-Output $prevMarketMsg.OutcomeToOutput($dataType, $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 'There is no output for the selected filter(s)!' -ForegroundColor Red
    }
}