From 57e9aa767c2ad21f924cf7d2b433e0ad916ed1c9 Mon Sep 17 00:00:00 2001 From: Vishal Date: Tue, 31 May 2022 14:18:41 +0530 Subject: [PATCH] [MM-44263] Only include user posts in post count (#20250) * Fetch users only posts * Explicitly validate posts count before testing usage --- api4/usage_test.go | 18 +++++++++++++++--- app/usage.go | 2 +- model/post.go | 1 + store/sqlstore/post_store.go | 7 +++++++ store/storetest/post_store.go | 7 ++++++- 5 files changed, 30 insertions(+), 5 deletions(-) diff --git a/api4/usage_test.go b/api4/usage_test.go index 754d3b1a18..5c6900f0ef 100644 --- a/api4/usage_test.go +++ b/api4/usage_test.go @@ -7,7 +7,9 @@ import ( "net/http" "testing" + "github.com/mattermost/mattermost-server/v6/model" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" ) func TestGetPostsUsage(t *testing.T) { @@ -24,11 +26,21 @@ func TestGetPostsUsage(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() defer th.TearDown() - th.CreatePost() - th.CreatePost() + + for i := 0; i < 14; i++ { + 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() assert.NoError(t, err) diff --git a/app/usage.go b/app/usage.go index de7f383e4f..262c419365 100644 --- a/app/usage.go +++ b/app/usage.go @@ -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 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 { return 0, model.NewAppError("GetPostsUsage", "app.post.analytics_posts_count.app_error", nil, err.Error(), http.StatusInternalServerError) } diff --git a/model/post.go b/model/post.go index 4858953055..740969e283 100644 --- a/model/post.go +++ b/model/post.go @@ -280,6 +280,7 @@ type PostCountOptions struct { MustHaveFile bool MustHaveHashtag bool ExcludeDeleted bool + UsersPostsOnly bool } func (o *Post) Etag() string { diff --git a/store/sqlstore/post_store.go b/store/sqlstore/post_store.go index e850d142ed..6a4dbf8ef7 100644 --- a/store/sqlstore/post_store.go +++ b/store/sqlstore/post_store.go @@ -2162,6 +2162,13 @@ func (s *SqlPostStore) AnalyticsPostCount(options *model.PostCountOptions) (int6 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 { query = query.Where(sq.Or{sq.NotEq{"p.FileIds": "[]"}, sq.NotEq{"p.Filenames": "[]"}}) } diff --git a/store/storetest/post_store.go b/store/storetest/post_store.go index 5f095f576a..ad639909a8 100644 --- a/store/storetest/post_store.go +++ b/store/storetest/post_store.go @@ -2163,7 +2163,7 @@ func testPostCountsByDay(t *testing.T, ss store.Store) { require.NoError(t, err) 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}) require.NoError(t, err) 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}) require.NoError(t, err) 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) {