81 lines
1.9 KiB
JavaScript
81 lines
1.9 KiB
JavaScript
const express = require('express')
|
||
const { getSituation } = require('./situationData')
|
||
const db = require('./db')
|
||
|
||
const router = express.Router()
|
||
|
||
// 数据库 Dashboard:返回各表原始数据
|
||
router.get('/db/dashboard', (req, res) => {
|
||
try {
|
||
const tables = [
|
||
'situation',
|
||
'force_summary',
|
||
'power_index',
|
||
'force_asset',
|
||
'key_location',
|
||
'combat_losses',
|
||
'wall_street_trend',
|
||
'retaliation_current',
|
||
'retaliation_history',
|
||
'situation_update',
|
||
'gdelt_events',
|
||
'conflict_stats',
|
||
]
|
||
const data = {}
|
||
const timeSort = {
|
||
situation: 'updated_at DESC',
|
||
situation_update: 'timestamp DESC',
|
||
gdelt_events: 'event_time DESC',
|
||
wall_street_trend: 'time DESC',
|
||
retaliation_history: 'time DESC',
|
||
conflict_stats: 'updated_at DESC',
|
||
}
|
||
for (const name of tables) {
|
||
try {
|
||
const order = timeSort[name]
|
||
let rows
|
||
try {
|
||
rows = order
|
||
? db.prepare(`SELECT * FROM ${name} ORDER BY ${order}`).all()
|
||
: db.prepare(`SELECT * FROM ${name}`).all()
|
||
} catch (qerr) {
|
||
rows = db.prepare(`SELECT * FROM ${name}`).all()
|
||
}
|
||
data[name] = rows
|
||
} catch (e) {
|
||
data[name] = { error: e.message }
|
||
}
|
||
}
|
||
res.json(data)
|
||
} catch (err) {
|
||
console.error(err)
|
||
res.status(500).json({ error: err.message })
|
||
}
|
||
})
|
||
|
||
router.get('/situation', (req, res) => {
|
||
try {
|
||
res.json(getSituation())
|
||
} catch (err) {
|
||
console.error(err)
|
||
res.status(500).json({ error: err.message })
|
||
}
|
||
})
|
||
|
||
router.get('/events', (req, res) => {
|
||
try {
|
||
const s = getSituation()
|
||
res.json({
|
||
updated_at: s.lastUpdated,
|
||
count: (s.conflictEvents || []).length,
|
||
events: s.conflictEvents || [],
|
||
conflict_stats: s.conflictStats || {},
|
||
})
|
||
} catch (err) {
|
||
console.error(err)
|
||
res.status(500).json({ error: err.message })
|
||
}
|
||
})
|
||
|
||
module.exports = router
|