Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix499 correct sizeu value #500

Merged
merged 4 commits into from
Jul 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 14 additions & 6 deletions API/models/entity_attributes.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package models

import (
"math"
"p3/repository"
u "p3/utils"
"strings"
Expand Down Expand Up @@ -156,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
Expand All @@ -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{
Expand Down
2 changes: 1 addition & 1 deletion API/models/schemas/device_schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
"$ref": "refs/types.json#/definitions/float"
},
"sizeU": {
"$ref": "refs/types.json#/definitions/float"
"type": "integer"
},
"color": {
"type": "string",
Expand Down
9 changes: 5 additions & 4 deletions CLI/controllers/create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
l "cli/logger"
"cli/models"
test_utils "cli/test"
"cli/utils"
"maps"
"testing"

Expand Down Expand Up @@ -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",
Expand All @@ -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",
Expand All @@ -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",
Expand Down
3 changes: 2 additions & 1 deletion CLI/controllers/template_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"cli/models"
test_utils "cli/test"
"maps"
"math"
"testing"

"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -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)

Expand Down
8 changes: 4 additions & 4 deletions CLI/controllers/update_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
}
Expand Down Expand Up @@ -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,
},
}
Expand Down Expand Up @@ -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,
},
}
Expand Down Expand Up @@ -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,
},
}
Expand Down
24 changes: 9 additions & 15 deletions CLI/models/attributes.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ package models

import (
l "cli/logger"
"cli/utils"
u "cli/utils"
"errors"
"fmt"
"strconv"
Expand Down Expand Up @@ -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)
}
}

Expand Down Expand Up @@ -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
Expand Down
15 changes: 8 additions & 7 deletions CLI/models/attributes_device.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package models

import (
"cli/utils"
u "cli/utils"
"fmt"
"math"
)

// Compute coherent sizeU or height according to given data
Expand All @@ -20,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
}
Expand All @@ -33,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
}
Expand All @@ -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
}
Expand All @@ -58,10 +59,10 @@ 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((height / 1000) / RACKUNIT)
deviceAttrs["sizeU"] = int(math.Ceil((height / 1000) / RACKUNIT))
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion CLI/models/attributes_device_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
6 changes: 6 additions & 0 deletions CLI/utils/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/hex"
"errors"
"fmt"
"math"
"os"
"path/filepath"
"reflect"
Expand Down Expand Up @@ -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
}
Loading