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

Remove daily summary index #141

Merged
merged 6 commits into from
May 27, 2020
Merged
Show file tree
Hide file tree
Changes from 2 commits
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
91 changes: 43 additions & 48 deletions server/mscalendar/daily_summary.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,21 +30,24 @@ type DailySummary interface {
}

func (m *mscalendar) GetDailySummarySettingsForUser(user *User) (*store.DailySummaryUserSettings, error) {
dsumIndex, err := m.Store.LoadDailySummaryIndex()
err := m.Filter(withUserExpanded(user))
if err != nil {
return nil, err
}

for _, dsum := range dsumIndex {
if dsum.MattermostUserID == user.MattermostUserID {
return dsum, nil
}
dsum := user.Settings.DailySummary
if dsum == nil {
return nil, errors.New("No daily summary settings found")
}

return nil, errors.New("No daily summary settings found")
return dsum, nil
}

func (m *mscalendar) SetDailySummaryPostTime(user *User, timeStr string) (*store.DailySummaryUserSettings, error) {
err := m.Filter(withUserExpanded(user))
if err != nil {
return nil, err
}

t, err := time.Parse(time.Kitchen, timeStr)
if err != nil {
return nil, errors.New("Invalid time value: " + timeStr)
Expand All @@ -59,55 +62,48 @@ func (m *mscalendar) SetDailySummaryPostTime(user *User, timeStr string) (*store
return nil, err
}

dsum, err := m.Store.LoadDailySummaryUserSettings(user.MattermostUserID)
if err != nil {
return nil, err
}
dsum := user.Settings.DailySummary
if dsum == nil {
dsum = &store.DailySummaryUserSettings{MattermostUserID: user.MattermostUserID}
dsum = &store.DailySummaryUserSettings{}
user.Settings.DailySummary = dsum
}

dsum.PostTime = timeStr
dsum.Timezone = timezone

err = m.Store.StoreDailySummaryUserSettings(dsum)
err = m.Store.StoreUser(user.User)
if err != nil {
return nil, err
}
return dsum, nil
}

func (m *mscalendar) SetDailySummaryEnabled(user *User, enable bool) (*store.DailySummaryUserSettings, error) {
err := m.Filter(
withClient,
withUserExpanded(user),
)
err := m.Filter(withUserExpanded(user))
if err != nil {
return nil, err
}

dsum, err := m.Store.LoadDailySummaryUserSettings(user.MattermostUserID)
if err != nil {
return nil, err
}
dsum := user.Settings.DailySummary
if dsum == nil {
dsum = &store.DailySummaryUserSettings{MattermostUserID: user.MattermostUserID}
dsum = &store.DailySummaryUserSettings{}
user.Settings.DailySummary = dsum
}
dsum.Enable = enable

err = m.Store.StoreDailySummaryUserSettings(dsum)
err = m.Store.StoreUser(user.User)
if err != nil {
return nil, err
}
return dsum, nil
}

func (m *mscalendar) ProcessAllDailySummary(now time.Time) error {
dsumIndex, err := m.Store.LoadDailySummaryIndex()
userIndex, err := m.Store.LoadUserIndex()
if err != nil {
return err
}
if len(dsumIndex) == 0 {
if len(userIndex) == 0 {
return nil
}

Expand All @@ -117,10 +113,19 @@ func (m *mscalendar) ProcessAllDailySummary(now time.Time) error {
}

requests := []*remote.ViewCalendarParams{}
for _, dsum := range dsumIndex {
byRemoteID := map[string]*store.User{}
for _, user := range userIndex {
storeUser, err := m.Store.LoadUser(user.MattermostUserID)
if err != nil {
m.Logger.Warnf("Error loading user %s for daily summary. err=%v", user.MattermostUserID, err)
continue
}
byRemoteID[storeUser.Remote.ID] = storeUser

dsum := storeUser.Settings.DailySummary
shouldPost, shouldPostErr := shouldPostDailySummary(dsum, now)
if shouldPostErr != nil {
m.Logger.Warnf("Error posting daily summary for user %s. err=%v", dsum.MattermostUserID, shouldPostErr)
m.Logger.Warnf("Error posting daily summary for user %s. err=%v", user.MattermostUserID, shouldPostErr)
continue
}
if !shouldPost {
Expand All @@ -129,7 +134,7 @@ func (m *mscalendar) ProcessAllDailySummary(now time.Time) error {

start, end := getTodayHoursForTimezone(now, dsum.Timezone)
req := &remote.ViewCalendarParams{
RemoteUserID: dsum.RemoteID,
RemoteUserID: storeUser.Remote.ID,
StartTime: start,
EndTime: end,
}
Expand All @@ -141,36 +146,26 @@ func (m *mscalendar) ProcessAllDailySummary(now time.Time) error {
return err
}

mappedPostTimes := map[string]string{}
byRemoteID := dsumIndex.ByRemoteID()
for _, res := range responses {
dsum := byRemoteID[res.RemoteUserID]
user := byRemoteID[res.RemoteUserID]
if res.Error != nil {
m.Logger.Warnf("Error rendering user %s calendar. err=%s %s", dsum.MattermostUserID, res.Error.Code, res.Error.Message)
m.Logger.Warnf("Error rendering user %s calendar. err=%s %s", user.MattermostUserID, res.Error.Code, res.Error.Message)
}
dsum := user.Settings.DailySummary
postStr, err := views.RenderCalendarView(res.Events, dsum.Timezone)
if err != nil {
m.Logger.Warnf("Error rendering user %s calendar. err=%v", dsum.MattermostUserID, err)
m.Logger.Warnf("Error rendering user %s calendar. err=%v", user.MattermostUserID, err)
}

m.Poster.DM(dsum.MattermostUserID, postStr)
mappedPostTimes[dsum.MattermostUserID] = time.Now().Format(time.RFC3339)
}

modErr := m.Store.ModifyDailySummaryIndex(func(dsumIndex store.DailySummaryIndex) (store.DailySummaryIndex, error) {
for _, setting := range dsumIndex {
postTime, ok := mappedPostTimes[setting.MattermostUserID]
if ok {
setting.LastPostTime = postTime
}
m.Poster.DM(user.MattermostUserID, postStr)
dsum.LastPostTime = time.Now().Format(time.RFC3339)
err = m.Store.StoreUser(user)
if err != nil {
m.Logger.Warnf("Error storing daily summary LastPostTime for user %s. err=%v", user.MattermostUserID, err)
}
return dsumIndex, nil
})
if modErr != nil {
return modErr
}
m.Logger.Infof("Processed daily summary for %d users", len(mappedPostTimes))

m.Logger.Infof("Processed daily summary for %d users", len(responses))
return nil
}

Expand Down
103 changes: 68 additions & 35 deletions server/mscalendar/daily_summary_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,32 +27,39 @@ func TestProcessAllDailySummary(t *testing.T) {
err: "index store error",
runAssertions: func(deps *Dependencies, client remote.Client) {
s := deps.Store.(*mock_store.MockStore)
s.EXPECT().LoadDailySummaryIndex().Return(store.DailySummaryIndex{}, errors.New("index store error"))
s.EXPECT().LoadUserIndex().Return(nil, errors.New("index store error"))
},
},
{
name: "No users",
err: "",
runAssertions: func(deps *Dependencies, client remote.Client) {
s := deps.Store.(*mock_store.MockStore)
s.EXPECT().LoadDailySummaryIndex().Return(store.DailySummaryIndex{}, nil)
s.EXPECT().LoadUserIndex().Return(store.UserIndex{}, nil)
},
},
{
name: "Error fetching events",
err: "error fetching events",
runAssertions: func(deps *Dependencies, client remote.Client) {
s := deps.Store.(*mock_store.MockStore)
s.EXPECT().LoadDailySummaryIndex().Return(store.DailySummaryIndex{
&store.DailySummaryUserSettings{
MattermostUserID: "user1_mm_id",
RemoteID: "user1_remote_id",
Enable: true,
PostTime: "9:00AM",
Timezone: "Eastern Standard Time",
LastPostTime: "",
s.EXPECT().LoadUserIndex().Return(store.UserIndex{{
MattermostUserID: "user1_mm_id",
RemoteID: "user1_remote_id",
}}, nil)

s.EXPECT().LoadUser("user1_mm_id").Return(&store.User{
MattermostUserID: "user1_mm_id",
Remote: &remote.User{ID: "user1_remote_id"},
Settings: store.Settings{
DailySummary: &store.DailySummaryUserSettings{
Enable: true,
PostTime: "9:00AM",
Timezone: "Eastern Standard Time",
LastPostTime: "",
},
},
}, nil).Times(1)
}, nil)

mockClient := client.(*mock_remote.MockClient)
mockRemote := deps.Remote.(*mock_remote.MockRemote)
Expand All @@ -66,32 +73,55 @@ func TestProcessAllDailySummary(t *testing.T) {
err: "",
runAssertions: func(deps *Dependencies, client remote.Client) {
s := deps.Store.(*mock_store.MockStore)
s.EXPECT().LoadDailySummaryIndex().Return(store.DailySummaryIndex{
&store.DailySummaryUserSettings{
MattermostUserID: "user1_mm_id",
RemoteID: "user1_remote_id",
Enable: true,
PostTime: "9:00AM",
Timezone: "Eastern Standard Time",
LastPostTime: "",
s.EXPECT().LoadUserIndex().Return(store.UserIndex{{
MattermostUserID: "user1_mm_id",
RemoteID: "user1_remote_id",
}, {
MattermostUserID: "user2_mm_id",
RemoteID: "user2_remote_id",
}, {
MattermostUserID: "user3_mm_id",
RemoteID: "user3_remote_id",
}}, nil)

s.EXPECT().LoadUser("user1_mm_id").Return(&store.User{
MattermostUserID: "user1_mm_id",
Remote: &remote.User{ID: "user1_remote_id"},
Settings: store.Settings{
DailySummary: &store.DailySummaryUserSettings{
Enable: true,
PostTime: "9:00AM",
Timezone: "Eastern Standard Time",
LastPostTime: "",
},
},
&store.DailySummaryUserSettings{
MattermostUserID: "user2_mm_id",
RemoteID: "user2_remote_id",
Enable: true,
PostTime: "6:00AM",
Timezone: "Pacific Standard Time",
LastPostTime: "",
}, nil)

s.EXPECT().LoadUser("user2_mm_id").Return(&store.User{
MattermostUserID: "user2_mm_id",
Remote: &remote.User{ID: "user2_remote_id"},
Settings: store.Settings{
DailySummary: &store.DailySummaryUserSettings{
Enable: true,
PostTime: "6:00AM",
Timezone: "Pacific Standard Time",
LastPostTime: "",
},
},
&store.DailySummaryUserSettings{
MattermostUserID: "user3_mm_id",
RemoteID: "user3_remote_id",
Enable: true,
PostTime: "10:00AM", // should not receive summary
Timezone: "Pacific Standard Time",
LastPostTime: "",
}, nil)

s.EXPECT().LoadUser("user3_mm_id").Return(&store.User{
MattermostUserID: "user3_mm_id",
Remote: &remote.User{ID: "user3_remote_id"},
Settings: store.Settings{
DailySummary: &store.DailySummaryUserSettings{
Enable: true,
PostTime: "10:00AM", // should not receive summary
Timezone: "Pacific Standard Time",
LastPostTime: "",
},
},
}, nil).Times(1)
}, nil)

mockClient := client.(*mock_remote.MockClient)
loc, err := time.LoadLocation("MST")
Expand Down Expand Up @@ -122,7 +152,10 @@ Wednesday February 12
| 9:00AM - 11:00AM | [The subject]() |`).Return("postID2", nil).Times(1),
)

s.EXPECT().ModifyDailySummaryIndex(gomock.Any()).Return(nil)
s.EXPECT().StoreUser(gomock.Any()).Times(2).DoAndReturn(func(u *store.User) error {
require.NotEmpty(t, u.Settings.DailySummary.LastPostTime)
return nil
})

mockLogger := deps.Logger.(*mock_bot.MockLogger)
mockLogger.EXPECT().Infof("Processed daily summary for %d users", 2)
Expand Down
5 changes: 0 additions & 5 deletions server/mscalendar/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,11 +152,6 @@ func (m *mscalendar) DisconnectUser(mattermostUserID string) error {
return err
}

err = m.Store.DeleteDailySummaryUserSettings(mattermostUserID)
if err != nil {
return err
}

return nil
}

Expand Down
Loading