mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
Chore: Add context to star and stats (#39591)
* Add context to star and stats * Use WithTransactionalDbSession * Add additional ctx * Remove convey * Fix star handler name * Use WithDbSession, use DispatchCtx * Remove xorm from star
This commit is contained in:
@@ -10,10 +10,10 @@ import (
|
||||
)
|
||||
|
||||
func init() {
|
||||
bus.AddHandler("sql", GetSystemStats)
|
||||
bus.AddHandler("sql", GetDataSourceStats)
|
||||
bus.AddHandler("sql", GetDataSourceAccessStats)
|
||||
bus.AddHandler("sql", GetAdminStats)
|
||||
bus.AddHandlerCtx("sql", GetSystemStats)
|
||||
bus.AddHandlerCtx("sql", GetDataSourceStats)
|
||||
bus.AddHandlerCtx("sql", GetDataSourceAccessStats)
|
||||
bus.AddHandlerCtx("sql", GetAdminStats)
|
||||
bus.AddHandlerCtx("sql", GetAlertNotifiersUsageStats)
|
||||
bus.AddHandlerCtx("sql", GetSystemUserCountStats)
|
||||
}
|
||||
@@ -28,21 +28,21 @@ func GetAlertNotifiersUsageStats(ctx context.Context, query *models.GetAlertNoti
|
||||
return err
|
||||
}
|
||||
|
||||
func GetDataSourceStats(query *models.GetDataSourceStatsQuery) error {
|
||||
func GetDataSourceStats(ctx context.Context, query *models.GetDataSourceStatsQuery) error {
|
||||
var rawSQL = `SELECT COUNT(*) AS count, type FROM ` + dialect.Quote("data_source") + ` GROUP BY type`
|
||||
query.Result = make([]*models.DataSourceStats, 0)
|
||||
err := x.SQL(rawSQL).Find(&query.Result)
|
||||
return err
|
||||
}
|
||||
|
||||
func GetDataSourceAccessStats(query *models.GetDataSourceAccessStatsQuery) error {
|
||||
func GetDataSourceAccessStats(ctx context.Context, query *models.GetDataSourceAccessStatsQuery) error {
|
||||
var rawSQL = `SELECT COUNT(*) AS count, type, access FROM ` + dialect.Quote("data_source") + ` GROUP BY type, access`
|
||||
query.Result = make([]*models.DataSourceAccessStats, 0)
|
||||
err := x.SQL(rawSQL).Find(&query.Result)
|
||||
return err
|
||||
}
|
||||
|
||||
func GetSystemStats(query *models.GetSystemStatsQuery) error {
|
||||
func GetSystemStats(ctx context.Context, query *models.GetSystemStatsQuery) error {
|
||||
sb := &SQLBuilder{}
|
||||
sb.Write("SELECT ")
|
||||
sb.Write(`(SELECT COUNT(*) FROM ` + dialect.Quote("user") + `) AS users,`)
|
||||
@@ -92,7 +92,7 @@ func GetSystemStats(query *models.GetSystemStatsQuery) error {
|
||||
sb.Write(`(SELECT COUNT(id) FROM `+dialect.Quote("library_element")+` WHERE kind = ?) AS library_panels,`, models.PanelElement)
|
||||
sb.Write(`(SELECT COUNT(id) FROM `+dialect.Quote("library_element")+` WHERE kind = ?) AS library_variables,`, models.VariableElement)
|
||||
|
||||
sb.Write(roleCounterSQL())
|
||||
sb.Write(roleCounterSQL(ctx))
|
||||
|
||||
var stats models.SystemStats
|
||||
_, err := x.SQL(sb.GetSQLString(), sb.params...).Get(&stats)
|
||||
@@ -105,9 +105,9 @@ func GetSystemStats(query *models.GetSystemStatsQuery) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func roleCounterSQL() string {
|
||||
func roleCounterSQL(ctx context.Context) string {
|
||||
const roleCounterTimeout = 20 * time.Second
|
||||
ctx, cancel := context.WithTimeout(context.Background(), roleCounterTimeout)
|
||||
ctx, cancel := context.WithTimeout(ctx, roleCounterTimeout)
|
||||
defer cancel()
|
||||
_ = updateUserRoleCountsIfNecessary(ctx, false)
|
||||
sqlQuery :=
|
||||
@@ -136,7 +136,7 @@ func viewersPermissionsCounterSQL(statName string, isFolder bool, permission mod
|
||||
) AS ` + statName + `, `
|
||||
}
|
||||
|
||||
func GetAdminStats(query *models.GetAdminStatsQuery) error {
|
||||
func GetAdminStats(ctx context.Context, query *models.GetAdminStatsQuery) error {
|
||||
activeEndDate := time.Now().Add(-activeUserTimeLimit)
|
||||
dailyActiveEndDate := time.Now().Add(-dailyActiveUserTimeLimit)
|
||||
|
||||
@@ -185,7 +185,7 @@ func GetAdminStats(query *models.GetAdminStatsQuery) error {
|
||||
SELECT COUNT(*)
|
||||
FROM ` + dialect.Quote("user") + ` WHERE last_seen_at > ?
|
||||
) AS daily_active_users,
|
||||
` + roleCounterSQL() + `,
|
||||
` + roleCounterSQL(ctx) + `,
|
||||
(
|
||||
SELECT COUNT(*)
|
||||
FROM ` + dialect.Quote("user_auth_token") + ` WHERE rotated_at > ?
|
||||
|
||||
Reference in New Issue
Block a user