Skip to content

Commit

Permalink
stats: replace pipeline with .all
Browse files Browse the repository at this point in the history
  • Loading branch information
sehyunc committed Sep 28, 2024
1 parent 0513a2c commit ef5d245
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 5 deletions.
27 changes: 23 additions & 4 deletions app/api/stats/historical-volume-kv/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,29 @@ export async function GET(req: NextRequest) {
try {
const allKeys = await getAllSetMembers(kv, HISTORICAL_VOLUME_SET_KEY)

// Use pipelining to fetch all data in a single round-trip
const pipeline = kv.pipeline()
allKeys.forEach((key) => pipeline.get(key))
const data = await pipeline.exec()
// 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 volumeData: VolumeDataPoint[] = []
let startTimestamp = Infinity
Expand Down
4 changes: 3 additions & 1 deletion app/stats/hooks/use-volume-data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ export function useVolumeData(): UseHistoricalVolumeResult {
}

export async function getHistoricalVolume(): Promise<VolumeDataPoint[]> {
const res = await fetch("/api/stats/historical-volume-kv", { cache: 'no-store' })
const res = await fetch("/api/stats/historical-volume-kv", {
cache: "no-store",
})

if (!res.ok) {
throw new Error(`Failed to fetch historical volume data: ${res.statusText}`)
Expand Down

0 comments on commit ef5d245

Please sign in to comment.