Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { RefreshButton } from "@/components/logs/refresh-button";
import { trpc } from "@/lib/trpc/client";
import { useRatelimitLogsContext } from "../../../context/logs";
import { useFilters } from "../../../hooks/use-filters";
import { refreshQueryTimestamp } from "../../table/hooks/utils";

export const LogsRefresh = () => {
const { toggleLive, isLive } = useRatelimitLogsContext();
Expand All @@ -10,6 +11,7 @@ export const LogsRefresh = () => {
const hasRelativeFilter = filters.find((f) => f.field === "since");

const handleRefresh = () => {
refreshQueryTimestamp();
ratelimit.logs.query.invalidate();
ratelimit.logs.queryRatelimitTimeseries.invalidate();
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import type { RatelimitLog } from "@unkey/clickhouse/src/ratelimits";
import { useCallback, useEffect, useMemo, useState } from "react";
import { useFilters } from "../../../hooks/use-filters";
import type { RatelimitQueryLogsPayload } from "../query-logs.schema";
import { useQueryTimestamp } from "./utils";

type UseLogsQueryParams = {
limit?: number;
Expand All @@ -23,6 +24,8 @@ export function useRatelimitLogsQuery({
const [realtimeLogsMap, setRealtimeLogsMap] = useState(() => new Map<string, RatelimitLog>());
const [totalCount, setTotalCount] = useState(0);

const timestamp = useQueryTimestamp();

const { filters } = useFilters();
const queryClient = trpc.useUtils();

Expand All @@ -32,13 +35,11 @@ export function useRatelimitLogsQuery({

const historicalLogs = useMemo(() => Array.from(historicalLogsMap.values()), [historicalLogsMap]);

//Required for preventing double trpc call during initial render
const dateNow = useMemo(() => Date.now(), []);
const queryParams = useMemo(() => {
const params: RatelimitQueryLogsPayload = {
limit,
startTime: dateNow - HISTORICAL_DATA_WINDOW,
endTime: dateNow,
startTime: timestamp - HISTORICAL_DATA_WINDOW,
endTime: timestamp,
requestIds: { filters: [] },
identifiers: { filters: [] },
status: { filters: [] },
Expand Down Expand Up @@ -105,7 +106,7 @@ export function useRatelimitLogsQuery({
});

return params;
}, [filters, limit, dateNow, namespaceId]);
}, [filters, limit, namespaceId, timestamp]);

// Main query for historical data
const {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { useEffect, useState } from "react";

let QUERY_TIMESTAMP = Date.now();
Comment thread
ogzhanolguncu marked this conversation as resolved.
Outdated
let subscribers: ((timestamp: number) => void)[] = [];

export const getQueryTimestamp = () => QUERY_TIMESTAMP;

export const refreshQueryTimestamp = () => {
QUERY_TIMESTAMP = Date.now();
subscribers.forEach((callback) => callback(QUERY_TIMESTAMP));
return QUERY_TIMESTAMP;
};

export const useQueryTimestamp = () => {
const [timestamp, setTimestamp] = useState(QUERY_TIMESTAMP);

useEffect(() => {
const callback = (newTimestamp: number) => setTimestamp(newTimestamp);
subscribers.push(callback);

return () => {
subscribers = subscribers.filter((cb) => cb !== callback);
};
}, []);

return timestamp;
};