ci-server-engineer/examples/CI-Eng-Sample/displays/add_trend_spares.ps1
2026-10-02 14:06:04 +10:00

85 lines
4.4 KiB
PowerShell

# Pad every trend (chart) in a display file to 50 visible pens so it also draws in the HTML
# display (web client). CI Server R1.03.00: the web trend always builds 50 data columns but only
# labels the visible pens with an item; with fewer than 50 such pens it draws nothing (ISS-041).
#
# Spare pens copy the first real pen's source, are scaled far outside the data (manual scale
# 1E30..2E30, so the line is clipped), hidden from the legend and get blank, unique legend texts
# (1, 2, 3 ... spaces) in the legend background colour, because the web legend ignores
# "Show in legend". Empty pens are removed; do not keep hidden real pens (they count against 50).
# Run it again after editing the trend in the Edit Module (it only adds what is missing).
#
# Usage: powershell -ExecutionPolicy Bypass -File add_trend_spares.ps1 -Path <display>.xml
param([Parameter(Mandatory = $true)][string]$Path)
$target = 50
$text = [IO.File]::ReadAllText($Path)
$nl = "`r`n"
if (-not $text.Contains($nl)) { $nl = "`n" }
function Set-Tag([string]$pen, [string]$tag, [string]$value) {
$rx = New-Object Text.RegularExpressions.Regex ("<$tag>[^<]*</$tag>|<$tag/>")
return $rx.Replace($pen, "<$tag>$value</$tag>", 1)
}
$panelRx = New-Object Text.RegularExpressions.Regex '(?s)<chartPanel>.*?</chartPanel>'
$penRx = New-Object Text.RegularExpressions.Regex '(?s)[ \t]*<pen type="pen">.*?</pen>\r?\n?'
$changed = 0
$text = $panelRx.Replace($text, {
param($m)
$panel = $m.Value
$pens = $penRx.Matches($panel)
if ($pens.Count -eq 0) { return $panel }
$real = @(); $spareCount = 0
foreach ($p in $pens) {
$v = $p.Value
$empty = $v.Contains('uri=""')
$isSpare = $v.Contains('<minimum>1.0E30</minimum>')
if ($isSpare) { $spareCount++ }
elseif (-not $empty) { $real += $v }
}
if ($real.Count -eq 0) { return $panel }
$visibleReal = 0
foreach ($v in $real) { if ($v.Contains('<visible>true</visible>')) { $visibleReal++ } }
$need = $target - $visibleReal
if ($need -lt 0) { $need = 0 }
$template = $real[0]
# spare colour = legend background, so their (blank) web legend entries do not show
$bg = (New-Object Text.RegularExpressions.Regex '<legendBackgroundColor type="colorSet" r="\d+" g="\d+" b="\d+"/>').Match($panel)
$spareColor = '<color type="colorSet" r="240" g="240" b="240"/>'
if ($bg.Success) { $spareColor = $bg.Value.Replace('<legendBackgroundColor ', '<color ') }
$spares = @()
for ($i = 1; $i -le $need; $i++) {
$s = $template
$s = Set-Tag $s 'legendText' (' ' * $i)
$s = Set-Tag $s 'description' ''
$s = Set-Tag $s 'type' '1'
$s = Set-Tag $s 'visible' 'true'
$s = Set-Tag $s 'visibleLegend' 'false'
$s = Set-Tag $s 'axisVisible' 'false'
$s = Set-Tag $s 'scaleRangeType' '1'
$s = Set-Tag $s 'minimum' '1.0E30'
$s = Set-Tag $s 'maximum' '2.0E30'
$s = Set-Tag $s 'procentual' 'false'
$s = Set-Tag $s 'annotationVisible' 'false'
foreach ($g in @('majorGridVisible', 'minorGridVisible', 'lowHighLimitGridVisible', 'lowHighLimitGridAreaVisible', 'lowLowHighHighLimitGridVisible')) { $s = Set-Tag $s $g 'false' }
$s = (New-Object Text.RegularExpressions.Regex '<color type="colorSet" r="\d+" g="\d+" b="\d+"/>').Replace($s, $spareColor, 1)
$s = (New-Object Text.RegularExpressions.Regex '<stroke type="stroke" width="[^"]*"').Replace($s, '<stroke type="stroke" width="0.0"', 1)
$s = (New-Object Text.RegularExpressions.Regex '<paint type="paint">.*?</paint>(?=\s*<visible>)', 'Singleline').Replace($s, '<paint>@NULL</paint>', 1)
$spares += $s
}
# rebuild: real pens first, then spares, at the place of the first pen
$first = $pens[0].Index
$last = $pens[$pens.Count - 1]
$before = $panel.Substring(0, $first)
$after = $panel.Substring($last.Index + $last.Length)
$body = ''
foreach ($v in $real) { $body += $v }
foreach ($v in $spares) { $body += $v }
$panel = $before + $body + $after
$panel = (New-Object Text.RegularExpressions.Regex '<maximumPens>\d+</maximumPens>').Replace($panel, "<maximumPens>$target</maximumPens>", 1)
$script:changed++
Write-Host ("Trend {0}: {1} real pen(s), {2} old spare(s) replaced, {3} spare(s) written" -f $script:changed, $real.Count, $spareCount, $need)
return $panel
})
[IO.File]::WriteAllText($Path, $text)
Write-Host "Done: $changed trend(s) in $Path"