Commit eb8af01a by Carl Bergquist Committed by GitHub

admin: add more stats about roles (#16667)

closes #14967
parent 739cdcfb
......@@ -51,7 +51,6 @@ type GetAlertNotifierUsageStatsQuery struct {
}
type AdminStats struct {
Users int `json:"users"`
Orgs int `json:"orgs"`
Dashboards int `json:"dashboards"`
Snapshots int `json:"snapshots"`
......@@ -60,7 +59,15 @@ type AdminStats struct {
Playlists int `json:"playlists"`
Stars int `json:"stars"`
Alerts int `json:"alerts"`
Users int `json:"users"`
Admins int `json:"admins"`
Editors int `json:"editors"`
Viewers int `json:"viewers"`
ActiveUsers int `json:"activeUsers"`
ActiveAdmins int `json:"activeAdmins"`
ActiveEditors int `json:"activeEditors"`
ActiveViewers int `json:"activeViewers"`
ActiveSessions int `json:"activeSessions"`
}
type GetAdminStatsQuery struct {
......
......@@ -36,7 +36,6 @@ func TestUserStarsDataAccess(t *testing.T) {
So(query.Result, ShouldBeFalse)
})
})
})
}
......@@ -89,11 +89,34 @@ func GetSystemStats(query *m.GetSystemStatsQuery) error {
}
func GetAdminStats(query *m.GetAdminStatsQuery) error {
var rawSql = `SELECT
activeEndDate := time.Now().Add(-activeUserTimeLimit)
roleCounter := func(role, alias string) string {
sql :=
`
(
SELECT COUNT(*)
FROM ` + dialect.Quote("user") + `
) AS users,
FROM ` + dialect.Quote("user") + ` as u
WHERE
(SELECT COUNT(*)
FROM org_user
WHERE org_user.user_id=u.id
AND org_user.role='` + role + `')>0
) as ` + alias + `,
(
SELECT COUNT(*)
FROM ` + dialect.Quote("user") + ` as u
WHERE
(SELECT COUNT(*)
FROM org_user
WHERE org_user.user_id=u.id
AND org_user.role='` + role + `')>0
AND u.last_seen_at>?
) as active_` + alias
return sql
}
var rawSql = `SELECT
(
SELECT COUNT(*)
FROM ` + dialect.Quote("org") + `
......@@ -127,14 +150,23 @@ func GetAdminStats(query *m.GetAdminStatsQuery) error {
) AS alerts,
(
SELECT COUNT(*)
from ` + dialect.Quote("user") + ` where last_seen_at > ?
) as active_users
FROM ` + dialect.Quote("user") + `
) AS users,
(
SELECT COUNT(*)
FROM ` + dialect.Quote("user") + ` where last_seen_at > ?
) as active_users,
` + roleCounter("Admin", "admins") + `,
` + roleCounter("Editor", "editors") + `,
` + roleCounter("Viewer", "viewers") + `,
(
SELECT COUNT(*)
FROM ` + dialect.Quote("user_auth_token") + ` where rotated_at > ?
) as active_sessions
`
activeUserDeadlineDate := time.Now().Add(-activeUserTimeLimit)
var stats m.AdminStats
_, err := x.SQL(rawSql, activeUserDeadlineDate).Get(&stats)
_, err := x.SQL(rawSql, activeEndDate, activeEndDate, activeEndDate, activeEndDate, activeEndDate.Unix()).Get(&stats)
if err != nil {
return err
}
......
......@@ -4,43 +4,48 @@ import (
"context"
"testing"
m "github.com/grafana/grafana/pkg/models"
"github.com/grafana/grafana/pkg/models"
. "github.com/smartystreets/goconvey/convey"
)
func TestStatsDataAccess(t *testing.T) {
Convey("Testing Stats Data Access", t, func() {
InitTestDB(t)
Convey("Get system stats should not results in error", func() {
query := m.GetSystemStatsQuery{}
query := models.GetSystemStatsQuery{}
err := GetSystemStats(&query)
So(err, ShouldBeNil)
})
Convey("Get system user count stats should not results in error", func() {
query := m.GetSystemUserCountStatsQuery{}
query := models.GetSystemUserCountStatsQuery{}
err := GetSystemUserCountStats(context.Background(), &query)
So(err, ShouldBeNil)
})
Convey("Get datasource stats should not results in error", func() {
query := m.GetDataSourceStatsQuery{}
query := models.GetDataSourceStatsQuery{}
err := GetDataSourceStats(&query)
So(err, ShouldBeNil)
})
Convey("Get datasource access stats should not results in error", func() {
query := m.GetDataSourceAccessStatsQuery{}
query := models.GetDataSourceAccessStatsQuery{}
err := GetDataSourceAccessStats(&query)
So(err, ShouldBeNil)
})
Convey("Get alert notifier stats should not results in error", func() {
query := m.GetAlertNotifierUsageStatsQuery{}
query := models.GetAlertNotifierUsageStatsQuery{}
err := GetAlertNotifiersUsageStats(context.Background(), &query)
So(err, ShouldBeNil)
})
Convey("Get admin stats should not result in error", func() {
query := models.GetAdminStatsQuery{}
err := GetAdminStats(&query)
So(err, ShouldBeNil)
})
})
}
......@@ -10,8 +10,15 @@ export const getServerStats = async (): Promise<ServerStat[]> => {
const res = await getBackendSrv().get('api/admin/stats');
return [
{ name: 'Total users', value: res.users },
{ name: 'Total dashboards', value: res.dashboards },
{ name: 'Total admins', value: res.admins },
{ name: 'Total editors', value: res.editors },
{ name: 'Total viewers', value: res.viewers },
{ name: 'Active users (seen last 30 days)', value: res.activeUsers },
{ name: 'Active admins (seen last 30 days)', value: res.activeAdmins },
{ name: 'Active editors (seen last 30 days)', value: res.activeEditors },
{ name: 'Active viewers (seen last 30 days)', value: res.activeViewers },
{ name: 'Active sessions', value: res.activeSessions },
{ name: 'Total dashboards', value: res.dashboards },
{ name: 'Total orgs', value: res.orgs },
{ name: 'Total playlists', value: res.playlists },
{ name: 'Total snapshots', value: res.snapshots },
......
......@@ -12,6 +12,7 @@ function exit_if_fail {
export GRAFANA_TEST_DB=postgres
time for d in $(go list ./pkg/...); do
exit_if_fail go test -tags=integration $d
done
\ No newline at end of file
exit_if_fail go test -v -run="StatsDataAccess" -tags=integration ./pkg/services/sqlstore/...
#time for d in $(go list ./pkg/...); do
# exit_if_fail go test -tags=integration $d
#done
\ No newline at end of file
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