[MM-43917] Cloud Freemium limits API: messages/posts (#20152)
* WIP - Add api and app funcs * Add test cases * Add utils testcases * Exclude deleted posts * Add doc for func * Move api from cloud to usage * Allow api access to authenticated users * Change int to int64 * Fix lint issue * Simplify err check Co-authored-by: Ashish Bhate <ashish.bhate@mattermost.com> Co-authored-by: Mattermod <mattermod@users.noreply.github.com> Co-authored-by: Ashish Bhate <ashish.bhate@mattermost.com>
Этот коммит содержится в:
@@ -5365,7 +5365,7 @@ func (s *OpenTracingLayerPluginStore) SetWithOptions(pluginID string, key string
|
||||
return result, err
|
||||
}
|
||||
|
||||
func (s *OpenTracingLayerPostStore) AnalyticsPostCount(teamID string, mustHaveFile bool, mustHaveHashtag bool) (int64, error) {
|
||||
func (s *OpenTracingLayerPostStore) AnalyticsPostCount(options *model.PostCountOptions) (int64, error) {
|
||||
origCtx := s.Root.Store.Context()
|
||||
span, newCtx := tracing.StartSpanWithParentByContext(s.Root.Store.Context(), "PostStore.AnalyticsPostCount")
|
||||
s.Root.Store.SetContext(newCtx)
|
||||
@@ -5374,7 +5374,7 @@ func (s *OpenTracingLayerPostStore) AnalyticsPostCount(teamID string, mustHaveFi
|
||||
}()
|
||||
|
||||
defer span.Finish()
|
||||
result, err := s.PostStore.AnalyticsPostCount(teamID, mustHaveFile, mustHaveHashtag)
|
||||
result, err := s.PostStore.AnalyticsPostCount(options)
|
||||
if err != nil {
|
||||
span.LogFields(spanlog.Error(err))
|
||||
ext.Error.Set(span, true)
|
||||
|
||||
@@ -6094,11 +6094,11 @@ func (s *RetryLayerPluginStore) SetWithOptions(pluginID string, key string, valu
|
||||
|
||||
}
|
||||
|
||||
func (s *RetryLayerPostStore) AnalyticsPostCount(teamID string, mustHaveFile bool, mustHaveHashtag bool) (int64, error) {
|
||||
func (s *RetryLayerPostStore) AnalyticsPostCount(options *model.PostCountOptions) (int64, error) {
|
||||
|
||||
tries := 0
|
||||
for {
|
||||
result, err := s.PostStore.AnalyticsPostCount(teamID, mustHaveFile, mustHaveHashtag)
|
||||
result, err := s.PostStore.AnalyticsPostCount(options)
|
||||
if err == nil {
|
||||
return result, nil
|
||||
}
|
||||
|
||||
@@ -2128,25 +2128,29 @@ func (s *SqlPostStore) AnalyticsPostCountsByDay(options *model.AnalyticsPostCoun
|
||||
return rows, nil
|
||||
}
|
||||
|
||||
func (s *SqlPostStore) AnalyticsPostCount(teamId string, mustHaveFile bool, mustHaveHashtag bool) (int64, error) {
|
||||
func (s *SqlPostStore) AnalyticsPostCount(options *model.PostCountOptions) (int64, error) {
|
||||
query := s.getQueryBuilder().
|
||||
Select("COUNT(p.Id) AS Value").
|
||||
From("Posts p")
|
||||
|
||||
if teamId != "" {
|
||||
if options.TeamId != "" {
|
||||
query = query.
|
||||
Join("Channels c ON (c.Id = p.ChannelId)").
|
||||
Where(sq.Eq{"c.TeamId": teamId})
|
||||
Where(sq.Eq{"c.TeamId": options.TeamId})
|
||||
}
|
||||
|
||||
if mustHaveFile {
|
||||
if options.MustHaveFile {
|
||||
query = query.Where(sq.Or{sq.NotEq{"p.FileIds": "[]"}, sq.NotEq{"p.Filenames": "[]"}})
|
||||
}
|
||||
|
||||
if mustHaveHashtag {
|
||||
if options.MustHaveHashtag {
|
||||
query = query.Where(sq.NotEq{"p.Hashtags": ""})
|
||||
}
|
||||
|
||||
if options.ExcludeDeleted {
|
||||
query = query.Where(sq.Eq{"p.DeleteAt": 0})
|
||||
}
|
||||
|
||||
queryString, args, err := query.ToSql()
|
||||
if err != nil {
|
||||
return 0, errors.Wrap(err, "post_tosql")
|
||||
|
||||
@@ -348,7 +348,7 @@ type PostStore interface {
|
||||
Search(teamID string, userID string, params *model.SearchParams) (*model.PostList, error)
|
||||
AnalyticsUserCountsWithPostsByDay(teamID string) (model.AnalyticsRows, error)
|
||||
AnalyticsPostCountsByDay(options *model.AnalyticsPostCountsOptions) (model.AnalyticsRows, error)
|
||||
AnalyticsPostCount(teamID string, mustHaveFile bool, mustHaveHashtag bool) (int64, error)
|
||||
AnalyticsPostCount(options *model.PostCountOptions) (int64, error)
|
||||
ClearCaches()
|
||||
InvalidateLastPostTimeCache(channelID string)
|
||||
GetLastPostRowCreateAt() (int64, error)
|
||||
|
||||
@@ -16,20 +16,20 @@ type PostStore struct {
|
||||
mock.Mock
|
||||
}
|
||||
|
||||
// AnalyticsPostCount provides a mock function with given fields: teamID, mustHaveFile, mustHaveHashtag
|
||||
func (_m *PostStore) AnalyticsPostCount(teamID string, mustHaveFile bool, mustHaveHashtag bool) (int64, error) {
|
||||
ret := _m.Called(teamID, mustHaveFile, mustHaveHashtag)
|
||||
// AnalyticsPostCount provides a mock function with given fields: options
|
||||
func (_m *PostStore) AnalyticsPostCount(options *model.PostCountOptions) (int64, error) {
|
||||
ret := _m.Called(options)
|
||||
|
||||
var r0 int64
|
||||
if rf, ok := ret.Get(0).(func(string, bool, bool) int64); ok {
|
||||
r0 = rf(teamID, mustHaveFile, mustHaveHashtag)
|
||||
if rf, ok := ret.Get(0).(func(*model.PostCountOptions) int64); ok {
|
||||
r0 = rf(options)
|
||||
} else {
|
||||
r0 = ret.Get(0).(int64)
|
||||
}
|
||||
|
||||
var r1 error
|
||||
if rf, ok := ret.Get(1).(func(string, bool, bool) error); ok {
|
||||
r1 = rf(teamID, mustHaveFile, mustHaveHashtag)
|
||||
if rf, ok := ret.Get(1).(func(*model.PostCountOptions) error); ok {
|
||||
r1 = rf(options)
|
||||
} else {
|
||||
r1 = ret.Error(1)
|
||||
}
|
||||
|
||||
@@ -2115,30 +2115,39 @@ func testPostCountsByDay(t *testing.T, ss store.Store) {
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, float64(1), r1[0].Value)
|
||||
|
||||
// total
|
||||
r2, err := ss.Post().AnalyticsPostCount(t1.Id, false, false)
|
||||
// total for single team
|
||||
r2, err := ss.Post().AnalyticsPostCount(&model.PostCountOptions{TeamId: t1.Id})
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, int64(6), r2)
|
||||
|
||||
// total across teams
|
||||
r2, err = ss.Post().AnalyticsPostCount("", false, false)
|
||||
r2, err = ss.Post().AnalyticsPostCount(&model.PostCountOptions{})
|
||||
require.NoError(t, err)
|
||||
assert.GreaterOrEqual(t, r2, int64(6))
|
||||
|
||||
// total across teams with files
|
||||
r2, err = ss.Post().AnalyticsPostCount("", true, false)
|
||||
r2, err = ss.Post().AnalyticsPostCount(&model.PostCountOptions{MustHaveFile: true})
|
||||
require.NoError(t, err)
|
||||
assert.GreaterOrEqual(t, r2, int64(3))
|
||||
|
||||
// total across teams with hastags
|
||||
r2, err = ss.Post().AnalyticsPostCount("", false, true)
|
||||
// total across teams with hashtags
|
||||
r2, err = ss.Post().AnalyticsPostCount(&model.PostCountOptions{MustHaveHashtag: true})
|
||||
require.NoError(t, err)
|
||||
assert.GreaterOrEqual(t, r2, int64(2))
|
||||
|
||||
// total across teams with hastags and files
|
||||
r2, err = ss.Post().AnalyticsPostCount("", true, true)
|
||||
r2, err = ss.Post().AnalyticsPostCount(&model.PostCountOptions{MustHaveFile: true, MustHaveHashtag: true})
|
||||
require.NoError(t, err)
|
||||
assert.GreaterOrEqual(t, r2, int64(1))
|
||||
|
||||
// delete 1 post
|
||||
err = ss.Post().Delete(o1.Id, 1, o1.UserId)
|
||||
require.NoError(t, err)
|
||||
|
||||
// total for single team with the deleted post excluded
|
||||
r2, err = ss.Post().AnalyticsPostCount(&model.PostCountOptions{TeamId: t1.Id, ExcludeDeleted: true})
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, int64(5), r2)
|
||||
}
|
||||
|
||||
func testPostStoreGetFlaggedPostsForTeam(t *testing.T, ss store.Store, s SqlStore) {
|
||||
|
||||
@@ -4859,10 +4859,10 @@ func (s *TimerLayerPluginStore) SetWithOptions(pluginID string, key string, valu
|
||||
return result, err
|
||||
}
|
||||
|
||||
func (s *TimerLayerPostStore) AnalyticsPostCount(teamID string, mustHaveFile bool, mustHaveHashtag bool) (int64, error) {
|
||||
func (s *TimerLayerPostStore) AnalyticsPostCount(options *model.PostCountOptions) (int64, error) {
|
||||
start := timemodule.Now()
|
||||
|
||||
result, err := s.PostStore.AnalyticsPostCount(teamID, mustHaveFile, mustHaveHashtag)
|
||||
result, err := s.PostStore.AnalyticsPostCount(options)
|
||||
|
||||
elapsed := float64(timemodule.Since(start)) / float64(timemodule.Second)
|
||||
if s.Root.Metrics != nil {
|
||||
|
||||
Ссылка в новой задаче
Block a user