diff --git a/app/analytics.go b/app/analytics.go index f9f5f7212c..6c248ffeea 100644 --- a/app/analytics.go +++ b/app/analytics.go @@ -59,7 +59,12 @@ func (a *App) GetAnalytics(name string, teamId string) (model.AnalyticsRows, *mo var userChan store.StoreChannel var userInactiveChan store.StoreChannel if teamId == "" { - userInactiveChan = a.Srv.Store.User().AnalyticsGetInactiveUsersCount() + userInactiveChan = make(chan store.StoreResult, 1) + go func() { + count, err := a.Srv.Store.User().AnalyticsGetInactiveUsersCount() + userInactiveChan <- store.StoreResult{Data: count, Err: err} + close(userInactiveChan) + }() } else { userChan := make(chan store.StoreResult, 1) go func() { diff --git a/app/diagnostics.go b/app/diagnostics.go index ddf7f456a7..33cef3eff5 100644 --- a/app/diagnostics.go +++ b/app/diagnostics.go @@ -157,8 +157,8 @@ func (a *App) trackActivity() { botAccountsCount = count } - if iucr := <-a.Srv.Store.User().AnalyticsGetInactiveUsersCount(); iucr.Err == nil { - inactiveUserCount = iucr.Data.(int64) + if iucr, err := a.Srv.Store.User().AnalyticsGetInactiveUsersCount(); err == nil { + inactiveUserCount = iucr } teamCount, err := a.Srv.Store.Team().AnalyticsTeamCount() diff --git a/store/sqlstore/user_store.go b/store/sqlstore/user_store.go index c8c9159f21..592379ae14 100644 --- a/store/sqlstore/user_store.go +++ b/store/sqlstore/user_store.go @@ -1417,14 +1417,12 @@ func (us SqlUserStore) performSearch(query sq.SelectBuilder, term string, option return result } -func (us SqlUserStore) AnalyticsGetInactiveUsersCount() store.StoreChannel { - return store.Do(func(result *store.StoreResult) { - if count, err := us.GetReplica().SelectInt("SELECT COUNT(Id) FROM Users WHERE DeleteAt > 0"); err != nil { - result.Err = model.NewAppError("SqlUserStore.AnalyticsGetInactiveUsersCount", "store.sql_user.analytics_get_inactive_users_count.app_error", nil, err.Error(), http.StatusInternalServerError) - } else { - result.Data = count - } - }) +func (us SqlUserStore) AnalyticsGetInactiveUsersCount() (int64, *model.AppError) { + count, err := us.GetReplica().SelectInt("SELECT COUNT(Id) FROM Users WHERE DeleteAt > 0") + if err != nil { + return int64(0), model.NewAppError("SqlUserStore.AnalyticsGetInactiveUsersCount", "store.sql_user.analytics_get_inactive_users_count.app_error", nil, err.Error(), http.StatusInternalServerError) + } + return count, nil } func (us SqlUserStore) AnalyticsGetSystemAdminCount() store.StoreChannel { diff --git a/store/store.go b/store/store.go index f391eed420..1dfc0ec3f5 100644 --- a/store/store.go +++ b/store/store.go @@ -293,7 +293,7 @@ type UserStore interface { SearchInChannel(channelId string, term string, options *model.UserSearchOptions) StoreChannel SearchNotInChannel(teamId string, channelId string, term string, options *model.UserSearchOptions) StoreChannel SearchWithoutTeam(term string, options *model.UserSearchOptions) ([]*model.User, *model.AppError) - AnalyticsGetInactiveUsersCount() StoreChannel + AnalyticsGetInactiveUsersCount() (int64, *model.AppError) AnalyticsGetSystemAdminCount() StoreChannel GetProfilesNotInTeam(teamId string, groupConstrained bool, offset int, limit int, viewRestrictions *model.ViewUsersRestrictions) StoreChannel GetEtagForProfilesNotInTeam(teamId string) StoreChannel diff --git a/store/storetest/mocks/UserStore.go b/store/storetest/mocks/UserStore.go index 8bac6ed63c..9aa9265517 100644 --- a/store/storetest/mocks/UserStore.go +++ b/store/storetest/mocks/UserStore.go @@ -30,19 +30,26 @@ func (_m *UserStore) AnalyticsActiveCount(time int64) store.StoreChannel { } // AnalyticsGetInactiveUsersCount provides a mock function with given fields: -func (_m *UserStore) AnalyticsGetInactiveUsersCount() store.StoreChannel { +func (_m *UserStore) AnalyticsGetInactiveUsersCount() (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 } // AnalyticsGetSystemAdminCount provides a mock function with given fields: diff --git a/store/storetest/user_store.go b/store/storetest/user_store.go index 35b2d4860d..b234c0d07c 100644 --- a/store/storetest/user_store.go +++ b/store/storetest/user_store.go @@ -3229,12 +3229,9 @@ func testUserStoreAnalyticsGetInactiveUsersCount(t *testing.T, ss store.Store) { store.Must(ss.User().Save(u1)) defer func() { require.Nil(t, ss.User().PermanentDelete(u1.Id)) }() - var count int64 - - if result := <-ss.User().AnalyticsGetInactiveUsersCount(); result.Err != nil { - t.Fatal(result.Err) - } else { - count = result.Data.(int64) + count, err := ss.User().AnalyticsGetInactiveUsersCount() + if err != nil { + t.Fatal(err) } u2 := &model.User{} @@ -3243,13 +3240,13 @@ func testUserStoreAnalyticsGetInactiveUsersCount(t *testing.T, ss store.Store) { store.Must(ss.User().Save(u2)) defer func() { require.Nil(t, ss.User().PermanentDelete(u2.Id)) }() - if result := <-ss.User().AnalyticsGetInactiveUsersCount(); result.Err != nil { - t.Fatal(result.Err) - } else { - newCount := result.Data.(int64) - if count != newCount-1 { - t.Fatal("Expected 1 more inactive users but found otherwise.", count, newCount) - } + newCount, err := ss.User().AnalyticsGetInactiveUsersCount() + if err != nil { + t.Fatal(err) + } + + if count != newCount-1 { + t.Fatal("Expected 1 more inactive users but found otherwise.", count, newCount) } }