feat(dashboard): 接入预测结果聚合
This commit is contained in:
@@ -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 }) => {
|
||||
|
||||
Reference in New Issue
Block a user