time-distribution api performance
This commit is contained in:
parent
7b5b73fb1a
commit
5a97bc2a8d
@ -1,6 +1,7 @@
|
|||||||
import { NextRequest, NextResponse } from 'next/server'
|
import { NextRequest, NextResponse } from 'next/server'
|
||||||
import { prisma } from '@/lib/prisma'
|
import { prisma } from '@/lib/prisma'
|
||||||
import { withCors } from '@/lib/middleware'
|
import { withCors } from '@/lib/middleware'
|
||||||
|
import { Prisma } from '@prisma/client' // 为了 Prisma.sql / Prisma.join
|
||||||
|
|
||||||
async function handleTimeDistribution(req: NextRequest) {
|
async function handleTimeDistribution(req: NextRequest) {
|
||||||
try {
|
try {
|
||||||
@ -12,55 +13,35 @@ async function handleTimeDistribution(req: NextRequest) {
|
|||||||
return NextResponse.json({ error: '缺少主机名' }, { status: 400 })
|
return NextResponse.json({ error: '缺少主机名' }, { status: 400 })
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get all records for the hostname and group by hour
|
const sp = req.nextUrl.searchParams
|
||||||
const records = await prisma.record.findMany({
|
const fromParam = sp.get('from') // ISO,如 2025-09-01T00:00:00Z
|
||||||
where: { hostname },
|
const toParam = sp.get('to') // ISO
|
||||||
select: {
|
|
||||||
timestamp: true
|
|
||||||
}
|
|
||||||
})
|
|
||||||
|
|
||||||
// Group by hour
|
const whereParts: any[] = [Prisma.sql`"hostname" = ${hostname}`]
|
||||||
interface DistributionEntry {
|
if (fromParam) whereParts.push(Prisma.sql`"timestamp" >= ${new Date(fromParam)}`)
|
||||||
timestamp: number
|
if (toParam) whereParts.push(Prisma.sql`"timestamp" < ${new Date(toParam)}`)
|
||||||
count: number
|
|
||||||
}
|
|
||||||
|
|
||||||
const distribution = records.reduce((acc: DistributionEntry[], record: { timestamp: Date }) => {
|
const rows = await prisma.$queryRaw<
|
||||||
const timestamp = new Date(record.timestamp)
|
{ ts: bigint | number; count: number }[]
|
||||||
// Create hour-level timestamp (set minutes, seconds, ms to 0)
|
>(Prisma.sql`
|
||||||
const hourTimestamp = new Date(
|
SELECT
|
||||||
timestamp.getFullYear(),
|
EXTRACT(EPOCH FROM date_trunc('hour', "timestamp"))::bigint AS ts,
|
||||||
timestamp.getMonth(),
|
COUNT(*)::int AS count
|
||||||
timestamp.getDate(),
|
FROM "records"
|
||||||
timestamp.getHours(),
|
WHERE ${Prisma.join(whereParts, ' AND ')}
|
||||||
0, 0, 0
|
GROUP BY 1
|
||||||
)
|
ORDER BY 1
|
||||||
|
`)
|
||||||
|
|
||||||
const existingEntry = acc.find(entry =>
|
// JSON 不支持 BigInt,转成 number(秒级时间戳安全)
|
||||||
entry.timestamp === Math.floor(hourTimestamp.getTime() / 1000)
|
const distribution = rows.map(r => ({
|
||||||
)
|
timestamp: Number(r.ts),
|
||||||
|
count: r.count
|
||||||
if (existingEntry) {
|
}))
|
||||||
existingEntry.count++
|
|
||||||
} else {
|
|
||||||
acc.push({
|
|
||||||
timestamp: Math.floor(hourTimestamp.getTime() / 1000),
|
|
||||||
count: 1
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
return acc
|
|
||||||
}, [])
|
|
||||||
|
|
||||||
// Sort by timestamp
|
|
||||||
distribution.sort((a: DistributionEntry, b: DistributionEntry) => a.timestamp - b.timestamp)
|
|
||||||
|
|
||||||
return NextResponse.json({
|
|
||||||
hostname,
|
|
||||||
distribution
|
|
||||||
})
|
|
||||||
|
|
||||||
|
return NextResponse.json(
|
||||||
|
{ hostname, distribution },
|
||||||
|
)
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('获取时间分布统计失败:', error)
|
console.error('获取时间分布统计失败:', error)
|
||||||
return NextResponse.json({ error: '获取时间分布统计失败' }, { status: 500 })
|
return NextResponse.json({ error: '获取时间分布统计失败' }, { status: 500 })
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user