Stats: Stop counting the same user multiple times (#26777)
* Stats: Cache based stats implementation * Stats: Correct logic and add larger scale test * Stats: linter * Stats: SQL implementation * Stats: cleanup SQL * Stats: Tab -> Spaces * Update pkg/services/sqlstore/stats.go Co-authored-by: Sofia Papagiannaki <papagian@users.noreply.github.com> * Stats: Quote 'user' table with dialect.Quote * Stats: Ensure test is run as integration test * Stats: Use boolean value ...because if (v) { true } else { false } is unnecessary at best. Co-authored-by: Sofia Papagiannaki <papagian@users.noreply.github.com>
This commit is contained in:
co-authored by
Sofia Papagiannaki
parent
09a1af3f91
commit
954a2811b3
@@ -0,0 +1,71 @@
|
||||
// +build integration
|
||||
|
||||
package sqlstore
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"testing"
|
||||
|
||||
"github.com/grafana/grafana/pkg/models"
|
||||
"github.com/grafana/grafana/pkg/setting"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
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
|
||||
}()
|
||||
}
|
||||
|
||||
users := make([]models.User, 5)
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
query := models.GetUserStatsQuery{
|
||||
MustUpdate: true,
|
||||
}
|
||||
err = GetUserStats(&query)
|
||||
require.NoError(t, err)
|
||||
assert.EqualValues(t, models.UserStats{
|
||||
Users: 6,
|
||||
Admins: 1,
|
||||
Editors: 5,
|
||||
Viewers: 0,
|
||||
}, query.Result)
|
||||
}
|
||||
Reference in New Issue
Block a user