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

Enable coordinateFair option for recommend by location #1235

Merged
merged 1 commit into from
Oct 21, 2022
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
2 changes: 1 addition & 1 deletion src/api/rest/docs/docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -672,7 +672,7 @@ const docTemplate = `{
},
"/mcisRecommendVm": {
"post": {
"description": "Recommend MCIS plan (filter and priority)",
"description": "Recommend MCIS plan (filter and priority) Find details from https://github.com/cloud-barista/cb-tumblebug/discussions/1234",
"consumes": [
"application/json"
],
Expand Down
2 changes: 1 addition & 1 deletion src/api/rest/docs/swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -664,7 +664,7 @@
},
"/mcisRecommendVm": {
"post": {
"description": "Recommend MCIS plan (filter and priority)",
"description": "Recommend MCIS plan (filter and priority) Find details from https://github.com/cloud-barista/cb-tumblebug/discussions/1234",
"consumes": [
"application/json"
],
Expand Down
2 changes: 1 addition & 1 deletion src/api/rest/docs/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2799,7 +2799,7 @@ paths:
post:
consumes:
- application/json
description: Recommend MCIS plan (filter and priority)
description: Recommend MCIS plan (filter and priority) Find details from https://github.com/cloud-barista/cb-tumblebug/discussions/1234
parameters:
- description: Recommend MCIS plan (filter and priority)
in: body
Expand Down
2 changes: 1 addition & 1 deletion src/api/rest/server/mcis/recommendation.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import (

// RestRecommendVm godoc
// @Summary Recommend MCIS plan (filter and priority)
// @Description Recommend MCIS plan (filter and priority)
// @Description Recommend MCIS plan (filter and priority) Find details from https://github.com/cloud-barista/cb-tumblebug/discussions/1234
// @Tags [Infra service] MCIS Provisioning management
// @Accept json
// @Produce json
Expand Down
75 changes: 74 additions & 1 deletion src/core/mcis/recommendation.go
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,80 @@ func RecommendVmLocation(nsId string, specList *[]mcir.TbSpecInfo, param *[]Para
case "coordinateWithin":
//
case "coordinateFair":
//
var err error

// Calculate centroid of coordinate clusters
latitudeSum := 0.0
longitudeSum := 0.0
for _, coordinateStr := range v.Val {
slice := strings.Split(coordinateStr, "/")
latitudeEach, err := strconv.ParseFloat(strings.ReplaceAll(slice[0], " ", ""), 32)
if err != nil {
common.CBLog.Error(err)
return []mcir.TbSpecInfo{}, err
}
longitudeEach, err := strconv.ParseFloat(strings.ReplaceAll(slice[1], " ", ""), 32)
if err != nil {
common.CBLog.Error(err)
return []mcir.TbSpecInfo{}, err
}
latitudeSum += latitudeEach
longitudeSum += longitudeEach
}
latitude := latitudeSum / (float64)(len(v.Val))
longitude := longitudeSum / (float64)(len(v.Val))

// Sorting, closes to the centroid.

type distanceType struct {
distance float64
index int
priorityIndex int
}
distances := []distanceType{}

for i := range *specList {
distances = append(distances, distanceType{})
distances[i].distance, err = getDistance(latitude, longitude, (*specList)[i].ConnectionName)
if err != nil {
common.CBLog.Error(err)
return []mcir.TbSpecInfo{}, err
}
distances[i].index = i
}

sort.Slice(distances, func(i, j int) bool {
return (*specList)[i].CostPerHour < (*specList)[j].CostPerHour
})
sort.Slice(distances, func(i, j int) bool {
return distances[i].distance < distances[j].distance
})
fmt.Printf("\n distances : %v \n", distances)

priorityCnt := 1
for i := range distances {

// priorityIndex++ if two distances are not equal (give the same priorityIndex if two variables are same)
if i != 0 {
if distances[i].distance > distances[i-1].distance {
priorityCnt++
}
}
distances[i].priorityIndex = priorityCnt

}

max := float32(distances[len(*specList)-1].distance)
min := float32(distances[0].distance)

for i := range *specList {
// update OrderInFilteredResult based on calculated priorityIndex
(*specList)[distances[i].index].OrderInFilteredResult = uint16(distances[i].priorityIndex)
// assign nomalized priorityIdex value to EvaluationScore09
(*specList)[distances[i].index].EvaluationScore09 = float32((max - float32(distances[i].distance)) / (max - min + 0.0000001)) // Add small value to avoid NaN by division
(*specList)[distances[i].index].EvaluationScore10 = float32(distances[i].distance)
// fmt.Printf("\n [%v] OrderInFilteredResult:%v, max:%v, min:%v, distance:%v, eval:%v \n", i, (*specList)[distances[i].index].OrderInFilteredResult, max, min, float32(distances[i].distance), (*specList)[distances[i].index].EvaluationScore09)
}
default:
// fmt.Println("[Checking] Not available metric " + metric)
}
Expand Down