Usage Stats: Rename service to use a more idiomatic name

This commit is contained in:
Joan López de la Franca Beltran 2021-05-25 23:43:37 +02:00
parent 063e1b5ff5
commit 910ecce3e8
4 changed files with 20 additions and 21 deletions

View File

@ -6,21 +6,20 @@ import (
"time"
"github.com/grafana/grafana/pkg/bus"
"github.com/grafana/grafana/pkg/infra/log"
"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/alerting"
"github.com/grafana/grafana/pkg/services/sqlstore"
"github.com/grafana/grafana/pkg/infra/log"
"github.com/grafana/grafana/pkg/registry"
"github.com/grafana/grafana/pkg/setting"
)
var metricsLogger log.Logger = log.New("metrics")
func init() {
registry.RegisterService(&UsageStatsService{
registry.RegisterService(&Service{
log: log.New("infra.usagestats"),
externalMetrics: make(map[string]MetricFunc),
})
@ -33,7 +32,7 @@ type UsageStats interface {
type MetricFunc func() (interface{}, error)
type UsageStatsService struct {
type Service struct {
Cfg *setting.Cfg `inject:""`
Bus bus.Bus `inject:""`
SQLStore *sqlstore.SQLStore `inject:""`
@ -48,12 +47,12 @@ type UsageStatsService struct {
concurrentUserStatsCache memoConcurrentUserStats
}
func (uss *UsageStatsService) Init() error {
func (uss *Service) Init() error {
uss.oauthProviders = social.GetOAuthProviders(uss.Cfg)
return nil
}
func (uss *UsageStatsService) Run(ctx context.Context) error {
func (uss *Service) Run(ctx context.Context) error {
uss.updateTotalStats()
sendReportTicker := time.NewTicker(time.Hour * 24)
@ -83,7 +82,7 @@ type memoConcurrentUserStats struct {
const concurrentUserStatsCacheLifetime = time.Hour
func (uss *UsageStatsService) GetConcurrentUsersStats(ctx context.Context) (*concurrentUsersStats, error) {
func (uss *Service) GetConcurrentUsersStats(ctx context.Context) (*concurrentUsersStats, error) {
memoizationPeriod := time.Now().Add(-concurrentUserStatsCacheLifetime)
if !uss.concurrentUserStatsCache.memoized.Before(memoizationPeriod) {
return uss.concurrentUserStatsCache.stats, nil

View File

@ -26,7 +26,7 @@ type UsageReport struct {
Packaging string `json:"packaging"`
}
func (uss *UsageStatsService) GetUsageReport(ctx context.Context) (UsageReport, error) {
func (uss *Service) GetUsageReport(ctx context.Context) (UsageReport, error) {
version := strings.ReplaceAll(uss.Cfg.BuildVersion, ".", "_")
metrics := map[string]interface{}{}
@ -239,7 +239,7 @@ func (uss *UsageStatsService) GetUsageReport(ctx context.Context) (UsageReport,
return report, nil
}
func (uss *UsageStatsService) registerExternalMetrics(metrics map[string]interface{}) {
func (uss *Service) registerExternalMetrics(metrics map[string]interface{}) {
for name, fn := range uss.externalMetrics {
result, err := fn()
if err != nil {
@ -250,11 +250,11 @@ func (uss *UsageStatsService) registerExternalMetrics(metrics map[string]interfa
}
}
func (uss *UsageStatsService) RegisterMetric(name string, fn MetricFunc) {
func (uss *Service) RegisterMetric(name string, fn MetricFunc) {
uss.externalMetrics[name] = fn
}
func (uss *UsageStatsService) sendUsageStats(ctx context.Context) error {
func (uss *Service) sendUsageStats(ctx context.Context) error {
if !uss.Cfg.ReportingEnabled {
return nil
}
@ -293,7 +293,7 @@ var sendUsageStats = func(data *bytes.Buffer) {
}()
}
func (uss *UsageStatsService) updateTotalStats() {
func (uss *Service) updateTotalStats() {
if !uss.Cfg.MetricsEndpointEnabled || uss.Cfg.MetricsEndpointDisableTotalStats {
return
}
@ -333,7 +333,7 @@ func (uss *UsageStatsService) updateTotalStats() {
}
}
func (uss *UsageStatsService) shouldBeReported(dsType string) bool {
func (uss *Service) shouldBeReported(dsType string) bool {
ds := uss.PluginManager.GetDataSource(dsType)
if ds == nil {
return false

View File

@ -16,9 +16,9 @@ import (
"github.com/stretchr/testify/require"
)
func TestUsageStatsService_GetConcurrentUsersStats(t *testing.T) {
func TestService_GetConcurrentUsersStats(t *testing.T) {
sqlStore := sqlstore.InitTestDB(t)
uss := &UsageStatsService{
uss := &Service{
Bus: bus.New(),
SQLStore: sqlStore,
License: &licensing.OSSLicensingService{},

View File

@ -29,9 +29,9 @@ import (
// This is to ensure that the interface contract is held by the implementation
func Test_InterfaceContractValidity(t *testing.T) {
newUsageStats := func() UsageStats {
return &UsageStatsService{}
return &Service{}
}
v, ok := newUsageStats().(*UsageStatsService)
v, ok := newUsageStats().(*Service)
assert.NotNil(t, v)
assert.True(t, ok)
@ -567,7 +567,7 @@ func (pm *fakePluginManager) PanelCount() int {
return len(pm.panels)
}
func setupSomeDataSourcePlugins(t *testing.T, uss *UsageStatsService) {
func setupSomeDataSourcePlugins(t *testing.T, uss *Service) {
t.Helper()
uss.PluginManager = &fakePluginManager{
@ -610,10 +610,10 @@ type httpResp struct {
err error
}
func createService(t *testing.T, cfg setting.Cfg) *UsageStatsService {
func createService(t *testing.T, cfg setting.Cfg) *Service {
t.Helper()
return &UsageStatsService{
return &Service{
Bus: bus.New(),
Cfg: &cfg,
SQLStore: sqlstore.InitTestDB(t),