Skip to content

Commit

Permalink
Merge pull request #1994 from slntopp/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
coddmeistr authored Feb 7, 2025
2 parents e4ff89f + 7eddb37 commit df016b3
Show file tree
Hide file tree
Showing 4 changed files with 178 additions and 3 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ require (
github.com/grpc-ecosystem/grpc-gateway/v2 v2.20.0
github.com/rabbitmq/amqp091-go v1.9.0
github.com/rs/cors v1.10.1
github.com/slntopp/nocloud-proto v0.0.0-20250111091130-cef7b71e8db5
github.com/slntopp/nocloud-proto v0.0.0-20250207050023-78355039f2b5
github.com/spf13/viper v1.18.2
github.com/stoewer/go-strcase v1.3.0
github.com/stretchr/testify v1.9.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,8 @@ github.com/sirupsen/logrus v1.4.2/go.mod h1:tLMulIdttU9McNUspp0xgXVQah82FyeX6Mwd
github.com/sirupsen/logrus v1.8.1/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic61uBYv0=
github.com/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ=
github.com/sirupsen/logrus v1.9.3/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ=
github.com/slntopp/nocloud-proto v0.0.0-20250111091130-cef7b71e8db5 h1:YER/mdWPB5BpwGxCtFo5uyhN2cD7bULscXen/MXggtc=
github.com/slntopp/nocloud-proto v0.0.0-20250111091130-cef7b71e8db5/go.mod h1:qPbslPB2J9Q7qm6H9Jaqf/Ysf61YlPL0DUFhIdAEikI=
github.com/slntopp/nocloud-proto v0.0.0-20250207050023-78355039f2b5 h1:D+ijVHOP+6WGy25mcogcZyBAaWwLGPQGf2PJRFDgm/w=
github.com/slntopp/nocloud-proto v0.0.0-20250207050023-78355039f2b5/go.mod h1:qPbslPB2J9Q7qm6H9Jaqf/Ysf61YlPL0DUFhIdAEikI=
github.com/sourcegraph/conc v0.3.0 h1:OQTbbt6P72L20UqAkXXuLOj79LfEanQ+YQFNpLA9ySo=
github.com/sourcegraph/conc v0.3.0/go.mod h1:Sdozi7LEKbFPqYX2/J+iBAM6HpqSLTASQIKqDmF7Mt0=
github.com/spf13/afero v1.11.0 h1:WJQKhtpdm3v2IzqG8VMqrr6Rf3UYpEF239Jy9wNepM8=
Expand Down
107 changes: 107 additions & 0 deletions pkg/nocloud/suspend_rules/rules.go
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
}
68 changes: 68 additions & 0 deletions pkg/nocloud/suspend_rules/rules_test.go
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")
}
}

0 comments on commit df016b3

Please sign in to comment.