modules/FeedProcessor/AuditLog/Message/Performance.psm1

using module '..\..\..\Enums.psm1'
using module '..\..\..\Helper\ObjectHelper.psm1'
using module '..\..\..\Helper\DateTimeHelper.psm1'

class Performance{
    [SOURCE_TYPE] $targetType
    hidden [long] $providerSend
    [long] $travelFromProvider
    hidden [long] $adapterReceived
    hidden [long] $adapterFinished
    [long] $processingAdapter
    hidden [long] $coreReceived
    [long] $travelFromAdapter
    hidden [long] $coreFinished
    [long] $processingCore
    hidden [long] $sbReceived
    [long] $travelFromCore
    hidden [long] $sbFinished
    [long] $processingSb

    [long] $total

    Performance([SOURCE_TYPE] $sourceType, [SOURCE_TYPE] $targetType, [long] $start, [long] $end, [PSCustomObject] $metrics, [bool] $showAbsoluteNumbers, [bool] $ignoreProviderTimestamp){

        $this.targetType = $targetType

        if ($sourceType -eq $targetType){
            switch ($sourceType) {
                ([SOURCE_TYPE]::backend) {
                    $this.sbFinished = $end
                    $this.sbReceived = $start
                }
                ([SOURCE_TYPE]::feedService) {
                    $this.coreFinished = $end
                    $this.coreReceived = $start
                }
                ([SOURCE_TYPE]::adapter) {
                    $this.adapterFinished = $end
                    $this.adapterReceived = $start
                }
            }
        }

        if (-not $this.coreFinished) { $this.coreFinished = $metrics.coreFinishedProcessingTimestamp }
        if (-not $this.coreReceived) { $this.coreReceived = $metrics.coreReceivedTimestamp }
        if (-not $this.adapterFinished) { $this.adapterFinished = $metrics.adapterFinishedProcessingTimestamp }
        if (-not $this.adapterReceived) { $this.adapterReceived = $metrics.adapterReceivedTimestamp }
        if (-not $this.providerSend) { $this.providerSend = $metrics.providerTimestamp }

        $this.Calculate($targetType, $showAbsoluteNumbers, $ignoreProviderTimestamp)
    }

    hidden [long] Abs([double] $number, [bool] $onlyPositiveNumbers){
        if ($onlyPositiveNumbers){
            return ([Math]::Abs($number))
        }
        else {
            return $number
        }
    }

