-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCharts.ps1
More file actions
244 lines (234 loc) · 16.9 KB
/
Copy pathCharts.ps1
File metadata and controls
244 lines (234 loc) · 16.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
# Charts.ps1 — email-safe plugin visualisations, layered ON TOP of the table/list.
#
# A plugin opts in by setting $Chart (a hashtable) alongside its usual $Display. The engine
# (vCheck.ps1) calls Get-HTMLChart with the raw row objects and prepends the result above the
# rendered table/list, so the chart never replaces the informative detail — it summarises it.
#
# Colours/fonts come from the ACTIVE THEME TOKENS ($cCrit/$cWarn/$cOk/$cAccent/$cSurface/...),
# read from the caller scope exactly like the _common.ps1 renderers — so all 8 themes inherit
# every chart with zero per-theme code.
#
# Donut/Pie are rasterised to a PNG entirely in PowerShell 7 — no System.Drawing (Windows-only),
# no native libraries, no NuGet. Just the BCL (System.IO.Compression for DEFLATE) plus a
# hand-rolled CRC32/Adler32. The PNG is embedded via the existing Add-ReportResource (cid),
# so it renders in every mail client AND the standalone .htm. See CHARTS.md for the contract.
#region ---------------------------------------------------------------- PNG encoder (pure BCL)
$script:CrcTable = New-Object 'uint32[]' 256
$script:CrcPoly = [uint32]0xEDB88320L
for ($n = 0; $n -lt 256; $n++) {
$c = [uint32]$n
for ($k = 0; $k -lt 8; $k++) {
if ($c -band 1) { $c = [uint32]($script:CrcPoly -bxor ([uint32]($c -shr 1))) } else { $c = [uint32]($c -shr 1) }
}
$script:CrcTable[$n] = $c
}
function Get-Crc32([byte[]]$bytes) {
$c = [uint32]0xFFFFFFFFL
foreach ($b in $bytes) { $c = [uint32]($script:CrcTable[[int](($c -bxor $b) -band 0xFF)] -bxor [uint32]($c -shr 8)) }
return [uint32]($c -bxor 0xFFFFFFFFL)
}
function Get-Adler32([byte[]]$data) {
$a = [uint32]1; $b = [uint32]0
foreach ($byte in $data) { $a = ($a + $byte) % 65521; $b = ($b + $a) % 65521 }
return [uint32](([uint32]($b -shl 16)) -bor $a)
}
function ConvertTo-BE([uint32]$v) { , ([byte[]]@([byte](($v -shr 24) -band 0xFF), [byte](($v -shr 16) -band 0xFF), [byte](($v -shr 8) -band 0xFF), [byte]($v -band 0xFF))) }
function New-PngChunk([string]$type, [byte[]]$data) {
$t = [System.Text.Encoding]::ASCII.GetBytes($type)
return (ConvertTo-BE ([uint32]$data.Length)) + $t + $data + (ConvertTo-BE (Get-Crc32 ($t + $data)))
}
function ConvertTo-Png([byte[]]$rgb, [int]$w, [int]$h) {
$stride = $w * 3
$raw = New-Object byte[] ($h * (1 + $stride))
for ($y = 0; $y -lt $h; $y++) { $raw[$y * (1 + $stride)] = 0; [Array]::Copy($rgb, $y * $stride, $raw, $y * (1 + $stride) + 1, $stride) }
$ms = New-Object System.IO.MemoryStream
$ds = New-Object System.IO.Compression.DeflateStream($ms, [System.IO.Compression.CompressionLevel]::Optimal, $true)
$ds.Write($raw, 0, $raw.Length); $ds.Dispose()
$zlib = ([byte[]]@(0x78, 0x9C)) + $ms.ToArray() + (ConvertTo-BE (Get-Adler32 $raw))
$ihdr = (ConvertTo-BE ([uint32]$w)) + (ConvertTo-BE ([uint32]$h)) + ([byte[]]@(8, 2, 0, 0, 0))
return ([byte[]]@(137, 80, 78, 71, 13, 10, 26, 10)) + (New-PngChunk 'IHDR' $ihdr) + (New-PngChunk 'IDAT' $zlib) + (New-PngChunk 'IEND' (New-Object byte[] 0))
}
#endregion
#region ---------------------------------------------------------------- donut/pie rasteriser
function ConvertFrom-Hex([string]$hex) { $hex = $hex.TrimStart('#'); return @([Convert]::ToInt32($hex.Substring(0, 2), 16), [Convert]::ToInt32($hex.Substring(2, 2), 16), [Convert]::ToInt32($hex.Substring(4, 2), 16)) }
$script:ChartFont = @{
'0' = @('01110', '10001', '10011', '10101', '11001', '10001', '01110'); '1' = @('00100', '01100', '00100', '00100', '00100', '00100', '01110')
'2' = @('01110', '10001', '00001', '00010', '00100', '01000', '11111'); '3' = @('11111', '00010', '00100', '00010', '00001', '10001', '01110')
'4' = @('00010', '00110', '01010', '10010', '11111', '00010', '00010'); '5' = @('11111', '10000', '11110', '00001', '00001', '10001', '01110')
'6' = @('00110', '01000', '10000', '11110', '10001', '10001', '01110'); '7' = @('11111', '00001', '00010', '00100', '01000', '01000', '01000')
'8' = @('01110', '10001', '10001', '01110', '10001', '10001', '01110'); '9' = @('01110', '10001', '10001', '01111', '00001', '00010', '01100')
}
# Render at 3x then box-downsample → anti-aliases the ring, segment seams AND the centre text.
function New-DonutPng {
param([array]$Segments, [int]$Size = 160, [string]$BgHex = '#161B22', [double]$HoleRatio = 0.56, [string]$InkHex = '#E5EDF5', [string]$CenterText = '')
$F = 3; $big = $Size * $F
$bg = ConvertFrom-Hex $BgHex; $ink = ConvertFrom-Hex $InkHex
$segCols = @($Segments | ForEach-Object { , (ConvertFrom-Hex $_.color) })
$total = ($Segments | Measure-Object -Property value -Sum).Sum; if ($total -le 0) { $total = 1 }
$cum = @(); $acc = 0.0; foreach ($s in $Segments) { $acc += $s.value / $total; $cum += $acc }
$cx = $big / 2.0; $cy = $big / 2.0; $outer = $big / 2.0 - ($F * 2); $inner = $outer * $HoleRatio
$o2 = $outer * $outer; $i2 = $inner * $inner; $twoPi = [math]::PI * 2
$bigRGB = New-Object byte[] ($big * $big * 3)
for ($y = 0; $y -lt $big; $y++) {
for ($x = 0; $x -lt $big; $x++) {
$fx = $x + 0.5 - $cx; $fy = $y + 0.5 - $cy; $r2 = $fx * $fx + $fy * $fy; $o = ($y * $big + $x) * 3
if ($r2 -le $o2 -and $r2 -ge $i2) {
$ang = [math]::Atan2($fx, - $fy); if ($ang -lt 0) { $ang += $twoPi }; $t = $ang / $twoPi
$idx = 0; for ($g = 0; $g -lt $cum.Count; $g++) { if ($t -le $cum[$g]) { $idx = $g; break } }
$col = $segCols[$idx]; $bigRGB[$o] = $col[0]; $bigRGB[$o + 1] = $col[1]; $bigRGB[$o + 2] = $col[2]
}
else { $bigRGB[$o] = $bg[0]; $bigRGB[$o + 1] = $bg[1]; $bigRGB[$o + 2] = $bg[2] }
}
}
if ($CenterText) {
$scale = [math]::Max($F, [int]([math]::Floor($inner * 1.15 / 7)))
$gw = 5 * $scale; $gap = $scale; $txtW = $CenterText.Length * $gw + ($CenterText.Length - 1) * $gap; $txtH = 7 * $scale
$sX = [int]($cx - $txtW / 2); $sY = [int]($cy - $txtH / 2)
for ($ci = 0; $ci -lt $CenterText.Length; $ci++) {
$glyph = $script:ChartFont["$($CenterText[$ci])"]; if (-not $glyph) { continue }
$gx0 = $sX + $ci * ($gw + $gap)
for ($ry = 0; $ry -lt 7; $ry++) {
for ($rx = 0; $rx -lt 5; $rx++) {
if ($glyph[$ry][$rx] -eq '1') {
for ($dy = 0; $dy -lt $scale; $dy++) {
for ($dx = 0; $dx -lt $scale; $dx++) {
$X = $gx0 + $rx * $scale + $dx; $Y = $sY + $ry * $scale + $dy
if ($X -ge 0 -and $X -lt $big -and $Y -ge 0 -and $Y -lt $big) { $o = ($Y * $big + $X) * 3; $bigRGB[$o] = $ink[0]; $bigRGB[$o + 1] = $ink[1]; $bigRGB[$o + 2] = $ink[2] }
}
}
}
}
}
}
}
$rgb = New-Object byte[] ($Size * $Size * 3); $d = $F * $F
for ($y = 0; $y -lt $Size; $y++) {
for ($x = 0; $x -lt $Size; $x++) {
$ar = 0; $ag = 0; $ab = 0
for ($dy = 0; $dy -lt $F; $dy++) { for ($dx = 0; $dx -lt $F; $dx++) { $o = ((($y * $F + $dy) * $big) + ($x * $F + $dx)) * 3; $ar += $bigRGB[$o]; $ag += $bigRGB[$o + 1]; $ab += $bigRGB[$o + 2] } }
$oo = ($y * $Size + $x) * 3; $rgb[$oo] = [byte]($ar / $d); $rgb[$oo + 1] = [byte]($ag / $d); $rgb[$oo + 2] = [byte]($ab / $d)
}
}
return (ConvertTo-Png $rgb $Size $Size)
}
#endregion
#region ---------------------------------------------------------------- colour helpers (theme tokens)
# Map a category label to a severity token by keyword; $null if it isn't a severity word.
function Get-ChartColour([string]$label) {
$l = ([string]$label).ToLower()
if ($l -match 'crit|fail|error|non.?compliant|noncompliant|down|unhealthy|expired|disconnect|disabled|insecure|missing|dead|out.?of|incompatible|unavailable|affected|exposed|vulnerable|\bred\b') { return $cCrit }
if ($l -match 'warn|degrad|aging|old|stale|pending|partial|nearly|approach|\byellow\b') { return $cWarn }
if ($l -match 'ok|compliant|healthy|pass|connected|enabled|success|online|good|secure|present|mitigated|patched|protected|\bgreen\b') { return $cOk }
return $null
}
# Threshold colour for a magnitude (higher = worse, unless $Invert).
function Get-ChartThresholdColour([double]$v, $warn, $crit, [bool]$invert) {
if ($null -eq $warn -and $null -eq $crit) { return $cAccent }
if ($invert) {
if ($null -ne $crit -and $v -le $crit) { return $cCrit }
if ($null -ne $warn -and $v -le $warn) { return $cWarn }
return $cOk
}
if ($null -ne $crit -and $v -ge $crit) { return $cCrit }
if ($null -ne $warn -and $v -ge $warn) { return $cWarn }
return $cOk
}
#endregion
#region ---------------------------------------------------------------- HTML chart builders
function Get-ChartDonut($Rows, $Config, [string]$Cid, [bool]$Pie) {
$grp = [string]$Config.Group
$segs = @()
if ($grp) {
$palette = @($cAccent, $cInfoNum, $cAccentLt); $pi = 0
foreach ($g in (@($Rows | Group-Object -Property $grp) | Sort-Object Count -Descending)) {
$col = Get-ChartColour $g.Name; if (-not $col) { $col = $palette[$pi % $palette.Count]; $pi++ }
$segs += @{ label = [string]$g.Name; value = $g.Count; color = $col }
}
}
elseif ($Config.Segments) { $segs = $Config.Segments }
if (-not $segs.Count) { return '' }
$total = ($segs | Measure-Object -Property value -Sum).Sum
$png = New-DonutPng -Segments $segs -Size 160 -BgHex $cSurface -HoleRatio ($Pie ? 0.0 : 0.56) -InkHex $cInk -CenterText ($Pie ? '' : [string]$total)
Add-ReportResource -cid $Cid -Type "Base64" -ResourceData ("png|" + [Convert]::ToBase64String($png)) -Used $true
$leg = ''
foreach ($s in $segs) { $leg += "<tr><td style='padding:3px 0;'><span style='display:inline-block;width:11px;height:11px;border-radius:3px;background-color:$($s.color);vertical-align:middle;margin-right:9px;'> </span><span style='font-family:$fBody;font-size:12px;color:$cText;'>$($s.label)</span></td><td align='right' style='font-family:$fMono;font-size:12px;font-weight:600;color:$cText;padding:3px 0 3px 20px;'>$($s.value)</td></tr>" }
return "<div style='margin:0 0 16px 0;'><table role='presentation' cellpadding='0' cellspacing='0' border='0'><tr><td style='vertical-align:middle;padding-right:24px;'><img src='cid:$Cid' width='160' height='160' style='display:block;border:0;' alt='chart' /></td><td style='vertical-align:middle;'><table role='presentation' cellpadding='0' cellspacing='0' border='0'>$leg</table></td></tr></table></div>"
}
function Get-ChartBar($Rows, $Config) {
$lblP = [string]$Config.Label; $valP = [string]$Config.Value
$max = $Config.Max; if (-not $max) { $max = 100 }
$top = $Config.Top; if (-not $top) { $top = 12 }
$items = @($Rows | Sort-Object -Property $valP -Descending | Select-Object -First $top)
$rows = ''
foreach ($it in $items) {
$v = [double]$it.$valP; $pct = [math]::Min(100, [math]::Round($v / $max * 100)); $rem = 100 - $pct
$col = Get-ChartThresholdColour $v $Config.Warn $Config.Crit ([bool]$Config.Invert)
$rows += "<tr><td width='170' style='font-family:$fBody;font-size:12px;color:$cText;padding:4px 10px 4px 0;white-space:nowrap;'>$($it.$lblP)</td><td style='padding:4px 0;'><table role='presentation' width='100%' cellpadding='0' cellspacing='0' border='0' bgcolor='$cSurfaceAlt' style='background-color:$cSurfaceAlt;border-radius:4px;'><tr><td width='$pct%' bgcolor='$col' style='background-color:$col;height:14px;line-height:14px;font-size:0;border-radius:4px;'> </td><td width='$rem%' style='font-size:0;line-height:14px;'> </td></tr></table></td><td width='58' align='right' style='font-family:$fMono;font-size:12px;font-weight:600;color:$col;padding:4px 0 4px 10px;font-variant-numeric:tabular-nums;'>$($it.$valP)</td></tr>"
}
return "<div style='margin:0 0 16px 0;'><table role='presentation' width='100%' cellpadding='0' cellspacing='0' border='0'>$rows</table></div>"
}
function Get-ChartStacked($Rows, $Config) {
$c = 0; $w = 0; $o = 0
if ($Config.Group) {
foreach ($r in $Rows) { $col = Get-ChartColour $r.($Config.Group); if ($col -eq $cCrit) { $c++ } elseif ($col -eq $cWarn) { $w++ } else { $o++ } }
}
else { $c = [int]$Config.Critical; $w = [int]$Config.Warning; $o = [int]$Config.OK }
$t = $c + $w + $o; if ($t -eq 0) { $t = 1 }
$cp = [math]::Round($c / $t * 100); $wp = [math]::Round($w / $t * 100); $op = 100 - $cp - $wp
$seg = ''
if ($cp) { $seg += "<td width='$cp%' bgcolor='$cCrit' style='background-color:$cCrit;height:22px;line-height:22px;font-size:0;'> </td>" }
if ($wp) { $seg += "<td width='$wp%' bgcolor='$cWarn' style='background-color:$cWarn;height:22px;line-height:22px;font-size:0;'> </td>" }
if ($op) { $seg += "<td width='$op%' bgcolor='$cOk' style='background-color:$cOk;height:22px;line-height:22px;font-size:0;'> </td>" }
$legend = "<table role='presentation' cellpadding='0' cellspacing='0' border='0' style='margin-top:10px;'><tr>" +
"<td style='padding-right:18px;font-family:$fBody;font-size:11px;color:$cTextMuted;'><span style='display:inline-block;width:9px;height:9px;background-color:$cCrit;border-radius:2px;margin-right:6px;'> </span>Critical $c</td>" +
"<td style='padding-right:18px;font-family:$fBody;font-size:11px;color:$cTextMuted;'><span style='display:inline-block;width:9px;height:9px;background-color:$cWarn;border-radius:2px;margin-right:6px;'> </span>Warning $w</td>" +
"<td style='font-family:$fBody;font-size:11px;color:$cTextMuted;'><span style='display:inline-block;width:9px;height:9px;background-color:$cOk;border-radius:2px;margin-right:6px;'> </span>OK $o</td></tr></table>"
return "<div style='margin:0 0 16px 0;'><table role='presentation' width='100%' cellpadding='0' cellspacing='0' border='0' style='border-radius:5px;overflow:hidden;'><tr>$seg</tr></table>$legend</div>"
}
function Get-ChartGauge($Rows, $Config) {
$lblP = [string]$Config.Label; $valP = [string]$Config.Value
$max = $Config.Max; if (-not $max) { $max = 100 }
$blocks = ''
foreach ($r in @($Rows)) {
$v = [double]$r.$valP; $pct = [math]::Min(100, [math]::Round($v / $max * 100)); $rem = 100 - $pct
$col = Get-ChartThresholdColour $v $Config.Warn $Config.Crit ([bool]$Config.Invert)
$blocks += "<div style='font-family:$fDisplay;font-size:13px;font-weight:600;letter-spacing:1.5px;text-transform:uppercase;color:$cInk;padding:0 0 7px 0;'>$($r.$lblP) <span style='font-family:$fMono;color:$col;float:right;'>$($r.$valP)</span></div><table role='presentation' width='100%' cellpadding='0' cellspacing='0' border='0' bgcolor='$cSurfaceAlt' style='background-color:$cSurfaceAlt;border-radius:5px;border:1px solid $cBorder;margin-bottom:14px;'><tr><td width='$pct%' bgcolor='$col' style='background-color:$col;height:18px;line-height:18px;font-size:0;border-radius:4px;'> </td><td width='$rem%' style='font-size:0;line-height:18px;'> </td></tr></table>"
}
return "<div style='margin:0 0 4px 0;'>$blocks</div>"
}
function Get-ChartHeat($Rows, $Config) {
$stP = [string]$Config.Status; $perRow = $Config.PerRow; if (-not $perRow) { $perRow = 24 }
$cells = ''; $n = 0
foreach ($r in @($Rows)) {
$col = $stP ? (Get-ChartColour $r.$stP) : $null; if (-not $col) { $col = $cOk }
$cells += "<td style='padding:2px;'><div style='width:18px;height:18px;background-color:$col;border-radius:3px;'> </div></td>"
$n++; if ($n % $perRow -eq 0) { $cells += "</tr><tr>" }
}
return "<div style='margin:0 0 16px 0;'><table role='presentation' cellpadding='0' cellspacing='0' border='0'><tr>$cells</tr></table></div>"
}
function Get-ChartSparkline($Rows, $Config) {
$valP = [string]$Config.Value
$vals = @($Rows | ForEach-Object { [double]$_.$valP })
$maxV = ($vals | Measure-Object -Maximum).Maximum; if ($maxV -le 0) { $maxV = 1 }
$tds = ''
foreach ($v in $vals) { $h = [math]::Max(3, [math]::Round($v / $maxV * 44)); $tds += "<td valign='bottom' style='padding:0 2px;'><table role='presentation' cellpadding='0' cellspacing='0' border='0'><tr><td width='10' bgcolor='$cAccent' style='background-color:$cAccent;height:${h}px;line-height:${h}px;font-size:0;border-radius:2px 2px 0 0;'> </td></tr></table></td>" }
return "<div style='margin:0 0 16px 0;'><table role='presentation' cellpadding='0' cellspacing='0' border='0' style='height:48px;'><tr valign='bottom'>$tds</tr></table></div>"
}
#endregion
# Dispatcher — called by the engine. $Cid must be unique per chart (for the PNG resource).
function Get-HTMLChart {
param($Rows, $Config, [string]$Cid)
if (-not $Rows -or -not $Config) { return '' }
try {
switch -Regex ([string]$Config.Type) {
'^(Donut|Pie)$' { return (Get-ChartDonut $Rows $Config $Cid ([string]$Config.Type -eq 'Pie')) }
'^Bar$' { return (Get-ChartBar $Rows $Config) }
'^Stacked$' { return (Get-ChartStacked $Rows $Config) }
'^Gauge$' { return (Get-ChartGauge $Rows $Config) }
'^Heat$' { return (Get-ChartHeat $Rows $Config) }
'^Spark(line)?$' { return (Get-ChartSparkline $Rows $Config) }
default { return '' }
}
}
catch { Write-Warning ("Chart '{0}' failed: {1}" -f $Config.Type, $_.Exception.Message); return '' }
}