From f18b07513d1e11d38bf19de9bd0b506449f5054e Mon Sep 17 00:00:00 2001 From: cedrok Date: Fri, 26 Jul 2024 17:41:24 +0200 Subject: [PATCH 1/4] fix(cli) sizeU as int & correct value --- API/models/entity_attributes.go | 18 +++++++++++++----- CLI/controllers/template_test.go | 3 ++- CLI/controllers/update_test.go | 8 ++++---- CLI/models/attributes_device.go | 5 +++-- CLI/models/attributes_device_test.go | 2 +- 5 files changed, 23 insertions(+), 13 deletions(-) diff --git a/API/models/entity_attributes.go b/API/models/entity_attributes.go index 087d417d..5917eead 100644 --- a/API/models/entity_attributes.go +++ b/API/models/entity_attributes.go @@ -1,6 +1,7 @@ package models import ( + "math" "p3/repository" u "p3/utils" "strings" @@ -169,15 +170,22 @@ func checkSizeUAndHeight(attributes map[string]any) *u.Error { Message: err.Error(), } } - height := attributes["height"] - h := sizeU * RACKUNIT + height, err := u.GetFloat(attributes["height"]) + if err != nil { + return &u.Error{ + Type: u.ErrBadFormat, + Message: err.Error(), + } + } + + s := height / RACKUNIT switch heightUnit := attributes["heightUnit"]; heightUnit { case "cm": - h *= 100 + s /= 100 case "mm": - h *= 1000 + s /= 1000 } - if height == h { + if sizeU == math.Ceil(s) { return nil } else { return &u.Error{ diff --git a/CLI/controllers/template_test.go b/CLI/controllers/template_test.go index c64a9b23..086348c7 100644 --- a/CLI/controllers/template_test.go +++ b/CLI/controllers/template_test.go @@ -4,6 +4,7 @@ import ( "cli/models" test_utils "cli/test" "maps" + "math" "testing" "github.com/stretchr/testify/assert" @@ -43,7 +44,7 @@ func TestApplyTemplateOfTypeDeviceWorks(t *testing.T) { test_utils.MockGetObjTemplate(mockAPI, template) - sizeU := int((float64(template["sizeWDHmm"].([]any)[2].(int)) / 1000) / models.RACKUNIT) + sizeU := int(math.Ceil((float64(template["sizeWDHmm"].([]any)[2].(int)) / 1000) / models.RACKUNIT)) err := controller.ApplyTemplate(attributes, device, models.DEVICE) assert.Nil(t, err) diff --git a/CLI/controllers/update_test.go b/CLI/controllers/update_test.go index dec44025..fd60c012 100644 --- a/CLI/controllers/update_test.go +++ b/CLI/controllers/update_test.go @@ -86,7 +86,7 @@ func TestUpdateDeviceSizeUmm(t *testing.T) { } mockDataUpdate := map[string]any{ "attributes": map[string]any{ - "sizeU": float64(1), + "sizeU": 1, "height": 44.45, }, } @@ -116,7 +116,7 @@ func TestUpdateDeviceSizeUcm(t *testing.T) { } mockDataUpdate := map[string]any{ "attributes": map[string]any{ - "sizeU": float64(1), + "sizeU": 1, "height": 4.445, }, } @@ -147,7 +147,7 @@ func TestUpdateDeviceheightmm(t *testing.T) { } mockDataUpdate := map[string]any{ "attributes": map[string]any{ - "sizeU": float64(1), + "sizeU": 1, "height": 44.45, }, } @@ -176,7 +176,7 @@ func TestUpdateDeviceheightcm(t *testing.T) { } mockDataUpdate := map[string]any{ "attributes": map[string]any{ - "sizeU": float64(1), + "sizeU": 1, "height": 4.445, }, } diff --git a/CLI/models/attributes_device.go b/CLI/models/attributes_device.go index ba8f9344..8e314de3 100644 --- a/CLI/models/attributes_device.go +++ b/CLI/models/attributes_device.go @@ -3,6 +3,7 @@ package models import ( "cli/utils" "fmt" + "math" ) // Compute coherent sizeU or height according to given data @@ -49,7 +50,7 @@ func ComputeSizeUAndHeight(obj, data map[string]any) error { default: return fmt.Errorf(errMsg) } - newAttrs["sizeU"] = sizeU + newAttrs["sizeU"] = int(math.Ceil(sizeU)) } return nil } @@ -61,7 +62,7 @@ func SetDeviceSizeUFromTemplate(deviceAttrs, tmpl map[string]any, tmplHeight any if height, err := utils.GetFloat(tmplHeight); err != nil { return err } else { - deviceAttrs["sizeU"] = int((height / 1000) / RACKUNIT) + deviceAttrs["sizeU"] = int(math.Ceil((height / 1000) / RACKUNIT)) } } } diff --git a/CLI/models/attributes_device_test.go b/CLI/models/attributes_device_test.go index 1b75577e..442a4797 100644 --- a/CLI/models/attributes_device_test.go +++ b/CLI/models/attributes_device_test.go @@ -122,7 +122,7 @@ func TestSetDeviceSizeUFromTemplateWorks(t *testing.T) { } err := models.SetDeviceSizeUFromTemplate(deviceAttrs, input, any(10000)) assert.Nil(t, err) - assert.Equal(t, 224, deviceAttrs["sizeU"]) + assert.Equal(t, 225, deviceAttrs["sizeU"]) } // endregion From 6399a8bdcf046916b7e4e2333c3b2b4bbe6dc228 Mon Sep 17 00:00:00 2001 From: cedrok Date: Fri, 26 Jul 2024 17:41:49 +0200 Subject: [PATCH 2/4] fic(api) update device json schema --- API/models/schemas/device_schema.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/API/models/schemas/device_schema.json b/API/models/schemas/device_schema.json index d4820b2a..d6c51372 100644 --- a/API/models/schemas/device_schema.json +++ b/API/models/schemas/device_schema.json @@ -44,7 +44,7 @@ "$ref": "refs/types.json#/definitions/float" }, "sizeU": { - "$ref": "refs/types.json#/definitions/float" + "type": "integer" }, "color": { "type": "string", From 6928046b56593285aa2293e66280f1a8bc5086db Mon Sep 17 00:00:00 2001 From: cedrok Date: Mon, 29 Jul 2024 17:48:48 +0200 Subject: [PATCH 3/4] docs(api) update func description --- API/models/entity_attributes.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/API/models/entity_attributes.go b/API/models/entity_attributes.go index 5917eead..d57e4150 100644 --- a/API/models/entity_attributes.go +++ b/API/models/entity_attributes.go @@ -157,7 +157,7 @@ func setCorridorColor(attributes map[string]any) { } } -// Check if sizeU and height are coherents +// Check if sizeU and height are consistent func checkSizeUAndHeight(attributes map[string]any) *u.Error { if attributes["sizeU"] == nil || attributes["height"] == nil { return nil From a6eff6ea93297268c5d8c26d6ac0fdfc8aa5e6f9 Mon Sep 17 00:00:00 2001 From: cedrok Date: Tue, 30 Jul 2024 17:44:54 +0200 Subject: [PATCH 4/4] fix(cli) round device height to avoid float imprecisions & sizeU is always an int --- CLI/controllers/create_test.go | 9 +++++---- CLI/models/attributes.go | 24 +++++++++--------------- CLI/models/attributes_device.go | 10 +++++----- CLI/utils/util.go | 6 ++++++ 4 files changed, 25 insertions(+), 24 deletions(-) diff --git a/CLI/controllers/create_test.go b/CLI/controllers/create_test.go index d8913f2d..aa8b4580 100644 --- a/CLI/controllers/create_test.go +++ b/CLI/controllers/create_test.go @@ -5,6 +5,7 @@ import ( l "cli/logger" "cli/models" test_utils "cli/test" + "cli/utils" "maps" "testing" @@ -460,8 +461,8 @@ func TestCreateDeviceWithSizeU(t *testing.T) { controller, mockAPI, _ := layersSetup(t) mockGetResponse := test_utils.GetEntity("rack", "A01", "BASIC.A.R1", "test-domain") - sizeU := float64(2) - height := sizeU * models.RACKUNIT * 1000 + sizeU := 2 + height := utils.RoundFloat(float64(sizeU)*models.RACKUNIT*1000, 3) mockCreateResponse := map[string]any{ "category": "device", "id": "BASIC.A.R1.A01.D1", @@ -472,7 +473,7 @@ func TestCreateDeviceWithSizeU(t *testing.T) { "attributes": map[string]any{ "height": height, "sizeU": sizeU, - "heightUnit": "U", + "heightUnit": "mm", "orientation": "front", "size": []float64{1, 1}, "sizeUnit": "cm", @@ -489,7 +490,7 @@ func TestCreateDeviceWithSizeU(t *testing.T) { "domain": "test-domain", "attributes": map[string]any{ "sizeU": sizeU, - "heightUnit": "U", + "heightUnit": "mm", "orientation": "front", "size": []float64{1, 1}, "sizeUnit": "cm", diff --git a/CLI/models/attributes.go b/CLI/models/attributes.go index 9cb827cd..345f0bf4 100644 --- a/CLI/models/attributes.go +++ b/CLI/models/attributes.go @@ -5,7 +5,7 @@ package models import ( l "cli/logger" - "cli/utils" + u "cli/utils" "errors" "fmt" "strconv" @@ -125,16 +125,10 @@ func SerialiseVector(attr map[string]interface{}, want string) []float64 { } func SetDeviceSizeUIfExists(attr EntityAttributes) { - if sizeU, ok := attr["sizeU"]; ok { - //Convert block - //And Set height - if sizeUInt, ok := sizeU.(int); ok { - attr["sizeU"] = sizeUInt - attr["height"] = float64(sizeUInt) * RACKUNIT * 1000 - } else if sizeUFloat, ok := sizeU.(float64); ok { - attr["sizeU"] = sizeUFloat - attr["height"] = sizeUFloat * RACKUNIT * 1000 - } + if sizeU, ok := attr["sizeU"].(int); ok { + attr["sizeU"] = sizeU + //Assuming heightUnit="mm" + attr["height"] = u.RoundFloat(float64(sizeU)*RACKUNIT*1000, 3) } } @@ -251,16 +245,16 @@ func ApplyTemplateToObj(attr, data, tmpl map[string]any, ent int) error { // fbxModel section if ent != BLDG && ent != ROOM { - utils.CopyMapVal(attr, tmpl, "fbxModel") + u.CopyMapVal(attr, tmpl, "fbxModel") } // Copy orientation and shape if available - utils.CopyMapVal(attr, tmpl, "orientation") - utils.CopyMapVal(attr, tmpl, "shape") + u.CopyMapVal(attr, tmpl, "orientation") + u.CopyMapVal(attr, tmpl, "shape") // Merge attributes if available if tmplAttrs, ok := tmpl["attributes"].(map[string]any); ok { - utils.MergeMaps(attr, tmplAttrs, false) + u.MergeMaps(attr, tmplAttrs, false) } return nil diff --git a/CLI/models/attributes_device.go b/CLI/models/attributes_device.go index 8e314de3..1627da77 100644 --- a/CLI/models/attributes_device.go +++ b/CLI/models/attributes_device.go @@ -1,7 +1,7 @@ package models import ( - "cli/utils" + u "cli/utils" "fmt" "math" ) @@ -21,7 +21,7 @@ func ComputeSizeUAndHeight(obj, data map[string]any) error { return err } if newAttrs["sizeU"] != nil { - sizeU, err := utils.GetFloat(newAttrs["sizeU"]) + sizeU, err := u.GetFloat(newAttrs["sizeU"]) if err != nil { return err } @@ -34,10 +34,10 @@ func ComputeSizeUAndHeight(obj, data map[string]any) error { default: return fmt.Errorf(errMsg) } - newAttrs["height"] = height + newAttrs["height"] = u.RoundFloat(height, 3) } if newAttrs["height"] != nil { - height, err := utils.GetFloat(newAttrs["height"]) + height, err := u.GetFloat(newAttrs["height"]) if err != nil { return err } @@ -59,7 +59,7 @@ func SetDeviceSizeUFromTemplate(deviceAttrs, tmpl map[string]any, tmplHeight any if tmplAttrs, ok := tmpl["attributes"].(map[string]any); ok { if tmplType, ok := tmplAttrs["type"].(string); ok && (tmplType == "chassis" || tmplType == "server") { - if height, err := utils.GetFloat(tmplHeight); err != nil { + if height, err := u.GetFloat(tmplHeight); err != nil { return err } else { deviceAttrs["sizeU"] = int(math.Ceil((height / 1000) / RACKUNIT)) diff --git a/CLI/utils/util.go b/CLI/utils/util.go index 58403454..450683b1 100755 --- a/CLI/utils/util.go +++ b/CLI/utils/util.go @@ -4,6 +4,7 @@ import ( "encoding/hex" "errors" "fmt" + "math" "os" "path/filepath" "reflect" @@ -321,3 +322,8 @@ func MergeMaps(x, y map[string]interface{}, overwrite bool) { } } + +func RoundFloat(val float64, precision int) float64 { + ratio := math.Pow(10, float64(precision)) + return math.Round(val*ratio) / ratio +}