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

[MI-2705]:Added summary for status sync job #4

Merged
merged 6 commits into from
Feb 6, 2023
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
4 changes: 2 additions & 2 deletions server/command/availability.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ package command
func (c *Command) debugAvailability(parameters ...string) (string, bool, error) {
switch {
case len(parameters) == 0:
resString, err := c.MSCalendar.Sync(c.Args.UserId)
resString, _, err := c.MSCalendar.Sync(c.Args.UserId)
if err != nil {
return "", false, err
}

return resString, false, nil
case len(parameters) == 1 && parameters[0] == "all":
resString, err := c.MSCalendar.SyncAll()
resString, _, err := c.MSCalendar.SyncAll()
if err != nil {
return "", false, err
}
Expand Down
4 changes: 2 additions & 2 deletions server/jobs/status_sync_job.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@ func NewStatusSyncJob() RegisteredJob {
func runSyncJob(env mscalendar.Env) {
env.Logger.Debugf("User status sync job beginning")

_, err := mscalendar.New(env, "").SyncAll()
_, syncJobSummary, err := mscalendar.New(env, "").SyncAll()
if err != nil {
env.Logger.Errorf("Error during user status sync job. err=%v", err)
}

env.Logger.Debugf("User status sync job finished")
env.Logger.Debugf("User status sync job finished. Summary\nNumber of users processed:- %d\nNumber of users had their status changed:- %d\nNumber of users had errors:- %d", syncJobSummary.NumberOfUserProcessed, syncJobSummary.NumberOfUserStatusChange, syncJobSummary.NumberOfUserErrorInStatusChange)
Kshitij-Katiyar marked this conversation as resolved.
Show resolved Hide resolved
}
99 changes: 60 additions & 39 deletions server/mscalendar/availability.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,42 +26,50 @@ const (
logTruncateLimit = 5
)

type StatusSyncJobSummary struct {
NumberOfUserErrorInStatusChange int
NumberOfUserStatusChange int
NumberOfUserProcessed int
Kshitij-Katiyar marked this conversation as resolved.
Show resolved Hide resolved
}

type Availability interface {
GetCalendarViews(users []*store.User) ([]*remote.ViewCalendarResponse, error)
Sync(mattermostUserID string) (string, error)
SyncAll() (string, error)
Sync(mattermostUserID string) (string, *StatusSyncJobSummary, error)
SyncAll() (string, *StatusSyncJobSummary, error)
}

func (m *mscalendar) Sync(mattermostUserID string) (string, error) {
func (m *mscalendar) Sync(mattermostUserID string) (string, *StatusSyncJobSummary, error) {
user, err := m.Store.LoadUserFromIndex(mattermostUserID)
if err != nil {
return "", err
return "", nil, err
}

return m.syncUsers(store.UserIndex{user})
}

func (m *mscalendar) SyncAll() (string, error) {
func (m *mscalendar) SyncAll() (string, *StatusSyncJobSummary, error) {
err := m.Filter(withSuperuserClient)
if err != nil {
return "", errors.Wrap(err, "not able to filter the super user client.")
return "", nil, errors.Wrap(err, "not able to filter the super user client.")
}

userIndex, err := m.Store.LoadUserIndex()
if err != nil {
if err.Error() == "not found" {
return "No users found in user index", nil
return "No users found in user index", nil, nil
ayusht2810 marked this conversation as resolved.
Show resolved Hide resolved
}
return "", errors.Wrap(err, "not able to load users from user index.")
return "", nil, errors.Wrap(err, "not able to load users from user index.")
Kshitij-Katiyar marked this conversation as resolved.
Show resolved Hide resolved
}

return m.syncUsers(userIndex)
}

func (m *mscalendar) syncUsers(userIndex store.UserIndex) (string, error) {
func (m *mscalendar) syncUsers(userIndex store.UserIndex) (string, *StatusSyncJobSummary, error) {
summarySyncJob := &StatusSyncJobSummary{}
Kshitij-Katiyar marked this conversation as resolved.
Show resolved Hide resolved
if len(userIndex) == 0 {
return "No connected users found", nil
return "No connected users found", summarySyncJob, nil
Kshitij-Katiyar marked this conversation as resolved.
Show resolved Hide resolved
}
summarySyncJob.NumberOfUserProcessed = len(userIndex)

numberOfLogs := 0
users := []*store.User{}
Expand All @@ -84,24 +92,27 @@ func (m *mscalendar) syncUsers(userIndex store.UserIndex) (string, error) {
}
}
if len(users) == 0 {
return "No users need to be synced", nil
return "No users need to be synced", summarySyncJob, nil
}

calendarViews, err := m.GetCalendarViews(users)
if err != nil {
return "", errors.Wrap(err, "not able to get calendar views for connected users.")
return "", summarySyncJob, errors.Wrap(err, "not able to get calendar views for connected users.")
}
if len(calendarViews) == 0 {
return "No calendar views found", nil
return "No calendar views found", summarySyncJob, nil
}

m.deliverReminders(users, calendarViews)
out, err := m.setUserStatuses(users, calendarViews)
out, numberOfUserStatusChange, numberOfUserErrorInStatusChange, err := m.setUserStatuses(users, calendarViews)
if err != nil {
return "", errors.Wrap(err, "error setting the user statuses.")
return "", summarySyncJob, errors.Wrap(err, "error setting the user statuses.")
}

return out, nil
summarySyncJob.NumberOfUserErrorInStatusChange = numberOfUserErrorInStatusChange
Kshitij-Katiyar marked this conversation as resolved.
Show resolved Hide resolved
Kshitij-Katiyar marked this conversation as resolved.
Show resolved Hide resolved
summarySyncJob.NumberOfUserStatusChange = numberOfUserStatusChange

return out, summarySyncJob, nil
}

func (m *mscalendar) deliverReminders(users []*store.User, calendarViews []*remote.ViewCalendarResponse) {
Expand Down Expand Up @@ -141,16 +152,16 @@ func (m *mscalendar) deliverReminders(users []*store.User, calendarViews []*remo
}
}

func (m *mscalendar) setUserStatuses(users []*store.User, calendarViews []*remote.ViewCalendarResponse) (string, error) {
numberOfLogs := 0
func (m *mscalendar) setUserStatuses(users []*store.User, calendarViews []*remote.ViewCalendarResponse) (string, int, int, error) {
numberOfLogs, numberOfUserStatusChange, numberOfUserErrorInStatusChange := 0, 0, 0
toUpdate := []*store.User{}
for _, u := range users {
if u.Settings.UpdateStatus {
toUpdate = append(toUpdate, u)
}
}
if len(toUpdate) == 0 {
return "No users want their status updated", nil
return "No users want their status updated", numberOfUserStatusChange, numberOfUserErrorInStatusChange, nil
}

mattermostUserIDs := []string{}
Expand All @@ -162,7 +173,7 @@ func (m *mscalendar) setUserStatuses(users []*store.User, calendarViews []*remot

statuses, appErr := m.PluginAPI.GetMattermostUserStatusesByIds(mattermostUserIDs)
if appErr != nil {
return "", errors.Wrap(appErr, "error in getting Mattermost user statuses for connected users.")
return "", numberOfUserStatusChange, numberOfUserErrorInStatusChange, errors.Wrap(appErr, "error in getting Mattermost user statuses for connected users.")
}
statusMap := map[string]*model.Status{}
for _, s := range statuses {
Expand All @@ -171,6 +182,7 @@ func (m *mscalendar) setUserStatuses(users []*store.User, calendarViews []*remot

var res string
for _, view := range calendarViews {
isStatusChange := false
Kshitij-Katiyar marked this conversation as resolved.
Show resolved Hide resolved
user, ok := usersByRemoteID[view.RemoteUserID]
if !ok {
continue
Expand All @@ -182,6 +194,7 @@ func (m *mscalendar) setUserStatuses(users []*store.User, calendarViews []*remot
m.Logger.Warnf(logTruncateMsg)
}
numberOfLogs++
numberOfUserErrorInStatusChange++
continue
}

Expand All @@ -192,27 +205,32 @@ func (m *mscalendar) setUserStatuses(users []*store.User, calendarViews []*remot
}

var err error
res, err = m.setStatusFromCalendarView(user, status, view)
res, isStatusChange, err = m.setStatusFromCalendarView(user, status, view)
if err != nil {
if numberOfLogs < logTruncateLimit {
m.Logger.Warnf("Error setting user %s status. err=%v", user.MattermostUserID, err)
} else if numberOfLogs == logTruncateLimit {
m.Logger.Warnf(logTruncateMsg)
}
numberOfLogs++
numberOfUserErrorInStatusChange++
}
if isStatusChange {
numberOfUserStatusChange++
}
numberOfLogs++
}
if res != "" {
return res, nil
return res, numberOfUserStatusChange, numberOfUserErrorInStatusChange, nil
}

return utils.JSONBlock(calendarViews), nil
return utils.JSONBlock(calendarViews), numberOfUserStatusChange, numberOfUserErrorInStatusChange, nil
}

func (m *mscalendar) setStatusFromCalendarView(user *store.User, status *model.Status, res *remote.ViewCalendarResponse) (string, error) {
func (m *mscalendar) setStatusFromCalendarView(user *store.User, status *model.Status, res *remote.ViewCalendarResponse) (string, bool, error) {
isStatusChange := false
currentStatus := status.Status
if currentStatus == model.STATUS_OFFLINE && !user.Settings.GetConfirmation {
return "User offline and does not want status change confirmations. No status change", nil
return "User offline and does not want status change confirmations. No status change", isStatusChange, nil
Kshitij-Katiyar marked this conversation as resolved.
Show resolved Hide resolved
}

events := filterBusyEvents(res.Events)
Expand All @@ -222,7 +240,7 @@ func (m *mscalendar) setStatusFromCalendarView(user *store.User, status *model.S
}

if len(user.ActiveEvents) == 0 && len(events) == 0 {
return "No events in local or remote. No status change.", nil
return "No events in local or remote. No status change.", isStatusChange, nil
}

if len(user.ActiveEvents) > 0 && len(events) == 0 {
Expand All @@ -234,15 +252,16 @@ func (m *mscalendar) setStatusFromCalendarView(user *store.User, status *model.S
}
err := m.setStatusOrAskUser(user, status, events, true)
if err != nil {
return "", errors.Wrapf(err, "error in setting user status for user %s", user.MattermostUserID)
return "", isStatusChange, errors.Wrapf(err, "error in setting user status for user %s", user.MattermostUserID)
}
isStatusChange = true
}

err := m.Store.StoreUserActiveEvents(user.MattermostUserID, []string{})
if err != nil {
return "", errors.Wrapf(err, "error in storing active events for user %s", user.MattermostUserID)
return "", isStatusChange, errors.Wrapf(err, "error in storing active events for user %s", user.MattermostUserID)
}
return message, nil
return message, isStatusChange, nil
}

remoteHashes := []string{}
Expand All @@ -264,19 +283,20 @@ func (m *mscalendar) setStatusFromCalendarView(user *store.User, status *model.S
m.Store.StoreUser(user)
err = m.Store.StoreUserActiveEvents(user.MattermostUserID, remoteHashes)
if err != nil {
return "", errors.Wrapf(err, "error in storing active events for user %s", user.MattermostUserID)
return "", isStatusChange, errors.Wrapf(err, "error in storing active events for user %s", user.MattermostUserID)
}
return "User was already marked as busy. No status change.", nil
return "User was already marked as busy. No status change.", isStatusChange, nil
}
err = m.setStatusOrAskUser(user, status, events, false)
if err != nil {
return "", errors.Wrapf(err, "error in setting user status for user %s", user.MattermostUserID)
return "", isStatusChange, errors.Wrapf(err, "error in setting user status for user %s", user.MattermostUserID)
}
isStatusChange = true
err = m.Store.StoreUserActiveEvents(user.MattermostUserID, remoteHashes)
if err != nil {
return "", errors.Wrapf(err, "error in storing active events for user %s", user.MattermostUserID)
return "", isStatusChange, errors.Wrapf(err, "error in storing active events for user %s", user.MattermostUserID)
}
return fmt.Sprintf("User was free, but is now busy (%s). Set status to busy.", busyStatus), nil
return fmt.Sprintf("User was free, but is now busy (%s). Set status to busy.", busyStatus), isStatusChange, nil
}

newEventExists := false
Expand All @@ -295,24 +315,25 @@ func (m *mscalendar) setStatusFromCalendarView(user *store.User, status *model.S
}

if !newEventExists {
return fmt.Sprintf("No change in active events. Total number of events: %d", len(events)), nil
return fmt.Sprintf("No change in active events. Total number of events: %d", len(events)), isStatusChange, nil
}

message := "User is already busy. No status change."
if currentStatus != busyStatus {
err := m.setStatusOrAskUser(user, status, events, false)
if err != nil {
return "", errors.Wrapf(err, "error in setting user status for user %s", user.MattermostUserID)
return "", isStatusChange, errors.Wrapf(err, "error in setting user status for user %s", user.MattermostUserID)
}
isStatusChange = true
message = fmt.Sprintf("User was free, but is now busy. Set status to busy (%s).", busyStatus)
}

err := m.Store.StoreUserActiveEvents(user.MattermostUserID, remoteHashes)
if err != nil {
return "", errors.Wrapf(err, "error in storing active events for user %s", user.MattermostUserID)
return "", isStatusChange, errors.Wrapf(err, "error in storing active events for user %s", user.MattermostUserID)
}

return message, nil
return message, isStatusChange, nil
}

// setStatusOrAskUser to which status change, and whether it should update the status automatically or ask the user.
Expand Down
6 changes: 3 additions & 3 deletions server/mscalendar/availability_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ func TestSyncStatusAll(t *testing.T) {
}

m := New(env, "")
res, err := m.SyncAll()
res, _, err := m.SyncAll()
require.Nil(t, err)
require.NotEmpty(t, res)
})
Expand Down Expand Up @@ -223,7 +223,7 @@ func TestSyncStatusUserConfig(t *testing.T) {
tc.runAssertions(env.Dependencies, client)

mscalendar := New(env, "")
_, err := mscalendar.SyncAll()
_, _, err := mscalendar.SyncAll()
require.Nil(t, err)
})
}
Expand Down Expand Up @@ -321,7 +321,7 @@ func TestReminders(t *testing.T) {
}

m := New(env, "")
res, err := m.SyncAll()
res, _, err := m.SyncAll()
require.Nil(t, err)
require.NotEmpty(t, res)
})
Expand Down
14 changes: 8 additions & 6 deletions server/mscalendar/mock_mscalendar/mock_mscalendar.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.