From 30d572000aff3c3a6db9356152b92833680b0f7f Mon Sep 17 00:00:00 2001 From: Alex Sahin Date: Tue, 25 Jun 2019 17:34:28 +0100 Subject: [PATCH] MM-16521 Migrate "Status.GetTotalActiveUsersCount" to Sync by default (#11401) * initial code * remove repeated var declaration --- app/security_update_check.go | 4 ++-- store/sqlstore/status_store.go | 17 +++++++---------- store/store.go | 2 +- store/storetest/mocks/StatusStore.go | 19 +++++++++++++------ store/storetest/status_store.go | 5 ++--- 5 files changed, 25 insertions(+), 22 deletions(-) diff --git a/app/security_update_check.go b/app/security_update_check.go index a338edfdf7..47a8ba2558 100644 --- a/app/security_update_check.go +++ b/app/security_update_check.go @@ -75,8 +75,8 @@ func (s *Server) DoSecurityUpdateCheck() { v.Set(PROP_SECURITY_USER_COUNT, strconv.FormatInt(ucr.Data.(int64), 10)) } - if ucr := <-s.Store.Status().GetTotalActiveUsersCount(); ucr.Err == nil { - v.Set(PROP_SECURITY_ACTIVE_USER_COUNT, strconv.FormatInt(ucr.Data.(int64), 10)) + if ucr, err := s.Store.Status().GetTotalActiveUsersCount(); err == nil { + v.Set(PROP_SECURITY_ACTIVE_USER_COUNT, strconv.FormatInt(ucr, 10)) } if teamCount, err := s.Store.Team().AnalyticsTeamCount(); err == nil { diff --git a/store/sqlstore/status_store.go b/store/sqlstore/status_store.go index 34e740a3b9..d8926d6e53 100644 --- a/store/sqlstore/status_store.go +++ b/store/sqlstore/status_store.go @@ -136,16 +136,13 @@ func (s SqlStatusStore) ResetAll() store.StoreChannel { }) } -func (s SqlStatusStore) GetTotalActiveUsersCount() store.StoreChannel { - return store.Do(func(result *store.StoreResult) { - time := model.GetMillis() - (1000 * 60 * 60 * 24) - - if count, err := s.GetReplica().SelectInt("SELECT COUNT(UserId) FROM Status WHERE LastActivityAt > :Time", map[string]interface{}{"Time": time}); err != nil { - result.Err = model.NewAppError("SqlStatusStore.GetTotalActiveUsersCount", "store.sql_status.get_total_active_users_count.app_error", nil, err.Error(), http.StatusInternalServerError) - } else { - result.Data = count - } - }) +func (s SqlStatusStore) GetTotalActiveUsersCount() (int64, *model.AppError) { + time := model.GetMillis() - (1000 * 60 * 60 * 24) + count, err := s.GetReplica().SelectInt("SELECT COUNT(UserId) FROM Status WHERE LastActivityAt > :Time", map[string]interface{}{"Time": time}) + if err != nil { + return count, model.NewAppError("SqlStatusStore.GetTotalActiveUsersCount", "store.sql_status.get_total_active_users_count.app_error", nil, err.Error(), http.StatusInternalServerError) + } + return count, nil } func (s SqlStatusStore) UpdateLastActivityAt(userId string, lastActivityAt int64) store.StoreChannel { diff --git a/store/store.go b/store/store.go index 6f3164d4a9..b018a0d349 100644 --- a/store/store.go +++ b/store/store.go @@ -474,7 +474,7 @@ type StatusStore interface { GetOnline() ([]*model.Status, *model.AppError) GetAllFromTeam(teamId string) ([]*model.Status, *model.AppError) ResetAll() StoreChannel - GetTotalActiveUsersCount() StoreChannel + GetTotalActiveUsersCount() (int64, *model.AppError) UpdateLastActivityAt(userId string, lastActivityAt int64) StoreChannel } diff --git a/store/storetest/mocks/StatusStore.go b/store/storetest/mocks/StatusStore.go index 02fe2f2e79..f1945afc56 100644 --- a/store/storetest/mocks/StatusStore.go +++ b/store/storetest/mocks/StatusStore.go @@ -112,19 +112,26 @@ func (_m *StatusStore) GetOnlineAway() store.StoreChannel { } // GetTotalActiveUsersCount provides a mock function with given fields: -func (_m *StatusStore) GetTotalActiveUsersCount() store.StoreChannel { +func (_m *StatusStore) GetTotalActiveUsersCount() (int64, *model.AppError) { ret := _m.Called() - var r0 store.StoreChannel - if rf, ok := ret.Get(0).(func() store.StoreChannel); ok { + var r0 int64 + if rf, ok := ret.Get(0).(func() int64); ok { r0 = rf() } else { - if ret.Get(0) != nil { - r0 = ret.Get(0).(store.StoreChannel) + r0 = ret.Get(0).(int64) + } + + var r1 *model.AppError + if rf, ok := ret.Get(1).(func() *model.AppError); ok { + r1 = rf() + } else { + if ret.Get(1) != nil { + r1 = ret.Get(1).(*model.AppError) } } - return r0 + return r0, r1 } // ResetAll provides a mock function with given fields: diff --git a/store/storetest/status_store.go b/store/storetest/status_store.go index ef3645c5b0..7a3e5a3c5a 100644 --- a/store/storetest/status_store.go +++ b/store/storetest/status_store.go @@ -88,10 +88,9 @@ func testActiveUserCount(t *testing.T, ss store.Store) { status := &model.Status{UserId: model.NewId(), Status: model.STATUS_ONLINE, Manual: false, LastActivityAt: model.GetMillis(), ActiveChannel: ""} require.Nil(t, ss.Status().SaveOrUpdate(status)) - if result := <-ss.Status().GetTotalActiveUsersCount(); result.Err != nil { - t.Fatal(result.Err) + if count, err := ss.Status().GetTotalActiveUsersCount(); err != nil { + t.Fatal(err) } else { - count := result.Data.(int64) require.True(t, count > 0, "expected count > 0, got %d", count) } }