From 9bd4d10bc761c039ca51b44e99e025d394f12be8 Mon Sep 17 00:00:00 2001 From: sehyunc <41171808+sehyunc@users.noreply.github.com> Date: Mon, 30 Sep 2024 11:52:45 -0700 Subject: [PATCH] stats: fetch using pipeline no-store --- app/api/stats/historical-volume-kv/route.ts | 56 +++++++++++++-------- 1 file changed, 34 insertions(+), 22 deletions(-) diff --git a/app/api/stats/historical-volume-kv/route.ts b/app/api/stats/historical-volume-kv/route.ts index 736f895b..8f4909b1 100644 --- a/app/api/stats/historical-volume-kv/route.ts +++ b/app/api/stats/historical-volume-kv/route.ts @@ -24,30 +24,42 @@ export async function GET(req: NextRequest) { try { const allKeys = await getAllSetMembers(kv, HISTORICAL_VOLUME_SET_KEY) - // Fetch all values for the keys using individual GET requests - const data = await Promise.all( - allKeys.map(async (key) => { - const response = await fetch( - `${process.env.KV_REST_API_URL}/get/${key}`, - { - headers: { - Authorization: `Bearer ${process.env.KV_REST_API_TOKEN}`, - }, - cache: "no-store", - }, - ) - - if (!response.ok) { - throw new Error( - `HTTP error! status: ${response.status} for key: ${key}`, - ) - } - - const { result } = await response.json() - return JSON.parse(result) as VolumeDataPoint - }), + const pipelineBody = JSON.stringify( + allKeys.map(key => ["GET", key]) ) + // Fetch all values for the keys using a single pipeline request + const pipelineResponse = await fetch(`${process.env.KV_REST_API_URL}/pipeline`, { + method: 'POST', + headers: { + Authorization: `Bearer ${process.env.KV_REST_API_TOKEN}`, + }, + body: pipelineBody, + cache: "no-store", + }) + + if (!pipelineResponse.ok) { + throw new Error(`HTTP error! status: ${pipelineResponse.status}`) + } + + const pipelineResults = await pipelineResponse.json() + + // Process the pipeline results + const data: VolumeDataPoint[] = pipelineResults + .map(({ result }: { result: string | null }, index: number) => { + if (result === null) { + console.warn(`No data found for key: ${allKeys[index]}`) + return null + } + try { + return JSON.parse(result) as VolumeDataPoint + } catch (error) { + console.error(`Error parsing result for key ${allKeys[index]}:`, error) + return null + } + }) + .filter((item: VolumeDataPoint | null): item is VolumeDataPoint => item !== null) + const volumeData: VolumeDataPoint[] = [] let startTimestamp = Infinity let endTimestamp = -Infinity