Skip to content

Commit dde6add

Browse files
committed
PR feedback
1 parent 11c89c5 commit dde6add

File tree

3 files changed

+32
-44
lines changed

3 files changed

+32
-44
lines changed

apps/insights/src/app/historical-prices/route.ts

Lines changed: 14 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -67,30 +67,18 @@ export async function GET(req: NextRequest) {
6767
}
6868

6969
const MAX_DATA_POINTS = 3000;
70-
function getNumDataPoints(
71-
to: number,
72-
from: number,
73-
resolution: "1 SECOND" | "1 MINUTE" | "5 MINUTE" | "1 HOUR" | "1 DAY",
74-
) {
75-
let diff = to - from;
76-
switch (resolution) {
77-
case "1 MINUTE": {
78-
diff = diff / 60;
79-
break;
80-
}
81-
case "5 MINUTE": {
82-
diff = diff / 60 / 5;
83-
break;
84-
}
85-
case "1 HOUR": {
86-
diff = diff / 3600;
87-
break;
88-
}
89-
case "1 DAY": {
90-
diff = diff / 86_400;
91-
break;
92-
}
93-
}
9470

95-
return diff;
96-
}
71+
type Resolution = "1 SECOND" | "1 MINUTE" | "5 MINUTE" | "1 HOUR" | "1 DAY";
72+
const ONE_MINUTE_IN_SECONDS = 60;
73+
const ONE_HOUR_IN_SECONDS = 60 * ONE_MINUTE_IN_SECONDS;
74+
75+
const SECONDS_IN_ONE_PERIOD: Record<Resolution, number> = {
76+
"1 SECOND": 1,
77+
"1 MINUTE": ONE_MINUTE_IN_SECONDS,
78+
"5 MINUTE": 5 * ONE_MINUTE_IN_SECONDS,
79+
"1 HOUR": ONE_HOUR_IN_SECONDS,
80+
"1 DAY": 24 * ONE_HOUR_IN_SECONDS,
81+
};
82+
83+
const getNumDataPoints = (from: number, to: number, resolution: Resolution) =>
84+
(to - from) / SECONDS_IN_ONE_PERIOD[resolution];

apps/insights/src/components/PriceFeed/Chart/chart.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,8 @@ const useChartElem = (symbol: string, feedId: string) => {
157157
const data = historicalDataSchema.parse(jsonData);
158158

159159
// Get the current historical price data
160-
// Note that .data() returns (WhitespaceData | LineData)[], hence the type cast
160+
// Note that .data() returns (WhitespaceData | LineData)[], hence the type cast.
161+
// We never populate the chart with WhitespaceData, so the type cast is safe.
161162
const currentHistoricalPriceData =
162163
chartRef.current.price.data() as LineData[];
163164
const currentHistoricalConfidenceHighData =

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

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -20,30 +20,29 @@ export function useChartResolution() {
2020
);
2121
}
2222

23+
const ONE_SECOND_IN_MS = 1000;
24+
const ONE_MINUTE_IN_MS = 60 * ONE_SECOND_IN_MS;
25+
const ONE_HOUR_IN_MS = 60 * ONE_MINUTE_IN_MS;
26+
const ONE_DAY_IN_MS = 24 * ONE_HOUR_IN_MS;
27+
const ONE_WEEK_IN_MS = 7 * ONE_DAY_IN_MS;
28+
const ONE_MONTH_IN_MS = 30 * ONE_DAY_IN_MS;
29+
30+
const QUICK_SELECT_WINDOW_TO_MS: Record<QuickSelectWindow, number> = {
31+
"1m": ONE_MINUTE_IN_MS,
32+
"1H": ONE_HOUR_IN_MS,
33+
"1D": ONE_DAY_IN_MS,
34+
"1W": ONE_WEEK_IN_MS,
35+
"1M": ONE_MONTH_IN_MS,
36+
};
37+
2338
/**
2439
* Converts a quick select window string (e.g., "1m", "1H", "1D") to its equivalent duration in milliseconds.
2540
* Used to determine the time range for chart data based on user selection.
2641
*/
2742
export function quickSelectWindowToMilliseconds(
2843
quickSelectWindow: QuickSelectWindow,
2944
): number {
30-
switch (quickSelectWindow) {
31-
case "1m": {
32-
return 60_000;
33-
}
34-
case "1H": {
35-
return 3_600_000;
36-
}
37-
case "1D": {
38-
return 86_400_000;
39-
}
40-
case "1W": {
41-
return 604_800_000;
42-
}
43-
case "1M": {
44-
return 2_629_746_000;
45-
}
46-
}
45+
return QUICK_SELECT_WINDOW_TO_MS[quickSelectWindow];
4746
}
4847

4948
export const RESOLUTION_TO_QUICK_SELECT_WINDOW: Record<

0 commit comments

Comments
 (0)