admin: add more stats about roles (#16667)

closes #14967
This commit is contained in:
Carl Bergquist
2019-04-24 13:18:16 +02:00
committed by GitHub
parent 739cdcfb6e
commit eb8af01a8a
6 changed files with 114 additions and 63 deletions

View File

@@ -51,16 +51,23 @@ type GetAlertNotifierUsageStatsQuery struct {
}
type AdminStats struct {
Users int `json:"users"`
Orgs int `json:"orgs"`
Dashboards int `json:"dashboards"`
Snapshots int `json:"snapshots"`
Tags int `json:"tags"`
Datasources int `json:"datasources"`
Playlists int `json:"playlists"`
Stars int `json:"stars"`
Alerts int `json:"alerts"`
ActiveUsers int `json:"activeUsers"`
Orgs int `json:"orgs"`
Dashboards int `json:"dashboards"`
Snapshots int `json:"snapshots"`
Tags int `json:"tags"`
Datasources int `json:"datasources"`
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 {

View File

@@ -36,7 +36,6 @@ func TestUserStarsDataAccess(t *testing.T) {
So(query.Result, ShouldBeFalse)
})
})
})
}

View File

@@ -89,52 +89,84 @@ func GetSystemStats(query *m.GetSystemStatsQuery) error {
}
func GetAdminStats(query *m.GetAdminStatsQuery) error {
var rawSql = `SELECT
(
SELECT COUNT(*)
FROM ` + dialect.Quote("user") + `
) AS users,
(
SELECT COUNT(*)
FROM ` + dialect.Quote("org") + `
) AS orgs,
(
SELECT COUNT(*)
FROM ` + dialect.Quote("dashboard") + `
) AS dashboards,
(
SELECT COUNT(*)
FROM ` + dialect.Quote("dashboard_snapshot") + `
) AS snapshots,
(
SELECT COUNT( DISTINCT ( ` + dialect.Quote("term") + ` ))
FROM ` + dialect.Quote("dashboard_tag") + `
) AS tags,
(
SELECT COUNT(*)
FROM ` + dialect.Quote("data_source") + `
) AS datasources,
(
SELECT COUNT(*)
FROM ` + dialect.Quote("playlist") + `
) AS playlists,
(
SELECT COUNT(*) FROM ` + dialect.Quote("star") + `
) AS stars,
(
SELECT COUNT(*)
FROM ` + dialect.Quote("alert") + `
) AS alerts,
activeEndDate := time.Now().Add(-activeUserTimeLimit)
roleCounter := func(role, alias string) string {
sql :=
`
(
SELECT COUNT(*)
from ` + dialect.Quote("user") + ` where last_seen_at > ?
) as active_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") + `
) AS orgs,
(
SELECT COUNT(*)
FROM ` + dialect.Quote("dashboard") + `
) AS dashboards,
(
SELECT COUNT(*)
FROM ` + dialect.Quote("dashboard_snapshot") + `
) AS snapshots,
(
SELECT COUNT( DISTINCT ( ` + dialect.Quote("term") + ` ))
FROM ` + dialect.Quote("dashboard_tag") + `
) AS tags,
(
SELECT COUNT(*)
FROM ` + dialect.Quote("data_source") + `
) AS datasources,
(
SELECT COUNT(*)
FROM ` + dialect.Quote("playlist") + `
) AS playlists,
(
SELECT COUNT(*) FROM ` + dialect.Quote("star") + `
) AS stars,
(
SELECT COUNT(*)
FROM ` + dialect.Quote("alert") + `
) AS alerts,
(
SELECT COUNT(*)
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
}

View File

@@ -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)
})
})
}