From ded4540173de5db8f72f99473f3be971e26c6daa Mon Sep 17 00:00:00 2001 From: Ulli Goschler <770135+nce@users.noreply.github.com> Date: Tue, 29 Nov 2022 18:00:26 +0100 Subject: [PATCH] feat: gather only relevant events (no travels/events) --- icsparser/daily.go | 38 ++++++++++++++++++++++---------------- icsparser/parser.go | 7 +++---- main.go | 3 ++- 3 files changed, 27 insertions(+), 21 deletions(-) diff --git a/icsparser/daily.go b/icsparser/daily.go index 7bbcd84..5d2170a 100644 --- a/icsparser/daily.go +++ b/icsparser/daily.go @@ -4,63 +4,69 @@ import ( "fmt" "time" "errors" + "strings" "github.com/nce/ics2mattermost/logger" ) type DailyIngest struct { EventsToday []Event - TravellingPersons string - AbsentPersons string + TravellingPersons []string + AbsentPersons []string } -func (c *Calendar) PrepareDailyIngest() (map[string]string, error) { - - logger.Info(fmt.Sprintf("amount of meetings: %d", len(c.Events))) +func (c *Calendar) gatherRelevantEvents() DailyIngest { - ingest := DailyIngest{ - EventsToday: []Event{}, - TravellingPersons: "*no one*", - AbsentPersons: "*no one*", - } + var ingest DailyIngest for _, event := range c.Events { travelers, err := event.GetPersonsByCategory("travel") if err == nil { ingest.TravellingPersons = travelers + continue } absents, err := event.GetPersonsByCategory("leaves") if err == nil { ingest.AbsentPersons = absents + continue } + + ingest.EventsToday = append(ingest.EventsToday, event) } + return ingest +} + +func (c *Calendar) PrepareDailyIngest() (map[string]string, error) { + + var err error - //ingest.Daily, err = c.GetEventByName("DAILY (ALL)") - if len(c.Events) == 0 { + ingest := c.gatherRelevantEvents() + if len(ingest.EventsToday) == 0 { logger.Error(err.Error()) return nil, errors.New("no events today") } else { + logger.Info(fmt.Sprintf("amount of meetings: %d", len(ingest.EventsToday))) loc := time.Local var formattedEvents string - for _, e := range c.Events { + for _, e := range ingest.EventsToday { formattedEvents = formattedEvents + ":calendar: " + e.Start.In(loc).Format("15:04") + " - " + e.End.In(loc).Format("15:04 MST") + " :fire: [" + e.Summary + "](" + e.Location + ")\n" } dailyMessage := map[string]string{ "name": "Foobar", - "text": "#### Welcome to today's daily ingest\n" + + "text": "#### Welcome to VRP's daily ingest\n" + formattedEvents + - ":airplane: " + ingest.TravellingPersons + "\n" + - ":palm_tree: " + ingest.AbsentPersons, + ":airplane: " + strings.Join(ingest.TravellingPersons, ", ") + "\n" + + ":palm_tree: " + strings.Join(ingest.AbsentPersons, ", "), } // logger.Info( diff --git a/icsparser/parser.go b/icsparser/parser.go index 0d4863d..5a65e9e 100644 --- a/icsparser/parser.go +++ b/icsparser/parser.go @@ -3,7 +3,6 @@ package icsparser import ( "bytes" "io" - "strings" "errors" "github.com/apognu/gocal" @@ -117,7 +116,7 @@ func (c *Calendar) GetEventByName(eventName string) (Event, error) { errors.New("no event found") } -func (e *Event) GetPersonsByCategory(calendarCategory string) (string, error) { +func (e *Event) GetPersonsByCategory(calendarCategory string) ([]string, error) { var attendees []string for _, category := range e.Categories { @@ -132,10 +131,10 @@ func (e *Event) GetPersonsByCategory(calendarCategory string) (string, error) { } if len(attendees) == 0 { - return "", errors.New("no attendees") + return nil, errors.New("no attendees") } - return strings.Join(attendees, ", "), nil + return attendees, nil } diff --git a/main.go b/main.go index 4a1b3bc..490d3ee 100644 --- a/main.go +++ b/main.go @@ -43,7 +43,8 @@ func main() { s := gocron.NewScheduler(time.Local) - s.Every("1h").Do(func() { + //s.Every(1).Weeks().Monday().Tuesday().Wednesday().Thursday().At("8:30").Do(func() { + s.Every(20).Seconds().Do(func() { cal := icsparser.Setup( icsUrl,