Received: from out0.migadu.com (out0.migadu.com [94.23.1.103]) by mail-b.sr.ht (Postfix) with ESMTPS id E4BA5FF134 for <~sircmpwn/chartsrv-devel@lists.sr.ht>; Wed, 21 Oct 2020 23:41:13 +0000 (UTC) Authentication-Results: mail-b.sr.ht; dkim=fail reason="key not found in DNS" (0-bit key) header.d=pixelhero.dev header.i=@pixelhero.dev header.b=akG4oGAI X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=pixelhero.dev; s=key1; t=1603323672; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding; bh=t9eb5H8Fra3KVNwSA0sZMW7zx+ADE/n8KcHpz4APruU=; b=akG4oGAIYxuOQ2Qc2pkUgDKeSZZO1vNhnmlmegQLiYJfsMGc+AdXrkLrQoC1q+JeahDegZ VgXF+0vuqeOQXEsfg9e1PG+SCHsO7zNS0pk5EVnIM5S41rY3ecjuxH6JRQ8asdRON9Phyw 2Pjw5297jHeUWkBr89NEL6HsnZWruOIz+9wu7FFXSWPMrTi1lX6iym35n9LZVTvhHImAHP fg9XM/you47wRY4Q0YqSz1Zmfbvivi0t8HtOxKRQ1TQ+P3415o7ecPyu2pXv5Q3/voC8gg /37R7JG26Yh+igpVkmln3/GQK7UWQD9sZsvt2wPSAXDi0zx5EJp8rJ0uNQc5ig== From: Noam Preil To: ~sircmpwn/chartsrv-devel@lists.sr.ht Cc: Noam Preil Subject: [PATCH] Support label templating Date: Wed, 21 Oct 2020 23:36:58 +0000 Message-Id: <20201021233658.16705-1-noam@pixelhero.dev> MIME-Version: 1.0 X-Spam-Score: 0.00 Content-Transfer-Encoding: quoted-printable --- main.go | 32 +++++++++++++++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-) diff --git a/main.go b/main.go index 12c5491..69fcca1 100644 --- a/main.go +++ b/main.go @@ -1,6 +1,7 @@ package main =20 import ( + "bytes" "encoding/json" "fmt" "image/color" @@ -11,6 +12,7 @@ import ( "sort" "strconv" "strings" + "text/template" "time" =20 "github.com/dustin/go-humanize" @@ -166,6 +168,12 @@ func registerExtension(router chi.Router, extension = string, mime string) { legend =3D l[0] } =20 + // Label template + var label string + if l, ok :=3D args["label"]; ok { + label =3D l[0] + } + // Set step so that there's approximately 25 data points per inch step :=3D int(end.Sub(start).Seconds() / (25 * float64(width / vg.Inch= ))) if s, ok :=3D args["step"]; ok { @@ -240,7 +248,29 @@ func registerExtension(router chi.Router, extension = string, mime string) { nextColor =3D 0 } plotters[i] =3D l - if legend !=3D "" { + if label !=3D "" && len(res.Metric) > 2 && res.Metric[0] =3D=3D '{' &= & res.Metric[len(res.Metric)-1] =3D=3D '}' { + raw :=3D res.Metric[1 : len(res.Metric)-1] + raw_tags :=3D strings.Split(raw, ",") + tags :=3D make(map[string]string) + for _, v :=3D range raw_tags { + tag :=3D strings.Split(v, "=3D") + if len(tag) !=3D 2 { + log.Printf("Expected tag format: 'name=3Dvalue'!") + continue + } + if len(tag[1]) > 2 && tag[1][0] =3D=3D '"' && tag[1][len(tag[1])-1]= =3D=3D '"' { + tags[tag[0]] =3D tag[1][1 : len(tag[1])-1] + } + } + tmpl, err :=3D template.New("label").Parse(label) + if err !=3D nil { + log.Printf("Failed to parse label template: ", err) + } else { + var label_out bytes.Buffer + tmpl.Execute(&label_out, tags) + p.Legend.Add(label_out.String(), l) + } + } else if legend !=3D "" { p.Legend.Add(legend, l) } else { p.Legend.Add(res.Metric, l) --=20 2.28.0