| | | 1 | | <# |
| | | 2 | | .SYNOPSIS |
| | | 3 | | Creates a new Kestrun health ProbeResult object. |
| | | 4 | | .DESCRIPTION |
| | | 5 | | Simplifies constructing a [Kestrun.Health.ProbeResult] from PowerShell without using the |
| | | 6 | | raw static ::new() syntax. Accepts status, description, and an optional hashtable of data |
| | | 7 | | which is converted to a strongly typed Dictionary[string, object]. Returns the created |
| | | 8 | | ProbeResult for piping back to Add-KrHealthProbe script blocks or custom logic. |
| | | 9 | | .PARAMETER Status |
| | | 10 | | Health status. Accepts Healthy, Degraded, or Unhealthy (case-insensitive). |
| | | 11 | | .PARAMETER Description |
| | | 12 | | Short human readable description for diagnostics. |
| | | 13 | | .PARAMETER Data |
| | | 14 | | Optional hashtable of additional metrics/values (serialized into response JSON). |
| | | 15 | | .EXAMPLE |
| | | 16 | | New-KrProbeResult -Status Healthy -Description 'Cache OK' |
| | | 17 | | .EXAMPLE |
| | | 18 | | New-KrProbeResult Degraded 'Latency high' -Data @{ p95 = 180; threshold = 150 } |
| | | 19 | | .NOTES |
| | | 20 | | Intended for use inside -ScriptBlock probes: `return New-KrProbeResult Healthy 'Ready'`. |
| | | 21 | | #> |
| | | 22 | | function New-KrProbeResult { |
| | | 23 | | [Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseShouldProcessForStateChangingFunctions', '')] |
| | | 24 | | [KestrunRuntimeApi('Runtime')] |
| | | 25 | | [CmdletBinding(PositionalBinding = $true)] |
| | | 26 | | [OutputType([Kestrun.Health.ProbeResult])] |
| | | 27 | | param( |
| | | 28 | | [Parameter(Mandatory, Position = 0)] |
| | | 29 | | [ValidateSet('Healthy', 'Degraded', 'Unhealthy')] |
| | | 30 | | [string]$Status, |
| | | 31 | | |
| | | 32 | | [Parameter(Mandatory, Position = 1)] |
| | | 33 | | [string]$Description, |
| | | 34 | | |
| | | 35 | | [Parameter(Position = 2)] |
| | | 36 | | [hashtable]$Data |
| | | 37 | | ) |
| | | 38 | | |
| | | 39 | | # Map string to enum |
| | 0 | 40 | | $enumStatus = [Kestrun.Health.ProbeStatus]::$Status |
| | | 41 | | |
| | 0 | 42 | | $dict = $null |
| | 0 | 43 | | if ($PSBoundParameters.ContainsKey('Data') -and $Data) { |
| | | 44 | | |
| | | 45 | | |
| | 0 | 46 | | $dict = [System.Collections.Generic.Dictionary[string, object]]::new() |
| | 0 | 47 | | foreach ($k in $Data.Keys) { |
| | 0 | 48 | | if ([string]::IsNullOrWhiteSpace([string]$k)) { continue } |
| | 0 | 49 | | $nv = _NormalizeValueToDictionary -Value $Data[$k] -Depth 0 |
| | 0 | 50 | | if ($null -ne $nv) { $dict[$k] = $nv } |
| | | 51 | | } |
| | | 52 | | } |
| | | 53 | | |
| | | 54 | | # Create and return the ProbeResult |
| | 0 | 55 | | return [Kestrun.Health.ProbeResult]::new($enumStatus, $Description, $dict) |
| | | 56 | | } |