Remove bus from usage stats (#45275)

This commit is contained in:
idafurjes
2022-02-11 14:04:15 +01:00
committed by GitHub
parent 0e8a5407d1
commit d8a56d08ba
11 changed files with 224 additions and 336 deletions

View File

@@ -13,23 +13,28 @@ type OrgListResponse []struct {
Response error
}
type SQLStoreMock struct {
LastGetAlertsQuery *models.GetAlertsQuery
LatestUserId int64
ExpectedUser *models.User
ExpectedDatasource *models.DataSource
ExpectedAlert *models.Alert
ExpectedPluginSetting *models.PluginSetting
ExpectedDashboard *models.Dashboard
ExpectedDashboards []*models.Dashboard
ExpectedDashboardVersions []*models.DashboardVersion
ExpectedDashboardAclInfoList []*models.DashboardAclInfoDTO
ExpectedUserOrgList []*models.UserOrgDTO
ExpectedOrgListResponse OrgListResponse
ExpectedDashboardSnapshot *models.DashboardSnapshot
ExpectedTeamsByUser []*models.TeamDTO
ExpectedSearchOrgList []*models.OrgDTO
ExpectedDatasources []*models.DataSource
ExpectedOrg *models.Org
LastGetAlertsQuery *models.GetAlertsQuery
LatestUserId int64
ExpectedUser *models.User
ExpectedDatasource *models.DataSource
ExpectedAlert *models.Alert
ExpectedPluginSetting *models.PluginSetting
ExpectedDashboard *models.Dashboard
ExpectedDashboards []*models.Dashboard
ExpectedDashboardVersions []*models.DashboardVersion
ExpectedDashboardAclInfoList []*models.DashboardAclInfoDTO
ExpectedUserOrgList []*models.UserOrgDTO
ExpectedOrgListResponse OrgListResponse
ExpectedDashboardSnapshot *models.DashboardSnapshot
ExpectedTeamsByUser []*models.TeamDTO
ExpectedSearchOrgList []*models.OrgDTO
ExpectedDatasources []*models.DataSource
ExpectedOrg *models.Org
ExpectedSystemStats *models.SystemStats
ExpectedDataSourceStats []*models.DataSourceStats
ExpectedDataSources []*models.DataSource
ExpectedDataSourcesAccessStats []*models.DataSourceAccessStats
ExpectedNotifierUsageStats []*models.NotifierUsageStats
ExpectedError error
}
@@ -42,6 +47,26 @@ func (m *SQLStoreMock) GetAdminStats(ctx context.Context, query *models.GetAdmin
return m.ExpectedError
}
func (m *SQLStoreMock) GetAlertNotifiersUsageStats(ctx context.Context, query *models.GetAlertNotifierUsageStatsQuery) error {
query.Result = m.ExpectedNotifierUsageStats
return m.ExpectedError
}
func (m *SQLStoreMock) GetDataSourceStats(ctx context.Context, query *models.GetDataSourceStatsQuery) error {
query.Result = m.ExpectedDataSourceStats
return m.ExpectedError
}
func (m *SQLStoreMock) GetDataSourceAccessStats(ctx context.Context, query *models.GetDataSourceAccessStatsQuery) error {
query.Result = m.ExpectedDataSourcesAccessStats
return m.ExpectedError
}
func (m *SQLStoreMock) GetSystemStats(ctx context.Context, query *models.GetSystemStatsQuery) error {
query.Result = m.ExpectedSystemStats
return m.ExpectedError
}
func (m *SQLStoreMock) DeleteExpiredSnapshots(ctx context.Context, cmd *models.DeleteExpiredSnapshotsCommand) error {
return m.ExpectedError
}

View File

@@ -10,42 +10,42 @@ import (
)
func init() {
bus.AddHandler("sql", GetSystemStats)
bus.AddHandler("sql", GetDataSourceStats)
bus.AddHandler("sql", GetDataSourceAccessStats)
bus.AddHandler("sql", GetAlertNotifiersUsageStats)
bus.AddHandler("sql", GetSystemUserCountStats)
}
func (ss *SQLStore) addStatsQueryAndCommandHandlers() {
bus.AddHandler("sql", ss.GetAdminStats)
bus.AddHandler("sql", ss.GetAlertNotifiersUsageStats)
bus.AddHandler("sql", ss.GetDataSourceAccessStats)
bus.AddHandler("sql", ss.GetDataSourceStats)
bus.AddHandler("sql", ss.GetSystemStats)
}
const activeUserTimeLimit = time.Hour * 24 * 30
const dailyActiveUserTimeLimit = time.Hour * 24
func GetAlertNotifiersUsageStats(ctx context.Context, query *models.GetAlertNotifierUsageStatsQuery) error {
func (ss *SQLStore) GetAlertNotifiersUsageStats(ctx context.Context, query *models.GetAlertNotifierUsageStatsQuery) error {
var rawSQL = `SELECT COUNT(*) AS count, type FROM ` + dialect.Quote("alert_notification") + ` GROUP BY type`
query.Result = make([]*models.NotifierUsageStats, 0)
err := x.SQL(rawSQL).Find(&query.Result)
return err
}
func GetDataSourceStats(ctx context.Context, query *models.GetDataSourceStatsQuery) error {
func (ss *SQLStore) GetDataSourceStats(ctx context.Context, query *models.GetDataSourceStatsQuery) error {
var rawSQL = `SELECT COUNT(*) AS count, type FROM ` + dialect.Quote("data_source") + ` GROUP BY type`
query.Result = make([]*models.DataSourceStats, 0)
err := x.SQL(rawSQL).Find(&query.Result)
return err
}
func GetDataSourceAccessStats(ctx context.Context, query *models.GetDataSourceAccessStatsQuery) error {
func (ss *SQLStore) GetDataSourceAccessStats(ctx context.Context, query *models.GetDataSourceAccessStatsQuery) error {
var rawSQL = `SELECT COUNT(*) AS count, type, access FROM ` + dialect.Quote("data_source") + ` GROUP BY type, access`
query.Result = make([]*models.DataSourceAccessStats, 0)
err := x.SQL(rawSQL).Find(&query.Result)
return err
}
func GetSystemStats(ctx context.Context, query *models.GetSystemStatsQuery) error {
func (ss *SQLStore) GetSystemStats(ctx context.Context, query *models.GetSystemStatsQuery) error {
sb := &SQLBuilder{}
sb.Write("SELECT ")
sb.Write(`(SELECT COUNT(*) FROM ` + dialect.Quote("user") + `) AS users,`)

View File

@@ -19,7 +19,7 @@ func TestStatsDataAccess(t *testing.T) {
t.Run("Get system stats should not results in error", func(t *testing.T) {
query := models.GetSystemStatsQuery{}
err := GetSystemStats(context.Background(), &query)
err := sqlStore.GetSystemStats(context.Background(), &query)
require.NoError(t, err)
assert.Equal(t, int64(3), query.Result.Users)
assert.Equal(t, int64(0), query.Result.Editors)
@@ -38,19 +38,19 @@ func TestStatsDataAccess(t *testing.T) {
t.Run("Get datasource stats should not results in error", func(t *testing.T) {
query := models.GetDataSourceStatsQuery{}
err := GetDataSourceStats(context.Background(), &query)
err := sqlStore.GetDataSourceStats(context.Background(), &query)
assert.NoError(t, err)
})
t.Run("Get datasource access stats should not results in error", func(t *testing.T) {
query := models.GetDataSourceAccessStatsQuery{}
err := GetDataSourceAccessStats(context.Background(), &query)
err := sqlStore.GetDataSourceAccessStats(context.Background(), &query)
assert.NoError(t, err)
})
t.Run("Get alert notifier stats should not results in error", func(t *testing.T) {
query := models.GetAlertNotifierUsageStatsQuery{}
err := GetAlertNotifiersUsageStats(context.Background(), &query)
err := sqlStore.GetAlertNotifiersUsageStats(context.Background(), &query)
assert.NoError(t, err)
})

View File

@@ -9,6 +9,10 @@ import (
type Store interface {
GetAdminStats(ctx context.Context, query *models.GetAdminStatsQuery) error
GetAlertNotifiersUsageStats(ctx context.Context, query *models.GetAlertNotifierUsageStatsQuery) error
GetDataSourceStats(ctx context.Context, query *models.GetDataSourceStatsQuery) error
GetDataSourceAccessStats(ctx context.Context, query *models.GetDataSourceAccessStatsQuery) error
GetSystemStats(ctx context.Context, query *models.GetSystemStatsQuery) error
DeleteExpiredSnapshots(ctx context.Context, cmd *models.DeleteExpiredSnapshotsCommand) error
CreateDashboardSnapshot(ctx context.Context, cmd *models.CreateDashboardSnapshotCommand) error
DeleteDashboardSnapshot(ctx context.Context, cmd *models.DeleteDashboardSnapshotCommand) error