mirror of
https://github.com/onepanelio/onepanel.git
synced 2025-09-26 17:51:13 +08:00
feat: added methods to Metrics to Add/Merge items
This commit is contained in:
1
go.sum
1
go.sum
@@ -654,6 +654,7 @@ github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UV
|
||||
github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk=
|
||||
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
|
||||
github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=
|
||||
github.com/stretchr/testify v1.6.1 h1:hDPOHmpOpP40lSULcqw7IrRb/u7w6RpDC9399XyoNd0=
|
||||
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
|
||||
github.com/subosito/gotenv v1.2.0/go.mod h1:N0PQaV/YGNqwC0u51sEeR/aUtSLEXKX9iv69rRypqCw=
|
||||
github.com/tidwall/gjson v1.3.5/go.mod h1:P256ACg0Mn+j1RXIDXoss50DeIABTYK1PULOJHhxOls=
|
||||
|
29
pkg/types.go
29
pkg/types.go
@@ -140,6 +140,35 @@ func WorkflowTemplatesToVersionIDs(workflowTemplates []*WorkflowTemplate) (ids [
|
||||
// Metrics is a convenience type to work with multiple Metric(s)
|
||||
type Metrics []*Metric
|
||||
|
||||
// Add adds the new metric to the metrics.
|
||||
// If there is already metrics with the same name, and override is true
|
||||
// the existing metrics will all be updated to the input value. Otherwise, they will be left unchanged.
|
||||
func (m *Metrics) Add(input *Metric, override bool) {
|
||||
foundExisting := false
|
||||
|
||||
for _, metric := range *m {
|
||||
if metric.Name == input.Name && override {
|
||||
foundExisting = true
|
||||
|
||||
metric.Value = input.Value
|
||||
metric.Format = input.Format
|
||||
}
|
||||
}
|
||||
|
||||
if !foundExisting {
|
||||
*m = append(*m, input)
|
||||
}
|
||||
}
|
||||
|
||||
// Merge merges the metrics with other metrics
|
||||
// If there is already metrics with the same name and override is true
|
||||
// the existing metrics will all be updated to the input value. Otherwise they will be left unchanged.
|
||||
func (m *Metrics) Merge(input Metrics, override bool) {
|
||||
for _, item := range input {
|
||||
m.Add(item, override)
|
||||
}
|
||||
}
|
||||
|
||||
// Unmarshal unmarshal's the json in m to v, as in json.Unmarshal.
|
||||
// This is to support Metrics working with JSONB column types in sql
|
||||
func (m *Metrics) Unmarshal(v interface{}) error {
|
||||
|
72
pkg/types_test.go
Normal file
72
pkg/types_test.go
Normal file
@@ -0,0 +1,72 @@
|
||||
package v1
|
||||
|
||||
import (
|
||||
"github.com/stretchr/testify/assert"
|
||||
"testing"
|
||||
)
|
||||
|
||||
// TestMetrics_Add tests the Add method of the Metrics type
|
||||
func TestMetrics_Add(t *testing.T) {
|
||||
var initial Metrics = []*Metric{{
|
||||
Name: "accuracy",
|
||||
Value: 0.98,
|
||||
Format: "",
|
||||
}}
|
||||
|
||||
initial.Add(&Metric{
|
||||
Name: "success",
|
||||
Value: 1.0,
|
||||
Format: "%",
|
||||
}, false)
|
||||
|
||||
assert.Len(t, initial, 2)
|
||||
|
||||
initial.Add(&Metric{
|
||||
Name: "accuracy",
|
||||
Value: 0.99,
|
||||
Format: "%",
|
||||
}, false)
|
||||
|
||||
assert.Len(t, initial, 2)
|
||||
|
||||
initial.Add(&Metric{
|
||||
Name: "accuracy",
|
||||
Value: 0.99,
|
||||
Format: "%",
|
||||
}, true)
|
||||
|
||||
assert.Len(t, initial, 2)
|
||||
assert.True(t, initial[0].Value == 0.99)
|
||||
}
|
||||
|
||||
// TestMetrics_Merge tests the Merge method of the Metrics Type
|
||||
func TestMetrics_Merge(t *testing.T) {
|
||||
var initial Metrics = []*Metric{{
|
||||
Name: "accuracy",
|
||||
Value: 0.98,
|
||||
Format: "",
|
||||
}, {
|
||||
Name: "success",
|
||||
Value: 1.0,
|
||||
Format: "%",
|
||||
}}
|
||||
|
||||
var toMerge Metrics = []*Metric{{
|
||||
Name: "accuracy",
|
||||
Value: 0.00,
|
||||
Format: "",
|
||||
}, {
|
||||
Name: "success",
|
||||
Value: 1.0,
|
||||
Format: "%",
|
||||
}, {
|
||||
Name: "test",
|
||||
Value: 0.5,
|
||||
Format: "",
|
||||
}}
|
||||
|
||||
initial.Merge(toMerge, true)
|
||||
|
||||
assert.Len(t, initial, 3)
|
||||
assert.True(t, initial[0].Value == 0.00)
|
||||
}
|
Reference in New Issue
Block a user