OOKNET                             [ /  search the index  ]  
──────────────────────────────────────────────────────────────────────────────────────
══════════════════════════════════════════════════════════════════════════════════════
OOKNET   [ /  search  ]  
────────────────────────────────────────────────
════════════════════════════════════════════════
 
HASH      1c300cee8730
DATE      2026-01-12
SUBJECT   ookstats: limit periods
FILES     5 CHANGED
HASH      1c300cee8730
DATE      2026-01-12
SUBJECT   ookstats: limit periods
FILES     5 CHANGED
 

diff --git a/nix/pkgs/ookstats/src/cmd/build.go b/nix/pkgs/ookstats/src/cmd/build.
go
index 23c31ba..3d9b27d 100644
--- a/nix/pkgs/ookstats/src/cmd/build.go
+++ b/nix/pkgs/ookstats/src/cmd/build.go
@@ -45,6 +45,7 @@ var buildCmd = &cobra.Command{
 		periodsCSV, _ := cmd.Flags().GetString("periods")
 		concurrency, _ := cmd.Flags().GetInt("concurrency")
 		workers, _ := cmd.Flags().GetInt("workers")
+		latestPeriodsOnly, _ := cmd.Flags().GetBool("latest-periods")
 
 		// optional verbose logging propagated to API client
 		verbose, _ := cmd.InheritedFlags().GetBool("verbose")
@@ -133,13 +134,14 @@ var buildCmd = &cobra.Command{
 
 		// Use pipeline function (handles child realm filtering automatically)
 		fetchOpts := pipeline.FetchCMOptions{
-			Verbose:     verbose,
-			Regions:     regions,
-			Realms:      []string{}, // no realm filter
-			Dungeons:    []string{}, // no dungeon filter
-			Periods:     periods,    // empty means fetch dynamically
-			Concurrency: concurrency,
-			Timeout:     45 * time.Minute,
+			Verbose:           verbose,
+			Regions:           regions,
+			Realms:            []string{}, // no realm filter
+			Dungeons:          []string{}, // no dungeon filter
+			Periods:           periods,    // empty means fetch dynamically
+			LatestPeriodsOnly: latestPeriodsOnly,
+			Concurrency:       concurrency,
+			Timeout:           45 * time.Minute,
 		}
 
 		result, err := pipeline.FetchChallengeMode(dbService, client, fetchOpts)
@@ -509,6 +511,7 @@ func init() {
 	buildCmd.Flags().String("wowsims-db", "", "Optional path to WoWSims items JSON f
or item enrichment")
 	buildCmd.Flags().Bool("skip-profiles", false, "Skip fetching player detailed pro
files")
 	buildCmd.Flags().String("periods", "", "Period specification: comma-separated li
st or ranges (e.g., '1020-1036' or '1020,1025,1030-1036'). Default: fetch all peri
ods from API")
+	buildCmd.Flags().Bool("latest-periods", false, "Only fetch the latest 2 periods 
from the current season per region (optimized for persistent databases)")
 	buildCmd.Flags().Int("concurrency", 20, "Max concurrent API requests")
 	buildCmd.Flags().Int("workers", 10, "Number of parallel workers for leaderboard 
generation")
 }

diff --git a/nix/pkgs/ookstats/src/cmd/build
.go b/nix/pkgs/ookstats/src/cmd/build.go
index 23c31ba..3d9b27d 100644
--- a/nix/pkgs/ookstats/src/cmd/build.go
+++ b/nix/pkgs/ookstats/src/cmd/build.go
@@ -45,6 +45,7 @@ var buildCmd = &cobra.Comm
and{
 		periodsCSV, _ := cmd.Flags().GetString("p
eriods")
 		concurrency, _ := cmd.Flags().GetInt("con
currency")
 		workers, _ := cmd.Flags().GetInt("workers
")
+		latestPeriodsOnly, _ := cmd.Flags().GetBo
ol("latest-periods")
 
 		// optional verbose logging propagated to
 API client
 		verbose, _ := cmd.InheritedFlags().GetBoo
l("verbose")
@@ -133,13 +134,14 @@ var buildCmd = &cobra.
Command{
 
 		// Use pipeline function (handles child r
ealm filtering automatically)
 		fetchOpts := pipeline.FetchCMOptions{
-			Verbose:     verbose,
-			Regions:     regions,
-			Realms:      []string{}, // no realm fil
ter
-			Dungeons:    []string{}, // no dungeon f
ilter
-			Periods:     periods,    // empty means 
fetch dynamically
-			Concurrency: concurrency,
-			Timeout:     45 * time.Minute,
+			Verbose:           verbose,
+			Regions:           regions,
+			Realms:            []string{}, // no rea
lm filter
+			Dungeons:          []string{}, // no dun
geon filter
+			Periods:           periods,    // empty 
means fetch dynamically
+			LatestPeriodsOnly: latestPeriodsOnly,
+			Concurrency:       concurrency,
+			Timeout:           45 * time.Minute,
 		}
 
 		result, err := pipeline.FetchChallengeMod
e(dbService, client, fetchOpts)
@@ -509,6 +511,7 @@ func init() {
 	buildCmd.Flags().String("wowsims-db", "", 
"Optional path to WoWSims items JSON for ite
m enrichment")
 	buildCmd.Flags().Bool("skip-profiles", fal
se, "Skip fetching player detailed profiles"
)
 	buildCmd.Flags().String("periods", "", "Pe
riod specification: comma-separated list or 
ranges (e.g., '1020-1036' or '1020,1025,1030
-1036'). Default: fetch all periods from API
")
+	buildCmd.Flags().Bool("latest-periods", fa
lse, "Only fetch the latest 2 periods from t
he current season per region (optimized for 
persistent databases)")
 	buildCmd.Flags().Int("concurrency", 20, "M
ax concurrent API requests")
 	buildCmd.Flags().Int("workers", 10, "Numbe
r of parallel workers for leaderboard genera
tion")
 }
 

diff --git a/nix/pkgs/ookstats/src/cmd/fetch.go b/nix/pkgs/ookstats/src/cmd/fetch.
go
index 6fe9aa6..c202a9f 100644
--- a/nix/pkgs/ookstats/src/cmd/fetch.go
+++ b/nix/pkgs/ookstats/src/cmd/fetch.go
@@ -52,6 +52,7 @@ var fetchCMCmd = &cobra.Command{
 		realmsCSV, _ := cmd.Flags().GetString("realms")
 		dungeonsCSV, _ := cmd.Flags().GetString("dungeons")
 		periodsSpec, _ := cmd.Flags().GetString("periods")
+		latestPeriodsOnly, _ := cmd.Flags().GetBool("latest-periods")
 		concurrency, _ := cmd.Flags().GetInt("concurrency")
 		timeoutSecs, _ := cmd.Flags().GetInt("api-timeout-seconds")
 
@@ -102,13 +103,14 @@ var fetchCMCmd = &cobra.Command{
 
 		// Build options
 		opts := pipeline.FetchCMOptions{
-			Verbose:     verbose,
-			Regions:     regions,
-			Realms:      realms,
-			Dungeons:    dungeons,
-			Periods:     periods,
-			Concurrency: concurrency,
-			Timeout:     time.Duration(timeoutSecs) * time.Second,
+			Verbose:           verbose,
+			Regions:           regions,
+			Realms:            realms,
+			Dungeons:          dungeons,
+			Periods:           periods,
+			LatestPeriodsOnly: latestPeriodsOnly,
+			Concurrency:       concurrency,
+			Timeout:           time.Duration(timeoutSecs) * time.Second,
 		}
 		if opts.Timeout == 0 {
 			opts.Timeout = 45 * time.Minute
@@ -467,6 +469,7 @@ func init() {
 	fetchCMCmd.Flags().String("realms", "", "Comma-separated realm slugs to include"
)
 	fetchCMCmd.Flags().String("dungeons", "", "Comma-separated dungeon IDs or slugs 
to include")
 	fetchCMCmd.Flags().String("periods", "", "Period specification: comma-separated 
list or ranges (e.g., '1020-1036' or '1020,1025,1030-1036'). Default: fetch all pe
riods from API")
+	fetchCMCmd.Flags().Bool("latest-periods", false, "Only fetch the latest 2 period
s from the current season per region (optimized for persistent databases)")
 
 	// add player profile fetching flags
 	fetchProfilesCmd.Flags().Int("batch-size", 20, "Number of players to process per
 batch")

diff --git a/nix/pkgs/ookstats/src/cmd/fetch
.go b/nix/pkgs/ookstats/src/cmd/fetch.go
index 6fe9aa6..c202a9f 100644
--- a/nix/pkgs/ookstats/src/cmd/fetch.go
+++ b/nix/pkgs/ookstats/src/cmd/fetch.go
@@ -52,6 +52,7 @@ var fetchCMCmd = &cobra.Co
mmand{
 		realmsCSV, _ := cmd.Flags().GetString("re
alms")
 		dungeonsCSV, _ := cmd.Flags().GetString("
dungeons")
 		periodsSpec, _ := cmd.Flags().GetString("
periods")
+		latestPeriodsOnly, _ := cmd.Flags().GetBo
ol("latest-periods")
 		concurrency, _ := cmd.Flags().GetInt("con
currency")
 		timeoutSecs, _ := cmd.Flags().GetInt("api
-timeout-seconds")
 
@@ -102,13 +103,14 @@ var fetchCMCmd = &cobr
a.Command{
 
 		// Build options
 		opts := pipeline.FetchCMOptions{
-			Verbose:     verbose,
-			Regions:     regions,
-			Realms:      realms,
-			Dungeons:    dungeons,
-			Periods:     periods,
-			Concurrency: concurrency,
-			Timeout:     time.Duration(timeoutSecs) 
* time.Second,
+			Verbose:           verbose,
+			Regions:           regions,
+			Realms:            realms,
+			Dungeons:          dungeons,
+			Periods:           periods,
+			LatestPeriodsOnly: latestPeriodsOnly,
+			Concurrency:       concurrency,
+			Timeout:           time.Duration(timeout
Secs) * time.Second,
 		}
 		if opts.Timeout == 0 {
 			opts.Timeout = 45 * time.Minute
@@ -467,6 +469,7 @@ func init() {
 	fetchCMCmd.Flags().String("realms", "", "C
omma-separated realm slugs to include")
 	fetchCMCmd.Flags().String("dungeons", "", 
"Comma-separated dungeon IDs or slugs to inc
lude")
 	fetchCMCmd.Flags().String("periods", "", "
Period specification: comma-separated list o
r ranges (e.g., '1020-1036' or '1020,1025,10
30-1036'). Default: fetch all periods from A
PI")
+	fetchCMCmd.Flags().Bool("latest-periods", 
false, "Only fetch the latest 2 periods from
 the current season per region (optimized fo
r persistent databases)")
 
 	// add player profile fetching flags
 	fetchProfilesCmd.Flags().Int("batch-size",
 20, "Number of players to process per batch
")
 

diff --git a/nix/pkgs/ookstats/src/internal/database/operations.go b/nix/pkgs/ooks
tats/src/internal/database/operations.go
index dfada77..227c093 100644
--- a/nix/pkgs/ookstats/src/internal/database/operations.go
+++ b/nix/pkgs/ookstats/src/internal/database/operations.go
@@ -1449,6 +1449,34 @@ func (ds *DatabaseService) GetPeriodsForRegion(region strin
g) ([]int, error) {
 	return periods, rows.Err()
 }
 
+// GetLatestPeriodsPerRegion retrieves only the latest 2 periods from the current
 season for a given region
+func (ds *DatabaseService) GetLatestPeriodsPerRegion(region string) ([]int, error
) {
+	query := `
+		SELECT DISTINCT ps.period_id
+		FROM period_seasons ps
+		JOIN seasons s ON ps.season_id = s.id
+		WHERE s.region = ?
+		  AND s.end_timestamp IS NULL
+		ORDER BY ps.period_id DESC
+		LIMIT 2
+	`
+	rows, err := ds.db.Query(query, region)
+	if err != nil {
+		return nil, err
+	}
+	defer rows.Close()
+
+	var periods []int
+	for rows.Next() {
+		var periodID int
+		if err := rows.Scan(&periodID); err != nil {
+			return nil, err
+		}
+		periods = append(periods, periodID)
+	}
+	return periods, rows.Err()
+}
+
 // DetermineSeasonForRun determines which season a run belongs to based on
 // completed_timestamp and region. Returns season_number (not id) for the matchin
g season.
 func (ds *DatabaseService) DetermineSeasonForRun(region string, completedTimestam
p int64) (int, error) {

diff --git a/nix/pkgs/ookstats/src/internal/
database/operations.go b/nix/pkgs/ookstats/s
rc/internal/database/operations.go
index dfada77..227c093 100644
--- a/nix/pkgs/ookstats/src/internal/databas
e/operations.go
+++ b/nix/pkgs/ookstats/src/internal/databas
e/operations.go
@@ -1449,6 +1449,34 @@ func (ds *DatabaseSer
vice) GetPeriodsForRegion(region string) ([]
int, error) {
 	return periods, rows.Err()
 }
 
+// GetLatestPeriodsPerRegion retrieves only
 the latest 2 periods from the current seaso
n for a given region
+func (ds *DatabaseService) GetLatestPeriods
PerRegion(region string) ([]int, error) {
+	query := `
+		SELECT DISTINCT ps.period_id
+		FROM period_seasons ps
+		JOIN seasons s ON ps.season_id = s.id
+		WHERE s.region = ?
+		  AND s.end_timestamp IS NULL
+		ORDER BY ps.period_id DESC
+		LIMIT 2
+	`
+	rows, err := ds.db.Query(query, region)
+	if err != nil {
+		return nil, err
+	}
+	defer rows.Close()
+
+	var periods []int
+	for rows.Next() {
+		var periodID int
+		if err := rows.Scan(&periodID); err != ni
l {
+			return nil, err
+		}
+		periods = append(periods, periodID)
+	}
+	return periods, rows.Err()
+}
+
 // DetermineSeasonForRun determines which s
eason a run belongs to based on
 // completed_timestamp and region. Returns 
season_number (not id) for the matching seas
on.
 func (ds *DatabaseService) DetermineSeasonF
orRun(region string, completedTimestamp int6
4) (int, error) {
 

diff --git a/nix/pkgs/ookstats/src/internal/pipeline/fetch.go b/nix/pkgs/ookstats/
src/internal/pipeline/fetch.go
index 8dc7301..0099bb1 100644
--- a/nix/pkgs/ookstats/src/internal/pipeline/fetch.go
+++ b/nix/pkgs/ookstats/src/internal/pipeline/fetch.go
@@ -11,13 +11,14 @@ import (
 
 // FetchCMOptions contains options for fetching challenge mode leaderboards
 type FetchCMOptions struct {
-	Verbose     bool
-	Regions     []string
-	Realms      []string
-	Dungeons    []string
-	Periods     []string
-	Concurrency int
-	Timeout     time.Duration
+	Verbose           bool
+	Regions           []string
+	Realms            []string
+	Dungeons          []string
+	Periods           []string
+	LatestPeriodsOnly bool
+	Concurrency       int
+	Timeout           time.Duration
 }
 
 // FetchCMResult contains statistics from the fetch operation
@@ -143,8 +144,17 @@ func FetchChallengeMode(db *database.DatabaseService, client 
*blizzard.Client, o
 			fmt.Printf("Using user-specified periods: %v (%d periods)\n", periods, len(per
iods))
 		} else {
 			// Fetch periods from database (populated by season sync)
-			fmt.Printf("Fetching period list from database for %s...\n", strings.ToUpper(r
egion))
-			periodInts, err := db.GetPeriodsForRegion(region)
+			var periodInts []int
+			var err error
+
+			if opts.LatestPeriodsOnly {
+				fmt.Printf("Fetching latest 2 periods from current season for %s...\n", strin
gs.ToUpper(region))
+				periodInts, err = db.GetLatestPeriodsPerRegion(region)
+			} else {
+				fmt.Printf("Fetching period list from database for %s...\n", strings.ToUpper(
region))
+				periodInts, err = db.GetPeriodsForRegion(region)
+			}
+
 			if err != nil {
 				fmt.Printf("Failed to fetch periods from database for %s: %v - skipping regio
n\n", strings.ToUpper(region), err)
 				continue
@@ -161,8 +171,13 @@ func FetchChallengeMode(db *database.DatabaseService, client 
*blizzard.Client, o
 				continue
 			}
 
-			fmt.Printf("[OK] Fetched %d periods from database for %s (newest: %s, oldest: 
%s)\n",
-				len(periods), strings.ToUpper(region), periods[0], periods[len(periods)-1])
+			if opts.LatestPeriodsOnly {
+				fmt.Printf("[OK] Using latest %d period(s) from current season for %s: %v\n",
+					len(periods), strings.ToUpper(region), periods)
+			} else {
+				fmt.Printf("[OK] Fetched %d periods from database for %s (newest: %s, oldest:
 %s)\n",
+					len(periods), strings.ToUpper(region), periods[0], periods[len(periods)-1])
+			}
 		}
 
 		if len(periods) == 0 {

diff --git a/nix/pkgs/ookstats/src/internal/
pipeline/fetch.go b/nix/pkgs/ookstats/src/in
ternal/pipeline/fetch.go
index 8dc7301..0099bb1 100644
--- a/nix/pkgs/ookstats/src/internal/pipelin
e/fetch.go
+++ b/nix/pkgs/ookstats/src/internal/pipelin
e/fetch.go
@@ -11,13 +11,14 @@ import (
 
 // FetchCMOptions contains options for fetc
hing challenge mode leaderboards
 type FetchCMOptions struct {
-	Verbose     bool
-	Regions     []string
-	Realms      []string
-	Dungeons    []string
-	Periods     []string
-	Concurrency int
-	Timeout     time.Duration
+	Verbose           bool
+	Regions           []string
+	Realms            []string
+	Dungeons          []string
+	Periods           []string
+	LatestPeriodsOnly bool
+	Concurrency       int
+	Timeout           time.Duration
 }
 
 // FetchCMResult contains statistics from t
he fetch operation
@@ -143,8 +144,17 @@ func FetchChallengeMode
(db *database.DatabaseService, client *blizz
ard.Client, o
 			fmt.Printf("Using user-specified periods
: %v (%d periods)\n", periods, len(periods))
 		} else {
 			// Fetch periods from database (populate
d by season sync)
-			fmt.Printf("Fetching period list from da
tabase for %s...\n", strings.ToUpper(region)
)
-			periodInts, err := db.GetPeriodsForRegio
n(region)
+			var periodInts []int
+			var err error
+
+			if opts.LatestPeriodsOnly {
+				fmt.Printf("Fetching latest 2 periods f
rom current season for %s...\n", strings.ToU
pper(region))
+				periodInts, err = db.GetLatestPeriodsPe
rRegion(region)
+			} else {
+				fmt.Printf("Fetching period list from d
atabase for %s...\n", strings.ToUpper(region
))
+				periodInts, err = db.GetPeriodsForRegio
n(region)
+			}
+
 			if err != nil {
 				fmt.Printf("Failed to fetch periods fro
m database for %s: %v - skipping region\n", 
strings.ToUpper(region), err)
 				continue
@@ -161,8 +171,13 @@ func FetchChallengeMode
(db *database.DatabaseService, client *blizz
ard.Client, o
 				continue
 			}
 
-			fmt.Printf("[OK] Fetched %d periods from
 database for %s (newest: %s, oldest: %s)\n"
,
-				len(periods), strings.ToUpper(region), 
periods[0], periods[len(periods)-1])
+			if opts.LatestPeriodsOnly {
+				fmt.Printf("[OK] Using latest %d period
(s) from current season for %s: %v\n",
+					len(periods), strings.ToUpper(region),
 periods)
+			} else {
+				fmt.Printf("[OK] Fetched %d periods fro
m database for %s (newest: %s, oldest: %s)\n
",
+					len(periods), strings.ToUpper(region),
 periods[0], periods[len(periods)-1])
+			}
 		}
 
 		if len(periods) == 0 {
 

diff --git a/nix/pkgs/ookstats/src/internal/pipeline/process.go b/nix/pkgs/ookstat
s/src/internal/pipeline/process.go
index 2db462c..3249d8e 100644
--- a/nix/pkgs/ookstats/src/internal/pipeline/process.go
+++ b/nix/pkgs/ookstats/src/internal/pipeline/process.go
@@ -184,6 +184,8 @@ func createPlayerAggregations(tx *sql.Tx) (int, error) {
 			rr_lf.percentile_bracket as realm_percentile_bracket
 		FROM run_members rm
 		INNER JOIN challenge_runs cr ON rm.run_id = cr.id
+		INNER JOIN realms r ON cr.realm_id = r.id
+		LEFT JOIN realms parent_r ON r.parent_realm_slug = parent_r.slug AND r.region =
 parent_r.region
 		INNER JOIN (
 			SELECT
 				rm2.player_id,
@@ -204,7 +206,7 @@ func createPlayerAggregations(tx *sql.Tx) (int, error) {
 			AND rr_rf.ranking_type = 'regional' AND rr_rf.ranking_scope = 'filtered'
 			AND rr_rf.season_id = cr.season_id
 		LEFT JOIN run_rankings rr_lf ON cr.id = rr_lf.run_id
-			AND rr_lf.ranking_type = 'realm' AND rr_lf.ranking_scope = 'filtered'
+			AND rr_lf.ranking_type = 'realm' AND rr_lf.ranking_scope = COALESCE(parent_r.s
lug, r.slug) || '_filtered'
 			AND rr_lf.season_id = cr.season_id
 		GROUP BY rm.player_id, cr.dungeon_id, cr.season_id
 		HAVING cr.id = MIN(cr.id)

diff --git a/nix/pkgs/ookstats/src/internal/
pipeline/process.go b/nix/pkgs/ookstats/src/
internal/pipeline/process.go
index 2db462c..3249d8e 100644
--- a/nix/pkgs/ookstats/src/internal/pipelin
e/process.go
+++ b/nix/pkgs/ookstats/src/internal/pipelin
e/process.go
@@ -184,6 +184,8 @@ func createPlayerAggrega
tions(tx *sql.Tx) (int, error) {
 			rr_lf.percentile_bracket as realm_percen
tile_bracket
 		FROM run_members rm
 		INNER JOIN challenge_runs cr ON rm.run_id
 = cr.id
+		INNER JOIN realms r ON cr.realm_id = r.id
+		LEFT JOIN realms parent_r ON r.parent_rea
lm_slug = parent_r.slug AND r.region = paren
t_r.region
 		INNER JOIN (
 			SELECT
 				rm2.player_id,
@@ -204,7 +206,7 @@ func createPlayerAggrega
tions(tx *sql.Tx) (int, error) {
 			AND rr_rf.ranking_type = 'regional' AND 
rr_rf.ranking_scope = 'filtered'
 			AND rr_rf.season_id = cr.season_id
 		LEFT JOIN run_rankings rr_lf ON cr.id = r
r_lf.run_id
-			AND rr_lf.ranking_type = 'realm' AND rr_
lf.ranking_scope = 'filtered'
+			AND rr_lf.ranking_type = 'realm' AND rr_
lf.ranking_scope = COALESCE(parent_r.slug, r
.slug) || '_filtered'
 			AND rr_lf.season_id = cr.season_id
 		GROUP BY rm.player_id, cr.dungeon_id, cr.
season_id
 		HAVING cr.id = MIN(cr.id)
 
[ BACK TO LOG ]
──────────────────────────────────────────────────────────────────────────────────────
OOKNET
────────────────────────────────────────────────
OOKNET