Filip Hallqvist 1 ماه پیش
والد
کامیت
dde6add741

+ 14 - 26
apps/insights/src/app/historical-prices/route.ts

@@ -67,30 +67,18 @@ export async function GET(req: NextRequest) {
 }
 
 const MAX_DATA_POINTS = 3000;
-function getNumDataPoints(
-  to: number,
-  from: number,
-  resolution: "1 SECOND" | "1 MINUTE" | "5 MINUTE" | "1 HOUR" | "1 DAY",
-) {
-  let diff = to - from;
-  switch (resolution) {
-    case "1 MINUTE": {
-      diff = diff / 60;
-      break;
-    }
-    case "5 MINUTE": {
-      diff = diff / 60 / 5;
-      break;
-    }
-    case "1 HOUR": {
-      diff = diff / 3600;
-      break;
-    }
-    case "1 DAY": {
-      diff = diff / 86_400;
-      break;
-    }
-  }
 
-  return diff;
-}
+type Resolution = "1 SECOND" | "1 MINUTE" | "5 MINUTE" | "1 HOUR" | "1 DAY";
+const ONE_MINUTE_IN_SECONDS = 60;
+const ONE_HOUR_IN_SECONDS = 60 * ONE_MINUTE_IN_SECONDS;
+
+const SECONDS_IN_ONE_PERIOD: Record<Resolution, number> = {
+  "1 SECOND": 1,
+  "1 MINUTE": ONE_MINUTE_IN_SECONDS,
+  "5 MINUTE": 5 * ONE_MINUTE_IN_SECONDS,
+  "1 HOUR": ONE_HOUR_IN_SECONDS,
+  "1 DAY": 24 * ONE_HOUR_IN_SECONDS,
+};
+
+const getNumDataPoints = (from: number, to: number, resolution: Resolution) =>
+  (to - from) / SECONDS_IN_ONE_PERIOD[resolution];

+ 2 - 1
apps/insights/src/components/PriceFeed/Chart/chart.tsx

@@ -157,7 +157,8 @@ const useChartElem = (symbol: string, feedId: string) => {
           const data = historicalDataSchema.parse(jsonData);
 
           // Get the current historical price data
-          // Note that .data() returns (WhitespaceData | LineData)[], hence the type cast
+          // Note that .data() returns (WhitespaceData | LineData)[], hence the type cast.
+          // We never populate the chart with WhitespaceData, so the type cast is safe.
           const currentHistoricalPriceData =
             chartRef.current.price.data() as LineData[];
           const currentHistoricalConfidenceHighData =

+ 16 - 17
apps/insights/src/components/PriceFeed/Chart/use-chart-toolbar.tsx

@@ -20,6 +20,21 @@ export function useChartResolution() {
   );
 }
 
+const ONE_SECOND_IN_MS = 1000;
+const ONE_MINUTE_IN_MS = 60 * ONE_SECOND_IN_MS;
+const ONE_HOUR_IN_MS = 60 * ONE_MINUTE_IN_MS;
+const ONE_DAY_IN_MS = 24 * ONE_HOUR_IN_MS;
+const ONE_WEEK_IN_MS = 7 * ONE_DAY_IN_MS;
+const ONE_MONTH_IN_MS = 30 * ONE_DAY_IN_MS;
+
+const QUICK_SELECT_WINDOW_TO_MS: Record<QuickSelectWindow, number> = {
+  "1m": ONE_MINUTE_IN_MS,
+  "1H": ONE_HOUR_IN_MS,
+  "1D": ONE_DAY_IN_MS,
+  "1W": ONE_WEEK_IN_MS,
+  "1M": ONE_MONTH_IN_MS,
+};
+
 /**
  * Converts a quick select window string (e.g., "1m", "1H", "1D") to its equivalent duration in milliseconds.
  * Used to determine the time range for chart data based on user selection.
@@ -27,23 +42,7 @@ export function useChartResolution() {
 export function quickSelectWindowToMilliseconds(
   quickSelectWindow: QuickSelectWindow,
 ): number {
-  switch (quickSelectWindow) {
-    case "1m": {
-      return 60_000;
-    }
-    case "1H": {
-      return 3_600_000;
-    }
-    case "1D": {
-      return 86_400_000;
-    }
-    case "1W": {
-      return 604_800_000;
-    }
-    case "1M": {
-      return 2_629_746_000;
-    }
-  }
+  return QUICK_SELECT_WINDOW_TO_MS[quickSelectWindow];
 }
 
 export const RESOLUTION_TO_QUICK_SELECT_WINDOW: Record<