mirror of
https://github.com/ClusterCockpit/cc-backend
synced 2026-03-10 18:17:30 +01:00
fix: Round floats in tagger message
Entire-Checkpoint: b68850c6fcff
This commit is contained in:
@@ -10,6 +10,7 @@ import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"maps"
|
||||
"math"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
@@ -111,6 +112,29 @@ type JobClassTagger struct {
|
||||
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) {
|
||||
var rule RuleFormat
|
||||
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
|
||||
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())
|
||||
continue
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user