-
Notifications
You must be signed in to change notification settings - Fork 0
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
Implement daily schedule sp #28
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
bbb1e88
Implement daily schedule sp
slai11 7a3305e
Fix test case
slai11 4df4bba
Update test case and remove println
slai11 1c21dad
Fix timezone issue
slai11 056108e
Merge remote-tracking branch 'origin/daily-schedule' into daily-schedule
slai11 e756fa7
Add failsafe for invalid TZ
slai11 a60fa00
Refactor out util functions
slai11 29354f3
Update comments for correctness
slai11 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
package subpolicy | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/datagovsg/nomad-parametric-autoscaler/resources" | ||
"github.com/datagovsg/nomad-parametric-autoscaler/utils" | ||
"github.com/mitchellh/mapstructure" | ||
) | ||
|
||
// DailyScheduleSubPolicy is a subpolicy that extends the `SubPolicy` interface | ||
// and takes in the GenericSubpolicy struct | ||
type DailyScheduleSubPolicy struct { | ||
Name string | ||
managedResources []resources.Resource | ||
metadata DailyScheduleSubPolicyMetadata | ||
} | ||
|
||
type ScalingWindow struct { | ||
Begin int `json:"Begin"` | ||
End int `json:"End"` | ||
Count int `json:"Count"` | ||
} | ||
|
||
func (sw ScalingWindow) IsInWindow(time int) bool { | ||
return sw.Begin <= time && time < sw.End | ||
} | ||
|
||
// DailyScheduleSubPolicyMetadata represents metadata unique to DailyScheduleSubPolicy | ||
// define windows of scaling through the day and a fallback default | ||
type DailyScheduleSubPolicyMetadata struct { | ||
Default *int `json:"Default"` | ||
Schedule []ScalingWindow `json:"Schedule"` | ||
} | ||
|
||
func NewDailyScheduleSubPolicy(name string, mr []resources.Resource, meta interface{}) (*DailyScheduleSubPolicy, error) { | ||
var decoded DailyScheduleSubPolicyMetadata | ||
mapstructure.Decode(meta, &decoded) | ||
|
||
if err := verifyDailyScheduleMetadata(decoded); err != nil { | ||
return nil, err | ||
} | ||
|
||
return &DailyScheduleSubPolicy{ | ||
Name: name, | ||
managedResources: mr, | ||
metadata: decoded, | ||
}, nil | ||
} | ||
|
||
func verifyDailyScheduleMetadata(meta DailyScheduleSubPolicyMetadata) error { | ||
if meta.Default == nil { | ||
return fmt.Errorf("Default missing from DailyScheduleSubPolicyMetadata") | ||
} | ||
|
||
if meta.Schedule == nil { | ||
return fmt.Errorf("Schedule missing from DailyScheduleSubPolicyMetadata") | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (dssp DailyScheduleSubPolicy) GetManagedResources() []resources.Resource { | ||
return dssp.managedResources | ||
} | ||
|
||
func (dssp *DailyScheduleSubPolicy) RecommendCount() map[resources.Resource]int { | ||
currentTime := utils.GetCurrentTimeHHMM() | ||
|
||
output := make(map[resources.Resource]int) | ||
|
||
for _, resc := range dssp.managedResources { | ||
var recommendation int | ||
windowExist := false | ||
|
||
// uses count of the first window that current time falls into | ||
for _, sw := range dssp.metadata.Schedule { | ||
if sw.IsInWindow(currentTime) { | ||
recommendation = sw.Count | ||
windowExist = true | ||
break | ||
} | ||
} | ||
|
||
if !windowExist { | ||
recommendation = *dssp.metadata.Default | ||
} | ||
|
||
output[resc] = recommendation | ||
} | ||
return output | ||
} | ||
|
||
func (dssp *DailyScheduleSubPolicy) DeriveGenericSubpolicy() GenericSubPolicy { | ||
resourceNameList := make([]string, 0) | ||
for _, r := range dssp.managedResources { | ||
resourceNameList = append(resourceNameList, r.ResourceName()) | ||
} | ||
|
||
return GenericSubPolicy{ | ||
Name: dssp.Name, | ||
ManagedResources: resourceNameList, | ||
Metadata: dssp.metadata, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package subpolicy | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/datagovsg/nomad-parametric-autoscaler/resources" | ||
) | ||
|
||
func TestDecodingDailyScheduleSP(t *testing.T) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. optional: can include tests for invalid body should return error, valid body but invalid params should return error |
||
input := map[string]interface{}{ | ||
"Default": 9.1, | ||
"Schedule": []map[string]int{ | ||
{"Begin": 1, "End": 3, "Count": 3}, | ||
{"Begin": 3, "End": 4, "Count": 3}, | ||
}, | ||
} | ||
emptyList := make([]resources.Resource, 0) | ||
dssp, err := NewDailyScheduleSubPolicy("a", emptyList, input) | ||
if err != nil { | ||
t.Errorf("Expected to have no errors") | ||
} | ||
|
||
if *dssp.metadata.Default != 9 { | ||
t.Errorf("Expected floats to be truncated") | ||
} | ||
} | ||
|
||
func TestWindowChecking(t *testing.T) { | ||
sw := ScalingWindow{ | ||
Begin: 0000, | ||
End: 0200, | ||
Count: 10, | ||
} | ||
|
||
if !sw.IsInWindow(0000) { | ||
t.Errorf("Expected 000hrs to be within window") | ||
} | ||
|
||
if sw.IsInWindow(0200) { | ||
t.Errorf("Expected 0200hrs to be out of window") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package utils | ||
|
||
import "os" | ||
|
||
func GetEnv(key, fallback string) string { | ||
if value, ok := os.LookupEnv(key); ok { | ||
return value | ||
} | ||
return fallback | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package utils | ||
|
||
import ( | ||
"time" | ||
) | ||
|
||
func GetCurrentTime() time.Time { | ||
loc, err := time.LoadLocation(GetEnv("TZ", "Asia/Singapore")) | ||
if err != nil { | ||
return time.Now().UTC().Add(8 * time.Hour) | ||
} | ||
return time.Now().In(loc) | ||
} | ||
|
||
func GetCurrentTimeHHMM() int { | ||
now := GetCurrentTime() | ||
return (now.Hour())*100 + now.Minute() | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
too bad
mapstructure
doesn't support the struct tagbinding:"Required"
like in Gin, otherwise this wouldnt be necessaryhttps://github.com/golang/go/wiki/Well-known-struct-tags