mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
Chore: Remove bus from admin (#44920)
* Chore: Remove bus from admin * fix test Co-authored-by: Ying WANG <ying.wang@grafana.com>
This commit is contained in:
parent
9d654bb6b8
commit
0e6300fb49
@ -5,7 +5,6 @@ import (
|
|||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
"github.com/grafana/grafana/pkg/api/response"
|
"github.com/grafana/grafana/pkg/api/response"
|
||||||
"github.com/grafana/grafana/pkg/bus"
|
|
||||||
"github.com/grafana/grafana/pkg/models"
|
"github.com/grafana/grafana/pkg/models"
|
||||||
ac "github.com/grafana/grafana/pkg/services/accesscontrol"
|
ac "github.com/grafana/grafana/pkg/services/accesscontrol"
|
||||||
"github.com/grafana/grafana/pkg/setting"
|
"github.com/grafana/grafana/pkg/setting"
|
||||||
@ -19,10 +18,10 @@ func (hs *HTTPServer) AdminGetSettings(c *models.ReqContext) response.Response {
|
|||||||
return response.JSON(http.StatusOK, settings)
|
return response.JSON(http.StatusOK, settings)
|
||||||
}
|
}
|
||||||
|
|
||||||
func AdminGetStats(c *models.ReqContext) response.Response {
|
func (hs *HTTPServer) AdminGetStats(c *models.ReqContext) response.Response {
|
||||||
statsQuery := models.GetAdminStatsQuery{}
|
statsQuery := models.GetAdminStatsQuery{}
|
||||||
|
|
||||||
if err := bus.Dispatch(c.Req.Context(), &statsQuery); err != nil {
|
if err := hs.SQLStore.GetAdminStats(c.Req.Context(), &statsQuery); err != nil {
|
||||||
return response.Error(500, "Failed to get admin stats from database", err)
|
return response.Error(500, "Failed to get admin stats from database", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -6,6 +6,7 @@ import (
|
|||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/grafana/grafana/pkg/services/accesscontrol"
|
"github.com/grafana/grafana/pkg/services/accesscontrol"
|
||||||
|
"github.com/grafana/grafana/pkg/services/sqlstore/mockstore"
|
||||||
"github.com/grafana/grafana/pkg/setting"
|
"github.com/grafana/grafana/pkg/setting"
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
)
|
)
|
||||||
@ -147,6 +148,7 @@ func TestAdmin_AccessControl(t *testing.T) {
|
|||||||
sc, hs := setupAccessControlScenarioContext(t, cfg, test.url, test.permissions)
|
sc, hs := setupAccessControlScenarioContext(t, cfg, test.url, test.permissions)
|
||||||
sc.resp = httptest.NewRecorder()
|
sc.resp = httptest.NewRecorder()
|
||||||
hs.SettingsProvider = &setting.OSSImpl{Cfg: cfg}
|
hs.SettingsProvider = &setting.OSSImpl{Cfg: cfg}
|
||||||
|
hs.SQLStore = mockstore.NewSQLStoreMock()
|
||||||
|
|
||||||
var err error
|
var err error
|
||||||
sc.req, err = http.NewRequest(test.method, test.url, nil)
|
sc.req, err = http.NewRequest(test.method, test.url, nil)
|
||||||
|
@ -463,7 +463,7 @@ func (hs *HTTPServer) registerRoutes() {
|
|||||||
if hs.Features.IsEnabled(featuremgmt.FlagShowFeatureFlagsInUI) {
|
if hs.Features.IsEnabled(featuremgmt.FlagShowFeatureFlagsInUI) {
|
||||||
adminRoute.Get("/settings/features", authorize(reqGrafanaAdmin, ac.EvalPermission(ac.ActionSettingsRead)), hs.Features.HandleGetSettings)
|
adminRoute.Get("/settings/features", authorize(reqGrafanaAdmin, ac.EvalPermission(ac.ActionSettingsRead)), hs.Features.HandleGetSettings)
|
||||||
}
|
}
|
||||||
adminRoute.Get("/stats", authorize(reqGrafanaAdmin, ac.EvalPermission(ac.ActionServerStatsRead)), routing.Wrap(AdminGetStats))
|
adminRoute.Get("/stats", authorize(reqGrafanaAdmin, ac.EvalPermission(ac.ActionServerStatsRead)), routing.Wrap(hs.AdminGetStats))
|
||||||
adminRoute.Post("/pause-all-alerts", reqGrafanaAdmin, routing.Wrap(hs.PauseAllAlerts))
|
adminRoute.Post("/pause-all-alerts", reqGrafanaAdmin, routing.Wrap(hs.PauseAllAlerts))
|
||||||
|
|
||||||
if hs.ThumbService != nil {
|
if hs.ThumbService != nil {
|
||||||
|
@ -23,6 +23,10 @@ func NewSQLStoreMock() *SQLStoreMock {
|
|||||||
return &SQLStoreMock{}
|
return &SQLStoreMock{}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (m *SQLStoreMock) GetAdminStats(ctx context.Context, query *models.GetAdminStatsQuery) error {
|
||||||
|
return m.ExpectedError
|
||||||
|
}
|
||||||
|
|
||||||
func (m *SQLStoreMock) DeleteExpiredSnapshots(ctx context.Context, cmd *models.DeleteExpiredSnapshotsCommand) error {
|
func (m *SQLStoreMock) DeleteExpiredSnapshots(ctx context.Context, cmd *models.DeleteExpiredSnapshotsCommand) error {
|
||||||
return m.ExpectedError
|
return m.ExpectedError
|
||||||
}
|
}
|
||||||
|
@ -115,6 +115,7 @@ func newSQLStore(cfg *setting.Cfg, cacheService *localcache.CacheService, b bus.
|
|||||||
ss.Bus.SetTransactionManager(ss)
|
ss.Bus.SetTransactionManager(ss)
|
||||||
|
|
||||||
// Register handlers
|
// Register handlers
|
||||||
|
ss.addStatsQueryAndCommandHandlers()
|
||||||
ss.addUserQueryAndCommandHandlers()
|
ss.addUserQueryAndCommandHandlers()
|
||||||
ss.addAlertNotificationUidByIdHandler()
|
ss.addAlertNotificationUidByIdHandler()
|
||||||
ss.addPreferencesQueryAndCommandHandlers()
|
ss.addPreferencesQueryAndCommandHandlers()
|
||||||
|
@ -13,11 +13,14 @@ func init() {
|
|||||||
bus.AddHandler("sql", GetSystemStats)
|
bus.AddHandler("sql", GetSystemStats)
|
||||||
bus.AddHandler("sql", GetDataSourceStats)
|
bus.AddHandler("sql", GetDataSourceStats)
|
||||||
bus.AddHandler("sql", GetDataSourceAccessStats)
|
bus.AddHandler("sql", GetDataSourceAccessStats)
|
||||||
bus.AddHandler("sql", GetAdminStats)
|
|
||||||
bus.AddHandler("sql", GetAlertNotifiersUsageStats)
|
bus.AddHandler("sql", GetAlertNotifiersUsageStats)
|
||||||
bus.AddHandler("sql", GetSystemUserCountStats)
|
bus.AddHandler("sql", GetSystemUserCountStats)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (ss *SQLStore) addStatsQueryAndCommandHandlers() {
|
||||||
|
bus.AddHandler("sql", ss.GetAdminStats)
|
||||||
|
}
|
||||||
|
|
||||||
const activeUserTimeLimit = time.Hour * 24 * 30
|
const activeUserTimeLimit = time.Hour * 24 * 30
|
||||||
const dailyActiveUserTimeLimit = time.Hour * 24
|
const dailyActiveUserTimeLimit = time.Hour * 24
|
||||||
|
|
||||||
@ -141,7 +144,7 @@ func viewersPermissionsCounterSQL(statName string, isFolder bool, permission mod
|
|||||||
) AS ` + statName + `, `
|
) AS ` + statName + `, `
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetAdminStats(ctx context.Context, query *models.GetAdminStatsQuery) error {
|
func (ss *SQLStore) GetAdminStats(ctx context.Context, query *models.GetAdminStatsQuery) error {
|
||||||
now := time.Now()
|
now := time.Now()
|
||||||
activeEndDate := now.Add(-activeUserTimeLimit)
|
activeEndDate := now.Add(-activeUserTimeLimit)
|
||||||
dailyActiveEndDate := now.Add(-dailyActiveUserTimeLimit)
|
dailyActiveEndDate := now.Add(-dailyActiveUserTimeLimit)
|
||||||
|
@ -12,9 +12,9 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func TestIntegration_GetAdminStats(t *testing.T) {
|
func TestIntegration_GetAdminStats(t *testing.T) {
|
||||||
InitTestDB(t)
|
sqlStore := InitTestDB(t)
|
||||||
|
|
||||||
query := models.GetAdminStatsQuery{}
|
query := models.GetAdminStatsQuery{}
|
||||||
err := GetAdminStats(context.Background(), &query)
|
err := sqlStore.GetAdminStats(context.Background(), &query)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
}
|
}
|
||||||
|
@ -56,7 +56,7 @@ func TestStatsDataAccess(t *testing.T) {
|
|||||||
|
|
||||||
t.Run("Get admin stats should not result in error", func(t *testing.T) {
|
t.Run("Get admin stats should not result in error", func(t *testing.T) {
|
||||||
query := models.GetAdminStatsQuery{}
|
query := models.GetAdminStatsQuery{}
|
||||||
err := GetAdminStats(context.Background(), &query)
|
err := sqlStore.GetAdminStats(context.Background(), &query)
|
||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
@ -8,6 +8,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type Store interface {
|
type Store interface {
|
||||||
|
GetAdminStats(ctx context.Context, query *models.GetAdminStatsQuery) error
|
||||||
DeleteExpiredSnapshots(ctx context.Context, cmd *models.DeleteExpiredSnapshotsCommand) error
|
DeleteExpiredSnapshots(ctx context.Context, cmd *models.DeleteExpiredSnapshotsCommand) error
|
||||||
CreateDashboardSnapshot(ctx context.Context, cmd *models.CreateDashboardSnapshotCommand) error
|
CreateDashboardSnapshot(ctx context.Context, cmd *models.CreateDashboardSnapshotCommand) error
|
||||||
DeleteDashboardSnapshot(ctx context.Context, cmd *models.DeleteDashboardSnapshotCommand) error
|
DeleteDashboardSnapshot(ctx context.Context, cmd *models.DeleteDashboardSnapshotCommand) error
|
||||||
|
Loading…
Reference in New Issue
Block a user