فهرست منبع

refactor: optimize date calculations in daily and hourly aggregation services

Dave 1 ماه پیش
والد
کامیت
80d9874233

+ 14 - 12
apps/box-stats-api/src/feature/stats-events/stats.daily.aggregation.service.ts

@@ -118,20 +118,22 @@ export class StatsDailyAggregationService {
           channelId: 1,
           uid: 1,
           dayStartAt: {
-            $toLong: {
-              $divide: [
-                {
-                  $toLong: {
-                    $dateTrunc: {
-                      date: { $toDate: { $multiply: ['$createAt', 1000] } },
-                      unit: 'day',
-                      timezone: '+08:00',
+            $subtract: [
+              {
+                $multiply: [
+                  {
+                    $floor: {
+                      $divide: [
+                        { $add: [{ $toLong: '$createAt' }, 28800] },
+                        86400,
+                      ],
                     },
                   },
-                },
-                1000,
-              ],
-            },
+                  86400,
+                ],
+              },
+              28800,
+            ],
           },
         },
       },

+ 28 - 25
apps/box-stats-api/src/feature/stats-events/stats.hourly.aggregation.service.ts

@@ -38,22 +38,23 @@ export class StatsHourlyAggregationService {
       {
         $project: {
           adsId: { $toString: '$adsId' },
-          // Convert seconds -> Date(ms), truncate to hour in GMT+8, convert back to seconds
           hourStartAt: {
-            $toLong: {
-              $divide: [
-                {
-                  $toLong: {
-                    $dateTrunc: {
-                      date: { $toDate: { $multiply: ['$clickedAt', 1000] } },
-                      unit: 'hour',
-                      timezone: '+08:00',
+            $subtract: [
+              {
+                $multiply: [
+                  {
+                    $floor: {
+                      $divide: [
+                        { $add: [{ $toLong: '$clickedAt' }, 28800] },
+                        3600,
+                      ],
                     },
                   },
-                },
-                1000,
-              ],
-            },
+                  3600,
+                ],
+              },
+              28800,
+            ],
           },
         },
       },
@@ -127,20 +128,22 @@ export class StatsHourlyAggregationService {
           channelId: 1,
           uid: 1,
           hourStartAt: {
-            $toLong: {
-              $divide: [
-                {
-                  $toLong: {
-                    $dateTrunc: {
-                      date: { $toDate: { $multiply: ['$createAt', 1000] } },
-                      unit: 'hour',
-                      timezone: '+08:00',
+            $subtract: [
+              {
+                $multiply: [
+                  {
+                    $floor: {
+                      $divide: [
+                        { $add: [{ $toLong: '$createAt' }, 28800] },
+                        3600,
+                      ],
                     },
                   },
-                },
-                1000,
-              ],
-            },
+                  3600,
+                ],
+              },
+              28800,
+            ],
           },
         },
       },