Skip to content

Commit

Permalink
stats: fetch using pipeline no-store
Browse files Browse the repository at this point in the history
  • Loading branch information
sehyunc committed Sep 30, 2024
1 parent 040f106 commit 9bd4d10
Showing 1 changed file with 34 additions and 22 deletions.
56 changes: 34 additions & 22 deletions app/api/stats/historical-volume-kv/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 9bd4d10

Please sign in to comment.