Commit ea3d368e by Agnès Toulet Committed by GitHub

Core: add active users stat (#22563)

* Stats: add active user count query

* License: add info in settings

* Stats: Retrieve active users stats for different roles

+ Clean files and tests

* Stats: add dialect.Quote in queries

* Stats: improve tests (require instead of assert)

* Stats: improve tests (require instead of assert)
parent c6cc840c
...@@ -89,3 +89,14 @@ type SystemUserCountStats struct { ...@@ -89,3 +89,14 @@ type SystemUserCountStats struct {
type GetSystemUserCountStatsQuery struct { type GetSystemUserCountStatsQuery struct {
Result *SystemUserCountStats Result *SystemUserCountStats
} }
type ActiveUserStats struct {
ActiveUsers int64
ActiveAdmins int64
ActiveEditors int64
ActiveViewers int64
}
type GetActiveUserStatsQuery struct {
Result *ActiveUserStats
}
...@@ -13,6 +13,7 @@ func init() { ...@@ -13,6 +13,7 @@ func init() {
bus.AddHandler("sql", GetDataSourceStats) bus.AddHandler("sql", GetDataSourceStats)
bus.AddHandler("sql", GetDataSourceAccessStats) bus.AddHandler("sql", GetDataSourceAccessStats)
bus.AddHandler("sql", GetAdminStats) bus.AddHandler("sql", GetAdminStats)
bus.AddHandler("sql", GetActiveUserStats)
bus.AddHandlerCtx("sql", GetAlertNotifiersUsageStats) bus.AddHandlerCtx("sql", GetAlertNotifiersUsageStats)
bus.AddHandlerCtx("sql", GetSystemUserCountStats) bus.AddHandlerCtx("sql", GetSystemUserCountStats)
} }
...@@ -20,21 +21,21 @@ func init() { ...@@ -20,21 +21,21 @@ func init() {
var activeUserTimeLimit = time.Hour * 24 * 30 var activeUserTimeLimit = time.Hour * 24 * 30
func GetAlertNotifiersUsageStats(ctx context.Context, query *models.GetAlertNotifierUsageStatsQuery) error { func GetAlertNotifiersUsageStats(ctx context.Context, query *models.GetAlertNotifierUsageStatsQuery) error {
var rawSql = `SELECT COUNT(*) as count, type FROM alert_notification GROUP BY type` var rawSql = `SELECT COUNT(*) AS count, type FROM ` + dialect.Quote("alert_notification") + ` GROUP BY type`
query.Result = make([]*models.NotifierUsageStats, 0) query.Result = make([]*models.NotifierUsageStats, 0)
err := x.SQL(rawSql).Find(&query.Result) err := x.SQL(rawSql).Find(&query.Result)
return err return err
} }
func GetDataSourceStats(query *models.GetDataSourceStatsQuery) error { func GetDataSourceStats(query *models.GetDataSourceStatsQuery) error {
var rawSql = `SELECT COUNT(*) as count, type FROM data_source GROUP BY type` var rawSql = `SELECT COUNT(*) AS count, type FROM ` + dialect.Quote("data_source") + ` GROUP BY type`
query.Result = make([]*models.DataSourceStats, 0) query.Result = make([]*models.DataSourceStats, 0)
err := x.SQL(rawSql).Find(&query.Result) err := x.SQL(rawSql).Find(&query.Result)
return err return err
} }
func GetDataSourceAccessStats(query *models.GetDataSourceAccessStatsQuery) error { func GetDataSourceAccessStats(query *models.GetDataSourceAccessStatsQuery) error {
var rawSql = `SELECT COUNT(*) as count, type, access FROM data_source GROUP BY type, access` var rawSql = `SELECT COUNT(*) AS count, type, access FROM ` + dialect.Quote("data_source") + ` GROUP BY type, access`
query.Result = make([]*models.DataSourceAccessStats, 0) query.Result = make([]*models.DataSourceAccessStats, 0)
err := x.SQL(rawSql).Find(&query.Result) err := x.SQL(rawSql).Find(&query.Result)
return err return err
...@@ -52,23 +53,23 @@ func GetSystemStats(query *models.GetSystemStatsQuery) error { ...@@ -52,23 +53,23 @@ func GetSystemStats(query *models.GetSystemStatsQuery) error {
sb.Write(`(SELECT COUNT(*) FROM ` + dialect.Quote("alert") + `) AS alerts,`) sb.Write(`(SELECT COUNT(*) FROM ` + dialect.Quote("alert") + `) AS alerts,`)
activeUserDeadlineDate := time.Now().Add(-activeUserTimeLimit) activeUserDeadlineDate := time.Now().Add(-activeUserTimeLimit)
sb.Write(`(SELECT COUNT(*) FROM `+dialect.Quote("user")+` where last_seen_at > ?) AS active_users,`, activeUserDeadlineDate) sb.Write(`(SELECT COUNT(*) FROM `+dialect.Quote("user")+` WHERE last_seen_at > ?) AS active_users,`, activeUserDeadlineDate)
sb.Write(`(SELECT COUNT(id) FROM `+dialect.Quote("dashboard")+` where is_folder = ?) AS folders,`, dialect.BooleanStr(true)) sb.Write(`(SELECT COUNT(id) FROM `+dialect.Quote("dashboard")+` WHERE is_folder = ?) AS folders,`, dialect.BooleanStr(true))
sb.Write(`( sb.Write(`(
SELECT COUNT(acl.id) SELECT COUNT(acl.id)
FROM `+dialect.Quote("dashboard_acl")+` as acl FROM `+dialect.Quote("dashboard_acl")+` AS acl
inner join `+dialect.Quote("dashboard")+` as d INNER JOIN `+dialect.Quote("dashboard")+` AS d
on d.id = acl.dashboard_id ON d.id = acl.dashboard_id
WHERE d.is_folder = ? WHERE d.is_folder = ?
) AS dashboard_permissions,`, dialect.BooleanStr(false)) ) AS dashboard_permissions,`, dialect.BooleanStr(false))
sb.Write(`( sb.Write(`(
SELECT COUNT(acl.id) SELECT COUNT(acl.id)
FROM `+dialect.Quote("dashboard_acl")+` as acl FROM `+dialect.Quote("dashboard_acl")+` AS acl
inner join `+dialect.Quote("dashboard")+` as d INNER JOIN `+dialect.Quote("dashboard")+` AS d
on d.id = acl.dashboard_id ON d.id = acl.dashboard_id
WHERE d.is_folder = ? WHERE d.is_folder = ?
) AS folder_permissions,`, dialect.BooleanStr(true)) ) AS folder_permissions,`, dialect.BooleanStr(true))
...@@ -77,9 +78,9 @@ func GetSystemStats(query *models.GetSystemStatsQuery) error { ...@@ -77,9 +78,9 @@ func GetSystemStats(query *models.GetSystemStatsQuery) error {
sb.Write(`(SELECT COUNT(id) FROM ` + dialect.Quote("team") + `) AS teams,`) sb.Write(`(SELECT COUNT(id) FROM ` + dialect.Quote("team") + `) AS teams,`)
sb.Write(`(SELECT COUNT(id) FROM ` + dialect.Quote("user_auth_token") + `) AS auth_tokens,`) sb.Write(`(SELECT COUNT(id) FROM ` + dialect.Quote("user_auth_token") + `) AS auth_tokens,`)
sb.Write(roleCounterSQL("Viewer", "viewers")+`,`, activeUserDeadlineDate) sb.Write(roleCounterSQL("Viewer", "viewers", false)+`,`, activeUserDeadlineDate)
sb.Write(roleCounterSQL("Editor", "editors")+`,`, activeUserDeadlineDate) sb.Write(roleCounterSQL("Editor", "editors", false)+`,`, activeUserDeadlineDate)
sb.Write(roleCounterSQL("Admin", "admins")+``, activeUserDeadlineDate) sb.Write(roleCounterSQL("Admin", "admins", false)+``, activeUserDeadlineDate)
var stats models.SystemStats var stats models.SystemStats
_, err := x.SQL(sb.GetSqlString(), sb.params...).Get(&stats) _, err := x.SQL(sb.GetSqlString(), sb.params...).Get(&stats)
...@@ -89,21 +90,27 @@ func GetSystemStats(query *models.GetSystemStatsQuery) error { ...@@ -89,21 +90,27 @@ func GetSystemStats(query *models.GetSystemStatsQuery) error {
query.Result = &stats query.Result = &stats
return err return nil
} }
func roleCounterSQL(role, alias string) string { func roleCounterSQL(role string, alias string, onlyActive bool) string {
return ` var sqlQuery = `
( (
SELECT COUNT(DISTINCT u.id) SELECT COUNT(DISTINCT u.id)
FROM ` + dialect.Quote("user") + ` as u, org_user FROM ` + dialect.Quote("user") + ` AS u, org_user
WHERE ( org_user.user_id=u.id AND org_user.role='` + role + `' ) WHERE u.last_seen_at > ? AND ( org_user.user_id=u.id AND org_user.role='` + role + `' )
) as ` + alias + `, ) AS active_` + alias
if !onlyActive {
sqlQuery += `,
( (
SELECT COUNT(DISTINCT u.id) SELECT COUNT(DISTINCT u.id)
FROM ` + dialect.Quote("user") + ` as u, org_user FROM ` + dialect.Quote("user") + ` AS u, org_user
WHERE u.last_seen_at>? AND ( org_user.user_id=u.id AND org_user.role='` + role + `' ) WHERE ( org_user.user_id=u.id AND org_user.role='` + role + `' )
) as active_` + alias ) AS ` + alias
}
return sqlQuery
} }
func GetAdminStats(query *models.GetAdminStatsQuery) error { func GetAdminStats(query *models.GetAdminStatsQuery) error {
...@@ -135,7 +142,8 @@ func GetAdminStats(query *models.GetAdminStatsQuery) error { ...@@ -135,7 +142,8 @@ func GetAdminStats(query *models.GetAdminStatsQuery) error {
FROM ` + dialect.Quote("playlist") + ` FROM ` + dialect.Quote("playlist") + `
) AS playlists, ) AS playlists,
( (
SELECT COUNT(*) FROM ` + dialect.Quote("star") + ` SELECT COUNT(*)
FROM ` + dialect.Quote("star") + `
) AS stars, ) AS stars,
( (
SELECT COUNT(*) SELECT COUNT(*)
...@@ -147,16 +155,15 @@ func GetAdminStats(query *models.GetAdminStatsQuery) error { ...@@ -147,16 +155,15 @@ func GetAdminStats(query *models.GetAdminStatsQuery) error {
) AS users, ) AS users,
( (
SELECT COUNT(*) SELECT COUNT(*)
FROM ` + dialect.Quote("user") + ` where last_seen_at > ? FROM ` + dialect.Quote("user") + ` WHERE last_seen_at > ?
) as active_users, ) AS active_users,
` + roleCounterSQL("Admin", "admins") + `, ` + roleCounterSQL("Admin", "admins", false) + `,
` + roleCounterSQL("Editor", "editors") + `, ` + roleCounterSQL("Editor", "editors", false) + `,
` + roleCounterSQL("Viewer", "viewers") + `, ` + roleCounterSQL("Viewer", "viewers", false) + `,
( (
SELECT COUNT(*) SELECT COUNT(*)
FROM ` + dialect.Quote("user_auth_token") + ` where rotated_at > ? FROM ` + dialect.Quote("user_auth_token") + ` WHERE rotated_at > ?
) as active_sessions ) AS active_sessions`
`
var stats models.AdminStats var stats models.AdminStats
_, err := x.SQL(rawSql, activeEndDate, activeEndDate, activeEndDate, activeEndDate, activeEndDate.Unix()).Get(&stats) _, err := x.SQL(rawSql, activeEndDate, activeEndDate, activeEndDate, activeEndDate, activeEndDate.Unix()).Get(&stats)
...@@ -165,12 +172,11 @@ func GetAdminStats(query *models.GetAdminStatsQuery) error { ...@@ -165,12 +172,11 @@ func GetAdminStats(query *models.GetAdminStatsQuery) error {
} }
query.Result = &stats query.Result = &stats
return err return nil
} }
func GetSystemUserCountStats(ctx context.Context, query *models.GetSystemUserCountStatsQuery) error { func GetSystemUserCountStats(ctx context.Context, query *models.GetSystemUserCountStatsQuery) error {
return withDbSession(ctx, func(sess *DBSession) error { return withDbSession(ctx, func(sess *DBSession) error {
var rawSql = `SELECT COUNT(id) AS Count FROM ` + dialect.Quote("user") var rawSql = `SELECT COUNT(id) AS Count FROM ` + dialect.Quote("user")
var stats models.SystemUserCountStats var stats models.SystemUserCountStats
_, err := sess.SQL(rawSql).Get(&stats) _, err := sess.SQL(rawSql).Get(&stats)
...@@ -180,6 +186,27 @@ func GetSystemUserCountStats(ctx context.Context, query *models.GetSystemUserCou ...@@ -180,6 +186,27 @@ func GetSystemUserCountStats(ctx context.Context, query *models.GetSystemUserCou
query.Result = &stats query.Result = &stats
return err return nil
}) })
} }
func GetActiveUserStats(query *models.GetActiveUserStatsQuery) error {
activeUserDeadlineDate := time.Now().Add(-activeUserTimeLimit)
sb := &SqlBuilder{}
sb.Write(`SELECT `)
sb.Write(`(SELECT COUNT(*) FROM `+dialect.Quote("user")+` WHERE last_seen_at > ?) AS active_users,`, activeUserDeadlineDate)
sb.Write(roleCounterSQL("Viewer", "viewers", true)+`,`, activeUserDeadlineDate)
sb.Write(roleCounterSQL("Editor", "editors", true)+`,`, activeUserDeadlineDate)
sb.Write(roleCounterSQL("Admin", "admins", true)+``, activeUserDeadlineDate)
var stats models.ActiveUserStats
_, err := x.SQL(sb.GetSqlString(), sb.params...).Get(&stats)
if err != nil {
return err
}
query.Result = &stats
return nil
}
...@@ -7,6 +7,7 @@ import ( ...@@ -7,6 +7,7 @@ import (
"github.com/grafana/grafana/pkg/models" "github.com/grafana/grafana/pkg/models"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
) )
func TestStatsDataAccess(t *testing.T) { func TestStatsDataAccess(t *testing.T) {
...@@ -18,41 +19,51 @@ func TestStatsDataAccess(t *testing.T) { ...@@ -18,41 +19,51 @@ func TestStatsDataAccess(t *testing.T) {
query := models.GetSystemStatsQuery{} query := models.GetSystemStatsQuery{}
err := GetSystemStats(&query) err := GetSystemStats(&query)
assert.Nil(t, err) require.NoError(t, err)
assert.Equal(t, query.Result.Users, int64(3)) assert.Equal(t, int64(3), query.Result.Users)
assert.Equal(t, query.Result.Editors, 1) assert.Equal(t, 1, query.Result.Editors)
assert.Equal(t, query.Result.Viewers, 1) assert.Equal(t, 1, query.Result.Viewers)
assert.Equal(t, query.Result.Admins, 3) assert.Equal(t, 3, query.Result.Admins)
}) })
t.Run("Get system user count stats should not results in error", func(t *testing.T) { t.Run("Get system user count stats should not results in error", func(t *testing.T) {
query := models.GetSystemUserCountStatsQuery{} query := models.GetSystemUserCountStatsQuery{}
err := GetSystemUserCountStats(context.Background(), &query) err := GetSystemUserCountStats(context.Background(), &query)
assert.Nil(t, err) assert.NoError(t, err)
}) })
t.Run("Get datasource stats should not results in error", func(t *testing.T) { t.Run("Get datasource stats should not results in error", func(t *testing.T) {
query := models.GetDataSourceStatsQuery{} query := models.GetDataSourceStatsQuery{}
err := GetDataSourceStats(&query) err := GetDataSourceStats(&query)
assert.Nil(t, err) assert.NoError(t, err)
}) })
t.Run("Get datasource access stats should not results in error", func(t *testing.T) { t.Run("Get datasource access stats should not results in error", func(t *testing.T) {
query := models.GetDataSourceAccessStatsQuery{} query := models.GetDataSourceAccessStatsQuery{}
err := GetDataSourceAccessStats(&query) err := GetDataSourceAccessStats(&query)
assert.Nil(t, err) assert.NoError(t, err)
}) })
t.Run("Get alert notifier stats should not results in error", func(t *testing.T) { t.Run("Get alert notifier stats should not results in error", func(t *testing.T) {
query := models.GetAlertNotifierUsageStatsQuery{} query := models.GetAlertNotifierUsageStatsQuery{}
err := GetAlertNotifiersUsageStats(context.Background(), &query) err := GetAlertNotifiersUsageStats(context.Background(), &query)
assert.Nil(t, err) assert.NoError(t, err)
}) })
t.Run("Get admin stats should not result in error", func(t *testing.T) { t.Run("Get admin stats should not result in error", func(t *testing.T) {
query := models.GetAdminStatsQuery{} query := models.GetAdminStatsQuery{}
err := GetAdminStats(&query) err := GetAdminStats(&query)
assert.Nil(t, err) assert.NoError(t, err)
})
t.Run("Get active user count stats should not result in error", func(t *testing.T) {
query := models.GetActiveUserStatsQuery{}
err := GetActiveUserStats(&query)
require.NoError(t, err)
assert.Equal(t, int64(1), query.Result.ActiveUsers)
assert.Equal(t, int64(1), query.Result.ActiveAdmins)
assert.Equal(t, int64(0), query.Result.ActiveEditors)
assert.Equal(t, int64(0), query.Result.ActiveViewers)
}) })
}) })
} }
...@@ -67,14 +78,14 @@ func populateDB(t *testing.T) { ...@@ -67,14 +78,14 @@ func populateDB(t *testing.T) {
OrgName: fmt.Sprintf("Org #%v", i), OrgName: fmt.Sprintf("Org #%v", i),
} }
err := CreateUser(context.Background(), cmd) err := CreateUser(context.Background(), cmd)
assert.Nil(t, err) require.NoError(t, err)
users[i] = cmd.Result users[i] = cmd.Result
} }
// get 1st user's organisation // get 1st user's organisation
getOrgByIdQuery := &models.GetOrgByIdQuery{Id: users[0].OrgId} getOrgByIdQuery := &models.GetOrgByIdQuery{Id: users[0].OrgId}
err := GetOrgById(getOrgByIdQuery) err := GetOrgById(getOrgByIdQuery)
assert.Nil(t, err) require.NoError(t, err)
org := getOrgByIdQuery.Result org := getOrgByIdQuery.Result
// add 2nd user as editor // add 2nd user as editor
...@@ -84,7 +95,7 @@ func populateDB(t *testing.T) { ...@@ -84,7 +95,7 @@ func populateDB(t *testing.T) {
Role: models.ROLE_EDITOR, Role: models.ROLE_EDITOR,
} }
err = AddOrgUser(cmd) err = AddOrgUser(cmd)
assert.Nil(t, err) require.NoError(t, err)
// add 3rd user as viewer // add 3rd user as viewer
cmd = &models.AddOrgUserCommand{ cmd = &models.AddOrgUserCommand{
...@@ -93,12 +104,12 @@ func populateDB(t *testing.T) { ...@@ -93,12 +104,12 @@ func populateDB(t *testing.T) {
Role: models.ROLE_VIEWER, Role: models.ROLE_VIEWER,
} }
err = AddOrgUser(cmd) err = AddOrgUser(cmd)
assert.Nil(t, err) require.NoError(t, err)
// get 2nd user's organisation // get 2nd user's organisation
getOrgByIdQuery = &models.GetOrgByIdQuery{Id: users[1].OrgId} getOrgByIdQuery = &models.GetOrgByIdQuery{Id: users[1].OrgId}
err = GetOrgById(getOrgByIdQuery) err = GetOrgById(getOrgByIdQuery)
assert.Nil(t, err) require.NoError(t, err)
org = getOrgByIdQuery.Result org = getOrgByIdQuery.Result
// add 1st user as admin // add 1st user as admin
...@@ -108,5 +119,12 @@ func populateDB(t *testing.T) { ...@@ -108,5 +119,12 @@ func populateDB(t *testing.T) {
Role: models.ROLE_ADMIN, Role: models.ROLE_ADMIN,
} }
err = AddOrgUser(cmd) err = AddOrgUser(cmd)
assert.Nil(t, err) require.NoError(t, err)
// update 1st user last seen at
updateUserLastSeenAtCmd := &models.UpdateUserLastSeenAtCommand{
UserId: users[0].Id,
}
err = UpdateUserLastSeenAt(updateUserLastSeenAtCmd)
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