Files
usa/docs/BACKEND_MODULES.md
2026-03-02 01:00:04 +08:00

92 lines
2.5 KiB
Markdown
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# 后端模块说明
## 一、现有模块结构
```
server/
├── index.js # HTTP + WebSocket 入口
├── routes.js # REST API 路由
├── db.js # SQLite schema 与连接
├── situationData.js # 态势数据聚合 (从 DB 读取)
├── seed.js # 初始数据填充
├── data.db # SQLite 数据库
└── package.json
crawler/
├── realtime_conflict_service.py # GDELT 实时冲突服务 (核心)
├── requirements.txt
├── config.py, db_writer.py # 旧 RSS 爬虫(可保留)
├── main.py
└── README.md
```
### 1. server/index.js
- Express + CORS
- WebSocket (`/ws`),每 5 秒广播 `situation`
- `POST /api/crawler/notify`:爬虫写入后触发立即广播
### 2. server/routes.js
- `GET /api/situation`:完整态势
- `GET /api/events`GDELT 事件 + 冲突统计
- `GET /api/health`:健康检查
### 3. server/db.js
- 表:`situation``force_summary``power_index``force_asset`
`key_location``combat_losses``wall_street_trend`
`retaliation_current``retaliation_history``situation_update`
**`gdelt_events`**、**`conflict_stats`**
---
## 二、GDELT 核心数据源
**GDELT Project**:全球冲突数据库,约 15 分钟级更新,含经纬度、事件编码、参与方、事件强度。
### realtime_conflict_service.py
- 定时(默认 60 秒)从 GDELT API 抓取
- 冲突强度评分missile +3, strike +2, killed +4 等
- 无经纬度时默认攻击源:`IRAN_COORD = [51.3890, 35.6892]`
- 写入 `gdelt_events``conflict_stats`
- 调用 `POST /api/crawler/notify` 触发 Node 广播
### 冲突强度 → 地图效果
| impact_score | 效果 |
|--------------|------------|
| 13 | 绿色点 |
| 46 | 橙色闪烁 |
| 710 | 红色脉冲扩散 |
### 战损统计模型(展示用)
- `total_events`
- `high_impact_events` (impact ≥ 7)
- `estimated_casualties`
- `estimated_strike_count`
---
## 三、数据流
```
GDELT API → Python 服务(60s) → gdelt_events, conflict_stats
POST /api/crawler/notify → situation.updated_at
WebSocket 广播 getSituation() → 前端
```
---
## 四、运行方式
```bash
# 1. 启动 Node API
npm run api
# 2. 启动 GDELT 服务
npm run gdelt
# 或: cd crawler && uvicorn realtime_conflict_service:app --port 8000
```