Files
urutau/internal/server/report_html_svg.go
T
Lucas RayanandClaude Opus 5.5 432f521ed1
Release / Publish tags (push) Successful in 18s
Release / Plan (reuse the published image?) (push) Successful in 5s
Release / ci (push) Successful in 1m47s
Release / Build image (amd64) (push) Successful in 21s
Release / Build image (arm64) (push) Successful in 28s
CI / Format and vet (push) Successful in 23s
CI / Tests (Linux, race detector) (push) Successful in 1m24s
CI / Dependency audit (push) Successful in 24s
CI / Docker image (build and smoke test) (push) Skipped
Release / Gitea release (push) Successful in 5s
feat: 1.4.1 pagination, network probes, survival by blocker, report polish
Backend pagination with one pager for sessions, IPs, campaigns and events;
network probe source (port-scan, T1046, second firewall reason port-scan);
host kit logs inbound probes; urutau call on the report cover.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-15 00:21:43 -03:00

184 lines
6.1 KiB
Go

package server
import (
"fmt"
"html"
"math"
"strings"
"time"
)
// Palette of the printed report (light theme, Ciberfauna identity).
var reportPalette = []string{"#f97316", "#087f68", "#f0b35b", "#45d6b0", "#c2410c", "#4b6b61", "#9aa9a2"}
type chartItem struct {
Label string
N float64
}
func clip(s string, n int) string {
r := []rune(s)
if len(r) <= n {
return s
}
return string(r[:n-1]) + "…"
}
// niceMax rounds a maximum up to 1, 2, 5 x 10^k so the axis reads well.
func niceMax(v float64) float64 {
if v <= 0 {
return 1
}
p := math.Pow(10, math.Floor(math.Log10(v)))
for _, m := range []float64{1, 2, 5, 10} {
if v <= m*p {
return m * p
}
}
return 10 * p
}
// svgArea draws a filled time series. All text is escaped; numbers are formatted here.
func svgArea(vals []uint32, start, step int64, fi func(float64) string, title string) string {
const w, h, ml, mr, mt, mb = 680.0, 150.0, 40.0, 8.0, 8.0, 20.0
var sb strings.Builder
fmt.Fprintf(&sb, `<svg class="chart" viewBox="0 0 %g %g" role="img" aria-label="%s" preserveAspectRatio="xMidYMid meet">`, w, h, html.EscapeString(title))
var mx float64
for _, v := range vals {
if float64(v) > mx {
mx = float64(v)
}
}
top := niceMax(mx)
pw, ph := w-ml-mr, h-mt-mb
for i := 0; i <= 4; i++ {
y := mt + ph - ph*float64(i)/4
fmt.Fprintf(&sb, `<line x1="%g" y1="%.1f" x2="%g" y2="%.1f" stroke="#d7e2dd" stroke-width="0.6"/>`, ml, y, w-mr, y)
fmt.Fprintf(&sb, `<text x="%g" y="%.1f" font-size="9" text-anchor="end" fill="#55665f">%s</text>`, ml-4, y+3, fi(top*float64(i)/4))
}
n := len(vals)
if n > 1 {
xs := func(i int) float64 { return ml + pw*float64(i)/float64(n-1) }
ys := func(v uint32) float64 { return mt + ph - ph*float64(v)/top }
var line strings.Builder
for i, v := range vals {
if i == 0 {
fmt.Fprintf(&line, "M%.1f %.1f", xs(i), ys(v))
} else {
fmt.Fprintf(&line, "L%.1f %.1f", xs(i), ys(v))
}
}
fmt.Fprintf(&sb, `<path d="%sL%.1f %.1f L%.1f %.1f Z" fill="#f97316" fill-opacity="0.18"/>`, line.String(), xs(n-1), mt+ph, xs(0), mt+ph)
fmt.Fprintf(&sb, `<path d="%s" fill="none" stroke="#f97316" stroke-width="1.4" stroke-linejoin="round"/>`, line.String())
if step > 0 {
for i := 0; i < 5; i++ {
idx := (n - 1) * i / 4
ts := start + int64(idx)*step
lbl := time.Unix(ts, 0).UTC().Format("02/01")
if step < 86400 && int64(n)*step <= 3*86400 {
lbl = time.Unix(ts, 0).UTC().Format("02/01 15h")
}
anchor := "middle"
if i == 0 {
anchor = "start"
} else if i == 4 {
anchor = "end"
}
fmt.Fprintf(&sb, `<text x="%.1f" y="%g" font-size="9" text-anchor="%s" fill="#55665f">%s</text>`, xs(idx), h-6, anchor, lbl)
}
}
} else if n == 1 {
fmt.Fprintf(&sb, `<circle cx="%g" cy="%.1f" r="2" fill="#f97316"/>`, ml+pw/2, mt+ph-ph*float64(vals[0])/top)
}
sb.WriteString(`</svg>`)
return sb.String()
}
// svgBars draws horizontal bars with the label on the left and the value on the right.
func svgBars(items []chartItem, color string, fi func(float64) string, title string) string {
const w, lw, rw, rh = 300.0, 112.0, 40.0, 19.0
if len(items) == 0 {
return ""
}
var mx float64
for _, it := range items {
if it.N > mx {
mx = it.N
}
}
if mx <= 0 {
mx = 1
}
h := rh*float64(len(items)) + 2
var sb strings.Builder
fmt.Fprintf(&sb, `<svg class="chart" viewBox="0 0 %g %g" role="img" aria-label="%s">`, w, h, html.EscapeString(title))
bw := w - lw - rw
for i, it := range items {
y := rh * float64(i)
l := it.N / mx * bw
if it.N > 0 && l < 1.5 {
l = 1.5
}
fmt.Fprintf(&sb, `<text x="%g" y="%.1f" font-size="10" text-anchor="end" fill="#22352f">%s</text>`, lw-6, y+13, html.EscapeString(clip(it.Label, 20)))
fmt.Fprintf(&sb, `<rect x="%g" y="%.1f" width="%.1f" height="11" rx="2" fill="%s"/>`, lw, y+3.5, l, color)
fmt.Fprintf(&sb, `<text x="%.1f" y="%.1f" font-size="10" fill="#55665f">%s</text>`, lw+l+4, y+13, fi(it.N))
}
sb.WriteString(`</svg>`)
return sb.String()
}
// svgDonut draws a ring; the legend is HTML next to it (see donutLegend).
func svgDonut(items []chartItem, title string) string {
var total float64
for _, it := range items {
total += it.N
}
var sb strings.Builder
fmt.Fprintf(&sb, `<svg class="donut" viewBox="0 0 120 120" role="img" aria-label="%s">`, html.EscapeString(title))
const r = 44.0
circ := 2 * math.Pi * r
sb.WriteString(`<circle cx="60" cy="60" r="44" fill="none" stroke="#eaf2ef" stroke-width="20"/>`)
if total > 0 {
off := 0.0
for i, it := range items {
if it.N <= 0 {
continue
}
l := it.N / total * circ
fmt.Fprintf(&sb, `<circle cx="60" cy="60" r="44" fill="none" stroke="%s" stroke-width="20" stroke-dasharray="%.2f %.2f" stroke-dashoffset="%.2f" transform="rotate(-90 60 60)"/>`,
reportPalette[i%len(reportPalette)], l, circ-l, -off)
off += l
}
}
sb.WriteString(`</svg>`)
return sb.String()
}
// svgMotif is the cover decoration: a stylised waveform of the urutau's call,
// five whistled notes that descend in pitch and fade, the way the bird sings
// at night. It does not depend on the data, so it never turns into a flat comb.
func svgMotif(_ []uint32) string {
var sb strings.Builder
sb.WriteString(`<svg class="motif" viewBox="0 0 210 120" preserveAspectRatio="none" aria-hidden="true">`)
for i := 1; i <= 7; i++ {
fmt.Fprintf(&sb, `<line x1="0" y1="%d" x2="210" y2="%d" stroke="#45d6b0" stroke-opacity="0.10" stroke-width="0.25"/>`, i*15, i*15)
}
type note struct{ x0, w, peak, y float64 }
notes := []note{{14, 30, 46, 54}, {52, 28, 40, 57}, {88, 27, 34, 60}, {123, 25, 27, 63}, {156, 23, 20, 66}}
for _, n := range notes {
lines := int(n.w / 1.3)
for k := 0; k <= lines; k++ {
t := float64(k) / float64(lines)
env := math.Pow(math.Sin(math.Pi*t), 1.6) * (0.78 + 0.22*math.Sin(23*t+n.x0))
h := 1.2 + n.peak*env
x := n.x0 + n.w*t
// fade toward the page edges so the call sits behind the bird
fade := 1 - math.Min(1, math.Abs(x-105)/115)
fmt.Fprintf(&sb, `<line x1="%.1f" y1="%.1f" x2="%.1f" y2="%.1f" stroke="#f0b35b" stroke-opacity="%.2f" stroke-width="0.75" stroke-linecap="round"/>`,
x, n.y-h/2, x, n.y+h/2, 0.18+0.5*fade)
}
}
sb.WriteString(`</svg>`)
return sb.String()
}