Add AWS Metering service support (#15290)

Этот коммит содержится в:
catalintomai
2020-09-28 11:43:08 -07:00
коммит произвёл GitHub
родитель 50e37068b5
Коммит 1c0d590c81
199 изменённых файлов: 38917 добавлений и 4 удалений

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

@@ -7777,6 +7777,24 @@ func (s *OpenTracingLayerUserStore) AnalyticsActiveCount(time int64, options mod
return result, err
}
func (s *OpenTracingLayerUserStore) AnalyticsActiveCountForPeriod(startTime int64, endTime int64, options model.UserCountOptions) (int64, error) {
origCtx := s.Root.Store.Context()
span, newCtx := tracing.StartSpanWithParentByContext(s.Root.Store.Context(), "UserStore.AnalyticsActiveCountForPeriod")
s.Root.Store.SetContext(newCtx)
defer func() {
s.Root.Store.SetContext(origCtx)
}()
defer span.Finish()
result, err := s.UserStore.AnalyticsActiveCountForPeriod(startTime, endTime, options)
if err != nil {
span.LogFields(spanlog.Error(err))
ext.Error.Set(span, true)
}
return result, err
}
func (s *OpenTracingLayerUserStore) AnalyticsGetExternalUsers(hostDomain string) (bool, *model.AppError) {
origCtx := s.Root.Store.Context()
span, newCtx := tracing.StartSpanWithParentByContext(s.Root.Store.Context(), "UserStore.AnalyticsGetExternalUsers")

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

@@ -7384,6 +7384,26 @@ func (s *RetryLayerUserStore) AnalyticsActiveCount(time int64, options model.Use
}
func (s *RetryLayerUserStore) AnalyticsActiveCountForPeriod(startTime int64, endTime int64, options model.UserCountOptions) (int64, error) {
tries := 0
for {
result, err := s.UserStore.AnalyticsActiveCountForPeriod(startTime, endTime, options)
if err == nil {
return result, nil
}
if !isRepeatableError(err) {
return result, err
}
tries++
if tries >= 3 {
err = errors.Wrap(err, "giving up after 3 consecutive repeatable transaction failures")
return result, err
}
}
}
func (s *RetryLayerUserStore) AnalyticsGetExternalUsers(hostDomain string) (bool, *model.AppError) {
return s.UserStore.AnalyticsGetExternalUsers(hostDomain)

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

@@ -13,6 +13,7 @@ import (
sq "github.com/Masterminds/squirrel"
"github.com/mattermost/gorp"
"github.com/pkg/errors"
"github.com/mattermost/mattermost-server/v5/einterfaces"
"github.com/mattermost/mattermost-server/v5/model"
@@ -1267,7 +1268,31 @@ func (us SqlUserStore) AnalyticsActiveCount(timePeriod int64, options model.User
v, err := us.GetReplica().SelectInt(queryStr, args...)
if err != nil {
return 0, model.NewAppError("SqlUserStore.AnalyticsDailyActiveUsers", "store.sql_user.analytics_daily_active_users.app_error", nil, err.Error(), http.StatusInternalServerError)
return 0, model.NewAppError("SqlUserStore.AnalyticsActiveCount", "store.sql_user.analytics_daily_active_users.app_error", nil, err.Error(), http.StatusInternalServerError)
}
return v, nil
}
func (us SqlUserStore) AnalyticsActiveCountForPeriod(startTime int64, endTime int64, options model.UserCountOptions) (int64, error) {
query := us.getQueryBuilder().Select("COUNT(*)").From("Status AS s").Where("LastActivityAt > :StartTime AND LastActivityAt <= :EndTime", map[string]interface{}{"StartTime": startTime, "EndTime": endTime})
if !options.IncludeBotAccounts {
query = query.LeftJoin("Bots ON s.UserId = Bots.UserId").Where("Bots.UserId IS NULL")
}
if !options.IncludeDeleted {
query = query.LeftJoin("Users ON s.UserId = Users.Id").Where("Users.DeleteAt = 0")
}
queryStr, args, err := query.ToSql()
if err != nil {
return 0, errors.Wrap(err, "Failed to build query.")
}
v, err := us.GetReplica().SelectInt(queryStr, args...)
if err != nil {
return 0, errors.Wrap(err, "Unable to get the active users during the requested period.")
}
return v, nil
}

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

@@ -327,6 +327,7 @@ type UserStore interface {
GetSystemAdminProfiles() (map[string]*model.User, *model.AppError)
PermanentDelete(userId string) *model.AppError
AnalyticsActiveCount(time int64, options model.UserCountOptions) (int64, *model.AppError)
AnalyticsActiveCountForPeriod(startTime int64, endTime int64, options model.UserCountOptions) (int64, error)
GetUnreadCount(userId string) (int64, *model.AppError)
GetUnreadCountForChannel(userId string, channelId string) (int64, *model.AppError)
GetAnyUnreadPostCountForChannel(userId string, channelId string) (int64, *model.AppError)

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

@@ -38,6 +38,27 @@ func (_m *UserStore) AnalyticsActiveCount(time int64, options model.UserCountOpt
return r0, r1
}
// AnalyticsActiveCountForPeriod provides a mock function with given fields: startTime, endTime, options
func (_m *UserStore) AnalyticsActiveCountForPeriod(startTime int64, endTime int64, options model.UserCountOptions) (int64, error) {
ret := _m.Called(startTime, endTime, options)
var r0 int64
if rf, ok := ret.Get(0).(func(int64, int64, model.UserCountOptions) int64); ok {
r0 = rf(startTime, endTime, options)
} else {
r0 = ret.Get(0).(int64)
}
var r1 error
if rf, ok := ret.Get(1).(func(int64, int64, model.UserCountOptions) error); ok {
r1 = rf(startTime, endTime, options)
} else {
r1 = ret.Error(1)
}
return r0, r1
}
// AnalyticsGetExternalUsers provides a mock function with given fields: hostDomain
func (_m *UserStore) AnalyticsGetExternalUsers(hostDomain string) (bool, *model.AppError) {
ret := _m.Called(hostDomain)

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

@@ -36,6 +36,7 @@ func TestUserStore(t *testing.T, ss store.Store, s SqlSupplier) {
t.Run("Count", func(t *testing.T) { testCount(t, ss) })
t.Run("AnalyticsActiveCount", func(t *testing.T) { testUserStoreAnalyticsActiveCount(t, ss, s) })
t.Run("AnalyticsActiveCountForPeriod", func(t *testing.T) { testUserStoreAnalyticsActiveCountForPeriod(t, ss, s) })
t.Run("AnalyticsGetInactiveUsersCount", func(t *testing.T) { testUserStoreAnalyticsGetInactiveUsersCount(t, ss) })
t.Run("AnalyticsGetSystemAdminCount", func(t *testing.T) { testUserStoreAnalyticsGetSystemAdminCount(t, ss) })
t.Run("AnalyticsGetGuestCount", func(t *testing.T) { testUserStoreAnalyticsGetGuestCount(t, ss) })
@@ -3828,7 +3829,89 @@ func testUserStoreAnalyticsActiveCount(t *testing.T, ss store.Store, s SqlSuppli
count, err = ss.User().AnalyticsActiveCount(MONTH_MILLISECONDS, model.UserCountOptions{IncludeBotAccounts: true, IncludeDeleted: false})
require.Nil(t, err)
assert.Equal(t, int64(4), count)
}
func testUserStoreAnalyticsActiveCountForPeriod(t *testing.T, ss store.Store, s SqlSupplier) {
cleanupStatusStore(t, s)
// Create 5 users statuses u0, u1, u2, u3, u4.
// u4 is also a bot
u0, err := ss.User().Save(&model.User{
Email: MakeEmail(),
Username: "u0" + model.NewId(),
})
require.Nil(t, err)
u1, err := ss.User().Save(&model.User{
Email: MakeEmail(),
Username: "u1" + model.NewId(),
})
require.Nil(t, err)
u2, err := ss.User().Save(&model.User{
Email: MakeEmail(),
Username: "u2" + model.NewId(),
})
require.Nil(t, err)
u3, err := ss.User().Save(&model.User{
Email: MakeEmail(),
Username: "u3" + model.NewId(),
})
require.Nil(t, err)
u4, err := ss.User().Save(&model.User{
Email: MakeEmail(),
Username: "u4" + model.NewId(),
})
require.Nil(t, err)
defer func() {
require.Nil(t, ss.User().PermanentDelete(u0.Id))
require.Nil(t, ss.User().PermanentDelete(u1.Id))
require.Nil(t, ss.User().PermanentDelete(u2.Id))
require.Nil(t, ss.User().PermanentDelete(u3.Id))
require.Nil(t, ss.User().PermanentDelete(u4.Id))
}()
_, nErr := ss.Bot().Save(&model.Bot{
UserId: u4.Id,
Username: u4.Username,
OwnerId: u1.Id,
})
require.Nil(t, nErr)
millis := model.GetMillis()
millisTwoDaysAgo := model.GetMillis() - (2 * DAY_MILLISECONDS)
millisTwoMonthsAgo := model.GetMillis() - (2 * MONTH_MILLISECONDS)
// u0 last activity status is two months ago.
// u1 last activity status is one month ago
// u2 last activiy is two days ago
// u2 last activity is one day ago
// u3 last activity is within last day
// u4 last activity is within last day
require.Nil(t, ss.Status().SaveOrUpdate(&model.Status{UserId: u0.Id, Status: model.STATUS_OFFLINE, LastActivityAt: millisTwoMonthsAgo}))
require.Nil(t, ss.Status().SaveOrUpdate(&model.Status{UserId: u1.Id, Status: model.STATUS_OFFLINE, LastActivityAt: millisTwoMonthsAgo + MONTH_MILLISECONDS}))
require.Nil(t, ss.Status().SaveOrUpdate(&model.Status{UserId: u2.Id, Status: model.STATUS_OFFLINE, LastActivityAt: millisTwoDaysAgo}))
require.Nil(t, ss.Status().SaveOrUpdate(&model.Status{UserId: u3.Id, Status: model.STATUS_OFFLINE, LastActivityAt: millisTwoDaysAgo + DAY_MILLISECONDS}))
require.Nil(t, ss.Status().SaveOrUpdate(&model.Status{UserId: u4.Id, Status: model.STATUS_OFFLINE, LastActivityAt: millis}))
// Two months to two days (without bots)
count, nerr := ss.User().AnalyticsActiveCountForPeriod(millisTwoMonthsAgo, millisTwoDaysAgo, model.UserCountOptions{IncludeBotAccounts: false, IncludeDeleted: false})
require.Nil(t, nerr)
assert.Equal(t, int64(2), count)
// Two months to two days (without bots)
count, nerr = ss.User().AnalyticsActiveCountForPeriod(millisTwoMonthsAgo, millisTwoDaysAgo, model.UserCountOptions{IncludeBotAccounts: false, IncludeDeleted: true})
require.Nil(t, nerr)
assert.Equal(t, int64(2), count)
// Two days to present - (with bots)
count, nerr = ss.User().AnalyticsActiveCountForPeriod(millisTwoDaysAgo, millis, model.UserCountOptions{IncludeBotAccounts: true, IncludeDeleted: false})
require.Nil(t, nerr)
assert.Equal(t, int64(2), count)
// Two days to present - (with bots, excluding deleted)
count, nerr = ss.User().AnalyticsActiveCountForPeriod(millisTwoDaysAgo, millis, model.UserCountOptions{IncludeBotAccounts: true, IncludeDeleted: true})
require.Nil(t, nerr)
assert.Equal(t, int64(2), count)
}
func testUserStoreAnalyticsGetInactiveUsersCount(t *testing.T, ss store.Store) {

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

@@ -7021,6 +7021,22 @@ func (s *TimerLayerUserStore) AnalyticsActiveCount(time int64, options model.Use
return result, err
}
func (s *TimerLayerUserStore) AnalyticsActiveCountForPeriod(startTime int64, endTime int64, options model.UserCountOptions) (int64, error) {
start := timemodule.Now()
result, err := s.UserStore.AnalyticsActiveCountForPeriod(startTime, endTime, options)
elapsed := float64(timemodule.Since(start)) / float64(timemodule.Second)
if s.Root.Metrics != nil {
success := "false"
if err == nil {
success = "true"
}
s.Root.Metrics.ObserveStoreMethodDuration("UserStore.AnalyticsActiveCountForPeriod", success, elapsed)
}
return result, err
}
func (s *TimerLayerUserStore) AnalyticsGetExternalUsers(hostDomain string) (bool, *model.AppError) {
start := timemodule.Now()