From 8280e0a3a7137303139ebf0924738a570cd1cad8 Mon Sep 17 00:00:00 2001 From: iChemy Date: Tue, 18 Jun 2024 13:35:45 +0900 Subject: [PATCH 1/5] :sparkles: add FilterDuration and replace FilterTime in InitPostEventToTraQ --- domain/filter/shorthand.go | 82 ++++++++++++++++++++++++++++++++++++++ utils/cron.go | 6 +-- 2 files changed, 85 insertions(+), 3 deletions(-) diff --git a/domain/filter/shorthand.go b/domain/filter/shorthand.go index 05bc7fa2..fdcfd4a4 100644 --- a/domain/filter/shorthand.go +++ b/domain/filter/shorthand.go @@ -93,3 +93,85 @@ func AddAnd(lhs, rhs Expr) Expr { Rhs: rhs, } } + +func addOr(lhs, rhs Expr) Expr { + if lhs == nil && rhs == nil { + return nil + } + if lhs == nil { + return rhs + } + if rhs == nil { + return lhs + } + return &LogicOpExpr{ + LogicOp: Or, + Lhs: lhs, + Rhs: rhs, + } +} + +// 期間中に始まる +// time_start >= min AND time_start < max +// +// 期間中に終わる +// time_end >= min AND time_end < max +// +// 期間より前に始まり期間より後に終わる +// time_start < min AND time_end >= max +// +// min < max であるべき +func FilterDuration(min, max time.Time) Expr { + startAfterMin := &CmpExpr{ + Attr: AttrTimeStart, + Relation: GreterEq, + Value: min, + } + startBeforeMax := &CmpExpr{ + Attr: AttrTimeStart, + Relation: Less, + Value: max, + } + // 期間中に始まる + startIn := &LogicOpExpr{ + LogicOp: And, + Lhs: startAfterMin, + Rhs: startBeforeMax, + } + + endAfterMin := &CmpExpr{ + Attr: AttrTimeEnd, + Relation: GreterEq, + Value: min, + } + endBeforeMax := &CmpExpr{ + Attr: AttrTimeEnd, + Relation: Less, + Value: max, + } + // 期間中に終わる + endIn := &LogicOpExpr{ + LogicOp: And, + Lhs: endAfterMin, + Rhs: endBeforeMax, + } + + startBeforeMin := &CmpExpr{ + Attr: AttrTimeStart, + Relation: Less, + Value: min, + } + endAfterMax := &CmpExpr{ + Attr: AttrTimeEnd, + Relation: GreterEq, + Value: max, + } + // 期間より前に始まり期間より後に終わる + throughout := &LogicOpExpr{ + LogicOp: And, + Lhs: startBeforeMin, + Rhs: endAfterMax, + } + + return addOr(addOr(startIn, endIn), throughout) +} diff --git a/utils/cron.go b/utils/cron.go index 545f6644..433b5ed3 100644 --- a/utils/cron.go +++ b/utils/cron.go @@ -27,7 +27,7 @@ func InitPostEventToTraQ(repo *db.GormRepository, secret, channelID, webhookID, tomorrow := now.AddDate(0, 0, 1) rooms, _ := repo.GetAllRooms(now, tomorrow, uuid.Nil) - events, _ := repo.GetAllEvents(filter.FilterTime(now, tomorrow)) + events, _ := repo.GetAllEvents(filter.FilterDuration(now, tomorrow)) message := createMessage(now, rooms, events, origin) err := RequestWebhook(message, secret, channelID, webhookID, 1) if err != nil { @@ -162,8 +162,8 @@ func createMessage(t time.Time, rooms []*domain.Room, events []*db.Event, origin } else { for _, event := range events { - eventMessage += fmt.Sprintf("- [%s](%s/events/%s) %s ~ %s @%s %s\n", event.Name, origin, event.ID, - event.TimeStart.In(tz.JST).Format("15:04"), event.TimeEnd.In(tz.JST).Format("15:04"), + eventMessage += fmt.Sprintf("- [%s](%s/events/%s) %s:%s ~ %s:%s @%s %s\n", event.Name, origin, event.ID, + event.TimeStart.In(tz.JST).Format("01/02"), event.TimeStart.In(tz.JST).Format("15:04"), event.TimeEnd.In(tz.JST).Format("01/02"), event.TimeEnd.In(tz.JST).Format("15:04"), event.Room.Place, combined[event.AllowTogether]) } From a2b852e287d4e5f47e45668cf00b8bdb303570c5 Mon Sep 17 00:00:00 2001 From: iChemy Date: Thu, 20 Jun 2024 15:58:27 +0900 Subject: [PATCH 2/5] :recycle: refactoring code --- domain/filter/shorthand.go | 59 +++++++++++++++++--------------------- utils/cron.go | 4 +-- 2 files changed, 28 insertions(+), 35 deletions(-) diff --git a/domain/filter/shorthand.go b/domain/filter/shorthand.go index fdcfd4a4..9d1c0ebc 100644 --- a/domain/filter/shorthand.go +++ b/domain/filter/shorthand.go @@ -94,43 +94,28 @@ func AddAnd(lhs, rhs Expr) Expr { } } -func addOr(lhs, rhs Expr) Expr { - if lhs == nil && rhs == nil { - return nil - } - if lhs == nil { - return rhs - } - if rhs == nil { - return lhs - } - return &LogicOpExpr{ - LogicOp: Or, - Lhs: lhs, - Rhs: rhs, +// 以下のいずれかを満たす +// * 期間中に始まる (time_start >= min AND time_start < max) +// * 期間中に終わる (time_end >= min AND time_end < max) +// * 期間より前に始まり期間より後に終わる (time_start < min AND time_end >= max) +// +// since < until でない場合 nil を返す +func FilterDuration(since, until time.Time) Expr { + if !since.IsZero() && !until.IsZero() { + if since.After(until) { + return nil + } } -} -// 期間中に始まる -// time_start >= min AND time_start < max -// -// 期間中に終わる -// time_end >= min AND time_end < max -// -// 期間より前に始まり期間より後に終わる -// time_start < min AND time_end >= max -// -// min < max であるべき -func FilterDuration(min, max time.Time) Expr { startAfterMin := &CmpExpr{ Attr: AttrTimeStart, Relation: GreterEq, - Value: min, + Value: since, } startBeforeMax := &CmpExpr{ Attr: AttrTimeStart, Relation: Less, - Value: max, + Value: until, } // 期間中に始まる startIn := &LogicOpExpr{ @@ -142,12 +127,12 @@ func FilterDuration(min, max time.Time) Expr { endAfterMin := &CmpExpr{ Attr: AttrTimeEnd, Relation: GreterEq, - Value: min, + Value: since, } endBeforeMax := &CmpExpr{ Attr: AttrTimeEnd, Relation: Less, - Value: max, + Value: until, } // 期間中に終わる endIn := &LogicOpExpr{ @@ -159,12 +144,12 @@ func FilterDuration(min, max time.Time) Expr { startBeforeMin := &CmpExpr{ Attr: AttrTimeStart, Relation: Less, - Value: min, + Value: since, } endAfterMax := &CmpExpr{ Attr: AttrTimeEnd, Relation: GreterEq, - Value: max, + Value: until, } // 期間より前に始まり期間より後に終わる throughout := &LogicOpExpr{ @@ -173,5 +158,13 @@ func FilterDuration(min, max time.Time) Expr { Rhs: endAfterMax, } - return addOr(addOr(startIn, endIn), throughout) + return &LogicOpExpr{ + LogicOp: Or, + Lhs: throughout, + Rhs: &LogicOpExpr{ + LogicOp: Or, + Lhs: endIn, + Rhs: startIn, + }, + } } diff --git a/utils/cron.go b/utils/cron.go index 433b5ed3..41c7f247 100644 --- a/utils/cron.go +++ b/utils/cron.go @@ -162,8 +162,8 @@ func createMessage(t time.Time, rooms []*domain.Room, events []*db.Event, origin } else { for _, event := range events { - eventMessage += fmt.Sprintf("- [%s](%s/events/%s) %s:%s ~ %s:%s @%s %s\n", event.Name, origin, event.ID, - event.TimeStart.In(tz.JST).Format("01/02"), event.TimeStart.In(tz.JST).Format("15:04"), event.TimeEnd.In(tz.JST).Format("01/02"), event.TimeEnd.In(tz.JST).Format("15:04"), + eventMessage += fmt.Sprintf("- [%s](%s/events/%s) %s ~ %s @%s %s\n", event.Name, origin, event.ID, + event.TimeStart.In(tz.JST).Format("01/02 15:04"), event.TimeEnd.In(tz.JST).Format("01/02 15:04"), event.Room.Place, combined[event.AllowTogether]) } From 68fa95bb9e39fcb73e7d2c3eb21bfccbc22ba1d9 Mon Sep 17 00:00:00 2001 From: iChemy Date: Sun, 23 Jun 2024 00:03:36 +0900 Subject: [PATCH 3/5] :art: add error for returning value of FilterDuration --- domain/filter/shorthand.go | 59 +++++++++++++++++--------------------- utils/cron.go | 8 ++++-- 2 files changed, 33 insertions(+), 34 deletions(-) diff --git a/domain/filter/shorthand.go b/domain/filter/shorthand.go index 9d1c0ebc..6d82d663 100644 --- a/domain/filter/shorthand.go +++ b/domain/filter/shorthand.go @@ -1,6 +1,7 @@ package filter import ( + "errors" "time" "github.com/gofrs/uuid" @@ -95,50 +96,44 @@ func AddAnd(lhs, rhs Expr) Expr { } // 以下のいずれかを満たす -// * 期間中に始まる (time_start >= min AND time_start < max) -// * 期間中に終わる (time_end >= min AND time_end < max) -// * 期間より前に始まり期間より後に終わる (time_start < min AND time_end >= max) -// -// since < until でない場合 nil を返す -func FilterDuration(since, until time.Time) Expr { +// * 期間中に始まる (time_start >= since AND time_start < until) +// * 期間中に終わる (time_end >= since AND time_end < until) +// * 期間より前に始まり期間より後に終わる (time_start < since AND time_end >= until) +func FilterDuration(since, until time.Time) (Expr, error) { if !since.IsZero() && !until.IsZero() { if since.After(until) { - return nil + return nil, errors.New("invalid time range") } } - startAfterMin := &CmpExpr{ - Attr: AttrTimeStart, - Relation: GreterEq, - Value: since, - } - startBeforeMax := &CmpExpr{ - Attr: AttrTimeStart, - Relation: Less, - Value: until, - } // 期間中に始まる startIn := &LogicOpExpr{ LogicOp: And, - Lhs: startAfterMin, - Rhs: startBeforeMax, + Lhs: &CmpExpr{ + Attr: AttrTimeStart, + Relation: GreterEq, + Value: since, + }, + Rhs: &CmpExpr{ + Attr: AttrTimeStart, + Relation: Less, + Value: until, + }, } - endAfterMin := &CmpExpr{ - Attr: AttrTimeEnd, - Relation: GreterEq, - Value: since, - } - endBeforeMax := &CmpExpr{ - Attr: AttrTimeEnd, - Relation: Less, - Value: until, - } // 期間中に終わる endIn := &LogicOpExpr{ LogicOp: And, - Lhs: endAfterMin, - Rhs: endBeforeMax, + Lhs: &CmpExpr{ + Attr: AttrTimeEnd, + Relation: GreterEq, + Value: since, + }, + Rhs: &CmpExpr{ + Attr: AttrTimeEnd, + Relation: Less, + Value: until, + }, } startBeforeMin := &CmpExpr{ @@ -166,5 +161,5 @@ func FilterDuration(since, until time.Time) Expr { Lhs: endIn, Rhs: startIn, }, - } + }, nil } diff --git a/utils/cron.go b/utils/cron.go index 41c7f247..85677b6a 100644 --- a/utils/cron.go +++ b/utils/cron.go @@ -27,9 +27,13 @@ func InitPostEventToTraQ(repo *db.GormRepository, secret, channelID, webhookID, tomorrow := now.AddDate(0, 0, 1) rooms, _ := repo.GetAllRooms(now, tomorrow, uuid.Nil) - events, _ := repo.GetAllEvents(filter.FilterDuration(now, tomorrow)) + expr, err := filter.FilterDuration(now, tomorrow) + if err != nil { + fmt.Println(err) + } + events, _ := repo.GetAllEvents(expr) message := createMessage(now, rooms, events, origin) - err := RequestWebhook(message, secret, channelID, webhookID, 1) + err = RequestWebhook(message, secret, channelID, webhookID, 1) if err != nil { fmt.Println(err) } From e80bc38dc79991e0ebae26afe7382406fd2f9456 Mon Sep 17 00:00:00 2001 From: iChemy Date: Mon, 24 Jun 2024 15:18:31 +0900 Subject: [PATCH 4/5] :art: add zero time handling --- domain/filter/shorthand.go | 48 +++++++++++++++++++++++++------------- 1 file changed, 32 insertions(+), 16 deletions(-) diff --git a/domain/filter/shorthand.go b/domain/filter/shorthand.go index 6d82d663..6835b968 100644 --- a/domain/filter/shorthand.go +++ b/domain/filter/shorthand.go @@ -99,11 +99,29 @@ func AddAnd(lhs, rhs Expr) Expr { // * 期間中に始まる (time_start >= since AND time_start < until) // * 期間中に終わる (time_end >= since AND time_end < until) // * 期間より前に始まり期間より後に終わる (time_start < since AND time_end >= until) +// +// ただしsinceがゼロのとき time_start <= until +// untilがゼロのとき since <= time_end +// 双方がゼロのき nil を返す func FilterDuration(since, until time.Time) (Expr, error) { - if !since.IsZero() && !until.IsZero() { - if since.After(until) { - return nil, errors.New("invalid time range") - } + if since.IsZero() && until.IsZero() { + return nil, nil + } else if since.IsZero() { + return &CmpExpr{ + Attr: AttrTimeStart, + Relation: LessEq, + Value: until, + }, nil + } else if until.IsZero() { + return &CmpExpr{ + Attr: AttrTimeEnd, + Relation: GreterEq, + Value: since, + }, nil + } + + if since.After(until) { + return nil, errors.New("invalid time range") } // 期間中に始まる @@ -136,21 +154,19 @@ func FilterDuration(since, until time.Time) (Expr, error) { }, } - startBeforeMin := &CmpExpr{ - Attr: AttrTimeStart, - Relation: Less, - Value: since, - } - endAfterMax := &CmpExpr{ - Attr: AttrTimeEnd, - Relation: GreterEq, - Value: until, - } // 期間より前に始まり期間より後に終わる throughout := &LogicOpExpr{ LogicOp: And, - Lhs: startBeforeMin, - Rhs: endAfterMax, + Lhs: &CmpExpr{ + Attr: AttrTimeStart, + Relation: Less, + Value: since, + }, + Rhs: &CmpExpr{ + Attr: AttrTimeEnd, + Relation: GreterEq, + Value: until, + }, } return &LogicOpExpr{ From 0b24a6a228144601a27aedb9044ae5fd3de43315 Mon Sep 17 00:00:00 2001 From: iChemy Date: Mon, 1 Jul 2024 20:14:53 +0900 Subject: [PATCH 5/5] :art: change nil return to empty struct --- domain/filter/shorthand.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/domain/filter/shorthand.go b/domain/filter/shorthand.go index 6835b968..d91ae875 100644 --- a/domain/filter/shorthand.go +++ b/domain/filter/shorthand.go @@ -105,7 +105,7 @@ func AddAnd(lhs, rhs Expr) Expr { // 双方がゼロのき nil を返す func FilterDuration(since, until time.Time) (Expr, error) { if since.IsZero() && until.IsZero() { - return nil, nil + return &CmpExpr{}, nil } else if since.IsZero() { return &CmpExpr{ Attr: AttrTimeStart,