Commit 2ea5b6fe by Marcus Efraimsson

add additional usage stats metrics

nr of folders
nr of folder permissions
nr of dashboard permissions
nr of snapshots
nr of teams
nr of provisioned dashboards
parent a6a12d36
...@@ -368,6 +368,12 @@ func sendUsageStats() { ...@@ -368,6 +368,12 @@ func sendUsageStats() {
metrics["stats.active_users.count"] = statsQuery.Result.ActiveUsers metrics["stats.active_users.count"] = statsQuery.Result.ActiveUsers
metrics["stats.datasources.count"] = statsQuery.Result.Datasources metrics["stats.datasources.count"] = statsQuery.Result.Datasources
metrics["stats.stars.count"] = statsQuery.Result.Stars metrics["stats.stars.count"] = statsQuery.Result.Stars
metrics["stats.folders.count"] = statsQuery.Result.Folders
metrics["stats.dashboard_permissions.count"] = statsQuery.Result.DashboardPermissions
metrics["stats.folder_permissions.count"] = statsQuery.Result.FolderPermissions
metrics["stats.provisioned_dashboards.count"] = statsQuery.Result.ProvisionedDashboards
metrics["stats.snapshots.count"] = statsQuery.Result.Snapshots
metrics["stats.teams.count"] = statsQuery.Result.Teams
dsStats := models.GetDataSourceStatsQuery{} dsStats := models.GetDataSourceStatsQuery{}
if err := bus.Dispatch(&dsStats); err != nil { if err := bus.Dispatch(&dsStats); err != nil {
......
...@@ -32,6 +32,12 @@ func TestMetrics(t *testing.T) { ...@@ -32,6 +32,12 @@ func TestMetrics(t *testing.T) {
Playlists: 6, Playlists: 6,
Alerts: 7, Alerts: 7,
Stars: 8, Stars: 8,
Folders: 9,
DashboardPermissions: 10,
FolderPermissions: 11,
ProvisionedDashboards: 12,
Snapshots: 13,
Teams: 14,
} }
getSystemStatsQuery = query getSystemStatsQuery = query
return nil return nil
...@@ -126,6 +132,12 @@ func TestMetrics(t *testing.T) { ...@@ -126,6 +132,12 @@ func TestMetrics(t *testing.T) {
So(metrics.Get("stats.active_users.count").MustInt(), ShouldEqual, getSystemStatsQuery.Result.ActiveUsers) So(metrics.Get("stats.active_users.count").MustInt(), ShouldEqual, getSystemStatsQuery.Result.ActiveUsers)
So(metrics.Get("stats.datasources.count").MustInt(), ShouldEqual, getSystemStatsQuery.Result.Datasources) So(metrics.Get("stats.datasources.count").MustInt(), ShouldEqual, getSystemStatsQuery.Result.Datasources)
So(metrics.Get("stats.stars.count").MustInt(), ShouldEqual, getSystemStatsQuery.Result.Stars) So(metrics.Get("stats.stars.count").MustInt(), ShouldEqual, getSystemStatsQuery.Result.Stars)
So(metrics.Get("stats.folders.count").MustInt(), ShouldEqual, getSystemStatsQuery.Result.Folders)
So(metrics.Get("stats.dashboard_permissions.count").MustInt(), ShouldEqual, getSystemStatsQuery.Result.DashboardPermissions)
So(metrics.Get("stats.folder_permissions.count").MustInt(), ShouldEqual, getSystemStatsQuery.Result.FolderPermissions)
So(metrics.Get("stats.provisioned_dashboards.count").MustInt(), ShouldEqual, getSystemStatsQuery.Result.ProvisionedDashboards)
So(metrics.Get("stats.snapshots.count").MustInt(), ShouldEqual, getSystemStatsQuery.Result.Snapshots)
So(metrics.Get("stats.teams.count").MustInt(), ShouldEqual, getSystemStatsQuery.Result.Teams)
So(metrics.Get("stats.ds."+models.DS_ES+".count").MustInt(), ShouldEqual, 9) So(metrics.Get("stats.ds."+models.DS_ES+".count").MustInt(), ShouldEqual, 9)
So(metrics.Get("stats.ds."+models.DS_PROMETHEUS+".count").MustInt(), ShouldEqual, 10) So(metrics.Get("stats.ds."+models.DS_PROMETHEUS+".count").MustInt(), ShouldEqual, 10)
......
...@@ -9,6 +9,12 @@ type SystemStats struct { ...@@ -9,6 +9,12 @@ type SystemStats struct {
Playlists int64 Playlists int64
Alerts int64 Alerts int64
Stars int64 Stars int64
Snapshots int64
Teams int64
DashboardPermissions int64
FolderPermissions int64
Folders int64
ProvisionedDashboards int64
} }
type DataSourceStats struct { type DataSourceStats struct {
...@@ -40,3 +46,11 @@ type AdminStats struct { ...@@ -40,3 +46,11 @@ type AdminStats struct {
type GetAdminStatsQuery struct { type GetAdminStatsQuery struct {
Result *AdminStats Result *AdminStats
} }
type SystemUserCountStats struct {
Count int64
}
type GetSystemUserCountStatsQuery struct {
Result *SystemUserCountStats
}
...@@ -86,13 +86,13 @@ func (ss *SqlStore) Init() error { ...@@ -86,13 +86,13 @@ func (ss *SqlStore) Init() error {
} }
func (ss *SqlStore) ensureAdminUser() error { func (ss *SqlStore) ensureAdminUser() error {
statsQuery := m.GetSystemStatsQuery{} systemUserCountQuery := m.GetSystemUserCountStatsQuery{}
if err := bus.Dispatch(&statsQuery); err != nil { if err := bus.Dispatch(&systemUserCountQuery); err != nil {
fmt.Errorf("Could not determine if admin user exists: %v", err) fmt.Errorf("Could not determine if admin user exists: %v", err)
} }
if statsQuery.Result.Users > 0 { if systemUserCountQuery.Result.Count > 0 {
return nil return nil
} }
......
...@@ -11,6 +11,7 @@ func init() { ...@@ -11,6 +11,7 @@ func init() {
bus.AddHandler("sql", GetSystemStats) bus.AddHandler("sql", GetSystemStats)
bus.AddHandler("sql", GetDataSourceStats) bus.AddHandler("sql", GetDataSourceStats)
bus.AddHandler("sql", GetAdminStats) bus.AddHandler("sql", GetAdminStats)
bus.AddHandler("sql", GetSystemUserCountStats)
} }
var activeUserTimeLimit = time.Hour * 24 * 30 var activeUserTimeLimit = time.Hour * 24 * 30
...@@ -53,12 +54,30 @@ func GetSystemStats(query *m.GetSystemStatsQuery) error { ...@@ -53,12 +54,30 @@ func GetSystemStats(query *m.GetSystemStatsQuery) error {
) AS alerts, ) AS alerts,
( (
SELECT COUNT(*) FROM ` + dialect.Quote("user") + ` where last_seen_at > ? SELECT COUNT(*) FROM ` + dialect.Quote("user") + ` where last_seen_at > ?
) as active_users ) as active_users,
(
SELECT COUNT(id) FROM ` + dialect.Quote("dashboard") + ` where is_folder = ?
) as folders,
(
SELECT COUNT(acl.id) FROM ` + dialect.Quote("dashboard_acl") + ` as acl inner join ` + dialect.Quote("dashboard") + ` as d on d.id = acl.dashboard_id where d.is_folder = ?
) as dashboard_permissions,
(
SELECT COUNT(acl.id) FROM ` + dialect.Quote("dashboard_acl") + ` as acl inner join ` + dialect.Quote("dashboard") + ` as d on d.id = acl.dashboard_id where d.is_folder = ?
) as folder_permissions,
(
SELECT COUNT(id) FROM ` + dialect.Quote("dashboard_provisioning") + `
) as provisioned_dashboards,
(
SELECT COUNT(id) FROM ` + dialect.Quote("dashboard_snapshot") + `
) as snapshots,
(
SELECT COUNT(id) FROM ` + dialect.Quote("team") + `
) as teams
` `
activeUserDeadlineDate := time.Now().Add(-activeUserTimeLimit) activeUserDeadlineDate := time.Now().Add(-activeUserTimeLimit)
var stats m.SystemStats var stats m.SystemStats
_, err := x.SQL(rawSql, activeUserDeadlineDate).Get(&stats) _, err := x.SQL(rawSql, activeUserDeadlineDate, dialect.BooleanStr(true), dialect.BooleanStr(false), dialect.BooleanStr(true)).Get(&stats)
if err != nil { if err != nil {
return err return err
} }
...@@ -122,3 +141,16 @@ func GetAdminStats(query *m.GetAdminStatsQuery) error { ...@@ -122,3 +141,16 @@ func GetAdminStats(query *m.GetAdminStatsQuery) error {
query.Result = &stats query.Result = &stats
return err return err
} }
func GetSystemUserCountStats(query *m.GetSystemUserCountStatsQuery) error {
var rawSql = `SELECT COUNT(id) AS Count FROM ` + dialect.Quote("user")
var stats m.SystemUserCountStats
_, err := x.SQL(rawSql).Get(&stats)
if err != nil {
return err
}
query.Result = &stats
return err
}
package sqlstore
import (
"testing"
m "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{}
err := GetSystemStats(&query)
So(err, ShouldBeNil)
})
Convey("Get system user count stats should not results in error", func() {
query := m.GetSystemUserCountStatsQuery{}
err := GetSystemUserCountStats(&query)
So(err, ShouldBeNil)
})
})
}
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