更新首页

This commit is contained in:
cheney
2026-08-06 16:41:47 +08:00
parent 547181d333
commit 4d02439ecb
32 changed files with 5470 additions and 93 deletions
+13 -8
View File
@@ -8,16 +8,21 @@ router.use(authRequired)
// 报警列表: 按时间最新优先, cursor 分页 (cursor = id)
router.get('/alerts', async (req, res, next) => {
try {
const limit = Math.min(Number(req.query.limit) || 10, 50)
const cursor = req.query.cursor ? Number(req.query.cursor) : null
const rawLimit = Number(req.query.limit)
const limit = Number.isFinite(rawLimit) && rawLimit >= 1 ? Math.min(Math.floor(rawLimit), 50) : 10
const rawCursor = Number(req.query.cursor)
const cursor = Number.isInteger(rawCursor) && rawCursor > 0 ? rawCursor : null
const where = cursor ? { id: { lt: cursor } } : {}
const items = await prisma.alert.findMany({
where,
orderBy: { id: 'desc' },
take: limit,
})
const [items, total] = await Promise.all([
prisma.alert.findMany({
where,
orderBy: { id: 'desc' },
take: limit,
}),
prisma.alert.count(),
])
const nextCursor = items.length === limit ? String(items[items.length - 1].id) : null
res.json({ items, nextCursor })
res.json({ items, nextCursor, total })
} catch (e) { next(e) }
})
+42
View File
@@ -0,0 +1,42 @@
import { Router } from 'express'
import { prisma } from '../db.js'
import { authRequired } from '../middleware.js'
const router = Router()
router.use(authRequired)
// 服务器节点列表: 元数据 + 最新快照
router.get('/servers', async (_req, res, next) => {
try {
const rows = await prisma.serverNode.findMany({
orderBy: { name: 'asc' },
})
res.json({ items: rows })
} catch (e) { next(e) }
})
// 节点时序: ?uuid=...&hours=24
router.get('/servers/:uuid/history', async (req, res, next) => {
try {
const hours = Math.min(Math.max(Number(req.query.hours) || 24, 1), 168)
const since = new Date(Date.now() - hours * 3600 * 1000)
const rows = await prisma.serverNodeHistory.findMany({
where: { uuid: req.params.uuid, period: { gte: since } },
orderBy: { period: 'asc' },
take: 2000,
})
res.json({ items: rows })
} catch (e) { next(e) }
})
// 服务健康列表
router.get('/services', async (_req, res, next) => {
try {
const rows = await prisma.serviceCheck.findMany({
orderBy: { name: 'asc' },
})
res.json({ items: rows })
} catch (e) { next(e) }
})
export default router
+32 -6
View File
@@ -5,21 +5,47 @@ import { authRequired } from '../middleware.js'
const router = Router()
router.use(authRequired)
// 新闻列表: 按时间最新优先, cursor 分页 (cursor = publishedAt ISO 字符串)
// 用 prisma cursor 配合 id 兜底 (同一秒发布的新闻)
// 新闻列表: 按时间最新优先, cursor 分页
// cursor 格式: "<publishedAt ISO>|<lastId>", 兼容旧版纯时间戳 (不带 |)
// 同秒多条时用 id 兜底, 避免翻页时整批漏掉
router.get('/news', async (req, res, next) => {
try {
const limit = Math.min(Number(req.query.limit) || 10, 50)
const cursor = req.query.cursor || null
const where = cursor ? { publishedAt: { lt: new Date(cursor) } } : {}
const rawLimit = Number(req.query.limit)
const limit = Number.isFinite(rawLimit) && rawLimit >= 1 ? Math.min(Math.floor(rawLimit), 50) : 10
let where = {}
const cursor = req.query.cursor || ''
if (cursor) {
const [ts, idRaw] = cursor.split('|')
const c = new Date(ts)
if (!isNaN(c.getTime())) {
const id = Number(idRaw)
where = id > 0
? { OR: [{ publishedAt: { lt: c } }, { publishedAt: c, id: { lt: id } }] }
: { publishedAt: { lt: c } }
}
}
const items = await prisma.news.findMany({
select: { id: true, title: true, summary: true, source: true, url: true, publishedAt: true, impactRank: true },
where,
orderBy: [{ publishedAt: 'desc' }, { id: 'desc' }],
take: limit,
})
const nextCursor = items.length === limit ? items[items.length - 1].publishedAt.toISOString() : null
const nextCursor = items.length === limit && items.length > 0
? items[items.length - 1].publishedAt.toISOString() + '|' + items[items.length - 1].id
: null
res.json({ items, nextCursor })
} catch (e) { next(e) }
})
// 新闻详情: 含正文 content
router.get('/news/:id', async (req, res, next) => {
try {
const id = Number(req.params.id)
if (!Number.isInteger(id) || id <= 0) return res.status(400).json({ error: '无效的新闻 id' })
const item = await prisma.news.findUnique({ where: { id } })
if (!item) return res.status(404).json({ error: '新闻不存在' })
res.json({ item })
} catch (e) { next(e) }
})
export default router