Skip to content

Commit

Permalink
Merge pull request #144 from inovex/timeframe-filter
Browse files Browse the repository at this point in the history
feat: Timeframe Filter
  • Loading branch information
MichaelEischer authored Jul 5, 2024
2 parents c616e7f + 29b18db commit 81a3ed4
Show file tree
Hide file tree
Showing 6 changed files with 148 additions and 2 deletions.
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ asdf plugin add calendarsync https://github.com/FeryET/asdf-calendarsync.git
## finally
asdf install calendarsync
```
Note: The `asdf` plugin is not managed by inovex, but is provided by a CalendarSync user. inovex assumes no responsibility for proper provisioning.
Note: The `asdf` plugin is not managed by inovex, but is provided by a CalendarSync user. inovex assumes no responsibility for proper provisioning.

## First Time Execution

Expand Down Expand Up @@ -167,6 +167,12 @@ filters:
- name: DeclinedEvents
# Events which cover the full day aren't synced
- name: AllDayEvents
# Events within the specified timeframe will be retained, while all others will be filtered out.
# hours are represented in the 24h time format
- name: TimeFrame
config:
HourStart: 8
HourEnd: 17
# Events where the title matches the ExcludeRegexp (RE2 Regex) aren't synced
- name: RegexTitle
config:
Expand Down
6 changes: 6 additions & 0 deletions example.sync.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,12 @@ filters:
- name: DeclinedEvents
# Events which cover the full day aren't synced
- name: AllDayEvents
# Events within the specified timeframe will be retained, while all others will be filtered out.
# hours are represented in the 24h time format
- name: TimeFrame
config:
HourStart: 8
HourEnd: 17
# Events where the title matches the ExcludeRegexp (RE2 Regex) aren't synced
- name: RegexTitle
config:
Expand Down
40 changes: 40 additions & 0 deletions internal/filter/timeFrame.go
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
}
88 changes: 88 additions & 0 deletions internal/filter/timeFrame_test.go
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)
}
2 changes: 2 additions & 0 deletions internal/sync/filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,14 @@ func FilterEvent(event models.Event, filters ...Filter) (result bool) {

var (
filterConfigMapping = map[string]Filter{
"TimeFrame": &filter.TimeFrameEvents{},
"DeclinedEvents": &filter.DeclinedEvents{},
"AllDayEvents": &filter.AllDayEvents{},
"RegexTitle": &filter.RegexTitle{},
}

filterOrder = []string{
"TimeFrame",
"DeclinedEvents",
"AllDayEvents",
"RegexTitle",
Expand Down
6 changes: 5 additions & 1 deletion internal/sync/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,11 @@ func autoConfigure(object any, config config.CustomMap) {
reflect.Int16,
reflect.Int32,
reflect.Int64:
field.SetInt(value.(int64))
if v, ok := value.(int); ok {
field.SetInt(int64(v))
} else {
field.SetInt(value.(int64))
}
case reflect.Bool:
field.SetBool(value.(bool))
case reflect.String:
Expand Down

0 comments on commit 81a3ed4

Please sign in to comment.