Skip to content
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

✨ add FilterDuration and replace FilterTime in InitPostEvent… #544

Merged
merged 5 commits into from
Jul 1, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 82 additions & 0 deletions domain/filter/shorthand.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

これをORで繋ぐことを明記すると良いと思います

Suggested change
// 期間中に始まる
// time_start >= min AND time_start < max
//
// 期間中に終わる
// time_end >= min AND time_end < max
//
// 期間より前に始まり期間より後に終わる
// time_start < min AND time_end >= max
// 以下のいずれかを満たす
// - 期間中に始まる (time_start >= min AND time_start < max)
// - 期間中に終わる (time_end >= min AND time_end < max)
// - 期間より前に始まり期間より後に終わる (time_start < min AND time_end >= max)

//
// min < max であるべき
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

これは直接コードで!min.IsZero() && !max.IsZero()であることを確認した上で時系列を判定するとよさそうです

func FilterDuration(min, max time.Time) Expr {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

最近minとmaxがbuiltinの関数名に追加されたので他の名前にしておくと余計な誤解を招かなくてよいです(動きはするんですが)

since, untilなど

startAfterMin := &CmpExpr{
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ここもstartAfterSinceとかにしたいが流石に文法に違和感があるのであまりいい名前が思いつかない
変に変数名付けずにstartInの中に直接入れてもそこまで分かりにくくはならないとも思ったり

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)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

addOr使わずに直接書いた方が見やすそう(この関数内のAndはそうしてるし)

}
6 changes: 3 additions & 3 deletions utils/cron.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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"),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Format("01/02:15:04")でよさそう
あと日付と時間の区切りに:を見るのはあまり見ないので空白区切りがいいと思います

event.Room.Place, combined[event.AllowTogether])
}

Expand Down
Loading