public/Market/Trace-DefinitionOdds.ps1

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

# TODO Test
function Trace-DefinitionOdds{
    [CmdletBinding()]
    [Alias('trco')]
    param(
        [SEARCH_SCOPE]$searchScope = [SEARCH_SCOPE]::internalId,
        [Parameter(ValueFromPipelineByPropertyName=$true, Mandatory=$true)][Alias('id')]
        [int]$marketId,
        [string] $referentTime,
        [string] $timeSpan,
        [switch]$displayEqual)

    BEGIN {
        if (-not $marketId) {
            Write-Error "Terminating function, marketId is not provided!"
            break
        }
        if ([Session]::activeSessions.Count -eq 0){
            Write-Error 'There is no active session. Import audit logs before they are analyzed.'
            break
        }

        #$logFile = [Session]::getCurrent().auditLogFile

        $afterDateTime, $beforeDateTime = [DateTimeHelper]::getAfterBefore($referentTime, $timeSpan)
        if ($timeSpan -and -not $referentTime){ $referentTime = (now)}
        $isPrinted=$false; $countSame=0; $countDiff=0; $maxDiffAdapter=0; $maxDiffProvider=0
    }
    PROCESS {
        #$core = $logFile.ReadContent([SOURCE]::core, [DATATYPE]::Market, $afterDateTime, $beforeDateTime, $searchScope, $marketId, '*', $true)
        $core = (Read-Market -source core -referentTime $afterDateTime -timeSpan ($beforeDateTime-$afterDateTime) -id $marketId -aggregate -asObject)

        $tempIds = $core | Where-Object { $_.event.markets.Count -gt 0 } | Select-Object @{Name='externalId'; Expression={ $_.event.markets.externalId }} -Unique
        $extIds = @()
        foreach ($i in $tempIds) { $extIds += $i.externalId }

        $allProducers = $core | Where-Object { $_.event.markets.Count -gt 0 -and $_.event.markets.externalId -in $extIds -and $_.producer -ne 'DFS' } | Select-Object @{Name='producer'; Expression={ $_.producer }} -Unique
        $coreWithExtMarkets = $core | Where-Object { $_.event.markets.externalId -in $extIds }

        # Find out how the producers were changed in the lifecycle of the market
        $producer = ''; $start = ''; $end = ''; $focus= @()
        foreach ($msg in $coreWithExtMarkets) {
            if ($producer -ne $msg.producer){
                if (-not $producer -and $msg.Producer -in $allProducers.producer) {
                    $producer = $msg.producer
                    $start = $msg.createdAt
                    $end = ''
                }
                if ($msg.Producer -ne $producer -and $msg.Producer -in $allProducers.producer) {
                    $focus += [PSCustomObject]@{producer=$producer; afterDateTime=$start; beforeDateTime=$end}

                    $producer = $msg.producer
                    $start = $msg.createdAt
                    $end = ''
                }
            }
            $end = $msg.createdAt
        }
        $focus += [PSCustomObject]@{producer=$producer; afterDateTime=$start; beforeDateTime=$end}

        if ($focus.Count -eq 0) {
            Write-Error "Terminating function, there are no producers!"
            return
        }
        else {
            foreach ($singleFocus in $focus) {
                $status = @()
                $target = [ProcessorFactory]::getSourceByProducer($singleFocus.producer)

                if ($null -ne $target){
                    $targetProcessor = [ProcessorFactory]::getBySource($target)
                    if ($targetProcessor.isTypeBased){
                        $targetSearchScope = [SEARCH_SCOPE]::typeId
                    }
                    else {
                        $targetSearchScope = [SEARCH_SCOPE]::internalId
                    }

                    $focusCore = ($core | Where-Object { $_.createdAt -ge $singleFocus.afterDateTime -and $_.createdAt -le $singleFocus.beforeDateTime })

                    $adapterAll = (Read-Market -source $target `
                        -searchScope $targetSearchScope -id $extIds -aggregate -asObject -progressMessage "Read $target audit log for markets: $extIds")
                    $focusAdapter = ($adapterAll | Where-Object { $_.createdAt -ge $singleFocus.afterDateTime -and $_.createdAt -le $singleFocus.beforeDateTime } )

                    $progressMsg = ('Comparing core and {0} odds ({1} from {2} in span {3})...' -f `
                        @($target, $singleFocus.producer, $singleFocus.afterDateTime, $singleFocus.beforeDateTime))

                    $status = (Compare-ProcessorsPerMarket -core $focusCore -adapter $focusAdapter -producers ([ProcessorFactory]::getProducersBySource($target)) `
                        -progressMessage $progressMsg -displayEqual $displayEqual)

                    foreach ($obj in $status.items) {
                        Write-Output $obj
                    }

                    $isPrinted = $isPrinted -or ($s.same -gt 0 -or $s.unique -gt 0 -or $s.diff -gt 0)
                    $countSame += $s.same
                    $countDiff += $s.diff
                    if ($maxDiffAdapter -lt $s.maxAdpater) { $maxDiffAdapter = $s.maxAdpater }
                    if ($maxDiffProvider -lt $s.maxProvider) { $maxDiffProvider = $s.maxProvider }
                }
                else {
                    Write-Error ('Unknown producer: {0}, cannot determine target!' -f $singleFocus.producer)
                    continue
                }
            }
        }
    }
    END {
        if ($countSame -gt 0){
            Write-Host "Total $countSame odd changes are same with maximum delay of adapter=$maxDiffAdapter ms and provider=$maxDiffProvider ms." -ForegroundColor Green
        }

        if ($countDiff -gt 0){
            Write-Host "Total $countDiff odd changes are different!" -ForegroundColor Red
        }
        else {
            Write-Host "There are no differences in odds." -ForegroundColor Green
        }

        if (-not $isPrinted -and $countSame -eq 0 -and $countDiff -eq 0){
            Write-Host 'There is no output for the selected filter(s)!' -ForegroundColor Red
        }
    }
}