Skip to content
This repository was archived by the owner on Jul 12, 2023. It is now read-only.

Commit 707cfe2

Browse files
authored
No e2 e stats (#2374)
* exclude E2E-TEST realm from system code stats * better error * moar compact sntaxes * pivot to list of IDs * lint
1 parent 1c267ed commit 707cfe2

File tree

3 files changed

+34
-5
lines changed

3 files changed

+34
-5
lines changed

pkg/config/server_config.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,9 @@ type ServerConfig struct {
103103
// MinRealmsForSystemStatistics gives a minimum threshold for displaying system
104104
// admin level statistics
105105
MinRealmsForSystemStatistics uint `env:"MIN_REALMS_FOR_SYSTEM_STATS, default=2"`
106+
// ExcludeFromSystemStatistics is a list of realm IDs to exclude from system stats, in addition
107+
// to any e2e test realms that are found.
108+
ExcludeFromSystemStatistics []uint `env:"EXCLUDE_FROM_SYSTEM_STATS"`
106109

107110
// Rate limiting configuration
108111
RateLimit ratelimit.Config

pkg/controller/admin/code_stats.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ func (c *Controller) HandleCodeStats() http.Handler {
2626
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
2727
ctx := r.Context()
2828

29-
stats, err := c.db.AllRealmCodeStatsCached(ctx, c.cacher, int(c.config.MinRealmsForSystemStatistics))
29+
stats, err := c.db.AllRealmCodeStatsCached(ctx, c.cacher, int(c.config.MinRealmsForSystemStatistics), c.config.ExcludeFromSystemStatistics)
3030
if err != nil {
3131
controller.InternalError(w, r, c.h, err)
3232
return

pkg/database/realm.go

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1323,6 +1323,18 @@ func (db *Database) MaximumUserReportTimeout() (time.Duration, error) {
13231323
return timeout, nil
13241324
}
13251325

1326+
func (db *Database) FindE2ETestRealm() (*Realm, error) {
1327+
var realm Realm
1328+
if err := db.db.
1329+
Model(&Realm{}).
1330+
Where("is_e2e = ?", true).
1331+
First(&realm).
1332+
Error; err != nil {
1333+
return nil, err
1334+
}
1335+
return &realm, nil
1336+
}
1337+
13261338
func (db *Database) FindRealmByRegion(region string) (*Realm, error) {
13271339
var realm Realm
13281340
if err := db.db.
@@ -1924,7 +1936,7 @@ func (r *Realm) Stats(db *Database) (RealmStats, error) {
19241936

19251937
// AllRealmCodeStatsCached returns combined code issue / claimed stats for all realms
19261938
// in the system, but cached.
1927-
func (db *Database) AllRealmCodeStatsCached(ctx context.Context, cacher cache.Cacher, requiredRealms int) (RealmStats, error) {
1939+
func (db *Database) AllRealmCodeStatsCached(ctx context.Context, cacher cache.Cacher, requiredRealms int, excludeRealmIDs []uint) (RealmStats, error) {
19281940
if cacher == nil {
19291941
return nil, fmt.Errorf("cacher cannot be nil")
19301942
}
@@ -1935,7 +1947,7 @@ func (db *Database) AllRealmCodeStatsCached(ctx context.Context, cacher cache.Ca
19351947
Key: fmt.Sprintf("all:min-%d", requiredRealms),
19361948
}
19371949
if err := cacher.Fetch(ctx, cacheKey, &stats, 30*time.Minute, func() (interface{}, error) {
1938-
return db.AllRealmCodeStats(ctx, requiredRealms)
1950+
return db.AllRealmCodeStats(ctx, requiredRealms, excludeRealmIDs)
19391951
}); err != nil {
19401952
return nil, err
19411953
}
@@ -1947,13 +1959,24 @@ func (db *Database) AllRealmCodeStatsCached(ctx context.Context, cacher cache.Ca
19471959
// This reuses the RealmStats data structure and the realm_id is the COUNT
19481960
// of all realms that contributed to statistics on that day, so that filtering
19491961
// can be done if there are not enough data points.
1950-
func (db *Database) AllRealmCodeStats(ctx context.Context, requiredRealms int) (RealmStats, error) {
1962+
func (db *Database) AllRealmCodeStats(ctx context.Context, requiredRealms int, excludeRealmIDs []uint) (RealmStats, error) {
19511963
stop := timeutils.UTCMidnight(time.Now())
19521964
start := stop.Add(project.StatsDisplayDays * -24 * time.Hour)
19531965
if start.After(stop) {
19541966
return nil, ErrBadDateRange
19551967
}
19561968

1969+
allExcludedRealmIDs := make([]uint, 0)
1970+
// Find and exclude e2e test realm if it exists.
1971+
e2eTestRealm, err := db.FindE2ETestRealm()
1972+
if err != nil {
1973+
db.logger.Warnf("unable o find e2e test realms for exclusion from system stats", "error", err)
1974+
} else {
1975+
allExcludedRealmIDs = append(allExcludedRealmIDs, e2eTestRealm.ID)
1976+
}
1977+
// Exclude other explicitly listed realms
1978+
allExcludedRealmIDs = append(allExcludedRealmIDs, excludeRealmIDs...)
1979+
19571980
sql := `
19581981
SELECT
19591982
d.date AS date,
@@ -1974,11 +1997,14 @@ func (db *Database) AllRealmCodeStats(ctx context.Context, requiredRealms int) (
19741997
SELECT date::date FROM generate_series($1, $2, '1 day'::interval) date
19751998
) d
19761999
LEFT JOIN realm_stats s ON s.date = d.date
2000+
WHERE NOT s.realm_id = ANY ($3)
19772001
GROUP BY d.date
19782002
ORDER BY date DESC`
19792003

2004+
values := []any{start, stop, pq.Array(allExcludedRealmIDs)}
2005+
19802006
var stats []*RealmStat
1981-
if err := db.db.Raw(sql, start, stop).Scan(&stats).Error; err != nil {
2007+
if err := db.db.Raw(sql, values).Scan(&stats).Error; err != nil {
19822008
if IsNotFound(err) {
19832009
return stats, nil
19842010
}

0 commit comments

Comments
 (0)