UsageStats: Add database age and driver (#66535)

Co-authored-by: Dan Cech <dcech@grafana.com>
This commit is contained in:
Ryan McKinley 2023-04-17 18:27:04 -07:00 committed by GitHub
parent 2f13c851e4
commit 15b469bd12
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 19 additions and 1 deletions

View File

@ -161,6 +161,12 @@ func (s *Service) collectSystemStats(ctx context.Context) (map[string]interface{
m["stats.active_data_keys.count"] = statsResult.ActiveDataKeys
m["stats.public_dashboards.count"] = statsResult.PublicDashboards
m["stats.correlations.count"] = statsResult.Correlations
if statsResult.DatabaseCreatedTime != nil {
m["stats.database.created.time"] = statsResult.DatabaseCreatedTime.Unix()
}
if statsResult.DatabaseDriver != "" {
m["stats.database.driver"] = statsResult.DatabaseDriver
}
ossEditionCount := 1
enterpriseEditionCount := 0

View File

@ -1,5 +1,7 @@
package stats
import "time"
type SystemStats struct {
Dashboards int64
DashboardBytesTotal int64
@ -45,6 +47,10 @@ type SystemStats struct {
ActiveDataKeys int64
PublicDashboards int64
Correlations int64
DatabaseCreatedTime *time.Time
// name of the driver
DatabaseDriver string
}
type DataSourceStats struct {

View File

@ -63,10 +63,10 @@ func notServiceAccount(dialect migrator.Dialect) string {
}
func (ss *sqlStatsService) GetSystemStats(ctx context.Context, query *stats.GetSystemStatsQuery) (result *stats.SystemStats, err error) {
dialect := ss.db.GetDialect()
err = ss.db.WithDbSession(ctx, func(dbSession *db.Session) error {
sb := &db.SQLBuilder{}
sb.Write("SELECT ")
dialect := ss.db.GetDialect()
sb.Write(`(SELECT COUNT(*) FROM ` + dialect.Quote("user") + ` WHERE ` + notServiceAccount(dialect) + `) AS users,`)
sb.Write(`(SELECT COUNT(*) FROM ` + dialect.Quote("org") + `) AS orgs,`)
sb.Write(`(SELECT COUNT(*) FROM ` + dialect.Quote("data_source") + `) AS datasources,`)
@ -127,6 +127,7 @@ func (ss *sqlStatsService) GetSystemStats(ctx context.Context, query *stats.GetS
sb.Write(`(SELECT COUNT(*) FROM ` + dialect.Quote("data_keys") + `) AS data_keys,`)
sb.Write(`(SELECT COUNT(*) FROM ` + dialect.Quote("data_keys") + `WHERE active = true) AS active_data_keys,`)
sb.Write(`(SELECT COUNT(*) FROM ` + dialect.Quote("dashboard_public") + `) AS public_dashboards,`)
sb.Write(`(SELECT MIN(timestamp) FROM ` + dialect.Quote("migration_log") + `) AS database_created_time,`)
sb.Write(ss.roleCounterSQL(ctx))
@ -138,8 +139,11 @@ func (ss *sqlStatsService) GetSystemStats(ctx context.Context, query *stats.GetS
result = &stats
result.DatabaseDriver = dialect.DriverName()
return nil
})
return result, err
}

View File

@ -41,6 +41,8 @@ func TestIntegrationStatsDataAccess(t *testing.T) {
assert.Equal(t, int64(0), result.LibraryVariables)
assert.Equal(t, int64(0), result.APIKeys)
assert.Equal(t, int64(2), result.Correlations)
assert.NotNil(t, result.DatabaseCreatedTime)
assert.Equal(t, db.Dialect.DriverName(), result.DatabaseDriver)
})
t.Run("Get system user count stats should not results in error", func(t *testing.T) {