2022-11-30 11:11:07 -06:00
|
|
|
package statsimpl
|
2018-05-25 07:33:37 -05:00
|
|
|
|
|
|
|
import (
|
2018-06-10 16:17:18 -05:00
|
|
|
"context"
|
2019-12-02 02:14:20 -06:00
|
|
|
"fmt"
|
2018-05-25 07:33:37 -05:00
|
|
|
"testing"
|
|
|
|
|
2022-12-07 10:03:22 -06:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
|
2023-04-11 01:53:34 -05:00
|
|
|
"github.com/grafana/grafana/pkg/services/correlations"
|
|
|
|
"github.com/grafana/grafana/pkg/services/correlations/correlationstest"
|
2022-08-10 04:56:48 -05:00
|
|
|
"github.com/grafana/grafana/pkg/services/org"
|
2022-11-30 11:11:07 -06:00
|
|
|
"github.com/grafana/grafana/pkg/services/org/orgimpl"
|
|
|
|
"github.com/grafana/grafana/pkg/services/quota/quotatest"
|
|
|
|
"github.com/grafana/grafana/pkg/services/sqlstore"
|
2023-01-17 07:17:54 -06:00
|
|
|
"github.com/grafana/grafana/pkg/services/stats"
|
2023-02-06 10:50:03 -06:00
|
|
|
"github.com/grafana/grafana/pkg/services/supportbundles/supportbundlestest"
|
2022-06-28 07:32:25 -05:00
|
|
|
"github.com/grafana/grafana/pkg/services/user"
|
2022-12-07 10:03:22 -06:00
|
|
|
"github.com/grafana/grafana/pkg/services/user/userimpl"
|
2023-03-17 06:19:18 -05:00
|
|
|
"github.com/grafana/grafana/pkg/setting"
|
2018-05-25 07:33:37 -05:00
|
|
|
)
|
|
|
|
|
2022-05-24 04:04:03 -05:00
|
|
|
func TestIntegrationStatsDataAccess(t *testing.T) {
|
2022-06-10 10:46:21 -05:00
|
|
|
if testing.Short() {
|
|
|
|
t.Skip("skipping integration test")
|
|
|
|
}
|
2022-11-30 11:11:07 -06:00
|
|
|
db := sqlstore.InitTestDB(t)
|
|
|
|
statsService := &sqlStatsService{db: db}
|
|
|
|
populateDB(t, db)
|
2020-08-24 04:23:14 -05:00
|
|
|
|
|
|
|
t.Run("Get system stats should not results in error", func(t *testing.T) {
|
2023-01-17 07:17:54 -06:00
|
|
|
query := stats.GetSystemStatsQuery{}
|
2023-03-30 01:08:47 -05:00
|
|
|
result, err := statsService.GetSystemStats(context.Background(), &query)
|
2020-08-24 04:23:14 -05:00
|
|
|
require.NoError(t, err)
|
2023-03-30 01:08:47 -05:00
|
|
|
assert.Equal(t, int64(3), result.Users)
|
|
|
|
assert.Equal(t, int64(0), result.Editors)
|
|
|
|
assert.Equal(t, int64(0), result.Viewers)
|
|
|
|
assert.Equal(t, int64(3), result.Admins)
|
|
|
|
assert.Equal(t, int64(0), result.LibraryPanels)
|
|
|
|
assert.Equal(t, int64(0), result.LibraryVariables)
|
|
|
|
assert.Equal(t, int64(0), result.APIKeys)
|
2023-04-11 01:53:34 -05:00
|
|
|
assert.Equal(t, int64(2), result.Correlations)
|
2023-04-17 20:27:04 -05:00
|
|
|
assert.NotNil(t, result.DatabaseCreatedTime)
|
|
|
|
assert.Equal(t, db.Dialect.DriverName(), result.DatabaseDriver)
|
2020-08-24 04:23:14 -05:00
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("Get system user count stats should not results in error", func(t *testing.T) {
|
2023-01-17 07:17:54 -06:00
|
|
|
query := stats.GetSystemUserCountStatsQuery{}
|
2023-03-30 01:08:47 -05:00
|
|
|
_, err := statsService.GetSystemUserCountStats(context.Background(), &query)
|
2020-08-24 04:23:14 -05:00
|
|
|
assert.NoError(t, err)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("Get datasource stats should not results in error", func(t *testing.T) {
|
2023-01-17 07:17:54 -06:00
|
|
|
query := stats.GetDataSourceStatsQuery{}
|
2023-03-30 01:08:47 -05:00
|
|
|
_, err := statsService.GetDataSourceStats(context.Background(), &query)
|
2020-08-24 04:23:14 -05:00
|
|
|
assert.NoError(t, err)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("Get datasource access stats should not results in error", func(t *testing.T) {
|
2023-01-17 07:17:54 -06:00
|
|
|
query := stats.GetDataSourceAccessStatsQuery{}
|
2023-03-30 01:08:47 -05:00
|
|
|
_, err := statsService.GetDataSourceAccessStats(context.Background(), &query)
|
2020-08-24 04:23:14 -05:00
|
|
|
assert.NoError(t, err)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("Get alert notifier stats should not results in error", func(t *testing.T) {
|
2023-01-17 07:17:54 -06:00
|
|
|
query := stats.GetAlertNotifierUsageStatsQuery{}
|
2023-03-30 01:08:47 -05:00
|
|
|
_, err := statsService.GetAlertNotifiersUsageStats(context.Background(), &query)
|
2020-08-24 04:23:14 -05:00
|
|
|
assert.NoError(t, err)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("Get admin stats should not result in error", func(t *testing.T) {
|
2023-01-17 07:17:54 -06:00
|
|
|
query := stats.GetAdminStatsQuery{}
|
2023-03-30 01:08:47 -05:00
|
|
|
_, err := statsService.GetAdminStats(context.Background(), &query)
|
2020-08-24 04:23:14 -05:00
|
|
|
assert.NoError(t, err)
|
|
|
|
})
|
2018-05-25 07:33:37 -05:00
|
|
|
}
|
2019-12-02 02:14:20 -06:00
|
|
|
|
2022-11-30 11:11:07 -06:00
|
|
|
func populateDB(t *testing.T, sqlStore *sqlstore.SQLStore) {
|
2021-03-17 10:06:10 -05:00
|
|
|
t.Helper()
|
|
|
|
|
2022-11-30 11:11:07 -06:00
|
|
|
orgService, _ := orgimpl.ProvideService(sqlStore, sqlStore.Cfg, quotatest.New(false, nil))
|
2023-02-06 10:50:03 -06:00
|
|
|
userSvc, _ := userimpl.ProvideService(sqlStore, orgService, sqlStore.Cfg, nil, nil, "atest.FakeQuotaService{}, supportbundlestest.NewFakeBundleService())
|
2022-11-30 11:11:07 -06:00
|
|
|
|
2023-04-11 01:53:34 -05:00
|
|
|
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",
|
2023-08-30 10:46:47 -05:00
|
|
|
Target: map[string]any{},
|
2023-04-11 01:53:34 -05:00
|
|
|
Type: correlations.ConfigTypeQuery,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
correlation, err := correlationsSvc.CreateCorrelation(context.Background(), cmd)
|
|
|
|
require.NoError(t, err)
|
|
|
|
c[i] = correlation
|
|
|
|
}
|
|
|
|
|
2022-06-28 07:32:25 -05:00
|
|
|
users := make([]user.User, 3)
|
2019-12-02 02:14:20 -06:00
|
|
|
for i := range users {
|
2022-06-28 07:32:25 -05:00
|
|
|
cmd := user.CreateUserCommand{
|
2019-12-02 02:14:20 -06:00
|
|
|
Email: fmt.Sprintf("usertest%v@test.com", i),
|
|
|
|
Name: fmt.Sprintf("user name %v", i),
|
|
|
|
Login: fmt.Sprintf("user_test_%v_login", i),
|
|
|
|
OrgName: fmt.Sprintf("Org #%v", i),
|
|
|
|
}
|
2023-03-03 10:01:23 -06:00
|
|
|
user, err := userSvc.Create(context.Background(), &cmd)
|
2020-03-05 05:34:44 -06:00
|
|
|
require.NoError(t, err)
|
2021-03-17 10:06:10 -05:00
|
|
|
users[i] = *user
|
2019-12-02 02:14:20 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// add 2nd user as editor
|
2022-11-30 11:11:07 -06:00
|
|
|
cmd := &org.AddOrgUserCommand{
|
|
|
|
OrgID: users[0].OrgID,
|
|
|
|
UserID: users[1].ID,
|
2022-08-10 04:56:48 -05:00
|
|
|
Role: org.RoleEditor,
|
2019-12-02 02:14:20 -06:00
|
|
|
}
|
2022-11-30 11:11:07 -06:00
|
|
|
err := orgService.AddOrgUser(context.Background(), cmd)
|
2020-03-05 05:34:44 -06:00
|
|
|
require.NoError(t, err)
|
2019-12-02 02:14:20 -06:00
|
|
|
|
|
|
|
// add 3rd user as viewer
|
2022-11-30 11:11:07 -06:00
|
|
|
cmd = &org.AddOrgUserCommand{
|
|
|
|
OrgID: users[0].OrgID,
|
|
|
|
UserID: users[2].ID,
|
2022-08-10 04:56:48 -05:00
|
|
|
Role: org.RoleViewer,
|
2019-12-02 02:14:20 -06:00
|
|
|
}
|
2022-11-30 11:11:07 -06:00
|
|
|
err = orgService.AddOrgUser(context.Background(), cmd)
|
2020-03-05 05:34:44 -06:00
|
|
|
require.NoError(t, err)
|
2019-12-02 02:14:20 -06:00
|
|
|
|
|
|
|
// add 1st user as admin
|
2022-11-30 11:11:07 -06:00
|
|
|
cmd = &org.AddOrgUserCommand{
|
|
|
|
OrgID: users[1].OrgID,
|
|
|
|
UserID: users[0].ID,
|
2022-08-10 04:56:48 -05:00
|
|
|
Role: org.RoleAdmin,
|
2019-12-02 02:14:20 -06:00
|
|
|
}
|
2022-11-30 11:11:07 -06:00
|
|
|
err = orgService.AddOrgUser(context.Background(), cmd)
|
2020-03-05 05:34:44 -06:00
|
|
|
require.NoError(t, err)
|
2022-11-30 11:11:07 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestIntegration_GetAdminStats(t *testing.T) {
|
|
|
|
if testing.Short() {
|
|
|
|
t.Skip("skipping integration test")
|
|
|
|
}
|
|
|
|
db := sqlstore.InitTestDB(t)
|
2023-03-17 06:19:18 -05:00
|
|
|
statsService := ProvideService(&setting.Cfg{}, db)
|
2020-03-05 05:34:44 -06:00
|
|
|
|
2023-01-17 07:17:54 -06:00
|
|
|
query := stats.GetAdminStatsQuery{}
|
2023-03-30 01:08:47 -05:00
|
|
|
_, err := statsService.GetAdminStats(context.Background(), &query)
|
2020-08-24 04:23:14 -05:00
|
|
|
require.NoError(t, err)
|
2019-12-02 02:14:20 -06:00
|
|
|
}
|