diff --git a/app/analytics.go b/app/analytics.go index 83980d9d89..ca4d713aea 100644 --- a/app/analytics.go +++ b/app/analytics.go @@ -66,7 +66,7 @@ func (a *App) GetAnalytics(name string, teamId string) (model.AnalyticsRows, *mo close(userInactiveChan) }() } else { - userChan := make(chan store.StoreResult, 1) + userChan = make(chan store.StoreResult, 1) go func() { count, err := a.Srv.Store.User().Count(model.UserCountOptions{TeamId: teamId}) userChan <- store.StoreResult{Data: count, Err: err} @@ -84,8 +84,6 @@ func (a *App) GetAnalytics(name string, teamId string) (model.AnalyticsRows, *mo }() } - dailyActiveChan := a.Srv.Store.User().AnalyticsActiveCount(DAY_MILLISECONDS) - monthlyActiveChan := a.Srv.Store.User().AnalyticsActiveCount(MONTH_MILLISECONDS) teamCountChan := make(chan store.StoreResult, 1) go func() { teamCount, err := a.Srv.Store.Team().AnalyticsTeamCount() @@ -93,6 +91,20 @@ func (a *App) GetAnalytics(name string, teamId string) (model.AnalyticsRows, *mo close(teamCountChan) }() + dailyActiveChan := make(chan store.StoreResult, 1) + go func() { + dailyActive, err := a.Srv.Store.User().AnalyticsActiveCount(DAY_MILLISECONDS) + dailyActiveChan <- store.StoreResult{Data: dailyActive, Err: err} + close(dailyActiveChan) + }() + + monthlyActiveChan := make(chan store.StoreResult, 1) + go func() { + monthlyActive, err := a.Srv.Store.User().AnalyticsActiveCount(MONTH_MILLISECONDS) + monthlyActiveChan <- store.StoreResult{Data: monthlyActive, Err: err} + close(monthlyActiveChan) + }() + r := <-openChan if r.Err != nil { return nil, r.Err diff --git a/app/diagnostics.go b/app/diagnostics.go index 29e0ede0be..8beb957f3f 100644 --- a/app/diagnostics.go +++ b/app/diagnostics.go @@ -8,9 +8,11 @@ import ( "runtime" "strings" + "github.com/segmentio/analytics-go" + "github.com/mattermost/mattermost-server/mlog" "github.com/mattermost/mattermost-server/model" - analytics "github.com/segmentio/analytics-go" + "github.com/mattermost/mattermost-server/store" ) const ( @@ -124,10 +126,7 @@ func pluginActivated(pluginStates map[string]*model.PluginState, pluginId string func (a *App) trackActivity() { var userCount int64 var botAccountsCount int64 - var activeUsersDailyCount int64 - var activeUsersMonthlyCount int64 var inactiveUserCount int64 - var teamCount int64 var publicChannelCount int64 var privateChannelCount int64 var directChannelCount int64 @@ -140,16 +139,19 @@ func (a *App) trackActivity() { var incomingWebhooksCount int64 var outgoingWebhooksCount int64 - dailyActiveChan := a.Srv.Store.User().AnalyticsActiveCount(DAY_MILLISECONDS) - monthlyActiveChan := a.Srv.Store.User().AnalyticsActiveCount(MONTH_MILLISECONDS) + activeUsersDailyCountChan := make(chan store.StoreResult, 1) + go func() { + count, err := a.Srv.Store.User().AnalyticsActiveCount(DAY_MILLISECONDS) + activeUsersDailyCountChan <- store.StoreResult{Data: count, Err: err} + close(activeUsersDailyCountChan) + }() - if r := <-dailyActiveChan; r.Err == nil { - activeUsersDailyCount = r.Data.(int64) - } - - if r := <-monthlyActiveChan; r.Err == nil { - activeUsersMonthlyCount = r.Data.(int64) - } + activeUsersMonthlyCountChan := make(chan store.StoreResult, 1) + go func() { + count, err := a.Srv.Store.User().AnalyticsActiveCount(MONTH_MILLISECONDS) + activeUsersMonthlyCountChan <- store.StoreResult{Data: count, Err: err} + close(activeUsersMonthlyCountChan) + }() if count, err := a.Srv.Store.User().Count(model.UserCountOptions{IncludeDeleted: true}); err == nil { userCount = count @@ -212,6 +214,16 @@ func (a *App) trackActivity() { outgoingWebhooksCount, _ = a.Srv.Store.Webhook().AnalyticsOutgoingCount("") + var activeUsersDailyCount int64 + if r := <-activeUsersDailyCountChan; r.Err == nil { + activeUsersDailyCount = r.Data.(int64) + } + + var activeUsersMonthlyCount int64 + if r := <-activeUsersMonthlyCountChan; r.Err == nil { + activeUsersMonthlyCount = r.Data.(int64) + } + a.SendDiagnostic(TRACK_ACTIVITY, map[string]interface{}{ "registered_users": userCount, "bot_accounts": botAccountsCount, diff --git a/store/sqlstore/user_store.go b/store/sqlstore/user_store.go index 217308b26c..df044c3a47 100644 --- a/store/sqlstore/user_store.go +++ b/store/sqlstore/user_store.go @@ -1171,19 +1171,17 @@ func (us SqlUserStore) Count(options model.UserCountOptions) (int64, *model.AppE return count, nil } -func (us SqlUserStore) AnalyticsActiveCount(timePeriod int64) store.StoreChannel { - return store.Do(func(result *store.StoreResult) { - time := model.GetMillis() - timePeriod +func (us SqlUserStore) AnalyticsActiveCount(timePeriod int64) (int64, *model.AppError) { - query := "SELECT COUNT(*) FROM Status WHERE LastActivityAt > :Time" + time := model.GetMillis() - timePeriod - v, err := us.GetReplica().SelectInt(query, map[string]interface{}{"Time": time}) - if err != nil { - result.Err = model.NewAppError("SqlUserStore.AnalyticsDailyActiveUsers", "store.sql_user.analytics_daily_active_users.app_error", nil, err.Error(), http.StatusInternalServerError) - } else { - result.Data = v - } - }) + query := "SELECT COUNT(*) FROM Status WHERE LastActivityAt > :Time" + + v, err := us.GetReplica().SelectInt(query, map[string]interface{}{"Time": time}) + if err != nil { + return 0, model.NewAppError("SqlUserStore.AnalyticsDailyActiveUsers", "store.sql_user.analytics_daily_active_users.app_error", nil, err.Error(), http.StatusInternalServerError) + } + return v, nil } func (us SqlUserStore) GetUnreadCount(userId string) (int64, error) { diff --git a/store/store.go b/store/store.go index b12857cb7f..9bfdc6079a 100644 --- a/store/store.go +++ b/store/store.go @@ -282,7 +282,7 @@ type UserStore interface { UpdateFailedPasswordAttempts(userId string, attempts int) StoreChannel GetSystemAdminProfiles() (map[string]*model.User, *model.AppError) PermanentDelete(userId string) *model.AppError - AnalyticsActiveCount(time int64) StoreChannel + AnalyticsActiveCount(time int64) (int64, *model.AppError) GetUnreadCount(userId string) (int64, error) GetUnreadCountForChannel(userId string, channelId string) StoreChannel GetAnyUnreadPostCountForChannel(userId string, channelId string) (int64, *model.AppError) diff --git a/store/storetest/mocks/UserStore.go b/store/storetest/mocks/UserStore.go index 2860044ce1..26c5e93aa2 100644 --- a/store/storetest/mocks/UserStore.go +++ b/store/storetest/mocks/UserStore.go @@ -14,19 +14,26 @@ type UserStore struct { } // AnalyticsActiveCount provides a mock function with given fields: time -func (_m *UserStore) AnalyticsActiveCount(time int64) store.StoreChannel { +func (_m *UserStore) AnalyticsActiveCount(time int64) (int64, *model.AppError) { ret := _m.Called(time) - var r0 store.StoreChannel - if rf, ok := ret.Get(0).(func(int64) store.StoreChannel); ok { + var r0 int64 + if rf, ok := ret.Get(0).(func(int64) int64); ok { r0 = rf(time) } 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(int64) *model.AppError); ok { + r1 = rf(time) + } else { + if ret.Get(1) != nil { + r1 = ret.Get(1).(*model.AppError) } } - return r0 + return r0, r1 } // AnalyticsGetInactiveUsersCount provides a mock function with given fields: