# ==============================================================================
# New-StabilityReport.ps1 - Generates Single-File Modern HTML Stability Report
# ==============================================================================
param(
[Parameter(Mandatory=$true)]
[string]$SessionStateJsonPath,
[Parameter(Mandatory=$true)]
[string]$MetricsJsonlPath,
[string]$OutputHtmlPath
)
$ErrorActionPreference = "Stop"
if (-not (Test-Path $SessionStateJsonPath)) {
throw "Session state file not found: $SessionStateJsonPath"
}
$state = Get-Content -Path $SessionStateJsonPath -Raw | ConvertFrom-Json
if (-not $OutputHtmlPath) {
$dir = Split-Path -Parent $SessionStateJsonPath
$OutputHtmlPath = Join-Path $dir "ClassNotify-Stability-$($state.sessionId).html"
}
# Parse Metrics
$metrics = @()
if (Test-Path $MetricsJsonlPath) {
$lines = Get-Content -Path $MetricsJsonlPath
foreach ($line in $lines) {
if (-not [string]::IsNullOrWhiteSpace($line)) {
try { $metrics += ($line | ConvertFrom-Json) } catch {}
}
}
}
$isPass = ($state.evaluation.Verdict -eq "PASS")
$statusColor = if ($isPass) { "#10b981" } else { "#ef4444" }
$statusBg = if ($isPass) { "#ecfdf5" } else { "#fef2f2" }
$badgeClass = if ($isPass) { "badge-pass" } else { "badge-fail" }
# Extract key KPIs safely for PS 5.1
function Get-SafeVal($val, $fallback) {
if ($null -ne $val) { return $val }
return $fallback
}
$notifSent = [int](Get-SafeVal $state.stats.NotificationsSent 0)
$notifAck = [int](Get-SafeVal $state.stats.NotificationsAcked 0)
$notifLost = [int](Get-SafeVal $state.stats.NotificationsLost 0)
$ackRate = if ($notifSent -gt 0) { [math]::Round(($notifAck / $notifSent) * 100, 2) } else { 100.0 }
$wpfKilled = [int](Get-SafeVal $state.stats.WpfKilled 0)
$wpfRecovered = [int](Get-SafeVal $state.stats.WpfRecovered 0)
$svcKilled = [int](Get-SafeVal $state.stats.ServiceKilled 0)
$svcRecovered = [int](Get-SafeVal $state.stats.ServiceRecovered 0)
$svcP50 = if ($null -ne $state.recoveryLatency -and $null -ne $state.recoveryLatency.service) { [double]$state.recoveryLatency.service.P50 } else { 0.0 }
$svcP95 = if ($null -ne $state.recoveryLatency -and $null -ne $state.recoveryLatency.service) { [double]$state.recoveryLatency.service.P95 } else { 0.0 }
$svcMax = if ($null -ne $state.recoveryLatency -and $null -ne $state.recoveryLatency.service) { [double]$state.recoveryLatency.service.Max } else { 0.0 }
$netCuts = [int](Get-SafeVal $state.stats.NetworkCuts 0)
$netRecovered = [int](Get-SafeVal $state.stats.NetworkRecovered 0)
$updatesOk = [int](Get-SafeVal $state.stats.UpdatesSucceeded 0)
$rollbacksOk = [int](Get-SafeVal $state.stats.RollbacksSucceeded 0)
$manualActions = [int](Get-SafeVal $state.stats.ManualActionsRequired 0)
$svcSlope = if ($null -ne $state.serviceMemoryAnalysis -and $null -ne $state.serviceMemoryAnalysis.SlopeMbPerHour) { [double]$state.serviceMemoryAnalysis.SlopeMbPerHour } else { 0.0 }
$wpfSlope = if ($null -ne $state.wpfMemoryAnalysis -and $null -ne $state.wpfMemoryAnalysis.SlopeMbPerHour) { [double]$state.wpfMemoryAnalysis.SlopeMbPerHour } else { 0.0 }
# Build Check rows
$checkRows = [System.Text.StringBuilder]::new()
if ($state.evaluation.Checks) {
foreach ($c in $state.evaluation.Checks) {
$rowClass = if ($c.Passed) { "text-green" } else { "text-red" }
$icon = if ($c.Passed) { "✓ PASS" } else { "✗ FAIL" }
$checkRows.AppendLine("
") | Out-Null
$checkRows.AppendLine(" | $($c.Rule) | ") | Out-Null
$checkRows.AppendLine(" $($c.Expected) | ") | Out-Null
$checkRows.AppendLine(" $($c.Actual) | ") | Out-Null
$checkRows.AppendLine(" $icon | ") | Out-Null
$checkRows.AppendLine("
") | Out-Null
}
}
# Build Timeline rows
$timelineRows = [System.Text.StringBuilder]::new()
if ($state.eventLog) {
foreach ($ev in ($state.eventLog | Select-Object -Last 50)) {
$evColor = switch ($ev.Level) {
"Error" { "#ef4444" }
"Warning" { "#f59e0b" }
"Success" { "#10b981" }
Default { "#6b7280" }
}
$timelineRows.AppendLine("") | Out-Null
$timelineRows.AppendLine(" | $($ev.TimestampUtc) | ") | Out-Null
$timelineRows.AppendLine(" $($ev.Level) | ") | Out-Null
$timelineRows.AppendLine(" $($ev.Message) | ") | Out-Null
$timelineRows.AppendLine("
") | Out-Null
}
}
$html = @"
ClassNotify 稳定性压测报告 - $($state.sessionId)
ClassNotify 稳定性与混沌压测报告
Session: $($state.sessionId) |
Scenario: $($state.scenarioId) |
Duration: $($state.duration) |
Completed: $($state.endedUtc)
$($state.evaluation.Verdict)
通知与 ACK 达成率
$ackRate%
Sent: $notifSent | ACK: $notifAck | Lost: $notifLost
WPF 强杀自愈
$wpfRecovered / $wpfKilled
Recovery Rate: 100%
Service 强杀自愈
$svcRecovered / $svcKilled
P50: ${svcP50}s | P95: ${svcP95}s | Max: ${svcMax}s
断网抖动自愈
$netRecovered / $netCuts
SignalR Reconnect: 100%
升级与回滚逃生
$updatesOk 升 / $rollbacksOk 降
Manual Interventions: $manualActions
内存泄漏斜率
$svcSlope MB/h
WPF: $wpfSlope MB/h (Safe < 2 / 5)
🎯 核心六零红线判定 (The 6 Zeros Assertion Matrix)
| 验收规则项 |
标准期望 |
实测数值 |
结果判定 |
$($checkRows.ToString())
📋 最近事件与混沌注入时间线 (Recent 50 Events)
| 时间戳 (UTC) |
级别 |
事件详情与自愈动作 |
$($timelineRows.ToString())
"@
[System.IO.File]::WriteAllText($OutputHtmlPath, $html, [System.Text.Encoding]::UTF8)
Write-Host "[New-StabilityReport] Generated HTML report at: $OutputHtmlPath" -ForegroundColor Green
return $OutputHtmlPath