mirror of
				https://github.com/grafana/grafana.git
				synced 2025-02-25 18:55:37 -06:00 
			
		
		
		
	Add monthlyActiveUsers to stats API and usage report (#41289)
Closes grafana/grafana-enterprise-partnerships-team#7
This commit is contained in:
		
				
					committed by
					
						
						GitHub
					
				
			
			
				
	
			
			
			
						parent
						
							e8e84f9c23
						
					
				
				
					commit
					5afaf8930c
				
			@@ -60,6 +60,7 @@ func (uss *UsageStats) GetUsageReport(ctx context.Context) (usagestats.Report, e
 | 
			
		||||
	metrics["stats.active_editors.count"] = statsQuery.Result.ActiveEditors
 | 
			
		||||
	metrics["stats.active_viewers.count"] = statsQuery.Result.ActiveViewers
 | 
			
		||||
	metrics["stats.active_sessions.count"] = statsQuery.Result.ActiveSessions
 | 
			
		||||
	metrics["stats.monthly_active_users.count"] = statsQuery.Result.MonthlyActiveUsers
 | 
			
		||||
	metrics["stats.daily_active_users.count"] = statsQuery.Result.DailyActiveUsers
 | 
			
		||||
	metrics["stats.daily_active_admins.count"] = statsQuery.Result.DailyActiveAdmins
 | 
			
		||||
	metrics["stats.daily_active_editors.count"] = statsQuery.Result.DailyActiveEditors
 | 
			
		||||
 
 | 
			
		||||
@@ -6,6 +6,7 @@ type SystemStats struct {
 | 
			
		||||
	Users                     int64
 | 
			
		||||
	ActiveUsers               int64
 | 
			
		||||
	DailyActiveUsers          int64
 | 
			
		||||
	MonthlyActiveUsers        int64
 | 
			
		||||
	Orgs                      int64
 | 
			
		||||
	Playlists                 int64
 | 
			
		||||
	Alerts                    int64
 | 
			
		||||
@@ -94,6 +95,7 @@ type AdminStats struct {
 | 
			
		||||
	DailyActiveEditors  int64 `json:"dailyActiveEditors"`
 | 
			
		||||
	DailyActiveViewers  int64 `json:"dailyActiveViewers"`
 | 
			
		||||
	DailyActiveSessions int64 `json:"dailyActiveSessions"`
 | 
			
		||||
	MonthlyActiveUsers  int64 `json:"monthlyActiveUsers"`
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
type GetAdminStatsQuery struct {
 | 
			
		||||
 
 | 
			
		||||
@@ -52,12 +52,16 @@ func GetSystemStats(ctx context.Context, query *models.GetSystemStatsQuery) erro
 | 
			
		||||
	sb.Write(`(SELECT COUNT(*) FROM ` + dialect.Quote("playlist") + `) AS playlists,`)
 | 
			
		||||
	sb.Write(`(SELECT COUNT(*) FROM ` + dialect.Quote("alert") + `) AS alerts,`)
 | 
			
		||||
 | 
			
		||||
	activeUserDeadlineDate := time.Now().Add(-activeUserTimeLimit)
 | 
			
		||||
	now := time.Now()
 | 
			
		||||
	activeUserDeadlineDate := now.Add(-activeUserTimeLimit)
 | 
			
		||||
	sb.Write(`(SELECT COUNT(*) FROM `+dialect.Quote("user")+` WHERE last_seen_at > ?) AS active_users,`, activeUserDeadlineDate)
 | 
			
		||||
 | 
			
		||||
	dailyActiveUserDeadlineDate := time.Now().Add(-dailyActiveUserTimeLimit)
 | 
			
		||||
	dailyActiveUserDeadlineDate := now.Add(-dailyActiveUserTimeLimit)
 | 
			
		||||
	sb.Write(`(SELECT COUNT(*) FROM `+dialect.Quote("user")+` WHERE last_seen_at > ?) AS daily_active_users,`, dailyActiveUserDeadlineDate)
 | 
			
		||||
 | 
			
		||||
	monthlyActiveUserDeadlineDate := time.Date(now.Year(), now.Month(), 1, 0, 0, 0, 0, now.Location())
 | 
			
		||||
	sb.Write(`(SELECT COUNT(*) FROM `+dialect.Quote("user")+` WHERE last_seen_at > ?) AS monthly_active_users,`, monthlyActiveUserDeadlineDate)
 | 
			
		||||
 | 
			
		||||
	sb.Write(`(SELECT COUNT(id) FROM `+dialect.Quote("dashboard")+` WHERE is_folder = ?) AS dashboards,`, dialect.BooleanStr(false))
 | 
			
		||||
	sb.Write(`(SELECT COUNT(id) FROM `+dialect.Quote("dashboard")+` WHERE is_folder = ?) AS folders,`, dialect.BooleanStr(true))
 | 
			
		||||
 | 
			
		||||
@@ -137,8 +141,10 @@ func viewersPermissionsCounterSQL(statName string, isFolder bool, permission mod
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func GetAdminStats(ctx context.Context, query *models.GetAdminStatsQuery) error {
 | 
			
		||||
	activeEndDate := time.Now().Add(-activeUserTimeLimit)
 | 
			
		||||
	dailyActiveEndDate := time.Now().Add(-dailyActiveUserTimeLimit)
 | 
			
		||||
	now := time.Now()
 | 
			
		||||
	activeEndDate := now.Add(-activeUserTimeLimit)
 | 
			
		||||
	dailyActiveEndDate := now.Add(-dailyActiveUserTimeLimit)
 | 
			
		||||
	monthlyActiveEndDate := time.Date(now.Year(), now.Month(), 1, 0, 0, 0, 0, now.Location())
 | 
			
		||||
 | 
			
		||||
	var rawSQL = `SELECT
 | 
			
		||||
		(
 | 
			
		||||
@@ -185,6 +191,10 @@ func GetAdminStats(ctx context.Context, query *models.GetAdminStatsQuery) error
 | 
			
		||||
			SELECT COUNT(*)
 | 
			
		||||
			FROM ` + dialect.Quote("user") + ` WHERE last_seen_at > ?
 | 
			
		||||
		) AS daily_active_users,
 | 
			
		||||
		(
 | 
			
		||||
			SELECT COUNT(*)
 | 
			
		||||
			FROM ` + dialect.Quote("user") + ` WHERE last_seen_at > ?
 | 
			
		||||
		) AS monthly_active_users,
 | 
			
		||||
		` + roleCounterSQL(ctx) + `,
 | 
			
		||||
		(
 | 
			
		||||
			SELECT COUNT(*)
 | 
			
		||||
@@ -196,7 +206,7 @@ func GetAdminStats(ctx context.Context, query *models.GetAdminStatsQuery) error
 | 
			
		||||
		) AS daily_active_sessions`
 | 
			
		||||
 | 
			
		||||
	var stats models.AdminStats
 | 
			
		||||
	_, err := x.SQL(rawSQL, activeEndDate, dailyActiveEndDate, activeEndDate.Unix(), dailyActiveEndDate.Unix()).Get(&stats)
 | 
			
		||||
	_, err := x.SQL(rawSQL, activeEndDate, dailyActiveEndDate, monthlyActiveEndDate, activeEndDate.Unix(), dailyActiveEndDate.Unix()).Get(&stats)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return err
 | 
			
		||||
	}
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user