Commit 1d399b89 by Vardan Torosyan Committed by GitHub

Usage Stats: Remove unused method for getting user stats (#30074)

* Usage Stats: Remove unused method for getting user stats

This was used in enterprise codebase, but is not longer in use, thus can be safely removed.

* Force refresh user stats in tests
parent b0b9158a
......@@ -98,9 +98,3 @@ type UserStats struct {
Editors int64
Viewers int64
}
type GetUserStatsQuery struct {
MustUpdate bool
Active bool
Result UserStats
}
......@@ -14,7 +14,6 @@ func init() {
bus.AddHandler("sql", GetDataSourceStats)
bus.AddHandler("sql", GetDataSourceAccessStats)
bus.AddHandler("sql", GetAdminStats)
bus.AddHandlerCtx("sql", GetUserStats)
bus.AddHandlerCtx("sql", GetAlertNotifiersUsageStats)
bus.AddHandlerCtx("sql", GetSystemUserCountStats)
}
......@@ -185,21 +184,6 @@ func GetSystemUserCountStats(ctx context.Context, query *models.GetSystemUserCou
})
}
func GetUserStats(ctx context.Context, query *models.GetUserStatsQuery) error {
err := updateUserRoleCountsIfNecessary(ctx, query.MustUpdate)
if err != nil {
return err
}
if query.Active {
query.Result = userStatsCache.active
} else {
query.Result = userStatsCache.total
}
return nil
}
func updateUserRoleCountsIfNecessary(ctx context.Context, forced bool) error {
memoizationPeriod := time.Now().Add(-userStatsCacheLimetime)
if forced || userStatsCache.memoized.Before(memoizationPeriod) {
......
......@@ -3,14 +3,9 @@
package sqlstore
import (
"context"
"fmt"
"testing"
"time"
"github.com/grafana/grafana/pkg/models"
"github.com/grafana/grafana/pkg/setting"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
......@@ -21,89 +16,3 @@ func TestIntegration_GetAdminStats(t *testing.T) {
err := GetAdminStats(&query)
require.NoError(t, err)
}
func TestIntegration_GetUserStats(t *testing.T) {
InitTestDB(t)
cmd := &models.CreateUserCommand{
Email: "admin@test.com",
Name: "Admin",
Login: "admin",
OrgName: MainOrgName,
IsAdmin: true,
}
err := CreateUser(context.Background(), cmd)
require.NoError(t, err)
firstUser := cmd.Result
{
defaultAutoAssign := setting.AutoAssignOrg
defaultOrgID := setting.AutoAssignOrgId
defaultRole := setting.AutoAssignOrgRole
setting.AutoAssignOrg = true
setting.AutoAssignOrgId = int(firstUser.OrgId)
setting.AutoAssignOrgRole = "Editor"
defer func() {
setting.AutoAssignOrg = defaultAutoAssign
setting.AutoAssignOrgId = defaultOrgID
setting.AutoAssignOrgRole = defaultRole
}()
}
const nUsers = 100
users := make([]models.User, nUsers)
for i := range users {
cmd := &models.CreateUserCommand{
Email: fmt.Sprintf("usertest%v@test.com", i),
Name: fmt.Sprintf("user name %v", i),
Login: fmt.Sprintf("user_test_%v_login", i),
OrgId: firstUser.OrgId,
}
err := CreateUser(context.Background(), cmd)
require.NoError(t, err)
users[i] = cmd.Result
}
orgs := make([]models.Org, 10)
for i := range orgs {
cmd := &models.CreateOrgCommand{
Name: fmt.Sprintf("org %d", i),
UserId: firstUser.Id,
}
err := CreateOrg(cmd)
require.NoError(t, err)
orgs[i] = cmd.Result
}
for _, u := range users {
for _, o := range orgs {
cmd := &models.AddOrgUserCommand{
Role: "Viewer",
UserId: u.Id,
OrgId: o.Id,
}
err := AddOrgUser(cmd)
require.NoErrorf(t, err, "uID %d oID %d", u.Id, o.Id)
}
}
query := models.GetUserStatsQuery{
MustUpdate: true,
}
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
err = GetUserStats(ctx, &query)
require.NoError(t, err)
assert.EqualValues(t, models.UserStats{
Users: nUsers + 1,
Admins: 1,
Editors: nUsers,
Viewers: 0,
}, query.Result)
}
......@@ -55,19 +55,6 @@ func TestStatsDataAccess(t *testing.T) {
err := GetAdminStats(&query)
assert.NoError(t, err)
})
t.Run("Get active user count stats should not result in error", func(t *testing.T) {
query := models.GetUserStatsQuery{
MustUpdate: true,
Active: true,
}
err := GetUserStats(context.Background(), &query)
require.NoError(t, err)
assert.Equal(t, int64(1), query.Result.Users)
assert.Equal(t, int64(1), query.Result.Admins)
assert.Equal(t, int64(0), query.Result.Editors)
assert.Equal(t, int64(0), query.Result.Viewers)
})
}
func populateDB(t *testing.T) {
......@@ -131,10 +118,6 @@ func populateDB(t *testing.T) {
require.NoError(t, err)
// force renewal of user stats
query := models.GetUserStatsQuery{
MustUpdate: true,
Active: true,
}
err = GetUserStats(context.Background(), &query)
err = updateUserRoleCountsIfNecessary(context.Background(), true)
require.NoError(t, err)
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment