fix: peak hours analysis uses all historical data
Remove date range filtering from peak-hours endpoint so the analysis considers all available data in the database, not just the last 30 days.
This commit is contained in:
@@ -2,7 +2,6 @@ import type { Context } from "hono";
|
|||||||
import type { QuoteType } from "../../../generated/prisma/client";
|
import type { QuoteType } from "../../../generated/prisma/client";
|
||||||
import { cache } from "../../../lib/cache";
|
import { cache } from "../../../lib/cache";
|
||||||
import { prisma } from "../../../lib/prisma";
|
import { prisma } from "../../../lib/prisma";
|
||||||
import { parseLocalDate, startOfNextLocalDay } from "../../../lib/utils";
|
|
||||||
|
|
||||||
const VALID_TYPES = ["BLUE", "BNA", "BELO"] as const;
|
const VALID_TYPES = ["BLUE", "BNA", "BELO"] as const;
|
||||||
|
|
||||||
@@ -18,21 +17,7 @@ export async function getPeakHours(c: Context) {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
const type = rawType as QuoteType;
|
const cacheKey = `quotes:peak-hours:${rawType}`;
|
||||||
const startDateParam = c.req.query("startDate");
|
|
||||||
const endDateParam = c.req.query("endDate");
|
|
||||||
|
|
||||||
const now = new Date();
|
|
||||||
const startDate = startDateParam
|
|
||||||
? parseLocalDate(startDateParam)
|
|
||||||
: new Date(now.getFullYear(), now.getMonth(), 1);
|
|
||||||
const endDate = endDateParam ? startOfNextLocalDay(endDateParam) : now;
|
|
||||||
|
|
||||||
if (Number.isNaN(startDate.getTime()) || Number.isNaN(endDate.getTime())) {
|
|
||||||
return c.json({ error: "Invalid date format. Use ISO 8601." }, 400);
|
|
||||||
}
|
|
||||||
|
|
||||||
const cacheKey = `quotes:peak-hours:${type}:${startDate.toISOString()}:${endDate.toISOString()}`;
|
|
||||||
const cached = cache.get(cacheKey);
|
const cached = cache.get(cacheKey);
|
||||||
if (cached) return c.json(cached);
|
if (cached) return c.json(cached);
|
||||||
|
|
||||||
@@ -56,8 +41,6 @@ export async function getPeakHours(c: Context) {
|
|||||||
) AS rn
|
) AS rn
|
||||||
FROM "quotes_history"
|
FROM "quotes_history"
|
||||||
WHERE "type" = ${rawType}::"QuoteType"
|
WHERE "type" = ${rawType}::"QuoteType"
|
||||||
AND "timeStamp" >= ${startDate}::timestamptz
|
|
||||||
AND "timeStamp" < ${endDate}::timestamptz
|
|
||||||
)
|
)
|
||||||
SELECT
|
SELECT
|
||||||
peak_hour AS hour,
|
peak_hour AS hour,
|
||||||
@@ -75,8 +58,6 @@ export async function getPeakHours(c: Context) {
|
|||||||
SELECT COUNT(DISTINCT DATE("timeStamp"))::int AS count
|
SELECT COUNT(DISTINCT DATE("timeStamp"))::int AS count
|
||||||
FROM "quotes_history"
|
FROM "quotes_history"
|
||||||
WHERE "type" = ${rawType}::"QuoteType"
|
WHERE "type" = ${rawType}::"QuoteType"
|
||||||
AND "timeStamp" >= ${startDate}::timestamptz
|
|
||||||
AND "timeStamp" < ${endDate}::timestamptz
|
|
||||||
`;
|
`;
|
||||||
|
|
||||||
const response = {
|
const response = {
|
||||||
|
|||||||
@@ -101,11 +101,8 @@ export function AnalysisPage({ currency }: { currency: string }) {
|
|||||||
);
|
);
|
||||||
const { data: monthlyLastData } = useMonthlyLast();
|
const { data: monthlyLastData } = useMonthlyLast();
|
||||||
|
|
||||||
const { data: peakHoursData, isLoading: peakHoursLoading } = usePeakHours(
|
const { data: peakHoursData, isLoading: peakHoursLoading } =
|
||||||
"BELO",
|
usePeakHours("BELO");
|
||||||
startDateStr,
|
|
||||||
endDateStr,
|
|
||||||
);
|
|
||||||
|
|
||||||
const chartData = useMemo(() => {
|
const chartData = useMemo(() => {
|
||||||
if (!daily) return [];
|
if (!daily) return [];
|
||||||
|
|||||||
@@ -122,13 +122,8 @@ export function getHistoricalMinMax(type: string): Promise<HistoricalMinMax> {
|
|||||||
return fetcher<HistoricalMinMax>(`/api/quotes/${type}/historical/min-max`);
|
return fetcher<HistoricalMinMax>(`/api/quotes/${type}/historical/min-max`);
|
||||||
}
|
}
|
||||||
|
|
||||||
export function getPeakHours(
|
export function getPeakHours(type: string): Promise<PeakHoursResponse> {
|
||||||
type: string,
|
return fetcher<PeakHoursResponse>(`/api/quotes/${type}/peak-hours`);
|
||||||
startDate: string,
|
|
||||||
endDate: string,
|
|
||||||
): Promise<PeakHoursResponse> {
|
|
||||||
const params = new URLSearchParams({ startDate, endDate });
|
|
||||||
return fetcher<PeakHoursResponse>(`/api/quotes/${type}/peak-hours?${params}`);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export type MonthlyLast = {
|
export type MonthlyLast = {
|
||||||
|
|||||||
@@ -64,8 +64,8 @@ export const quoteKeys = {
|
|||||||
["quotes", type, "daily", startDate, endDate] as const,
|
["quotes", type, "daily", startDate, endDate] as const,
|
||||||
historicalMinMax: (type: string) =>
|
historicalMinMax: (type: string) =>
|
||||||
["quotes", type, "historicalMinMax"] as const,
|
["quotes", type, "historicalMinMax"] as const,
|
||||||
peakHours: (type: string, startDate: string, endDate: string) =>
|
peakHours: (type: string) =>
|
||||||
["quotes", type, "peakHours", startDate, endDate] as const,
|
["quotes", type, "peakHours"] as const,
|
||||||
};
|
};
|
||||||
|
|
||||||
export function useQuotes() {
|
export function useQuotes() {
|
||||||
@@ -135,14 +135,10 @@ export function useHistoricalMinMax(type: string) {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
export function usePeakHours(
|
export function usePeakHours(type: string) {
|
||||||
type: string,
|
|
||||||
startDate: string,
|
|
||||||
endDate: string,
|
|
||||||
) {
|
|
||||||
return useQuery({
|
return useQuery({
|
||||||
queryKey: quoteKeys.peakHours(type, startDate, endDate),
|
queryKey: quoteKeys.peakHours(type),
|
||||||
queryFn: () => getPeakHours(type, startDate, endDate),
|
queryFn: () => getPeakHours(type),
|
||||||
staleTime: 30_000,
|
staleTime: 30_000,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user