Migrate Channel.AnalyticsTypeCount to Sync by default (#11097)

* Migrate Channel.AnalyticsTypeCount to Sync by default

* Fixing tests
Этот коммит содержится в:
Jesús Espino
2019-06-14 19:06:30 +02:00
коммит произвёл GitHub
родитель b13c5eabff
Коммит 7c4cc21475
9 изменённых файлов: 65 добавлений и 65 удалений

Просмотреть файл

@@ -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[9] = &model.AnalyticsRow{Name: "monthly_active_users", Value: 0}
rows[10] = &model.AnalyticsRow{Name: "inactive_user_count", Value: 0} rows[10] = &model.AnalyticsRow{Name: "inactive_user_count", Value: 0}
openChan := a.Srv.Store.Channel().AnalyticsTypeCount(teamId, model.CHANNEL_OPEN) openChan := make(chan store.StoreResult, 1)
privateChan := a.Srv.Store.Channel().AnalyticsTypeCount(teamId, model.CHANNEL_PRIVATE) 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 userChan store.StoreChannel
var userInactiveChan store.StoreChannel var userInactiveChan store.StoreChannel

Просмотреть файл

@@ -171,16 +171,16 @@ func (a *App) trackActivity() {
mlog.Error(err.Error()) mlog.Error(err.Error())
} }
if ucc := <-a.Srv.Store.Channel().AnalyticsTypeCount("", "O"); ucc.Err == nil { if ucc, err := a.Srv.Store.Channel().AnalyticsTypeCount("", "O"); err == nil {
publicChannelCount = ucc.Data.(int64) publicChannelCount = ucc
} }
if pcc := <-a.Srv.Store.Channel().AnalyticsTypeCount("", "P"); pcc.Err == nil { if pcc, err := a.Srv.Store.Channel().AnalyticsTypeCount("", "P"); err == nil {
privateChannelCount = pcc.Data.(int64) privateChannelCount = pcc
} }
if dcc := <-a.Srv.Store.Channel().AnalyticsTypeCount("", "D"); dcc.Err == nil { if dcc, err := a.Srv.Store.Channel().AnalyticsTypeCount("", "D"); err == nil {
directChannelCount = dcc.Data.(int64) directChannelCount = dcc
} }
if duccr := <-a.Srv.Store.Channel().AnalyticsDeletedTypeCount("", "O"); duccr.Err == nil { if duccr := <-a.Srv.Store.Channel().AnalyticsDeletedTypeCount("", "O"); duccr.Err == nil {

Просмотреть файл

@@ -488,9 +488,9 @@ func (me *TestHelper) CheckTeamCount(t *testing.T, expected int64) {
} }
func (me *TestHelper) CheckChannelsCount(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 count, err := me.App.Srv.Store.Channel().AnalyticsTypeCount("", model.CHANNEL_OPEN); err == nil {
if r.Data.(int64) != expected { if count != expected {
t.Fatalf("Unexpected number of channels. Expected: %v, found: %v", expected, r.Data.(int64)) t.Fatalf("Unexpected number of channels. Expected: %v, found: %v", expected, count)
} }
} else { } else {
t.Fatalf("Failed to get channel count.") t.Fatalf("Failed to get channel count.")

Просмотреть файл

@@ -679,12 +679,8 @@ func TestImportImportChannel(t *testing.T) {
} }
// Check how many channels are in the database. // Check how many channels are in the database.
var channelCount int64 channelCount, err := th.App.Srv.Store.Channel().AnalyticsTypeCount("", model.CHANNEL_OPEN)
if r := <-th.App.Srv.Store.Channel().AnalyticsTypeCount("", model.CHANNEL_OPEN); r.Err == nil { require.Nil(t, err, "Failed to get team count.")
channelCount = r.Data.(int64)
} else {
t.Fatalf("Failed to get team count.")
}
// Do an invalid channel in dry-run mode. // Do an invalid channel in dry-run mode.
data := ChannelImportData{ data := ChannelImportData{
@@ -2173,19 +2169,11 @@ func TestImportImportDirectChannel(t *testing.T) {
defer th.TearDown() defer th.TearDown()
// Check how many channels are in the database. // Check how many channels are in the database.
var directChannelCount int64 directChannelCount, err := th.App.Srv.Store.Channel().AnalyticsTypeCount("", model.CHANNEL_DIRECT)
if r := <-th.App.Srv.Store.Channel().AnalyticsTypeCount("", model.CHANNEL_DIRECT); r.Err == nil { require.Nil(t, err, "Failed to get direct channel count.")
directChannelCount = r.Data.(int64)
} else {
t.Fatalf("Failed to get direct channel count.")
}
var groupChannelCount int64 groupChannelCount, err := th.App.Srv.Store.Channel().AnalyticsTypeCount("", model.CHANNEL_GROUP)
if r := <-th.App.Srv.Store.Channel().AnalyticsTypeCount("", model.CHANNEL_GROUP); r.Err == nil { require.Nil(t, err, "Failed to get group channel count.")
groupChannelCount = r.Data.(int64)
} else {
t.Fatalf("Failed to get group channel count.")
}
// Do an invalid channel in dry-run mode. // Do an invalid channel in dry-run mode.
data := DirectChannelImportData{ data := DirectChannelImportData{
@@ -2194,7 +2182,7 @@ func TestImportImportDirectChannel(t *testing.T) {
}, },
Header: ptrStr("Channel Header"), Header: ptrStr("Channel Header"),
} }
err := th.App.ImportDirectChannel(&data, true) err = th.App.ImportDirectChannel(&data, true)
require.NotNil(t, err) require.NotNil(t, err)
// Check that no more channels are in the DB. // Check that no more channels are in the DB.

Просмотреть файл

@@ -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) { func AssertChannelCount(t *testing.T, a *App, channelType string, expectedCount int64) {
if r := <-a.Srv.Store.Channel().AnalyticsTypeCount("", channelType); r.Err == nil { if count, err := a.Srv.Store.Channel().AnalyticsTypeCount("", channelType); err == nil {
count := r.Data.(int64)
if count != expectedCount { if count != expectedCount {
debug.PrintStack() debug.PrintStack()
t.Fatalf("Channel count of type: %v. Expected: %v, Got: %v", channelType, expectedCount, count) t.Fatalf("Channel count of type: %v. Expected: %v, Got: %v", channelType, expectedCount, count)

Просмотреть файл

@@ -1909,22 +1909,18 @@ func (s SqlChannelStore) GetForPost(postId string) store.StoreChannel {
}) })
} }
func (s SqlChannelStore) AnalyticsTypeCount(teamId string, channelType string) store.StoreChannel { func (s SqlChannelStore) AnalyticsTypeCount(teamId string, channelType string) (int64, *model.AppError) {
return store.Do(func(result *store.StoreResult) {
query := "SELECT COUNT(Id) AS Value FROM Channels WHERE Type = :ChannelType" query := "SELECT COUNT(Id) AS Value FROM Channels WHERE Type = :ChannelType"
if len(teamId) > 0 { if len(teamId) > 0 {
query += " AND TeamId = :TeamId" query += " AND TeamId = :TeamId"
} }
v, err := s.GetReplica().SelectInt(query, map[string]interface{}{"TeamId": teamId, "ChannelType": channelType}) value, err := s.GetReplica().SelectInt(query, map[string]interface{}{"TeamId": teamId, "ChannelType": channelType})
if err != nil { if err != nil {
result.Err = model.NewAppError("SqlChannelStore.AnalyticsTypeCount", "store.sql_channel.analytics_type_count.app_error", nil, err.Error(), http.StatusInternalServerError) return int64(0), model.NewAppError("SqlChannelStore.AnalyticsTypeCount", "store.sql_channel.analytics_type_count.app_error", nil, err.Error(), http.StatusInternalServerError)
return
} }
return value, nil
result.Data = v
})
} }
func (s SqlChannelStore) AnalyticsDeletedTypeCount(teamId string, channelType string) store.StoreChannel { func (s SqlChannelStore) AnalyticsDeletedTypeCount(teamId string, channelType string) store.StoreChannel {

Просмотреть файл

@@ -177,7 +177,7 @@ type ChannelStore interface {
PermanentDeleteMembersByChannel(channelId string) StoreChannel PermanentDeleteMembersByChannel(channelId string) StoreChannel
UpdateLastViewedAt(channelIds []string, userId string) StoreChannel UpdateLastViewedAt(channelIds []string, userId string) StoreChannel
IncrementMentionCount(channelId 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 GetMembersForUser(teamId string, userId string) StoreChannel
GetMembersForUserWithPagination(teamId, userId string, page, perPage int) StoreChannel GetMembersForUserWithPagination(teamId, userId string, page, perPage int) StoreChannel
AutocompleteInTeam(teamId string, term string, includeDeleted bool) StoreChannel AutocompleteInTeam(teamId string, term string, includeDeleted bool) StoreChannel

Просмотреть файл

@@ -1321,15 +1321,15 @@ func testChannelStoreGetMoreChannels(t *testing.T, ss store.Store) {
}) })
t.Run("verify analytics for open channels", func(t *testing.T) { t.Run("verify analytics for open channels", func(t *testing.T) {
result := <-ss.Channel().AnalyticsTypeCount(teamId, model.CHANNEL_OPEN) count, err := ss.Channel().AnalyticsTypeCount(teamId, model.CHANNEL_OPEN)
require.Nil(t, result.Err) require.Nil(t, err)
require.EqualValues(t, 4, result.Data.(int64)) require.EqualValues(t, 4, count)
}) })
t.Run("verify analytics for private channels", func(t *testing.T) { t.Run("verify analytics for private channels", func(t *testing.T) {
result := <-ss.Channel().AnalyticsTypeCount(teamId, model.CHANNEL_PRIVATE) count, err := ss.Channel().AnalyticsTypeCount(teamId, model.CHANNEL_PRIVATE)
require.Nil(t, result.Err) require.Nil(t, err)
require.EqualValues(t, 2, result.Data.(int64)) 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) { t.Run("verify analytics for open channels", func(t *testing.T) {
result := <-ss.Channel().AnalyticsTypeCount(teamId, model.CHANNEL_OPEN) count, err := ss.Channel().AnalyticsTypeCount(teamId, model.CHANNEL_OPEN)
require.Nil(t, result.Err) require.Nil(t, err)
require.EqualValues(t, 3, result.Data.(int64)) require.EqualValues(t, 3, count)
}) })
t.Run("verify analytics for private channels", func(t *testing.T) { t.Run("verify analytics for private channels", func(t *testing.T) {
result := <-ss.Channel().AnalyticsTypeCount(teamId, model.CHANNEL_PRIVATE) count, err := ss.Channel().AnalyticsTypeCount(teamId, model.CHANNEL_PRIVATE)
require.Nil(t, result.Err) require.Nil(t, err)
require.EqualValues(t, 1, result.Data.(int64)) require.EqualValues(t, 1, count)
}) })
} }

Просмотреть файл

@@ -30,19 +30,26 @@ func (_m *ChannelStore) AnalyticsDeletedTypeCount(teamId string, channelType str
} }
// AnalyticsTypeCount provides a mock function with given fields: teamId, channelType // 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) ret := _m.Called(teamId, channelType)
var r0 store.StoreChannel var r0 int64
if rf, ok := ret.Get(0).(func(string, string) store.StoreChannel); ok { if rf, ok := ret.Get(0).(func(string, string) int64); ok {
r0 = rf(teamId, channelType) r0 = rf(teamId, channelType)
} else { } else {
if ret.Get(0) != nil { r0 = ret.Get(0).(int64)
r0 = ret.Get(0).(store.StoreChannel) }
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 // AutocompleteInTeam provides a mock function with given fields: teamId, term, includeDeleted