package main import ( "fmt" "html/template" "strings" pb "git.ipng.ch/ipng/nginx-logtail/proto/logtailpb" ) const ( svgW = 300.0 svgH = 60.0 svgPad = 4.0 ) // renderSparkline converts trend points into an inline SVG polyline. // Returns "" if there are fewer than 2 points or all counts are zero. func renderSparkline(points []*pb.TrendPoint) template.HTML { if len(points) < 2 { return "" } var maxCount int64 for _, p := range points { if p.Count > maxCount { maxCount = p.Count } } if maxCount == 0 { return "" } n := len(points) var pts strings.Builder for i, p := range points { x := svgPad + float64(i)*(svgW-2*svgPad)/float64(n-1) y := svgPad + (svgH-2*svgPad)*(1.0-float64(p.Count)/float64(maxCount)) if i > 0 { pts.WriteByte(' ') } fmt.Fprintf(&pts, "%.1f,%.1f", x, y) } return template.HTML(fmt.Sprintf( ``+ ``+ ``, int(svgW), int(svgH), int(svgW), int(svgH), pts.String(), )) }