-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1994 from slntopp/dev
Dev
- Loading branch information
Showing
4 changed files
with
178 additions
and
3 deletions.
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
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,107 @@ | ||
package suspend_rules | ||
|
||
import ( | ||
"fmt" | ||
sppb "github.com/slntopp/nocloud-proto/services_providers" | ||
"strconv" | ||
"strings" | ||
"time" | ||
) | ||
|
||
func SuspendAllowed(rules *sppb.SuspendRules, now time.Time) bool { | ||
if rules == nil || !rules.Enabled || rules.Schedules == nil || len(rules.Schedules) == 0 { | ||
return true | ||
} | ||
dayOfWeek := int(now.Weekday()) | ||
if dayOfWeek == 0 { | ||
dayOfWeek = 6 | ||
} else { | ||
dayOfWeek-- | ||
} | ||
nowTimeNum := timeToNumber(now.Hour(), now.Minute()) | ||
for _, rule := range rules.GetSchedules() { | ||
if int(rule.Day) != dayOfWeek { | ||
continue | ||
} | ||
if len(rule.AllowedSuspendTime) == 0 { | ||
return true | ||
} | ||
for _, tme := range rule.AllowedSuspendTime { | ||
if tme == nil { | ||
continue | ||
} | ||
startHour, startMin, err := parseTime(tme.StartTime) | ||
if err != nil { | ||
fmt.Println("SuspendAllowed: error parsing start time: " + err.Error()) | ||
continue | ||
} | ||
endHour, endMin, err := parseTime(tme.EndTime) | ||
if err != nil { | ||
fmt.Println("SuspendAllowed: error parsing end time: " + err.Error()) | ||
continue | ||
} | ||
if nowTimeNum >= timeToNumber(startHour, startMin) && nowTimeNum <= timeToNumber(endHour, endMin) { | ||
return true | ||
} | ||
} | ||
} | ||
return false | ||
} | ||
|
||
func parseTime(t string) (int, int, error) { | ||
hour, err := parseHour(t) | ||
if err != nil { | ||
return 0, 0, err | ||
} | ||
minute, err := parseMinute(t) | ||
if err != nil { | ||
return 0, 0, err | ||
} | ||
return hour, minute, nil | ||
} | ||
|
||
func parseHour(t string) (int, error) { | ||
if len(t) != 5 { | ||
return 0, fmt.Errorf("invalid time format. Must be HH:MM") | ||
} | ||
hour, err := strconv.Atoi(trimZero(t[0:2])) | ||
if err != nil { | ||
return 0, fmt.Errorf("cannot parse integer") | ||
} | ||
if hour > 23 { | ||
return 23, nil | ||
} | ||
if hour < 0 { | ||
return 0, nil | ||
} | ||
return hour, nil | ||
} | ||
|
||
func parseMinute(t string) (int, error) { | ||
if len(t) != 5 { | ||
return 0, fmt.Errorf("invalid time format. Must be HH:MM") | ||
} | ||
minute, err := strconv.Atoi(trimZero(t[3:5])) | ||
if err != nil { | ||
return 0, fmt.Errorf("cannot parse integer") | ||
} | ||
if minute > 59 { | ||
return 59, nil | ||
} | ||
if minute < 0 { | ||
return 0, nil | ||
} | ||
return minute, nil | ||
} | ||
|
||
func timeToNumber(hour, minute int) int { | ||
return (hour * 60) + minute | ||
} | ||
|
||
func trimZero(s string) string { | ||
s = strings.TrimPrefix(s, "0") | ||
if s == "" { | ||
return "0" | ||
} | ||
return s | ||
} |
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,68 @@ | ||
package suspend_rules | ||
|
||
import ( | ||
sppb "github.com/slntopp/nocloud-proto/services_providers" | ||
"testing" | ||
"time" | ||
) | ||
|
||
func TestSuspendAllowed(t *testing.T) { | ||
timeNow := time.Date(2024, 2, 7, 10, 30, 0, 0, time.UTC) // Среда, 10:30 UTC | ||
rules := &sppb.SuspendRules{ | ||
Enabled: true, | ||
Schedules: []*sppb.DaySchedule{ | ||
{ | ||
Day: sppb.DayOfWeek_WEDNESDAY, | ||
AllowedSuspendTime: []*sppb.TimeRange{ | ||
{StartTime: "09:00", EndTime: "11:00"}, | ||
}, | ||
}, | ||
}, | ||
} | ||
if !SuspendAllowed(rules, timeNow) { | ||
t.Errorf("Expected suspend to be allowed, but it was not") | ||
} | ||
} | ||
|
||
func TestSuspendNotAllowedOutsideRange(t *testing.T) { | ||
timeNow := time.Date(2024, 2, 7, 12, 0, 0, 0, time.UTC) // Среда, 12:00 UTC | ||
rules := &sppb.SuspendRules{ | ||
Enabled: true, | ||
Schedules: []*sppb.DaySchedule{ | ||
{ | ||
Day: sppb.DayOfWeek_WEDNESDAY, | ||
AllowedSuspendTime: []*sppb.TimeRange{ | ||
{StartTime: "09:00", EndTime: "11:00"}, | ||
}, | ||
}, | ||
}, | ||
} | ||
if SuspendAllowed(rules, timeNow) { | ||
t.Errorf("Expected suspend to not be allowed, but it was") | ||
} | ||
} | ||
|
||
func TestSuspendAllowedWithNoSchedules(t *testing.T) { | ||
rules := &sppb.SuspendRules{ | ||
Enabled: true, | ||
Schedules: []*sppb.DaySchedule{}, | ||
} | ||
if !SuspendAllowed(rules, time.Now()) { | ||
t.Errorf("Expected suspend to be allowed, but it was not") | ||
} | ||
} | ||
|
||
func TestSuspendAllowedWhenDisabled(t *testing.T) { | ||
rules := &sppb.SuspendRules{ | ||
Enabled: false, | ||
} | ||
if !SuspendAllowed(rules, time.Now()) { | ||
t.Errorf("Expected suspend to be allowed, but it was not") | ||
} | ||
} | ||
|
||
func TestSuspendAllowedNilRules(t *testing.T) { | ||
if !SuspendAllowed(nil, time.Now()) { | ||
t.Errorf("Expected suspend to be allowed with nil rules, but it was not") | ||
} | ||
} |