Skip to content

Commit d79eacb

Browse files
fix: set buried historical baseline to 800
Use 800 as the default pre-Redis buried count and initialize missing Redis buried stats at that baseline before incrementing. Made-with: Cursor
1 parent b95a670 commit d79eacb

1 file changed

Lines changed: 11 additions & 3 deletions

File tree

src/app/api/stats/route.ts

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import { NextRequest, NextResponse } from 'next/server'
22

3+
const BURIED_HISTORICAL_BASELINE = 800
4+
35
async function getRedis() {
46
if (!process.env.KV_REST_API_URL || !process.env.KV_REST_API_TOKEN) return null
57
try {
@@ -13,19 +15,19 @@ async function getRedis() {
1315
export async function GET() {
1416
try {
1517
const redis = await getRedis()
16-
if (!redis) return NextResponse.json({ buried: 0, shared: 0, downloaded: 0 })
18+
if (!redis) return NextResponse.json({ buried: BURIED_HISTORICAL_BASELINE, shared: 0, downloaded: 0 })
1719
const [buried, shared, downloaded] = await Promise.all([
1820
redis.get<number>('stats:buried'),
1921
redis.get<number>('stats:shared'),
2022
redis.get<number>('stats:downloaded'),
2123
])
2224
return NextResponse.json({
23-
buried: buried ?? 0,
25+
buried: buried ?? BURIED_HISTORICAL_BASELINE,
2426
shared: shared ?? 0,
2527
downloaded: downloaded ?? 0,
2628
})
2729
} catch {
28-
return NextResponse.json({ buried: 0, shared: 0, downloaded: 0 })
30+
return NextResponse.json({ buried: BURIED_HISTORICAL_BASELINE, shared: 0, downloaded: 0 })
2931
}
3032
}
3133

@@ -37,6 +39,12 @@ export async function POST(req: NextRequest) {
3739
}
3840
const redis = await getRedis()
3941
if (!redis) return NextResponse.json({ ok: true })
42+
if (counter === 'buried') {
43+
const currentBuried = await redis.get<number>('stats:buried')
44+
if (currentBuried == null) {
45+
await redis.set('stats:buried', BURIED_HISTORICAL_BASELINE)
46+
}
47+
}
4048
await redis.incr(`stats:${counter}`)
4149
return NextResponse.json({ ok: true })
4250
} catch {

0 commit comments

Comments
 (0)