Ver código fonte

refactor(stats-aggregation): simplify hourly and daily time window calculations

Dave 1 mês atrás
pai
commit
0f70a65acc

+ 8 - 14
apps/box-stats-api/src/feature/stats-events/stats-aggregation.scheduler.ts

@@ -116,7 +116,8 @@ export class StatsAggregationScheduler implements OnModuleInit {
     const nowSec = Math.floor(Date.now() / 1000);
     const effectiveNowSec = nowSec - this.runDelaySec;
 
-    const { fromSec, toSec } = this.prevHourWindow(effectiveNowSec);
+    const toSec = this.floorToHour(effectiveNowSec);
+    const fromSec = toSec - 3600;
     const tag = `⏰ Hourly aggregation [${fromSec},${toSec})`;
 
     this.logger.log(`${tag} start`);
@@ -180,10 +181,14 @@ export class StatsAggregationScheduler implements OnModuleInit {
       return;
     }
 
+    const nowSec = Math.floor(Date.now() / 1000);
+    const yesterdayStartSec = this.floorToDayGmt8(nowSec) - 86400;
+    const yesterdayEndSec = yesterdayStartSec + 86400;
+    const tag = `🗓️ Daily catch-all (GMT+8) [${yesterdayStartSec},${yesterdayEndSec})`;
+
     if (!this.hasDailyRefresh(this.statsAggregation)) {
-      // No-op if your service doesn’t support daily refresh yet
       this.logger.warn(
-        'ℹ️ Daily catch-all skipped: refreshDailyDerivedFromHourly not implemented',
+        `${tag} skipped: refreshDailyDerivedFromHourly not implemented`,
       );
       return;
     }
@@ -191,12 +196,6 @@ export class StatsAggregationScheduler implements OnModuleInit {
     this.runningDaily = true;
     const startedAt = Date.now();
 
-    // Refresh “yesterday” (GMT+8 aligned) to catch late events
-    const nowSec = Math.floor(Date.now() / 1000);
-    const yesterdayStartSec = this.floorToDayGmt8(nowSec) - 86400;
-    const yesterdayEndSec = yesterdayStartSec + 86400;
-    const tag = `🗓️ Daily catch-all (GMT+8) [${yesterdayStartSec},${yesterdayEndSec})`;
-
     this.logger.log(`${tag} start`);
 
     try {
@@ -218,11 +217,6 @@ export class StatsAggregationScheduler implements OnModuleInit {
     }
   }
 
-  private prevHourWindow(nowSec: number): { fromSec: number; toSec: number } {
-    const endSec = this.floorToHour(nowSec);
-    return { fromSec: endSec - 3600, toSec: endSec };
-  }
-
   private floorToHour(sec: number): number {
     return sec - (sec % 3600);
   }