Private/Utils/Show-ApiStatistics.ps1

function Show-ApiStatistics {
    <#
    .SYNOPSIS
        Display Azure API call statistics collected during execution.
     
    .DESCRIPTION
        Displays comprehensive statistics about Azure API calls including:
        - Total calls, throttled calls, failed calls
        - Cache hit ratio and counts
        - Calls per minute (rate)
        - Breakdown by API type (PolicyDefinition, PolicySet, Assignment, etc.)
        - Total execution duration
     
    .EXAMPLE
        Show-ApiStatistics
        # Displays formatted statistics table
     
    .OUTPUTS
        None (displays to console)
    #>

    [CmdletBinding()]
    param()
    
    # Check if statistics have been initialized
    if (-not $script:ApiCallStats) {
        Write-Warning "API statistics not initialized. Call Initialize-ApiStatistics first."
        return
    }
    
    # Calculate derived metrics
    $duration = (Get-Date) - $script:ApiCallStats.StartTime
    
    $callsPerMinute = if ($duration.TotalMinutes -gt 0) { 
        [math]::Round($script:ApiCallStats.TotalCalls / $duration.TotalMinutes, 2) 
    } else { 0 }
    
    $totalCacheCalls = $script:ApiCallStats.CacheHits + $script:ApiCallStats.CacheMisses
    $cacheHitRatio = if ($totalCacheCalls -gt 0) {
        [math]::Round(($script:ApiCallStats.CacheHits / $totalCacheCalls) * 100, 2)
    } else { 0 }
    
    $successRate = if ($script:ApiCallStats.TotalCalls -gt 0) {
        $successful = $script:ApiCallStats.TotalCalls - $script:ApiCallStats.FailedCalls
        [math]::Round(($successful / $script:ApiCallStats.TotalCalls) * 100, 2)
    } else { 100 }
    
    # Display statistics
    Write-Host ""
    Write-Host "╔══════════════════════════════════════════════════════╗" -ForegroundColor Cyan
    Write-Host "║ 📊 AZURE API CALL STATISTICS ║" -ForegroundColor Cyan
    Write-Host "╠══════════════════════════════════════════════════════╣" -ForegroundColor Cyan
    Write-Host ("║ 🌐 Total API calls: {0,-25} ║" -f $script:ApiCallStats.TotalCalls) -ForegroundColor DarkCyan
    Write-Host ("║ 🔄 Throttled calls: {0,-25} ║" -f $script:ApiCallStats.ThrottledCalls) -ForegroundColor $(if ($script:ApiCallStats.ThrottledCalls -gt 0) { "Yellow" } else { "DarkCyan" })
    Write-Host ("║ ❌ Failed calls: {0,-25} ║" -f $script:ApiCallStats.FailedCalls) -ForegroundColor $(if ($script:ApiCallStats.FailedCalls -gt 0) { "Red" } else { "DarkCyan" })
    Write-Host ("║ ✅ Success rate: {0,-24}% ║" -f $successRate) -ForegroundColor $(if ($successRate -ge 95) { "Green" } elseif ($successRate -ge 80) { "Yellow" } else { "Red" })
    Write-Host "╠══════════════════════════════════════════════════════╣" -ForegroundColor Cyan
    Write-Host ("║ 💾 Cache hits: {0,-25} ║" -f $script:ApiCallStats.CacheHits) -ForegroundColor Green
    Write-Host ("║ 🔍 Cache misses: {0,-25} ║" -f $script:ApiCallStats.CacheMisses) -ForegroundColor DarkYellow
    Write-Host ("║ 📈 Cache hit ratio: {0,-24}% ║" -f $cacheHitRatio) -ForegroundColor $(if ($cacheHitRatio -ge 70) { "Green" } elseif ($cacheHitRatio -ge 40) { "Yellow" } else { "Red" })
    Write-Host "╠══════════════════════════════════════════════════════╣" -ForegroundColor Cyan
    Write-Host ("║ ⚡ Calls per minute: {0,-25} ║" -f $callsPerMinute) -ForegroundColor DarkCyan
    Write-Host ("║ ⏱️ Total duration: {0,-23} min ║" -f [math]::Round($duration.TotalMinutes, 2)) -ForegroundColor DarkCyan
    Write-Host ("║ 🕐 Started at: {0,-25} ║" -f $script:ApiCallStats.StartTime.ToString("yyyy-MM-dd HH:mm:ss")) -ForegroundColor DarkGray
    Write-Host "╠══════════════════════════════════════════════════════╣" -ForegroundColor Cyan
    Write-Host "║ 🔬 API BREAKDOWN: ║" -ForegroundColor Cyan
    Write-Host ("║ ├─ Policy Definitions: {0,-25} ║" -f $script:ApiCallStats.PolicyDefinitionCalls) -ForegroundColor DarkGray
    Write-Host ("║ ├─ Policy Sets: {0,-25} ║" -f $script:ApiCallStats.PolicySetDefinitionCalls) -ForegroundColor DarkGray
    Write-Host ("║ ├─ Assignments: {0,-25} ║" -f $script:ApiCallStats.PolicyAssignmentCalls) -ForegroundColor DarkGray
    Write-Host ("║ ├─ Management Groups: {0,-25} ║" -f $script:ApiCallStats.ManagementGroupCalls) -ForegroundColor DarkGray
    Write-Host ("║ └─ Subscriptions: {0,-25} ║" -f $script:ApiCallStats.SubscriptionCalls) -ForegroundColor DarkGray
    Write-Host "╚══════════════════════════════════════════════════════╝" -ForegroundColor Cyan
    
    # Warning messages
    if ($script:ApiCallStats.ThrottledCalls -gt 0) {
        Write-Warning "⚠️ Throttling detected $($script:ApiCallStats.ThrottledCalls) times during execution"
        Write-Warning " Consider reducing request rate or increasing delays between calls"
    }
    
    if ($script:ApiCallStats.FailedCalls -gt 0) {
        Write-Warning "⚠️ $($script:ApiCallStats.FailedCalls) API calls failed during execution"
        Write-Warning " Check permissions and network connectivity"
    }
    
    if ($cacheHitRatio -lt 30 -and $totalCacheCalls -gt 10) {
        Write-Warning "⚠️ Low cache hit ratio ($cacheHitRatio%). Consider preloading data or reviewing query patterns"
    }
    
    Write-Host ""
}