[MM-44263] Only include user posts in post count (#20250)

* Fetch users only posts

* Explicitly validate posts count before testing usage
Этот коммит содержится в:
Vishal
2022-05-31 14:18:41 +05:30
коммит произвёл GitHub
родитель 70610a9c96
Коммит 57e9aa767c
5 изменённых файлов: 30 добавлений и 5 удалений

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

@@ -7,7 +7,9 @@ import (
"net/http" "net/http"
"testing" "testing"
"github.com/mattermost/mattermost-server/v6/model"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
) )
func TestGetPostsUsage(t *testing.T) { func TestGetPostsUsage(t *testing.T) {
@@ -24,11 +26,21 @@ func TestGetPostsUsage(t *testing.T) {
}) })
t.Run("good request returns response", func(t *testing.T) { t.Run("good request returns response", func(t *testing.T) {
// Following calls create a total of 15 posts
th := Setup(t).InitBasic() th := Setup(t).InitBasic()
defer th.TearDown() defer th.TearDown()
for i := 0; i < 14; i++ {
th.CreatePost() th.CreatePost()
th.CreatePost() }
total, err := th.Server.Store.Post().AnalyticsPostCount(&model.PostCountOptions{ExcludeDeleted: true})
require.NoError(t, err)
usersOnly, err := th.Server.Store.Post().AnalyticsPostCount(&model.PostCountOptions{ExcludeDeleted: true, UsersPostsOnly: true})
require.NoError(t, err)
require.GreaterOrEqual(t, usersOnly, int64(14))
require.LessOrEqual(t, usersOnly, int64(20))
require.GreaterOrEqual(t, total, usersOnly)
usage, r, err := th.Client.GetPostsUsage() usage, r, err := th.Client.GetPostsUsage()
assert.NoError(t, err) assert.NoError(t, err)

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

@@ -47,7 +47,7 @@ func (ch *Channels) getIntegrationsUsage() (*model.IntegrationsUsage, *model.App
// GetPostsUsage returns "rounded off" total posts count like returns 900 instead of 987 // GetPostsUsage returns "rounded off" total posts count like returns 900 instead of 987
func (a *App) GetPostsUsage() (int64, *model.AppError) { func (a *App) GetPostsUsage() (int64, *model.AppError) {
count, err := a.Srv().Store.Post().AnalyticsPostCount(&model.PostCountOptions{ExcludeDeleted: true}) count, err := a.Srv().Store.Post().AnalyticsPostCount(&model.PostCountOptions{ExcludeDeleted: true, UsersPostsOnly: true})
if err != nil { if err != nil {
return 0, model.NewAppError("GetPostsUsage", "app.post.analytics_posts_count.app_error", nil, err.Error(), http.StatusInternalServerError) return 0, model.NewAppError("GetPostsUsage", "app.post.analytics_posts_count.app_error", nil, err.Error(), http.StatusInternalServerError)
} }

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

@@ -280,6 +280,7 @@ type PostCountOptions struct {
MustHaveFile bool MustHaveFile bool
MustHaveHashtag bool MustHaveHashtag bool
ExcludeDeleted bool ExcludeDeleted bool
UsersPostsOnly bool
} }
func (o *Post) Etag() string { func (o *Post) Etag() string {

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

@@ -2162,6 +2162,13 @@ func (s *SqlPostStore) AnalyticsPostCount(options *model.PostCountOptions) (int6
Where(sq.Eq{"c.TeamId": options.TeamId}) Where(sq.Eq{"c.TeamId": options.TeamId})
} }
if options.UsersPostsOnly {
query = query.Where(sq.And{
sq.Eq{"p.Type": ""},
sq.Expr("p.UserId NOT IN (SELECT UserId FROM Bots)"),
})
}
if options.MustHaveFile { if options.MustHaveFile {
query = query.Where(sq.Or{sq.NotEq{"p.FileIds": "[]"}, sq.NotEq{"p.Filenames": "[]"}}) query = query.Where(sq.Or{sq.NotEq{"p.FileIds": "[]"}, sq.NotEq{"p.Filenames": "[]"}})
} }

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

@@ -2163,7 +2163,7 @@ func testPostCountsByDay(t *testing.T, ss store.Store) {
require.NoError(t, err) require.NoError(t, err)
assert.GreaterOrEqual(t, r2, int64(2)) assert.GreaterOrEqual(t, r2, int64(2))
// total across teams with hastags and files // total across teams with hashtags and files
r2, err = ss.Post().AnalyticsPostCount(&model.PostCountOptions{MustHaveFile: true, MustHaveHashtag: true}) r2, err = ss.Post().AnalyticsPostCount(&model.PostCountOptions{MustHaveFile: true, MustHaveHashtag: true})
require.NoError(t, err) require.NoError(t, err)
assert.GreaterOrEqual(t, r2, int64(1)) assert.GreaterOrEqual(t, r2, int64(1))
@@ -2176,6 +2176,11 @@ func testPostCountsByDay(t *testing.T, ss store.Store) {
r2, err = ss.Post().AnalyticsPostCount(&model.PostCountOptions{TeamId: t1.Id, ExcludeDeleted: true}) r2, err = ss.Post().AnalyticsPostCount(&model.PostCountOptions{TeamId: t1.Id, ExcludeDeleted: true})
require.NoError(t, err) require.NoError(t, err)
assert.Equal(t, int64(5), r2) assert.Equal(t, int64(5), r2)
// total users only posts for single team with the deleted post excluded
r2, err = ss.Post().AnalyticsPostCount(&model.PostCountOptions{TeamId: t1.Id, ExcludeDeleted: true, UsersPostsOnly: true})
require.NoError(t, err)
assert.Equal(t, int64(3), r2)
} }
func testPostStoreGetFlaggedPostsForTeam(t *testing.T, ss store.Store, s SqlStore) { func testPostStoreGetFlaggedPostsForTeam(t *testing.T, ss store.Store, s SqlStore) {