    hidden [void] Calculate([SOURCE_TYPE] $targetType, [bool] $showAbsoluteNumbers, [bool] $ignoreProviderTimestamp){
        $tempTargetType = $targetType

        $isTotalCalculated = $false
        while ($tempTargetType -ne [SOURCE_TYPE]::provider){
            switch ($tempTargetType) {
                ([SOURCE_TYPE]::backend) {

                    if ($this.sbReceived -ne 0 -and $this.coreFinished -ne 0) {
                        $this.travelFromCore = $this.Abs($this.sbReceived - $this.coreFinished, $showAbsoluteNumbers)
                    }
                    if ($this.sbFinished -ne 0 -and $this.sbReceived -ne 0) {
                        $this.processingSb = $this.Abs($this.sbFinished - $this.sbReceived, $showAbsoluteNumbers)
                    }
                    if (-not $isTotalCalculated -and $this.sbFinished -ne 0 -and $this.providerSend -ne 0) {
                        if ($showAbsoluteNumbers){
                            if ($ignoreProviderTimestamp){
                                $this.total = $this.processingSb + $this.travelFromCore + $this.processingCore + $this.travelFromAdapter + $this.processingAdapter
                            }
                            else {
                                $this.total = $this.processingSb + $this.travelFromCore + $this.processingCore + $this.travelFromAdapter + $this.processingAdapter + $this.travelFromProvider
                            }
                        }
                        else {
                            if ($ignoreProviderTimestamp){
                                $this.total = $this.sbFinished - $this.adapterReceived
                            }
                            else {
                                $this.total = $this.sbFinished - $this.providerSend
                            }
                        }

                        $isTotalCalculated = $true
                    }
                    $tempTargetType = [SOURCE_TYPE]::feedService
                }
                ([SOURCE_TYPE]::feedService) {

                    if ($this.coreReceived -ne 0 -and $this.adapterFinished -ne 0) {
                        $this.travelFromAdapter = $this.Abs($this.coreReceived - $this.adapterFinished, $showAbsoluteNumbers)
                    }
                    if ($this.coreFinished -ne 0 -and $this.coreReceived -ne 0) {
                        $this.processingCore = $this.Abs($this.coreFinished - $this.coreReceived, $showAbsoluteNumbers)
                    }
                    if (-not $isTotalCalculated -and $this.coreFinished -ne 0 -and $this.providerSend -ne 0) {
                        if ($showAbsoluteNumbers){
                            if ($ignoreProviderTimestamp){
                                $this.total = $this.processingCore + $this.travelFromAdapter + $this.processingAdapter
                            }
                            else {
                                $this.total = $this.processingCore + $this.travelFromAdapter + $this.processingAdapter + $this.travelFromProvider
                            }
                        }
                        else {
                            if ($ignoreProviderTimestamp){
                                $this.total = $this.coreFinished - $this.adapterReceived
                            }
                            else {
                                $this.total = $this.coreFinished - $this.providerSend
                            }
                        }

                        $isTotalCalculated = $true
                    }
                    $tempTargetType = [SOURCE_TYPE]::adapter
                }
                ([SOURCE_TYPE]::adapter) {

                    if ($this.adapterReceived -ne 0 -and $this.providerSend -ne 0) {
                        $this.travelFromProvider = $this.Abs($this.adapterReceived - $this.providerSend, $showAbsoluteNumbers)
                    }
                    if ($this.adapterFinished -ne 0 -and $this.adapterReceived -ne 0) {
                        $this.processingAdapter = $this.Abs($this.adapterFinished - $this.adapterReceived, $showAbsoluteNumbers)
                    }
                    if (-not $isTotalCalculated -and $this.adapterFinished -ne 0 -and $this.providerSend -ne 0) {
                        if ($showAbsoluteNumbers){
                            if ($ignoreProviderTimestamp){
                                $this.total = $this.processingAdapter + $this.travelFromProvider
                            }
                            else {
                                $this.total = $this.processingAdapter
                            }
                        }
                        else {
                            if ($ignoreProviderTimestamp){
                                $this.total = $this.adapterFinished - $this.adapterReceived
                            }
                            else {
                                $this.total = $this.adapterFinished - $this.providerSend
                            }
                        }

                        $isTotalCalculated = $true
                    }
                    $tempTargetType = [SOURCE_TYPE]::provider
                }
            }
        }
    }

    [PSCustomObject] ToOutput(){
        [PSCustomObject] $retValue = $null

        switch ($this.targetType) {
            ([SOURCE_TYPE]::backend) {
                $retValue = [PSCustomObject]@{sb=$this.processingSb; travelCore=$this.travelFromCore; core=$this.processingCore;
                    travelAdapter=$this.travelFromAdapter; adapter=$this.processingAdapter; travelProvider=$this.travelFromProvider; total=$this.total}
            }
            ([SOURCE_TYPE]::feedService) {
                $retValue = [PSCustomObject]@{core=$this.processingCore; travelAdapter=$this.travelFromAdapter; adapter=$this.processingAdapter;
                    travelProvider=$this.travelFromProvider; total=$this.total}
            }
            ([SOURCE_TYPE]::adapter) {
                $retValue = [PSCustomObject]@{adapter=$this.processingAdapter; travelProvider=$this.travelFromProvider; total=$this.total}
            }
        }

        return $retValue
    }

    [Performance] Copy(){
        $performance = [Performance]::new()

        $performance.targetType = $this.targetType
        $performance.providerSend = $this.providerSend
        $performance.travelFromProvider = $this.travelFromProvider
        $performance.adapterReceived = $this.adapterReceived
        $performance.adapterFinished = $this.adapterFinished
        $performance.processingAdapter = $this.processingAdapter
        $performance.coreReceived = $this.coreReceived
        $performance.travelFromAdapter = $this.travelFromAdapter
        $performance.coreFinished = $this.coreFinished
        $performance.processingCore = $this.processingCore
        $performance.sbReceived = $this.sbReceived
        $performance.travelFromCore = $this.travelFromCore
        $performance.sbFinished = $this.sbFinished
        $performance.processingSb = $this.processingSb
        $performance.total = $this.total

        return $performance
    }
}