Add usage stats about correlations

This commit is contained in:
Piotr Jamróz 2023-04-05 14:59:19 +02:00
parent defc230b2c
commit 6f0c70084a
4 changed files with 47 additions and 0 deletions

View File

@ -0,0 +1,23 @@
package correlationstest
import (
"github.com/grafana/grafana/pkg/api/routing"
"github.com/grafana/grafana/pkg/services/accesscontrol/acimpl"
"github.com/grafana/grafana/pkg/services/correlations"
"github.com/grafana/grafana/pkg/services/datasources"
fakeDatasources "github.com/grafana/grafana/pkg/services/datasources/fakes"
"github.com/grafana/grafana/pkg/services/quota/quotatest"
"github.com/grafana/grafana/pkg/services/sqlstore"
"github.com/grafana/grafana/pkg/setting"
)
func New(sqlStore *sqlstore.SQLStore) *correlations.CorrelationsService {
ds := &fakeDatasources.FakeDataSourceService{
DataSources: []*datasources.DataSource{
{ID: 1, UID: "graphite", Type: datasources.DS_GRAPHITE},
},
}
correlationsSvc, _ := correlations.ProvideService(sqlStore, routing.NewRouteRegister(), ds, acimpl.ProvideAccessControl(setting.NewCfg()), sqlStore.Bus(), quotatest.New(false, nil), sqlStore.Cfg)
return correlationsSvc
}

View File

@ -42,6 +42,7 @@ type SystemStats struct {
DataKeys int64
ActiveDataKeys int64
PublicDashboards int64
Correlations int64
}
type DataSourceStats struct {

View File

@ -73,6 +73,7 @@ func (ss *sqlStatsService) GetSystemStats(ctx context.Context, query *stats.GetS
sb.Write(`(SELECT COUNT(*) FROM ` + dialect.Quote("star") + `) AS stars,`)
sb.Write(`(SELECT COUNT(*) FROM ` + dialect.Quote("playlist") + `) AS playlists,`)
sb.Write(`(SELECT COUNT(*) FROM ` + dialect.Quote("alert") + `) AS alerts,`)
sb.Write(`(SELECT COUNT(*) FROM ` + dialect.Quote("correlation") + `) AS correlations,`)
now := time.Now()
activeUserDeadlineDate := now.Add(-activeUserTimeLimit)

View File

@ -3,6 +3,8 @@ package statsimpl
import (
"context"
"fmt"
"github.com/grafana/grafana/pkg/services/correlations"
"github.com/grafana/grafana/pkg/services/correlations/correlationstest"
"testing"
"github.com/stretchr/testify/assert"
@ -38,6 +40,7 @@ func TestIntegrationStatsDataAccess(t *testing.T) {
assert.Equal(t, int64(0), result.LibraryPanels)
assert.Equal(t, int64(0), result.LibraryVariables)
assert.Equal(t, int64(0), result.APIKeys)
assert.Equal(t, int64(2), result.Correlations)
})
t.Run("Get system user count stats should not results in error", func(t *testing.T) {
@ -77,6 +80,25 @@ func populateDB(t *testing.T, sqlStore *sqlstore.SQLStore) {
orgService, _ := orgimpl.ProvideService(sqlStore, sqlStore.Cfg, quotatest.New(false, nil))
userSvc, _ := userimpl.ProvideService(sqlStore, orgService, sqlStore.Cfg, nil, nil, &quotatest.FakeQuotaService{}, supportbundlestest.NewFakeBundleService())
correlationsSvc := correlationstest.New(sqlStore)
c := make([]correlations.Correlation, 2)
for i := range c {
cmd := correlations.CreateCorrelationCommand{
Label: fmt.Sprintf("correlation %v", i),
SourceUID: "graphite",
OrgId: 1,
Config: correlations.CorrelationConfig{
Field: "field",
Target: map[string]interface{}{},
Type: correlations.ConfigTypeQuery,
},
}
correlation, err := correlationsSvc.CreateCorrelation(context.Background(), cmd)
require.NoError(t, err)
c[i] = correlation
}
users := make([]user.User, 3)
for i := range users {
cmd := user.CreateUserCommand{