mirror of
https://github.com/grafana/grafana.git
synced 2025-02-10 07:35:45 -06:00
Chore: Remove pkg/models/stats.go (#61613)
* remove pkg/models/stats.go * rename models package in tests
This commit is contained in:
parent
1b86a49622
commit
efed0151f8
@ -7,6 +7,7 @@ import (
|
||||
"github.com/grafana/grafana/pkg/api/response"
|
||||
"github.com/grafana/grafana/pkg/models"
|
||||
ac "github.com/grafana/grafana/pkg/services/accesscontrol"
|
||||
"github.com/grafana/grafana/pkg/services/stats"
|
||||
"github.com/grafana/grafana/pkg/services/user"
|
||||
"github.com/grafana/grafana/pkg/setting"
|
||||
)
|
||||
@ -45,7 +46,7 @@ func (hs *HTTPServer) AdminGetSettings(c *models.ReqContext) response.Response {
|
||||
// 403: forbiddenError
|
||||
// 500: internalServerError
|
||||
func (hs *HTTPServer) AdminGetStats(c *models.ReqContext) response.Response {
|
||||
statsQuery := models.GetAdminStatsQuery{}
|
||||
statsQuery := stats.GetAdminStatsQuery{}
|
||||
|
||||
if err := hs.statsService.GetAdminStats(c.Req.Context(), &statsQuery); err != nil {
|
||||
return response.Error(500, "Failed to get admin stats from database", err)
|
||||
@ -108,5 +109,5 @@ type GetSettingsResponse struct {
|
||||
// swagger:response adminGetStatsResponse
|
||||
type GetStatsResponse struct {
|
||||
// in:body
|
||||
Body models.AdminStats `json:"body"`
|
||||
Body stats.AdminStats `json:"body"`
|
||||
}
|
||||
|
@ -18,6 +18,7 @@ import (
|
||||
"github.com/grafana/grafana/pkg/api/routing"
|
||||
"github.com/grafana/grafana/pkg/components/simplejson"
|
||||
"github.com/grafana/grafana/pkg/infra/db"
|
||||
"github.com/grafana/grafana/pkg/infra/db/dbtest"
|
||||
"github.com/grafana/grafana/pkg/infra/usagestats"
|
||||
"github.com/grafana/grafana/pkg/models"
|
||||
"github.com/grafana/grafana/pkg/plugins"
|
||||
@ -63,7 +64,7 @@ func TestGetHomeDashboard(t *testing.T) {
|
||||
hs := &HTTPServer{
|
||||
Cfg: cfg,
|
||||
pluginStore: &plugins.FakePluginStore{},
|
||||
SQLStore: mockstore.NewSQLStoreMock(),
|
||||
SQLStore: dbtest.NewFakeDB(),
|
||||
preferenceService: prefService,
|
||||
dashboardVersionService: dashboardVersionService,
|
||||
Kinds: corekind.NewBase(nil),
|
||||
@ -140,7 +141,7 @@ func TestDashboardAPIEndpoint(t *testing.T) {
|
||||
q := args.Get(1).(*dashboards.GetDashboardQuery)
|
||||
q.Result = fakeDash
|
||||
}).Return(nil)
|
||||
mockSQLStore := mockstore.NewSQLStoreMock()
|
||||
mockSQLStore := dbtest.NewFakeDB()
|
||||
|
||||
hs := &HTTPServer{
|
||||
Cfg: setting.NewCfg(),
|
||||
@ -259,7 +260,7 @@ func TestDashboardAPIEndpoint(t *testing.T) {
|
||||
}
|
||||
}).Return(nil)
|
||||
|
||||
mockSQLStore := mockstore.NewSQLStoreMock()
|
||||
mockSQLStore := dbtest.NewFakeDB()
|
||||
cfg := setting.NewCfg()
|
||||
sql := db.InitTestDB(t)
|
||||
|
||||
@ -803,7 +804,7 @@ func TestDashboardAPIEndpoint(t *testing.T) {
|
||||
}),
|
||||
},
|
||||
}
|
||||
sqlmock := mockstore.SQLStoreMock{}
|
||||
sqlmock := dbtest.NewFakeDB()
|
||||
setUp := func() {
|
||||
teamSvc := &teamtest.FakeService{}
|
||||
dashSvc := dashboards.NewFakeDashboardService(t)
|
||||
@ -815,7 +816,7 @@ func TestDashboardAPIEndpoint(t *testing.T) {
|
||||
ID: q.ID,
|
||||
}
|
||||
}).Return(nil)
|
||||
guardian.InitLegacyGuardian(&sqlmock, dashSvc, teamSvc)
|
||||
guardian.InitLegacyGuardian(sqlmock, dashSvc, teamSvc)
|
||||
}
|
||||
|
||||
cmd := dtos.CalculateDiffOptions{
|
||||
@ -837,7 +838,7 @@ func TestDashboardAPIEndpoint(t *testing.T) {
|
||||
|
||||
callPostDashboard(sc)
|
||||
assert.Equal(t, 403, sc.resp.Code)
|
||||
}, &sqlmock, fakeDashboardVersionService)
|
||||
}, sqlmock, fakeDashboardVersionService)
|
||||
})
|
||||
|
||||
t.Run("when user does have permission", func(t *testing.T) {
|
||||
@ -847,7 +848,7 @@ func TestDashboardAPIEndpoint(t *testing.T) {
|
||||
sc.dashboardVersionService = fakeDashboardVersionService
|
||||
callPostDashboard(sc)
|
||||
assert.Equal(t, 200, sc.resp.Code)
|
||||
}, &sqlmock, fakeDashboardVersionService)
|
||||
}, sqlmock, fakeDashboardVersionService)
|
||||
})
|
||||
})
|
||||
|
||||
|
@ -13,6 +13,7 @@ import (
|
||||
"github.com/grafana/grafana/pkg/models"
|
||||
"github.com/grafana/grafana/pkg/services/contexthandler/ctxkey"
|
||||
"github.com/grafana/grafana/pkg/services/sqlstore/mockstore"
|
||||
"github.com/grafana/grafana/pkg/services/stats"
|
||||
"github.com/grafana/grafana/pkg/services/user"
|
||||
"github.com/grafana/grafana/pkg/setting"
|
||||
"github.com/grafana/grafana/pkg/web"
|
||||
@ -49,10 +50,10 @@ func TestApi_getUsageStats(t *testing.T) {
|
||||
uss := createService(t, setting.Cfg{}, sqlStore, false)
|
||||
uss.registerAPIEndpoints()
|
||||
|
||||
sqlStore.ExpectedSystemStats = &models.SystemStats{}
|
||||
sqlStore.ExpectedDataSourceStats = []*models.DataSourceStats{}
|
||||
sqlStore.ExpectedDataSourcesAccessStats = []*models.DataSourceAccessStats{}
|
||||
sqlStore.ExpectedNotifierUsageStats = []*models.NotifierUsageStats{}
|
||||
sqlStore.ExpectedSystemStats = &stats.SystemStats{}
|
||||
sqlStore.ExpectedDataSourceStats = []*stats.DataSourceStats{}
|
||||
sqlStore.ExpectedDataSourcesAccessStats = []*stats.DataSourceAccessStats{}
|
||||
sqlStore.ExpectedNotifierUsageStats = []*stats.NotifierUsageStats{}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.desc, func(t *testing.T) {
|
||||
@ -69,13 +70,13 @@ func TestApi_getUsageStats(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func getUsageStats(t *testing.T, server *web.Mux) (*models.SystemStats, *httptest.ResponseRecorder) {
|
||||
func getUsageStats(t *testing.T, server *web.Mux) (*stats.SystemStats, *httptest.ResponseRecorder) {
|
||||
req, err := http.NewRequest(http.MethodGet, "/api/admin/usage-report-preview", http.NoBody)
|
||||
require.NoError(t, err)
|
||||
recorder := httptest.NewRecorder()
|
||||
server.ServeHTTP(recorder, req)
|
||||
|
||||
var usageStats models.SystemStats
|
||||
var usageStats stats.SystemStats
|
||||
if recorder.Code == http.StatusOK {
|
||||
require.NoError(t, json.NewDecoder(recorder.Body).Decode(&usageStats))
|
||||
}
|
||||
|
@ -14,7 +14,6 @@ import (
|
||||
"github.com/grafana/grafana/pkg/infra/metrics"
|
||||
"github.com/grafana/grafana/pkg/infra/usagestats"
|
||||
"github.com/grafana/grafana/pkg/login/social"
|
||||
"github.com/grafana/grafana/pkg/models"
|
||||
"github.com/grafana/grafana/pkg/plugins"
|
||||
"github.com/grafana/grafana/pkg/registry"
|
||||
"github.com/grafana/grafana/pkg/services/datasources"
|
||||
@ -109,7 +108,7 @@ func (s *Service) Run(ctx context.Context) error {
|
||||
func (s *Service) collectSystemStats(ctx context.Context) (map[string]interface{}, error) {
|
||||
m := map[string]interface{}{}
|
||||
|
||||
statsQuery := models.GetSystemStatsQuery{}
|
||||
statsQuery := stats.GetSystemStatsQuery{}
|
||||
if err := s.statsService.GetSystemStats(ctx, &statsQuery); err != nil {
|
||||
s.log.Error("Failed to get system stats", "error", err)
|
||||
return nil, err
|
||||
@ -222,7 +221,7 @@ func (s *Service) collectAdditionalMetrics(ctx context.Context) (map[string]inte
|
||||
func (s *Service) collectAlertNotifierStats(ctx context.Context) (map[string]interface{}, error) {
|
||||
m := map[string]interface{}{}
|
||||
// get stats about alert notifier usage
|
||||
anStats := models.GetAlertNotifierUsageStatsQuery{}
|
||||
anStats := stats.GetAlertNotifierUsageStatsQuery{}
|
||||
if err := s.statsService.GetAlertNotifiersUsageStats(ctx, &anStats); err != nil {
|
||||
s.log.Error("Failed to get alert notification stats", "error", err)
|
||||
return nil, err
|
||||
@ -236,7 +235,7 @@ func (s *Service) collectAlertNotifierStats(ctx context.Context) (map[string]int
|
||||
|
||||
func (s *Service) collectDatasourceStats(ctx context.Context) (map[string]interface{}, error) {
|
||||
m := map[string]interface{}{}
|
||||
dsStats := models.GetDataSourceStatsQuery{}
|
||||
dsStats := stats.GetDataSourceStatsQuery{}
|
||||
if err := s.statsService.GetDataSourceStats(ctx, &dsStats); err != nil {
|
||||
s.log.Error("Failed to get datasource stats", "error", err)
|
||||
return nil, err
|
||||
@ -283,7 +282,7 @@ func (s *Service) collectDatasourceAccess(ctx context.Context) (map[string]inter
|
||||
m := map[string]interface{}{}
|
||||
|
||||
// fetch datasource access stats
|
||||
dsAccessStats := models.GetDataSourceAccessStatsQuery{}
|
||||
dsAccessStats := stats.GetDataSourceAccessStatsQuery{}
|
||||
if err := s.statsService.GetDataSourceAccessStats(ctx, &dsAccessStats); err != nil {
|
||||
s.log.Error("Failed to get datasource access stats", "error", err)
|
||||
return nil, err
|
||||
@ -319,7 +318,7 @@ func (s *Service) updateTotalStats(ctx context.Context) bool {
|
||||
return false
|
||||
}
|
||||
|
||||
statsQuery := models.GetSystemStatsQuery{}
|
||||
statsQuery := stats.GetSystemStatsQuery{}
|
||||
if err := s.statsService.GetSystemStats(ctx, &statsQuery); err != nil {
|
||||
s.log.Error("Failed to get system stats", "error", err)
|
||||
return false
|
||||
@ -354,7 +353,7 @@ func (s *Service) updateTotalStats(ctx context.Context) bool {
|
||||
|
||||
metrics.MStatTotalPublicDashboards.Set(float64(statsQuery.Result.PublicDashboards))
|
||||
|
||||
dsStats := models.GetDataSourceStatsQuery{}
|
||||
dsStats := stats.GetDataSourceStatsQuery{}
|
||||
if err := s.statsService.GetDataSourceStats(ctx, &dsStats); err != nil {
|
||||
s.log.Error("Failed to get datasource stats", "error", err)
|
||||
return true
|
||||
|
@ -18,7 +18,6 @@ import (
|
||||
"github.com/grafana/grafana/pkg/infra/httpclient"
|
||||
"github.com/grafana/grafana/pkg/infra/usagestats"
|
||||
"github.com/grafana/grafana/pkg/login/social"
|
||||
"github.com/grafana/grafana/pkg/models"
|
||||
"github.com/grafana/grafana/pkg/plugins"
|
||||
"github.com/grafana/grafana/pkg/registry"
|
||||
"github.com/grafana/grafana/pkg/services/datasources"
|
||||
@ -36,7 +35,7 @@ func TestTotalStatsUpdate(t *testing.T) {
|
||||
s.cfg.MetricsEndpointEnabled = true
|
||||
s.cfg.MetricsEndpointDisableTotalStats = false
|
||||
|
||||
statsService.ExpectedSystemStats = &models.SystemStats{}
|
||||
statsService.ExpectedSystemStats = &stats.SystemStats{}
|
||||
|
||||
tests := []struct {
|
||||
MetricsEndpointEnabled bool
|
||||
@ -254,7 +253,7 @@ func TestDatasourceStats(t *testing.T) {
|
||||
|
||||
setupSomeDataSourcePlugins(t, s)
|
||||
|
||||
statsService.ExpectedDataSourceStats = []*models.DataSourceStats{
|
||||
statsService.ExpectedDataSourceStats = []*stats.DataSourceStats{
|
||||
{
|
||||
Type: datasources.DS_ES,
|
||||
Count: 9,
|
||||
@ -291,7 +290,7 @@ func TestDatasourceStats(t *testing.T) {
|
||||
},
|
||||
}
|
||||
|
||||
statsService.ExpectedDataSourcesAccessStats = []*models.DataSourceAccessStats{
|
||||
statsService.ExpectedDataSourcesAccessStats = []*stats.DataSourceAccessStats{
|
||||
{
|
||||
Type: datasources.DS_ES,
|
||||
Access: "direct",
|
||||
@ -360,7 +359,7 @@ func TestAlertNotifiersStats(t *testing.T) {
|
||||
statsService := statstest.NewFakeService()
|
||||
s := createService(t, &setting.Cfg{}, sqlStore, statsService)
|
||||
|
||||
statsService.ExpectedNotifierUsageStats = []*models.NotifierUsageStats{
|
||||
statsService.ExpectedNotifierUsageStats = []*stats.NotifierUsageStats{
|
||||
{
|
||||
Type: "slack",
|
||||
Count: 1,
|
||||
@ -379,7 +378,7 @@ func TestAlertNotifiersStats(t *testing.T) {
|
||||
}
|
||||
|
||||
func mockSystemStats(statsService *statstest.FakeService) {
|
||||
statsService.ExpectedSystemStats = &models.SystemStats{
|
||||
statsService.ExpectedSystemStats = &stats.SystemStats{
|
||||
Dashboards: 1,
|
||||
Datasources: 2,
|
||||
Users: 3,
|
||||
|
@ -9,6 +9,7 @@ import (
|
||||
"github.com/grafana/grafana/pkg/services/sqlstore"
|
||||
"github.com/grafana/grafana/pkg/services/sqlstore/migrator"
|
||||
"github.com/grafana/grafana/pkg/services/sqlstore/session"
|
||||
"github.com/grafana/grafana/pkg/services/stats"
|
||||
"github.com/grafana/grafana/pkg/services/team"
|
||||
"github.com/grafana/grafana/pkg/services/user"
|
||||
)
|
||||
@ -23,10 +24,10 @@ type SQLStoreMock struct {
|
||||
ExpectedUser *user.User
|
||||
ExpectedTeamsByUser []*team.TeamDTO
|
||||
ExpectedAlert *models.Alert
|
||||
ExpectedSystemStats *models.SystemStats
|
||||
ExpectedDataSourceStats []*models.DataSourceStats
|
||||
ExpectedDataSourcesAccessStats []*models.DataSourceAccessStats
|
||||
ExpectedNotifierUsageStats []*models.NotifierUsageStats
|
||||
ExpectedSystemStats *stats.SystemStats
|
||||
ExpectedDataSourceStats []*stats.DataSourceStats
|
||||
ExpectedDataSourcesAccessStats []*stats.DataSourceAccessStats
|
||||
ExpectedNotifierUsageStats []*stats.NotifierUsageStats
|
||||
ExpectedSignedInUser *user.SignedInUser
|
||||
|
||||
ExpectedError error
|
||||
@ -36,30 +37,6 @@ func NewSQLStoreMock() *SQLStoreMock {
|
||||
return &SQLStoreMock{}
|
||||
}
|
||||
|
||||
func (m *SQLStoreMock) GetAdminStats(ctx context.Context, query *models.GetAdminStatsQuery) error {
|
||||
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) GetDialect() migrator.Dialect {
|
||||
return nil
|
||||
}
|
||||
|
@ -24,13 +24,13 @@ import (
|
||||
"github.com/grafana/grafana/pkg/infra/localcache"
|
||||
"github.com/grafana/grafana/pkg/infra/log"
|
||||
"github.com/grafana/grafana/pkg/infra/tracing"
|
||||
"github.com/grafana/grafana/pkg/models"
|
||||
"github.com/grafana/grafana/pkg/registry"
|
||||
"github.com/grafana/grafana/pkg/services/featuremgmt"
|
||||
"github.com/grafana/grafana/pkg/services/sqlstore/migrations"
|
||||
"github.com/grafana/grafana/pkg/services/sqlstore/migrator"
|
||||
"github.com/grafana/grafana/pkg/services/sqlstore/session"
|
||||
"github.com/grafana/grafana/pkg/services/sqlstore/sqlutil"
|
||||
"github.com/grafana/grafana/pkg/services/stats"
|
||||
"github.com/grafana/grafana/pkg/services/user"
|
||||
"github.com/grafana/grafana/pkg/setting"
|
||||
"github.com/grafana/grafana/pkg/util"
|
||||
@ -202,7 +202,7 @@ func (ss *SQLStore) ensureMainOrgAndAdminUser(test bool) error {
|
||||
|
||||
// If this is a test database, don't exit early when any user is found.
|
||||
if !test {
|
||||
var stats models.SystemUserCountStats
|
||||
var stats stats.SystemUserCountStats
|
||||
// TODO: Should be able to rename "Count" to "count", for more standard SQL style
|
||||
// Just have to make sure it gets deserialized properly into models.SystemUserCountStats
|
||||
rawSQL := `SELECT COUNT(id) AS Count FROM ` + ss.Dialect.Quote("user")
|
||||
|
@ -1,4 +1,4 @@
|
||||
package models
|
||||
package stats
|
||||
|
||||
type SystemStats struct {
|
||||
Dashboards int64
|
@ -2,15 +2,13 @@ package stats
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/grafana/grafana/pkg/models"
|
||||
)
|
||||
|
||||
type Service 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
|
||||
GetSystemUserCountStats(ctx context.Context, query *models.GetSystemUserCountStatsQuery) error
|
||||
GetAdminStats(ctx context.Context, query *GetAdminStatsQuery) error
|
||||
GetAlertNotifiersUsageStats(ctx context.Context, query *GetAlertNotifierUsageStatsQuery) error
|
||||
GetDataSourceStats(ctx context.Context, query *GetDataSourceStatsQuery) error
|
||||
GetDataSourceAccessStats(ctx context.Context, query *GetDataSourceAccessStatsQuery) error
|
||||
GetSystemStats(ctx context.Context, query *GetSystemStatsQuery) error
|
||||
GetSystemUserCountStats(ctx context.Context, query *GetSystemUserCountStatsQuery) error
|
||||
}
|
||||
|
@ -21,28 +21,28 @@ func ProvideService(db db.DB) stats.Service {
|
||||
|
||||
type sqlStatsService struct{ db db.DB }
|
||||
|
||||
func (ss *sqlStatsService) GetAlertNotifiersUsageStats(ctx context.Context, query *models.GetAlertNotifierUsageStatsQuery) error {
|
||||
func (ss *sqlStatsService) GetAlertNotifiersUsageStats(ctx context.Context, query *stats.GetAlertNotifierUsageStatsQuery) error {
|
||||
return ss.db.WithDbSession(ctx, func(dbSession *db.Session) error {
|
||||
var rawSQL = `SELECT COUNT(*) AS count, type FROM ` + ss.db.GetDialect().Quote("alert_notification") + ` GROUP BY type`
|
||||
query.Result = make([]*models.NotifierUsageStats, 0)
|
||||
query.Result = make([]*stats.NotifierUsageStats, 0)
|
||||
err := dbSession.SQL(rawSQL).Find(&query.Result)
|
||||
return err
|
||||
})
|
||||
}
|
||||
|
||||
func (ss *sqlStatsService) GetDataSourceStats(ctx context.Context, query *models.GetDataSourceStatsQuery) error {
|
||||
func (ss *sqlStatsService) GetDataSourceStats(ctx context.Context, query *stats.GetDataSourceStatsQuery) error {
|
||||
return ss.db.WithDbSession(ctx, func(dbSession *db.Session) error {
|
||||
var rawSQL = `SELECT COUNT(*) AS count, type FROM ` + ss.db.GetDialect().Quote("data_source") + ` GROUP BY type`
|
||||
query.Result = make([]*models.DataSourceStats, 0)
|
||||
query.Result = make([]*stats.DataSourceStats, 0)
|
||||
err := dbSession.SQL(rawSQL).Find(&query.Result)
|
||||
return err
|
||||
})
|
||||
}
|
||||
|
||||
func (ss *sqlStatsService) GetDataSourceAccessStats(ctx context.Context, query *models.GetDataSourceAccessStatsQuery) error {
|
||||
func (ss *sqlStatsService) GetDataSourceAccessStats(ctx context.Context, query *stats.GetDataSourceAccessStatsQuery) error {
|
||||
return ss.db.WithDbSession(ctx, func(dbSession *db.Session) error {
|
||||
var rawSQL = `SELECT COUNT(*) AS count, type, access FROM ` + ss.db.GetDialect().Quote("data_source") + ` GROUP BY type, access`
|
||||
query.Result = make([]*models.DataSourceAccessStats, 0)
|
||||
query.Result = make([]*stats.DataSourceAccessStats, 0)
|
||||
err := dbSession.SQL(rawSQL).Find(&query.Result)
|
||||
return err
|
||||
})
|
||||
@ -53,7 +53,7 @@ func notServiceAccount(dialect migrator.Dialect) string {
|
||||
dialect.BooleanStr(false)
|
||||
}
|
||||
|
||||
func (ss *sqlStatsService) GetSystemStats(ctx context.Context, query *models.GetSystemStatsQuery) error {
|
||||
func (ss *sqlStatsService) GetSystemStats(ctx context.Context, query *stats.GetSystemStatsQuery) error {
|
||||
return ss.db.WithDbSession(ctx, func(dbSession *db.Session) error {
|
||||
sb := &db.SQLBuilder{}
|
||||
sb.Write("SELECT ")
|
||||
@ -120,7 +120,7 @@ func (ss *sqlStatsService) GetSystemStats(ctx context.Context, query *models.Get
|
||||
|
||||
sb.Write(ss.roleCounterSQL(ctx))
|
||||
|
||||
var stats models.SystemStats
|
||||
var stats stats.SystemStats
|
||||
_, err := dbSession.SQL(sb.GetSQLString(), sb.GetParams()...).Get(&stats)
|
||||
if err != nil {
|
||||
return err
|
||||
@ -164,7 +164,7 @@ func viewersPermissionsCounterSQL(db db.DB, statName string, isFolder bool, perm
|
||||
) AS ` + statName + `, `
|
||||
}
|
||||
|
||||
func (ss *sqlStatsService) GetAdminStats(ctx context.Context, query *models.GetAdminStatsQuery) error {
|
||||
func (ss *sqlStatsService) GetAdminStats(ctx context.Context, query *stats.GetAdminStatsQuery) error {
|
||||
return ss.db.WithDbSession(ctx, func(dbSession *db.Session) error {
|
||||
dialect := ss.db.GetDialect()
|
||||
now := time.Now()
|
||||
@ -231,7 +231,7 @@ func (ss *sqlStatsService) GetAdminStats(ctx context.Context, query *models.GetA
|
||||
FROM ` + dialect.Quote("user_auth_token") + ` WHERE rotated_at > ?
|
||||
) AS daily_active_sessions`
|
||||
|
||||
var stats models.AdminStats
|
||||
var stats stats.AdminStats
|
||||
_, err := dbSession.SQL(rawSQL, activeEndDate, dailyActiveEndDate, monthlyActiveEndDate, activeEndDate.Unix(), dailyActiveEndDate.Unix()).Get(&stats)
|
||||
if err != nil {
|
||||
return err
|
||||
@ -242,10 +242,10 @@ func (ss *sqlStatsService) GetAdminStats(ctx context.Context, query *models.GetA
|
||||
})
|
||||
}
|
||||
|
||||
func (ss *sqlStatsService) GetSystemUserCountStats(ctx context.Context, query *models.GetSystemUserCountStatsQuery) error {
|
||||
func (ss *sqlStatsService) GetSystemUserCountStats(ctx context.Context, query *stats.GetSystemUserCountStatsQuery) error {
|
||||
return ss.db.WithDbSession(ctx, func(sess *db.Session) error {
|
||||
var rawSQL = `SELECT COUNT(id) AS Count FROM ` + ss.db.GetDialect().Quote("user")
|
||||
var stats models.SystemUserCountStats
|
||||
var stats stats.SystemUserCountStats
|
||||
_, err := sess.SQL(rawSQL).Get(&stats)
|
||||
if err != nil {
|
||||
return err
|
||||
@ -269,9 +269,9 @@ func (ss *sqlStatsService) updateUserRoleCountsIfNecessary(ctx context.Context,
|
||||
}
|
||||
|
||||
type memoUserStats struct {
|
||||
active models.UserStats
|
||||
dailyActive models.UserStats
|
||||
total models.UserStats
|
||||
active stats.UserStats
|
||||
dailyActive stats.UserStats
|
||||
total stats.UserStats
|
||||
|
||||
memoized time.Time
|
||||
}
|
||||
@ -338,7 +338,7 @@ GROUP BY active, daily_active, role;`
|
||||
})
|
||||
}
|
||||
|
||||
func addToStats(base models.UserStats, role org.RoleType, count int64) models.UserStats {
|
||||
func addToStats(base stats.UserStats, role org.RoleType, count int64) stats.UserStats {
|
||||
base.Users += count
|
||||
|
||||
switch role {
|
||||
|
@ -8,11 +8,11 @@ import (
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
|
||||
"github.com/grafana/grafana/pkg/models"
|
||||
"github.com/grafana/grafana/pkg/services/org"
|
||||
"github.com/grafana/grafana/pkg/services/org/orgimpl"
|
||||
"github.com/grafana/grafana/pkg/services/quota/quotatest"
|
||||
"github.com/grafana/grafana/pkg/services/sqlstore"
|
||||
"github.com/grafana/grafana/pkg/services/stats"
|
||||
"github.com/grafana/grafana/pkg/services/user"
|
||||
"github.com/grafana/grafana/pkg/services/user/userimpl"
|
||||
)
|
||||
@ -26,7 +26,7 @@ func TestIntegrationStatsDataAccess(t *testing.T) {
|
||||
populateDB(t, db)
|
||||
|
||||
t.Run("Get system stats should not results in error", func(t *testing.T) {
|
||||
query := models.GetSystemStatsQuery{}
|
||||
query := stats.GetSystemStatsQuery{}
|
||||
err := statsService.GetSystemStats(context.Background(), &query)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, int64(3), query.Result.Users)
|
||||
@ -39,31 +39,31 @@ func TestIntegrationStatsDataAccess(t *testing.T) {
|
||||
})
|
||||
|
||||
t.Run("Get system user count stats should not results in error", func(t *testing.T) {
|
||||
query := models.GetSystemUserCountStatsQuery{}
|
||||
query := stats.GetSystemUserCountStatsQuery{}
|
||||
err := statsService.GetSystemUserCountStats(context.Background(), &query)
|
||||
assert.NoError(t, err)
|
||||
})
|
||||
|
||||
t.Run("Get datasource stats should not results in error", func(t *testing.T) {
|
||||
query := models.GetDataSourceStatsQuery{}
|
||||
query := stats.GetDataSourceStatsQuery{}
|
||||
err := statsService.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{}
|
||||
query := stats.GetDataSourceAccessStatsQuery{}
|
||||
err := statsService.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{}
|
||||
query := stats.GetAlertNotifierUsageStatsQuery{}
|
||||
err := statsService.GetAlertNotifiersUsageStats(context.Background(), &query)
|
||||
assert.NoError(t, err)
|
||||
})
|
||||
|
||||
t.Run("Get admin stats should not result in error", func(t *testing.T) {
|
||||
query := models.GetAdminStatsQuery{}
|
||||
query := stats.GetAdminStatsQuery{}
|
||||
err := statsService.GetAdminStats(context.Background(), &query)
|
||||
assert.NoError(t, err)
|
||||
})
|
||||
@ -123,7 +123,7 @@ func TestIntegration_GetAdminStats(t *testing.T) {
|
||||
db := sqlstore.InitTestDB(t)
|
||||
statsService := ProvideService(db)
|
||||
|
||||
query := models.GetAdminStatsQuery{}
|
||||
query := stats.GetAdminStatsQuery{}
|
||||
err := statsService.GetAdminStats(context.Background(), &query)
|
||||
require.NoError(t, err)
|
||||
}
|
||||
|
@ -3,14 +3,14 @@ package statstest
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/grafana/grafana/pkg/models"
|
||||
"github.com/grafana/grafana/pkg/services/stats"
|
||||
)
|
||||
|
||||
type FakeService struct {
|
||||
ExpectedSystemStats *models.SystemStats
|
||||
ExpectedDataSourceStats []*models.DataSourceStats
|
||||
ExpectedDataSourcesAccessStats []*models.DataSourceAccessStats
|
||||
ExpectedNotifierUsageStats []*models.NotifierUsageStats
|
||||
ExpectedSystemStats *stats.SystemStats
|
||||
ExpectedDataSourceStats []*stats.DataSourceStats
|
||||
ExpectedDataSourcesAccessStats []*stats.DataSourceAccessStats
|
||||
ExpectedNotifierUsageStats []*stats.NotifierUsageStats
|
||||
|
||||
ExpectedError error
|
||||
}
|
||||
@ -19,30 +19,30 @@ func NewFakeService() *FakeService {
|
||||
return &FakeService{}
|
||||
}
|
||||
|
||||
func (s *FakeService) GetAdminStats(ctx context.Context, query *models.GetAdminStatsQuery) error {
|
||||
func (s *FakeService) GetAdminStats(ctx context.Context, query *stats.GetAdminStatsQuery) error {
|
||||
return s.ExpectedError
|
||||
}
|
||||
|
||||
func (s *FakeService) GetAlertNotifiersUsageStats(ctx context.Context, query *models.GetAlertNotifierUsageStatsQuery) error {
|
||||
func (s *FakeService) GetAlertNotifiersUsageStats(ctx context.Context, query *stats.GetAlertNotifierUsageStatsQuery) error {
|
||||
query.Result = s.ExpectedNotifierUsageStats
|
||||
return s.ExpectedError
|
||||
}
|
||||
|
||||
func (s *FakeService) GetDataSourceStats(ctx context.Context, query *models.GetDataSourceStatsQuery) error {
|
||||
func (s *FakeService) GetDataSourceStats(ctx context.Context, query *stats.GetDataSourceStatsQuery) error {
|
||||
query.Result = s.ExpectedDataSourceStats
|
||||
return s.ExpectedError
|
||||
}
|
||||
|
||||
func (s *FakeService) GetDataSourceAccessStats(ctx context.Context, query *models.GetDataSourceAccessStatsQuery) error {
|
||||
func (s *FakeService) GetDataSourceAccessStats(ctx context.Context, query *stats.GetDataSourceAccessStatsQuery) error {
|
||||
query.Result = s.ExpectedDataSourcesAccessStats
|
||||
return s.ExpectedError
|
||||
}
|
||||
|
||||
func (s *FakeService) GetSystemStats(ctx context.Context, query *models.GetSystemStatsQuery) error {
|
||||
func (s *FakeService) GetSystemStats(ctx context.Context, query *stats.GetSystemStatsQuery) error {
|
||||
query.Result = s.ExpectedSystemStats
|
||||
return s.ExpectedError
|
||||
}
|
||||
|
||||
func (s *FakeService) GetSystemUserCountStats(ctx context.Context, query *models.GetSystemUserCountStatsQuery) error {
|
||||
func (s *FakeService) GetSystemUserCountStats(ctx context.Context, query *stats.GetSystemUserCountStatsQuery) error {
|
||||
return s.ExpectedError
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user