From 7c4cc214758ff860aa93e21510a6b69acfda9451 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jes=C3=BAs=20Espino?= Date: Fri, 14 Jun 2019 19:06:30 +0200 Subject: [PATCH] Migrate Channel.AnalyticsTypeCount to Sync by default (#11097) * Migrate Channel.AnalyticsTypeCount to Sync by default * Fixing tests --- app/analytics.go | 14 ++++++++++++-- app/diagnostics.go | 12 ++++++------ app/helper_test.go | 6 +++--- app/import_functions_test.go | 26 +++++++------------------- app/import_test.go | 3 +-- store/sqlstore/channel_store.go | 24 ++++++++++-------------- store/store.go | 2 +- store/storetest/channel_store.go | 24 ++++++++++++------------ store/storetest/mocks/ChannelStore.go | 19 +++++++++++++------ 9 files changed, 65 insertions(+), 65 deletions(-) diff --git a/app/analytics.go b/app/analytics.go index 1a2620dbbe..77065d9381 100644 --- a/app/analytics.go +++ b/app/analytics.go @@ -44,8 +44,18 @@ func (a *App) GetAnalytics(name string, teamId string) (model.AnalyticsRows, *mo rows[9] = &model.AnalyticsRow{Name: "monthly_active_users", Value: 0} rows[10] = &model.AnalyticsRow{Name: "inactive_user_count", Value: 0} - openChan := a.Srv.Store.Channel().AnalyticsTypeCount(teamId, model.CHANNEL_OPEN) - privateChan := a.Srv.Store.Channel().AnalyticsTypeCount(teamId, model.CHANNEL_PRIVATE) + openChan := make(chan store.StoreResult, 1) + privateChan := make(chan store.StoreResult, 1) + go func() { + count, err := a.Srv.Store.Channel().AnalyticsTypeCount(teamId, model.CHANNEL_OPEN) + openChan <- store.StoreResult{Data: count, Err: err} + close(openChan) + }() + go func() { + count, err := a.Srv.Store.Channel().AnalyticsTypeCount(teamId, model.CHANNEL_PRIVATE) + privateChan <- store.StoreResult{Data: count, Err: err} + close(privateChan) + }() var userChan store.StoreChannel var userInactiveChan store.StoreChannel diff --git a/app/diagnostics.go b/app/diagnostics.go index e694364503..4647b4ebc9 100644 --- a/app/diagnostics.go +++ b/app/diagnostics.go @@ -171,16 +171,16 @@ func (a *App) trackActivity() { mlog.Error(err.Error()) } - if ucc := <-a.Srv.Store.Channel().AnalyticsTypeCount("", "O"); ucc.Err == nil { - publicChannelCount = ucc.Data.(int64) + if ucc, err := a.Srv.Store.Channel().AnalyticsTypeCount("", "O"); err == nil { + publicChannelCount = ucc } - if pcc := <-a.Srv.Store.Channel().AnalyticsTypeCount("", "P"); pcc.Err == nil { - privateChannelCount = pcc.Data.(int64) + if pcc, err := a.Srv.Store.Channel().AnalyticsTypeCount("", "P"); err == nil { + privateChannelCount = pcc } - if dcc := <-a.Srv.Store.Channel().AnalyticsTypeCount("", "D"); dcc.Err == nil { - directChannelCount = dcc.Data.(int64) + if dcc, err := a.Srv.Store.Channel().AnalyticsTypeCount("", "D"); err == nil { + directChannelCount = dcc } if duccr := <-a.Srv.Store.Channel().AnalyticsDeletedTypeCount("", "O"); duccr.Err == nil { diff --git a/app/helper_test.go b/app/helper_test.go index 81ddc9531a..425edcd608 100644 --- a/app/helper_test.go +++ b/app/helper_test.go @@ -488,9 +488,9 @@ func (me *TestHelper) CheckTeamCount(t *testing.T, expected int64) { } func (me *TestHelper) CheckChannelsCount(t *testing.T, expected int64) { - if r := <-me.App.Srv.Store.Channel().AnalyticsTypeCount("", model.CHANNEL_OPEN); r.Err == nil { - if r.Data.(int64) != expected { - t.Fatalf("Unexpected number of channels. Expected: %v, found: %v", expected, r.Data.(int64)) + if count, err := me.App.Srv.Store.Channel().AnalyticsTypeCount("", model.CHANNEL_OPEN); err == nil { + if count != expected { + t.Fatalf("Unexpected number of channels. Expected: %v, found: %v", expected, count) } } else { t.Fatalf("Failed to get channel count.") diff --git a/app/import_functions_test.go b/app/import_functions_test.go index cffa5cccdc..fa877a1d4a 100644 --- a/app/import_functions_test.go +++ b/app/import_functions_test.go @@ -679,12 +679,8 @@ func TestImportImportChannel(t *testing.T) { } // Check how many channels are in the database. - var channelCount int64 - if r := <-th.App.Srv.Store.Channel().AnalyticsTypeCount("", model.CHANNEL_OPEN); r.Err == nil { - channelCount = r.Data.(int64) - } else { - t.Fatalf("Failed to get team count.") - } + channelCount, err := th.App.Srv.Store.Channel().AnalyticsTypeCount("", model.CHANNEL_OPEN) + require.Nil(t, err, "Failed to get team count.") // Do an invalid channel in dry-run mode. data := ChannelImportData{ @@ -2173,19 +2169,11 @@ func TestImportImportDirectChannel(t *testing.T) { defer th.TearDown() // Check how many channels are in the database. - var directChannelCount int64 - if r := <-th.App.Srv.Store.Channel().AnalyticsTypeCount("", model.CHANNEL_DIRECT); r.Err == nil { - directChannelCount = r.Data.(int64) - } else { - t.Fatalf("Failed to get direct channel count.") - } + directChannelCount, err := th.App.Srv.Store.Channel().AnalyticsTypeCount("", model.CHANNEL_DIRECT) + require.Nil(t, err, "Failed to get direct channel count.") - var groupChannelCount int64 - if r := <-th.App.Srv.Store.Channel().AnalyticsTypeCount("", model.CHANNEL_GROUP); r.Err == nil { - groupChannelCount = r.Data.(int64) - } else { - t.Fatalf("Failed to get group channel count.") - } + groupChannelCount, err := th.App.Srv.Store.Channel().AnalyticsTypeCount("", model.CHANNEL_GROUP) + require.Nil(t, err, "Failed to get group channel count.") // Do an invalid channel in dry-run mode. data := DirectChannelImportData{ @@ -2194,7 +2182,7 @@ func TestImportImportDirectChannel(t *testing.T) { }, Header: ptrStr("Channel Header"), } - err := th.App.ImportDirectChannel(&data, true) + err = th.App.ImportDirectChannel(&data, true) require.NotNil(t, err) // Check that no more channels are in the DB. diff --git a/app/import_test.go b/app/import_test.go index c654ee6196..d2d97298c0 100644 --- a/app/import_test.go +++ b/app/import_test.go @@ -92,8 +92,7 @@ func AssertAllPostsCount(t *testing.T, a *App, initialCount int64, change int64, } func AssertChannelCount(t *testing.T, a *App, channelType string, expectedCount int64) { - if r := <-a.Srv.Store.Channel().AnalyticsTypeCount("", channelType); r.Err == nil { - count := r.Data.(int64) + if count, err := a.Srv.Store.Channel().AnalyticsTypeCount("", channelType); err == nil { if count != expectedCount { debug.PrintStack() t.Fatalf("Channel count of type: %v. Expected: %v, Got: %v", channelType, expectedCount, count) diff --git a/store/sqlstore/channel_store.go b/store/sqlstore/channel_store.go index f4cdd16a85..d521dc7292 100644 --- a/store/sqlstore/channel_store.go +++ b/store/sqlstore/channel_store.go @@ -1909,22 +1909,18 @@ func (s SqlChannelStore) GetForPost(postId string) store.StoreChannel { }) } -func (s SqlChannelStore) AnalyticsTypeCount(teamId string, channelType string) store.StoreChannel { - return store.Do(func(result *store.StoreResult) { - query := "SELECT COUNT(Id) AS Value FROM Channels WHERE Type = :ChannelType" +func (s SqlChannelStore) AnalyticsTypeCount(teamId string, channelType string) (int64, *model.AppError) { + query := "SELECT COUNT(Id) AS Value FROM Channels WHERE Type = :ChannelType" - if len(teamId) > 0 { - query += " AND TeamId = :TeamId" - } + if len(teamId) > 0 { + query += " AND TeamId = :TeamId" + } - v, err := s.GetReplica().SelectInt(query, map[string]interface{}{"TeamId": teamId, "ChannelType": channelType}) - if err != nil { - result.Err = model.NewAppError("SqlChannelStore.AnalyticsTypeCount", "store.sql_channel.analytics_type_count.app_error", nil, err.Error(), http.StatusInternalServerError) - return - } - - result.Data = v - }) + value, err := s.GetReplica().SelectInt(query, map[string]interface{}{"TeamId": teamId, "ChannelType": channelType}) + if err != nil { + return int64(0), model.NewAppError("SqlChannelStore.AnalyticsTypeCount", "store.sql_channel.analytics_type_count.app_error", nil, err.Error(), http.StatusInternalServerError) + } + return value, nil } func (s SqlChannelStore) AnalyticsDeletedTypeCount(teamId string, channelType string) store.StoreChannel { diff --git a/store/store.go b/store/store.go index f477bc0734..cfae448454 100644 --- a/store/store.go +++ b/store/store.go @@ -177,7 +177,7 @@ type ChannelStore interface { PermanentDeleteMembersByChannel(channelId string) StoreChannel UpdateLastViewedAt(channelIds []string, userId string) StoreChannel IncrementMentionCount(channelId string, userId string) StoreChannel - AnalyticsTypeCount(teamId string, channelType string) StoreChannel + AnalyticsTypeCount(teamId string, channelType string) (int64, *model.AppError) GetMembersForUser(teamId string, userId string) StoreChannel GetMembersForUserWithPagination(teamId, userId string, page, perPage int) StoreChannel AutocompleteInTeam(teamId string, term string, includeDeleted bool) StoreChannel diff --git a/store/storetest/channel_store.go b/store/storetest/channel_store.go index 329d442919..8cdfbc0274 100644 --- a/store/storetest/channel_store.go +++ b/store/storetest/channel_store.go @@ -1321,15 +1321,15 @@ func testChannelStoreGetMoreChannels(t *testing.T, ss store.Store) { }) t.Run("verify analytics for open channels", func(t *testing.T) { - result := <-ss.Channel().AnalyticsTypeCount(teamId, model.CHANNEL_OPEN) - require.Nil(t, result.Err) - require.EqualValues(t, 4, result.Data.(int64)) + count, err := ss.Channel().AnalyticsTypeCount(teamId, model.CHANNEL_OPEN) + require.Nil(t, err) + require.EqualValues(t, 4, count) }) t.Run("verify analytics for private channels", func(t *testing.T) { - result := <-ss.Channel().AnalyticsTypeCount(teamId, model.CHANNEL_PRIVATE) - require.Nil(t, result.Err) - require.EqualValues(t, 2, result.Data.(int64)) + count, err := ss.Channel().AnalyticsTypeCount(teamId, model.CHANNEL_PRIVATE) + require.Nil(t, err) + require.EqualValues(t, 2, count) }) } @@ -1413,15 +1413,15 @@ func testChannelStoreGetPublicChannelsForTeam(t *testing.T, ss store.Store) { }) t.Run("verify analytics for open channels", func(t *testing.T) { - result := <-ss.Channel().AnalyticsTypeCount(teamId, model.CHANNEL_OPEN) - require.Nil(t, result.Err) - require.EqualValues(t, 3, result.Data.(int64)) + count, err := ss.Channel().AnalyticsTypeCount(teamId, model.CHANNEL_OPEN) + require.Nil(t, err) + require.EqualValues(t, 3, count) }) t.Run("verify analytics for private channels", func(t *testing.T) { - result := <-ss.Channel().AnalyticsTypeCount(teamId, model.CHANNEL_PRIVATE) - require.Nil(t, result.Err) - require.EqualValues(t, 1, result.Data.(int64)) + count, err := ss.Channel().AnalyticsTypeCount(teamId, model.CHANNEL_PRIVATE) + require.Nil(t, err) + require.EqualValues(t, 1, count) }) } diff --git a/store/storetest/mocks/ChannelStore.go b/store/storetest/mocks/ChannelStore.go index b011206a1d..9035087b01 100644 --- a/store/storetest/mocks/ChannelStore.go +++ b/store/storetest/mocks/ChannelStore.go @@ -30,19 +30,26 @@ func (_m *ChannelStore) AnalyticsDeletedTypeCount(teamId string, channelType str } // AnalyticsTypeCount provides a mock function with given fields: teamId, channelType -func (_m *ChannelStore) AnalyticsTypeCount(teamId string, channelType string) store.StoreChannel { +func (_m *ChannelStore) AnalyticsTypeCount(teamId string, channelType string) (int64, *model.AppError) { ret := _m.Called(teamId, channelType) - var r0 store.StoreChannel - if rf, ok := ret.Get(0).(func(string, string) store.StoreChannel); ok { + var r0 int64 + if rf, ok := ret.Get(0).(func(string, string) int64); ok { r0 = rf(teamId, channelType) } 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(string, string) *model.AppError); ok { + r1 = rf(teamId, channelType) + } else { + if ret.Get(1) != nil { + r1 = ret.Get(1).(*model.AppError) } } - return r0 + return r0, r1 } // AutocompleteInTeam provides a mock function with given fields: teamId, term, includeDeleted