feat(dashboard): 接入预测结果聚合

This commit is contained in:
2026-05-11 22:21:57 +08:00
parent b11d37e9d8
commit 1a2ff19cf4
4 changed files with 125 additions and 12 deletions
+14 -2
View File
@@ -1,11 +1,23 @@
import { createBatteriesResponse, createDashboardSnapshot } from '@/domain/battery'
import { os } from '@/server/api/server'
import { getBatteryHistory, getLatestBatteryPerDevice } from '@/server/battery/mysql'
import { getBatteryHistory, getBatteryPredictionHistory, getLatestBatteryPerDevice } from '@/server/battery/mysql'
import { isPredictionEnabled, predictSoh } from '@/server/prediction/client'
export const dashboard = os.battery.dashboard.handler(async () => {
const items = await getLatestBatteryPerDevice()
const predictionEntries = isPredictionEnabled()
? await Promise.all(
items.map(async (item) => {
const history = await getBatteryPredictionHistory(item.mac)
const prediction = await predictSoh(item, history)
return createDashboardSnapshot(items)
return prediction ? ([item.mac, prediction] as const) : null
}),
)
: []
const predictions = new Map(predictionEntries.filter((entry) => entry !== null))
return createDashboardSnapshot(items, new Date(), predictions)
})
export const batteries = os.battery.batteries.handler(async ({ input }) => {