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[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

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

@@ -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 {

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

@@ -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.")

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

@@ -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.

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

@@ -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)

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

@@ -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 {

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

@@ -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

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

@@ -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)
})
}

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

@@ -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