Add end date support for Repeating schedule. Added tests

This commit is contained in:
zlawrence
2022-12-13 12:53:05 +01:00
parent 37bf73c7fe
commit 8ba4d19c35
5 changed files with 79 additions and 16 deletions

View File

@ -55,8 +55,8 @@ spec:
format: date-time
type: string
durationMinutes:
description: The duration in minutes that the configured value
will be returned for the defined schedule.
description: The duration in minutes (default 0) that the configured
value will be returned for the defined schedule.
type: integer
endDate:
description: Defines the ending date of a OneTime schedule.
@ -95,7 +95,6 @@ spec:
type: string
required:
- days
- endTime
- startTime
- timezone
type: object
@ -113,7 +112,6 @@ spec:
format: int64
type: integer
required:
- durationMinutes
- type
- value
type: object

View File

@ -55,8 +55,8 @@ spec:
format: date-time
type: string
durationMinutes:
description: The duration in minutes that the configured value
will be returned for the defined schedule.
description: The duration in minutes (default 0) that the configured
value will be returned for the defined schedule.
type: integer
endDate:
description: Defines the ending date of a OneTime schedule.
@ -95,7 +95,6 @@ spec:
type: string
required:
- days
- endTime
- startTime
- timezone
type: object
@ -113,7 +112,6 @@ spec:
format: int64
type: integer
required:
- durationMinutes
- type
- value
type: object

View File

@ -65,8 +65,9 @@ type Schedule struct {
// a RFC3339 formated date.
// +optional
EndDate *ScheduleDate `json:"endDate,omitempty"`
// The duration in minutes that the configured value will be
// The duration in minutes (default 0) that the configured value will be
// returned for the defined schedule.
// +optional
DurationMinutes int `json:"durationMinutes"`
// The metric value that will be returned for the defined schedule.
Value int64 `json:"value"`
@ -96,6 +97,7 @@ type SchedulePeriod struct {
StartTime string `json:"startTime"`
// The endTime has the format HH:MM
// +kubebuilder:validation:Pattern="(([0-1][0-9])|([2][0-3])):([0-5][0-9])"
// +optional
EndTime string `json:"endTime"`
// The days that this schedule will be active.
Days []ScheduleDay `json:"days"`

View File

@ -294,8 +294,10 @@ func calculateMetrics(spec v1.ScalingScheduleSpec, defaultScalingWindow time.Dur
location,
)
// first check if an end time is provided
if schedule.Period.EndTime != "" {
// If no end time was provided, set it to equal the start time
if schedule.Period.EndTime == "" {
scheduledEndTime = scheduledTime
} else {
parsedEndTime, err := time.Parse(hourColonMinuteLayout, schedule.Period.EndTime)
if err != nil {
return nil, ErrInvalidScheduleDate
@ -362,9 +364,6 @@ func valueForEntry(timestamp time.Time, startTime time.Time, entryDuration time.
// Use either the defined end time/date or the start time/date + the
// duration, whichever is longer.
fmt.Println("-----")
fmt.Printf("starttime: %v\n", startTime)
fmt.Printf("end time: %v\n", scheduledEndTime)
if startTime.Add(entryDuration).Before(scheduledEndTime) {
endTime = scheduledEndTime
} else {

View File

@ -23,6 +23,7 @@ type schedule struct {
date string
endDate string
startTime string
endTime string
days []v1.ScheduleDay
timezone string
duration int
@ -77,7 +78,7 @@ func TestScalingScheduleCollector(t *testing.T) {
expectedValue: 100,
},
{
msg: "Return 100 - utilise end date instead of start date + duration for one time config",
msg: "Return the right value - utilise end date instead of start date + duration for one time config",
schedules: []schedule{
{
date: nowTime.Add(-2 * time.Hour).Format(time.RFC3339),
@ -89,6 +90,42 @@ func TestScalingScheduleCollector(t *testing.T) {
},
expectedValue: 100,
},
{
msg: "Return the right value - utilise start date + duration instead of end date for one time config",
schedules: []schedule{
{
date: nowTime.Add(-2 * time.Hour).Format(time.RFC3339),
kind: "OneTime",
duration: 150,
endDate: nowTime.Add(-1 * time.Hour).Format(time.RFC3339),
value: 100,
},
},
expectedValue: 100,
},
{
msg: "Return the right value - use end date with no duration set for one time config",
schedules: []schedule{
{
date: nowTime.Add(-2 * time.Hour).Format(time.RFC3339),
kind: "OneTime",
endDate: nowTime.Add(1 * time.Hour).Format(time.RFC3339),
value: 100,
},
},
expectedValue: 100,
},
{
msg: "Return the right value (0) for one time config no duration or end date set",
schedules: []schedule{
{
date: nowTime.Add(time.Minute * 1).Format(time.RFC3339),
kind: "OneTime",
value: 100,
},
},
expectedValue: 0,
},
{
msg: "Return the right value for one time config - 30 seconds before ending",
schedules: []schedule{
@ -269,6 +306,35 @@ func TestScalingScheduleCollector(t *testing.T) {
},
expectedValue: 100,
},
{
msg: "Return the right value - utilise end date instead of start time + duration for repeating schedule",
schedules: []schedule{
{
kind: "Repeating",
duration: 60,
value: 100,
startTime: nowTime.Add(-2 * time.Hour).Format(hHMMFormat),
// nowTime + 59m = 23:59.
endTime: nowTime.Add(59 * time.Minute).Format(hHMMFormat),
days: []v1.ScheduleDay{nowWeekday},
},
},
expectedValue: 100,
},
{
msg: "Return the right value - utilise start time + duration instead of end time for repeating schedule",
schedules: []schedule{
{
kind: "Repeating",
duration: 150,
value: 100,
startTime: nowTime.Add(-2 * time.Hour).Format(hHMMFormat),
endTime: nowTime.Add(-1 * time.Hour).Format(hHMMFormat),
days: []v1.ScheduleDay{nowWeekday},
},
},
expectedValue: 100,
},
{
msg: "Return the right value for a repeating schedule - 5 minutes after started",
schedules: []schedule{
@ -759,7 +825,6 @@ func newClusterMockStoreFirstRun(name string, scalingWindowDurationMinutes *int6
}
}
// comment DEBUG
func getSchedules(schedules []schedule) (result []v1.Schedule) {
for _, schedule := range schedules {
switch schedule.kind {
@ -778,6 +843,7 @@ func getSchedules(schedules []schedule) (result []v1.Schedule) {
case string(v1.RepeatingSchedule):
period := v1.SchedulePeriod{
StartTime: schedule.startTime,
EndTime: schedule.endTime,
Days: schedule.days,
Timezone: schedule.timezone,
}