fix: Round floats in tagger message

Entire-Checkpoint: b68850c6fcff
This commit is contained in:
2026-03-10 06:01:31 +01:00
parent d2bc046fc6
commit 282197ebef

View File

@@ -10,6 +10,7 @@ import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"maps" "maps"
"math"
"os" "os"
"path/filepath" "path/filepath"
"strings" "strings"
@@ -111,6 +112,29 @@ type JobClassTagger struct {
getMetricConfig func(cluster, subCluster string) map[string]*schema.Metric getMetricConfig func(cluster, subCluster string) map[string]*schema.Metric
} }
// roundEnv returns a copy of env with all float64 values rounded to 2 decimal places.
// Nested map[string]any and map[string]float64 values are recursed into.
func roundEnv(env map[string]any) map[string]any {
rounded := make(map[string]any, len(env))
for k, v := range env {
switch val := v.(type) {
case float64:
rounded[k] = math.Round(val*100) / 100
case map[string]any:
rounded[k] = roundEnv(val)
case map[string]float64:
rm := make(map[string]float64, len(val))
for mk, mv := range val {
rm[mk] = math.Round(mv*100) / 100
}
rounded[k] = rm
default:
rounded[k] = v
}
}
return rounded
}
func (t *JobClassTagger) prepareRule(b []byte, fns string) { func (t *JobClassTagger) prepareRule(b []byte, fns string) {
var rule RuleFormat var rule RuleFormat
if err := json.NewDecoder(bytes.NewReader(b)).Decode(&rule); err != nil { if err := json.NewDecoder(bytes.NewReader(b)).Decode(&rule); err != nil {
@@ -408,7 +432,7 @@ func (t *JobClassTagger) Match(job *schema.Job) {
// process hint template // process hint template
var msg bytes.Buffer var msg bytes.Buffer
if err := ri.hint.Execute(&msg, env); err != nil { if err := ri.hint.Execute(&msg, roundEnv(env)); err != nil {
cclog.Errorf("Template error: %s", err.Error()) cclog.Errorf("Template error: %s", err.Error())
continue continue
} }