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:
Jose Selesan
2026-08-28 13:31:30 -03:00
parent 0998d64cbb
commit f2c26c266c
4 changed files with 10 additions and 41 deletions

View File

@@ -2,7 +2,6 @@ import type { Context } from "hono";
import type { QuoteType } from "../../../generated/prisma/client";
import { cache } from "../../../lib/cache";
import { prisma } from "../../../lib/prisma";
import { parseLocalDate, startOfNextLocalDay } from "../../../lib/utils";
const VALID_TYPES = ["BLUE", "BNA", "BELO"] as const;
@@ -18,21 +17,7 @@ export async function getPeakHours(c: Context) {
);
}
const type = rawType as QuoteType;
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 cacheKey = `quotes:peak-hours:${rawType}`;
const cached = cache.get(cacheKey);
if (cached) return c.json(cached);
@@ -56,8 +41,6 @@ export async function getPeakHours(c: Context) {
) AS rn
FROM "quotes_history"
WHERE "type" = ${rawType}::"QuoteType"
AND "timeStamp" >= ${startDate}::timestamptz
AND "timeStamp" < ${endDate}::timestamptz
)
SELECT
peak_hour AS hour,
@@ -75,8 +58,6 @@ export async function getPeakHours(c: Context) {
SELECT COUNT(DISTINCT DATE("timeStamp"))::int AS count
FROM "quotes_history"
WHERE "type" = ${rawType}::"QuoteType"
AND "timeStamp" >= ${startDate}::timestamptz
AND "timeStamp" < ${endDate}::timestamptz
`;
const response = {

View File

@@ -101,11 +101,8 @@ export function AnalysisPage({ currency }: { currency: string }) {
);
const { data: monthlyLastData } = useMonthlyLast();
const { data: peakHoursData, isLoading: peakHoursLoading } = usePeakHours(
"BELO",
startDateStr,
endDateStr,
);
const { data: peakHoursData, isLoading: peakHoursLoading } =
usePeakHours("BELO");
const chartData = useMemo(() => {
if (!daily) return [];

View File

@@ -122,13 +122,8 @@ export function getHistoricalMinMax(type: string): Promise<HistoricalMinMax> {
return fetcher<HistoricalMinMax>(`/api/quotes/${type}/historical/min-max`);
}
export function getPeakHours(
type: string,
startDate: string,
endDate: string,
): Promise<PeakHoursResponse> {
const params = new URLSearchParams({ startDate, endDate });
return fetcher<PeakHoursResponse>(`/api/quotes/${type}/peak-hours?${params}`);
export function getPeakHours(type: string): Promise<PeakHoursResponse> {
return fetcher<PeakHoursResponse>(`/api/quotes/${type}/peak-hours`);
}
export type MonthlyLast = {

View File

@@ -64,8 +64,8 @@ export const quoteKeys = {
["quotes", type, "daily", startDate, endDate] as const,
historicalMinMax: (type: string) =>
["quotes", type, "historicalMinMax"] as const,
peakHours: (type: string, startDate: string, endDate: string) =>
["quotes", type, "peakHours", startDate, endDate] as const,
peakHours: (type: string) =>
["quotes", type, "peakHours"] as const,
};
export function useQuotes() {
@@ -135,14 +135,10 @@ export function useHistoricalMinMax(type: string) {
});
}
export function usePeakHours(
type: string,
startDate: string,
endDate: string,
) {
export function usePeakHours(type: string) {
return useQuery({
queryKey: quoteKeys.peakHours(type, startDate, endDate),
queryFn: () => getPeakHours(type, startDate, endDate),
queryKey: quoteKeys.peakHours(type),
queryFn: () => getPeakHours(type),
staleTime: 30_000,
});
}