Refactor for readability
This commit is contained in:
1 parent
ff9860023b
commit
17baba1490
11 files changed
+536
-405
No files matched your search
@@ -0,0 +1,126 @@
|
||||
package chart
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"html/template"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"powerwatts/internal/store"
|
||||
)
|
||||
|
||||
func FormatGermanNumber(n int) string {
|
||||
s := fmt.Sprintf("%d", n)
|
||||
if len(s) <= 3 {
|
||||
return s
|
||||
}
|
||||
|
||||
neg := s[0] == '-'
|
||||
if neg {
|
||||
s = s[1:]
|
||||
}
|
||||
|
||||
var b strings.Builder
|
||||
if neg {
|
||||
b.WriteByte('-')
|
||||
}
|
||||
rem := len(s) % 3
|
||||
if rem == 0 {
|
||||
rem = 3
|
||||
}
|
||||
b.WriteString(s[:rem])
|
||||
for i := rem; i < len(s); i += 3 {
|
||||
b.WriteByte('.')
|
||||
b.WriteString(s[i : i+3])
|
||||
}
|
||||
return b.String()
|
||||
}
|
||||
|
||||
func BuildHistoryChart(history []store.Sample, maxWatts int) template.HTML {
|
||||
const (
|
||||
width = 640
|
||||
height = 220
|
||||
padL = 72
|
||||
padR = 18
|
||||
padT = 18
|
||||
padB = 42
|
||||
)
|
||||
|
||||
if len(history) == 0 {
|
||||
return template.HTML(`<svg viewBox="0 0 640 220" class="chart-svg" aria-label="Power history chart" role="img"><rect x="0" y="0" width="640" height="220" rx="16" fill="#0b1220" stroke="#1e293b"/><text x="320" y="112" text-anchor="middle" fill="#64748b" font-size="16" font-family="system-ui, sans-serif">No power history yet</text></svg>`)
|
||||
}
|
||||
|
||||
minV, maxV := 0, history[0].Value
|
||||
if maxWatts > 0 {
|
||||
maxV = maxWatts
|
||||
}
|
||||
start := history[0].At
|
||||
end := history[len(history)-1].At
|
||||
for _, point := range history[1:] {
|
||||
if maxWatts <= 0 && point.Value > maxV {
|
||||
maxV = point.Value
|
||||
}
|
||||
if point.At.Before(start) {
|
||||
start = point.At
|
||||
}
|
||||
if point.At.After(end) {
|
||||
end = point.At
|
||||
}
|
||||
}
|
||||
if maxV <= 0 {
|
||||
maxV = 1
|
||||
}
|
||||
span := end.Sub(start)
|
||||
if span <= 0 {
|
||||
span = time.Minute
|
||||
}
|
||||
|
||||
plotW := float64(width - padL - padR)
|
||||
plotH := float64(height - padT - padB)
|
||||
points := make([]string, 0, len(history))
|
||||
var lastX, lastY float64
|
||||
for _, point := range history {
|
||||
x := float64(padL) + plotW*point.At.Sub(start).Seconds()/span.Seconds()
|
||||
y := float64(height-padB) - plotH*(float64(point.Value-minV)/float64(maxV-minV))
|
||||
if y < float64(padT) {
|
||||
y = float64(padT)
|
||||
}
|
||||
if y > float64(height-padB) {
|
||||
y = float64(height - padB)
|
||||
}
|
||||
points = append(points, fmt.Sprintf("%.1f,%.1f", x, y))
|
||||
lastX, lastY = x, y
|
||||
}
|
||||
|
||||
midV := minV + (maxV-minV)/2
|
||||
midT := start.Add(span / 2)
|
||||
x0 := float64(padL)
|
||||
x1 := float64(width - padR)
|
||||
yTop := float64(padT)
|
||||
yMid := float64(padT) + plotH/2
|
||||
yBottom := float64(height - padB)
|
||||
|
||||
var b strings.Builder
|
||||
b.WriteString(`<svg viewBox="0 0 640 220" class="chart-svg" aria-label="Power history chart" role="img">`)
|
||||
b.WriteString(`<rect x="0" y="0" width="640" height="220" rx="16" fill="#0b1220" stroke="#1e293b"/>`)
|
||||
b.WriteString(`<g stroke="#1e293b" stroke-width="1">`)
|
||||
fmt.Fprintf(&b, `<path d="M %.1f %.1f H %.1f"/>`, x0, yTop, x1)
|
||||
fmt.Fprintf(&b, `<path d="M %.1f %.1f H %.1f"/>`, x0, yMid, x1)
|
||||
fmt.Fprintf(&b, `<path d="M %.1f %.1f H %.1f"/>`, x0, yBottom, x1)
|
||||
fmt.Fprintf(&b, `<path d="M %.1f %.1f V %.1f"/>`, x0, yTop, yBottom)
|
||||
fmt.Fprintf(&b, `<path d="M %.1f %.1f V %.1f"/>`, x0+(plotW/2), yTop, yBottom)
|
||||
fmt.Fprintf(&b, `<path d="M %.1f %.1f V %.1f"/>`, x1, yTop, yBottom)
|
||||
b.WriteString(`</g>`)
|
||||
b.WriteString(`<g fill="#64748b" font-size="12" font-family="system-ui, sans-serif">`)
|
||||
fmt.Fprintf(&b, `<text x="%.1f" y="%d" text-anchor="end">%s W</text>`, x0-10, int(yTop)+14, FormatGermanNumber(maxV))
|
||||
fmt.Fprintf(&b, `<text x="%.1f" y="%d" text-anchor="end">%s W</text>`, x0-10, int(yMid)+14, FormatGermanNumber(midV))
|
||||
fmt.Fprintf(&b, `<text x="%.1f" y="%d" text-anchor="end">0 W</text>`, x0-10, int(yBottom)+14)
|
||||
fmt.Fprintf(&b, `<text x="%.1f" y="%d" text-anchor="middle">%s</text>`, x0, int(height)-10, start.Format("15:04"))
|
||||
fmt.Fprintf(&b, `<text x="%.1f" y="%d" text-anchor="middle">%s</text>`, x0+(plotW/2), int(height)-10, midT.Format("15:04"))
|
||||
fmt.Fprintf(&b, `<text x="%.1f" y="%d" text-anchor="middle">%s</text>`, x1, int(height)-10, end.Format("15:04"))
|
||||
b.WriteString(`</g>`)
|
||||
fmt.Fprintf(&b, `<polyline fill="none" stroke="#38bdf8" stroke-width="3" stroke-linejoin="round" stroke-linecap="round" points="%s"/>`, strings.Join(points, " "))
|
||||
fmt.Fprintf(&b, `<circle cx="%.1f" cy="%.1f" r="4" fill="#22c55e"/>`, lastX, lastY)
|
||||
b.WriteString(`</svg>`)
|
||||
return template.HTML(b.String())
|
||||
}
|
||||
@@ -0,0 +1,65 @@
|
||||
package chart
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"powerwatts/internal/store"
|
||||
)
|
||||
|
||||
func TestFormatGermanNumber(t *testing.T) {
|
||||
tests := map[int]string{
|
||||
0: "0",
|
||||
12: "12",
|
||||
999: "999",
|
||||
1000: "1.000",
|
||||
10000: "10.000",
|
||||
1234567: "1.234.567",
|
||||
-1000: "-1.000",
|
||||
}
|
||||
for input, want := range tests {
|
||||
if got := FormatGermanNumber(input); got != want {
|
||||
t.Fatalf("FormatGermanNumber(%d) = %q, want %q", input, got, want)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestBuildHistoryChartEmpty(t *testing.T) {
|
||||
chart := BuildHistoryChart(nil, 0)
|
||||
if !strings.Contains(string(chart), "No power history yet") {
|
||||
t.Fatalf("empty chart did not contain fallback text: %s", string(chart))
|
||||
}
|
||||
}
|
||||
|
||||
func TestBuildHistoryChartRendersPolyline(t *testing.T) {
|
||||
history := []store.Sample{
|
||||
{At: time.Date(2026, 4, 20, 0, 0, 0, 0, time.UTC), Value: 100},
|
||||
{At: time.Date(2026, 4, 20, 6, 0, 0, 0, time.UTC), Value: 200},
|
||||
{At: time.Date(2026, 4, 20, 12, 0, 0, 0, time.UTC), Value: 150},
|
||||
}
|
||||
|
||||
chartHTML := BuildHistoryChart(history, 0)
|
||||
if !strings.Contains(string(chartHTML), "<polyline") {
|
||||
t.Fatalf("chart did not contain polyline: %s", string(chartHTML))
|
||||
}
|
||||
for _, want := range []string{"W", "00:00", "06:00", "12:00"} {
|
||||
if !strings.Contains(string(chartHTML), want) {
|
||||
t.Fatalf("chart did not include %q: %s", want, string(chartHTML))
|
||||
}
|
||||
}
|
||||
if !strings.Contains(string(chartHTML), "0 W") {
|
||||
t.Fatalf("chart did not include zero baseline: %s", string(chartHTML))
|
||||
}
|
||||
}
|
||||
|
||||
func TestBuildHistoryChartUsesConfiguredMax(t *testing.T) {
|
||||
history := []store.Sample{{At: time.Date(2026, 4, 20, 12, 0, 0, 0, time.UTC), Value: 250}}
|
||||
chartHTML := BuildHistoryChart(history, 1000)
|
||||
if !strings.Contains(string(chartHTML), "1.000 W") {
|
||||
t.Fatalf("chart did not include configured max: %s", string(chartHTML))
|
||||
}
|
||||
if strings.Contains(string(chartHTML), "250 W") {
|
||||
t.Fatalf("chart unexpectedly used sample max instead of configured max: %s", string(chartHTML))
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user