-
Notifications
You must be signed in to change notification settings - Fork 14
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 #144 from inovex/timeframe-filter
feat: Timeframe Filter
- Loading branch information
Showing
6 changed files
with
148 additions
and
2 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,40 @@ | ||
package filter | ||
|
||
import ( | ||
"github.com/inovex/CalendarSync/internal/models" | ||
) | ||
|
||
type TimeFrameEvents struct { | ||
HourStart int | ||
HourEnd int | ||
} | ||
|
||
func (a TimeFrameEvents) Name() string { | ||
return "TimeFrame" | ||
} | ||
|
||
func (a TimeFrameEvents) Filter(event models.Event) bool { | ||
|
||
// if start time is inside the timeframe | ||
// example: event from 10-12, timeframe is 8-18 | ||
// starttime and endtime are inside the timeframe | ||
if event.StartTime.Hour() >= a.HourStart && event.StartTime.Hour() <= a.HourEnd { | ||
return true | ||
} | ||
|
||
// if the endtime is inside the timeframe | ||
// example: event from 7-9, timeframe is 8-18 | ||
// then the endtime of the event is inside the timeframe and therefore should be kept | ||
if event.EndTime.Hour() <= a.HourEnd && event.EndTime.Hour() >= a.HourStart { | ||
return true | ||
} | ||
|
||
// if the starttime is inside the timeframe | ||
// example: event from 17-19 timeframe is 8-18 | ||
// then the starttime of the event is inside the timeframe and therefore should be kept | ||
if event.StartTime.Hour() >= a.HourStart && event.StartTime.Hour() <= a.HourEnd { | ||
return true | ||
} | ||
|
||
return false | ||
} |
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,88 @@ | ||
package filter_test | ||
|
||
import ( | ||
"testing" | ||
"time" | ||
|
||
"github.com/inovex/CalendarSync/internal/filter" | ||
"github.com/inovex/CalendarSync/internal/models" | ||
) | ||
|
||
const timeFormat string = "2006-01-02T15:04" | ||
|
||
// Events which match the start and end hour should be kept | ||
func TestTimeFrameEventsFilter(t *testing.T) { | ||
t1, err := time.Parse(timeFormat, "2024-01-01T13:00") | ||
if err != nil { | ||
t.Error(err) | ||
} | ||
|
||
t2, err := time.Parse(timeFormat, "2024-01-01T18:00") | ||
if err != nil { | ||
t.Error(err) | ||
} | ||
|
||
t3, err := time.Parse(timeFormat, "2024-01-01T07:00") | ||
if err != nil { | ||
t.Error(err) | ||
} | ||
|
||
sourceEvents := []models.Event{ | ||
// Should be kept, as Start AND Endtime is inside the timeframe | ||
{ | ||
ICalUID: "testId", | ||
ID: "testUid", | ||
Title: "test", | ||
Description: "bar", | ||
AllDay: true, | ||
StartTime: t1, | ||
EndTime: t1.Add(time.Hour * 1), | ||
}, | ||
// Should be filtered, as the Start and EndTime is out of range | ||
{ | ||
ICalUID: "testId2", | ||
ID: "testUid2", | ||
Title: "Test 2", | ||
Description: "bar", | ||
AllDay: false, | ||
StartTime: t2, | ||
EndTime: t2.Add(time.Hour * 1), | ||
}, | ||
// Should be kept | ||
{ | ||
ICalUID: "testId3", | ||
ID: "testUid2", | ||
Title: "foo", | ||
Description: "bar", | ||
StartTime: t1, | ||
EndTime: t1.Add(time.Hour * 1), | ||
}, | ||
// Should be kept, as the end time is part of the timeframe | ||
{ | ||
ICalUID: "testId4", | ||
ID: "testUid3", | ||
Title: "foo", | ||
Description: "bar", | ||
StartTime: t3, | ||
EndTime: t3.Add(time.Hour * 1), | ||
}, | ||
// Should be kept, as the start time is part of the timeframe | ||
{ | ||
ICalUID: "testId5", | ||
ID: "testUid4", | ||
Title: "foo", | ||
Description: "bar", | ||
StartTime: t1, | ||
EndTime: t1.Add(time.Hour * 7), | ||
}, | ||
} | ||
|
||
expectedSinkEvents := []models.Event{sourceEvents[0], sourceEvents[2], sourceEvents[3], sourceEvents[4]} | ||
|
||
eventFilter := filter.TimeFrameEvents{ | ||
// Events outside 8 am and 5 pm will be filtered. | ||
HourStart: 8, | ||
HourEnd: 17, | ||
} | ||
checkEventFilter(t, eventFilter, sourceEvents, expectedSinkEvents) | ||
} |
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