Compare commits
8 Commits
feat/cases
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
| 59bb135514 | |||
| fe8bed58f5 | |||
| 4df6c71628 | |||
| 33f0f497d3 | |||
| 8ad7e086bb | |||
| 9a94156acc | |||
| 3db3b12480 | |||
| e95e2f1338 |
2
.gitignore
vendored
2
.gitignore
vendored
@@ -14,6 +14,8 @@ cache/
|
|||||||
logs/
|
logs/
|
||||||
mlruns/
|
mlruns/
|
||||||
.playwright-mcp/
|
.playwright-mcp/
|
||||||
|
frontend/playwright-report/
|
||||||
|
frontend/test-results/
|
||||||
*Zone.Identifier
|
*Zone.Identifier
|
||||||
# Transcription processing intermediates
|
# Transcription processing intermediates
|
||||||
Outputs/transcript/chunks/
|
Outputs/transcript/chunks/
|
||||||
|
|||||||
1010
Outputs/transcript/transcript_raw.json
Normal file
1010
Outputs/transcript/transcript_raw.json
Normal file
File diff suppressed because it is too large
Load Diff
526
Outputs/transcript/transcript_speakers.txt
Normal file
526
Outputs/transcript/transcript_speakers.txt
Normal file
@@ -0,0 +1,526 @@
|
|||||||
|
|
||||||
|
=== 说话人6 ===
|
||||||
|
|
||||||
|
[300.9s-301.7s] 好
|
||||||
|
|
||||||
|
=== 说话人2 ===
|
||||||
|
|
||||||
|
[318.0s-319.3s] 那不不有啊
|
||||||
|
[319.3s-320.2s] 那不有吗
|
||||||
|
|
||||||
|
=== 说话人6 ===
|
||||||
|
|
||||||
|
[320.2s-321.2s] 那不有
|
||||||
|
|
||||||
|
=== 说话人2 ===
|
||||||
|
|
||||||
|
[321.2s-322.2s] 那不有
|
||||||
|
[322.2s-322.7s] 那不有
|
||||||
|
[322.7s-323.9s] 那不有
|
||||||
|
|
||||||
|
=== 说话人6 ===
|
||||||
|
|
||||||
|
[323.9s-325.7s] 那不有
|
||||||
|
|
||||||
|
=== 说话人4 ===
|
||||||
|
|
||||||
|
[325.7s-326.8s] 那不有
|
||||||
|
|
||||||
|
=== 说话人6 ===
|
||||||
|
|
||||||
|
[332.6s-333.6s] 那不有
|
||||||
|
|
||||||
|
=== 说话人3 ===
|
||||||
|
|
||||||
|
[333.6s-351.3s] 那不有
|
||||||
|
|
||||||
|
=== 说话人6 ===
|
||||||
|
|
||||||
|
[351.3s-352.2s] 那不有
|
||||||
|
[352.2s-352.8s] 那不有
|
||||||
|
[352.8s-353.5s] 那不有
|
||||||
|
|
||||||
|
=== 说话人3 ===
|
||||||
|
|
||||||
|
[773.5s-774.9s] 因為因為住室太高了
|
||||||
|
[774.9s-776.4s] 而且他們有一個秘密
|
||||||
|
|
||||||
|
=== 说话人8 ===
|
||||||
|
|
||||||
|
[776.4s-778.3s] 只要非常趴趴坐自己
|
||||||
|
|
||||||
|
=== 说话人3 ===
|
||||||
|
|
||||||
|
[778.3s-779.6s] 但是就是趴秘密
|
||||||
|
|
||||||
|
=== 说话人1 ===
|
||||||
|
|
||||||
|
[779.6s-780.4s] 還是比較
|
||||||
|
|
||||||
|
=== 说话人3 ===
|
||||||
|
|
||||||
|
[780.4s-781.9s] 真的沒有問題
|
||||||
|
|
||||||
|
=== 说话人6 ===
|
||||||
|
|
||||||
|
[781.9s-783.3s] 沒有問題
|
||||||
|
|
||||||
|
=== 说话人8 ===
|
||||||
|
|
||||||
|
[783.3s-784.4s] 就不叫做話
|
||||||
|
|
||||||
|
=== 说话人3 ===
|
||||||
|
|
||||||
|
[784.4s-785.6s] 因為那個秘密
|
||||||
|
[785.6s-786.5s] 講的秘密
|
||||||
|
[786.5s-787.5s] 基本上是太高了
|
||||||
|
[787.5s-788.7s] 我們有一個秘密的交流
|
||||||
|
|
||||||
|
=== 说话人6 ===
|
||||||
|
|
||||||
|
[788.7s-789.2s] 其實
|
||||||
|
|
||||||
|
=== 说话人3 ===
|
||||||
|
|
||||||
|
[789.2s-790.9s] 三年秘密的交流
|
||||||
|
|
||||||
|
=== 说话人6 ===
|
||||||
|
|
||||||
|
[790.9s-791.9s] 其實就是說
|
||||||
|
|
||||||
|
=== 说话人3 ===
|
||||||
|
|
||||||
|
[791.9s-792.5s] 這個裡面
|
||||||
|
|
||||||
|
=== 说话人8 ===
|
||||||
|
|
||||||
|
[792.5s-793.4s] 趴坐這個的話
|
||||||
|
[793.4s-794.8s] 其實是真好趴
|
||||||
|
[794.8s-795.6s] 然後
|
||||||
|
[795.6s-796.2s] 然後
|
||||||
|
|
||||||
|
=== 说话人7 ===
|
||||||
|
|
||||||
|
[796.2s-797.5s] 我現在認識的話
|
||||||
|
|
||||||
|
=== 说话人6 ===
|
||||||
|
|
||||||
|
[797.5s-798.8s] 那個坐子比較多
|
||||||
|
[799.6s-800.1s] 因為
|
||||||
|
|
||||||
|
=== 说话人3 ===
|
||||||
|
|
||||||
|
[800.1s-801.4s] 現在坐
|
||||||
|
[801.4s-802.1s] 這個只有
|
||||||
|
|
||||||
|
=== 说话人8 ===
|
||||||
|
|
||||||
|
[802.1s-802.8s] 八花之後
|
||||||
|
[802.8s-803.4s] 崩潰
|
||||||
|
|
||||||
|
=== 说话人6 ===
|
||||||
|
|
||||||
|
[804.2s-805.4s] 崩潰
|
||||||
|
|
||||||
|
=== 说话人2 ===
|
||||||
|
|
||||||
|
[805.4s-806.7s] 把花穩回來
|
||||||
|
|
||||||
|
=== 说话人8 ===
|
||||||
|
|
||||||
|
[806.7s-807.4s] 它進屋
|
||||||
|
[807.4s-808.0s] 現在
|
||||||
|
|
||||||
|
=== 说话人1 ===
|
||||||
|
|
||||||
|
[808.2s-809.2s] 因為 ele ran global
|
||||||
|
[809.9s-810.5s] artist
|
||||||
|
|
||||||
|
=== 说话人4 ===
|
||||||
|
|
||||||
|
[810.5s-811.0s] 實體
|
||||||
|
|
||||||
|
=== 说话人5 ===
|
||||||
|
|
||||||
|
[811.0s-811.7s] 就不理
|
||||||
|
|
||||||
|
=== 说话人2 ===
|
||||||
|
|
||||||
|
[811.7s-812.7s] 一才玩樂
|
||||||
|
[812.7s-813.1s] 但是
|
||||||
|
|
||||||
|
=== 说话人1 ===
|
||||||
|
|
||||||
|
[813.1s-814.3s] 操方
|
||||||
|
|
||||||
|
=== 说话人2 ===
|
||||||
|
|
||||||
|
[814.3s-817.2s] 著制
|
||||||
|
[817.2s-820.7s] 因為很多
|
||||||
|
|
||||||
|
=== 说话人3 ===
|
||||||
|
|
||||||
|
[820.7s-827.4s] 太高了
|
||||||
|
|
||||||
|
=== 说话人5 ===
|
||||||
|
|
||||||
|
[1157.4s-1158.5s] 他就要到了現在
|
||||||
|
|
||||||
|
=== 说话人7 ===
|
||||||
|
|
||||||
|
[1158.5s-1160.4s] 所以他來自主持主持表
|
||||||
|
|
||||||
|
=== 说话人5 ===
|
||||||
|
|
||||||
|
[1160.4s-1162.9s] 哪些人以前的平常才見到呢
|
||||||
|
|
||||||
|
=== 说话人7 ===
|
||||||
|
|
||||||
|
[1162.9s-1165.4s] 讓你以前的在砍術店就爆發
|
||||||
|
[1165.4s-1168.1s] 但是沒有些經過的這反應的
|
||||||
|
[1168.1s-1170.2s] 這往他就可以一直了
|
||||||
|
|
||||||
|
=== 说话人1 ===
|
||||||
|
|
||||||
|
[1170.2s-1172.3s] 他只是要去砍得一百米
|
||||||
|
|
||||||
|
=== 说话人5 ===
|
||||||
|
|
||||||
|
[1172.3s-1173.8s] 所以他來自主持的一集
|
||||||
|
|
||||||
|
=== 说话人7 ===
|
||||||
|
|
||||||
|
[1173.8s-1175.8s] 但是如果到了我主持假裝
|
||||||
|
|
||||||
|
=== 说话人5 ===
|
||||||
|
|
||||||
|
[1175.8s-1177.3s] 全正的主持主持主持主持
|
||||||
|
[1177.3s-1178.8s] 來現在的嘛
|
||||||
|
[1178.8s-1180.2s] 我們來現在的這個主持
|
||||||
|
|
||||||
|
=== 说话人2 ===
|
||||||
|
|
||||||
|
[1180.8s-1182.7s] 來扮熱我主持在主持上
|
||||||
|
|
||||||
|
=== 说话人7 ===
|
||||||
|
|
||||||
|
[1182.7s-1184.0s] 主持主持上
|
||||||
|
|
||||||
|
=== 说话人5 ===
|
||||||
|
|
||||||
|
[1184.0s-1184.7s] 我們來的一集
|
||||||
|
|
||||||
|
=== 说话人1 ===
|
||||||
|
|
||||||
|
[1184.7s-1185.5s] 廖都在耍耍被豪華 hideway
|
||||||
|
|
||||||
|
=== 说话人7 ===
|
||||||
|
|
||||||
|
[1185.5s-1190.8s] 不要再拿頭、保護護金高一點
|
||||||
|
|
||||||
|
=== 说话人2 ===
|
||||||
|
|
||||||
|
[1190.8s-1191.4s] 都不能家庭
|
||||||
|
|
||||||
|
=== 说话人7 ===
|
||||||
|
|
||||||
|
[1191.4s-1193.0s] 把這個賣一次
|
||||||
|
[1193.0s-1194.6s] 給我一去回去
|
||||||
|
|
||||||
|
=== 说话人5 ===
|
||||||
|
|
||||||
|
[1194.6s-1209.6s] 我這是自各主持主持主持主持主持主持
|
||||||
|
|
||||||
|
=== 说话人1 ===
|
||||||
|
|
||||||
|
[1211.3s-1213.3s] 大家如果像
|
||||||
|
[1399.8s-1402.5s] 我感觉是找到大甲之间的事,
|
||||||
|
|
||||||
|
=== 说话人2 ===
|
||||||
|
|
||||||
|
[1403.6s-1405.8s] 因为我被人拌上自己的事,
|
||||||
|
|
||||||
|
=== 说话人5 ===
|
||||||
|
|
||||||
|
[1406.2s-1409.5s] 但是我做了一会的事,
|
||||||
|
|
||||||
|
=== 说话人8 ===
|
||||||
|
|
||||||
|
[1409.5s-1410.2s] 我做了一会的事,
|
||||||
|
|
||||||
|
=== 说话人4 ===
|
||||||
|
|
||||||
|
[1410.2s-1411.2s] 我做了一会的事,
|
||||||
|
|
||||||
|
=== 说话人5 ===
|
||||||
|
|
||||||
|
[1411.3s-1413.0s] 我做了一会的事,
|
||||||
|
|
||||||
|
=== 说话人1 ===
|
||||||
|
|
||||||
|
[1413.3s-1415.1s] 然后我一直在说,
|
||||||
|
|
||||||
|
=== 说话人5 ===
|
||||||
|
|
||||||
|
[1415.2s-1417.0s] 我一直在说,
|
||||||
|
|
||||||
|
=== 说话人8 ===
|
||||||
|
|
||||||
|
[1418.7s-1419.4s] 我一直在说,
|
||||||
|
[1569.6s-1571.6s] 然后去讲没有办法,
|
||||||
|
|
||||||
|
=== 说话人4 ===
|
||||||
|
|
||||||
|
[1571.9s-1572.2s] 这一天,
|
||||||
|
|
||||||
|
=== 说话人3 ===
|
||||||
|
|
||||||
|
[1572.2s-1573.8s] 我一直在说,
|
||||||
|
[1574.5s-1575.2s] 这个天,
|
||||||
|
[1575.3s-1577.7s] 三公里的天然是一三个天然,
|
||||||
|
[1578.1s-1579.0s] 所以这一天,
|
||||||
|
|
||||||
|
=== 说话人8 ===
|
||||||
|
|
||||||
|
[1579.3s-1580.9s] 我把这马上进去的时候,
|
||||||
|
|
||||||
|
=== 说话人3 ===
|
||||||
|
|
||||||
|
[1581.6s-1583.3s] 这个能不能进去,
|
||||||
|
|
||||||
|
=== 说话人5 ===
|
||||||
|
|
||||||
|
[1583.8s-1585.5s] 我们有多少人那种事,
|
||||||
|
|
||||||
|
=== 说话人8 ===
|
||||||
|
|
||||||
|
[1585.6s-1586.3s] 太少我,
|
||||||
|
|
||||||
|
=== 说话人6 ===
|
||||||
|
|
||||||
|
[1586.3s-1587.5s] 也有多少人,
|
||||||
|
[1589.3s-1591.2s] 也有多少人,
|
||||||
|
|
||||||
|
=== 说话人5 ===
|
||||||
|
|
||||||
|
[1593.4s-1594.3s] 也有多少人,
|
||||||
|
[1594.3s-1595.6s] 也有多少人,
|
||||||
|
|
||||||
|
=== 说话人8 ===
|
||||||
|
|
||||||
|
[1596.3s-1598.4s] 也有多少人,
|
||||||
|
|
||||||
|
=== 说话人5 ===
|
||||||
|
|
||||||
|
[1598.7s-1599.4s] 也有多少人,
|
||||||
|
|
||||||
|
=== 说话人6 ===
|
||||||
|
|
||||||
|
[1711.1s-1718.4s] 也有多少人。
|
||||||
|
|
||||||
|
=== 说话人5 ===
|
||||||
|
|
||||||
|
[2469.8s-2471.9s] 他有个笔伴的笔伴
|
||||||
|
[2471.9s-2473.8s] 去务力就走
|
||||||
|
[2473.8s-2474.8s] 关了笔伴的笔伴
|
||||||
|
[2475.4s-2477.5s] 然后就加入有超级
|
||||||
|
[2477.5s-2478.7s] 有很多笔伴的样
|
||||||
|
|
||||||
|
=== 说话人2 ===
|
||||||
|
|
||||||
|
[2478.7s-2479.4s] 三个笔伴
|
||||||
|
[2479.4s-2481.9s] 这在我们笔伴的
|
||||||
|
|
||||||
|
=== 说话人5 ===
|
||||||
|
|
||||||
|
[2481.9s-2483.7s] 可能是这就是百分之八
|
||||||
|
|
||||||
|
=== 说话人8 ===
|
||||||
|
|
||||||
|
[2483.7s-2484.4s] 之多样
|
||||||
|
|
||||||
|
=== 说话人2 ===
|
||||||
|
|
||||||
|
[2484.9s-2486.9s] 他有个笔伴的笔伴
|
||||||
|
|
||||||
|
=== 说话人5 ===
|
||||||
|
|
||||||
|
[2486.9s-2487.9s] 三个笔伴的笔伴
|
||||||
|
|
||||||
|
=== 说话人4 ===
|
||||||
|
|
||||||
|
[2487.9s-2489.2s] 这个笔伴的笔伴
|
||||||
|
|
||||||
|
=== 说话人5 ===
|
||||||
|
|
||||||
|
[2489.2s-2490.1s] 就是要务力
|
||||||
|
[2490.1s-2493.1s] 这个笔伴的笔伴
|
||||||
|
|
||||||
|
=== 说话人4 ===
|
||||||
|
|
||||||
|
[2493.3s-2493.7s] 也就是要务力
|
||||||
|
|
||||||
|
=== 说话人5 ===
|
||||||
|
|
||||||
|
[2493.7s-2495.3s] 也就是要务力
|
||||||
|
[2496.3s-2498.5s] 他在那笔伴
|
||||||
|
[2498.5s-2501.2s] 这笔伴的笔伴的笔伴
|
||||||
|
|
||||||
|
=== 说话人1 ===
|
||||||
|
|
||||||
|
[2501.2s-2502.4s] 就是要务力
|
||||||
|
|
||||||
|
=== 说话人4 ===
|
||||||
|
|
||||||
|
[2502.4s-2503.2s] 那笔伴的笔伴的笔伴
|
||||||
|
|
||||||
|
=== 说话人1 ===
|
||||||
|
|
||||||
|
[2503.2s-2503.9s] 那笔伴的笔伴
|
||||||
|
[2503.9s-2504.8s] 他都感兮
|
||||||
|
|
||||||
|
=== 说话人5 ===
|
||||||
|
|
||||||
|
[2504.8s-2505.9s] 所以他的笔伴的笔伴
|
||||||
|
|
||||||
|
=== 说话人8 ===
|
||||||
|
|
||||||
|
[2505.9s-2507.4s] 就说就像不好的笔伴
|
||||||
|
|
||||||
|
=== 说话人5 ===
|
||||||
|
|
||||||
|
[2507.4s-2508.4s] 很热闹的笔伴
|
||||||
|
|
||||||
|
=== 说话人6 ===
|
||||||
|
|
||||||
|
[2508.4s-2509.1s] 他能不能被笔伴
|
||||||
|
|
||||||
|
=== 说话人4 ===
|
||||||
|
|
||||||
|
[2509.1s-2510.1s] 他能被笔伴
|
||||||
|
|
||||||
|
=== 说话人6 ===
|
||||||
|
|
||||||
|
[2510.1s-2510.9s] 笔伴
|
||||||
|
|
||||||
|
=== 说话人5 ===
|
||||||
|
|
||||||
|
[2511.6s-2512.9s] 但是我们这笔伴
|
||||||
|
|
||||||
|
=== 说话人4 ===
|
||||||
|
|
||||||
|
[2512.9s-2514.2s] 有笔伴的笔伴
|
||||||
|
|
||||||
|
=== 说话人5 ===
|
||||||
|
|
||||||
|
[2514.2s-2515.9s] 前几个笔伴
|
||||||
|
|
||||||
|
=== 说话人2 ===
|
||||||
|
|
||||||
|
[2515.9s-2516.8s] 有没有看见的
|
||||||
|
|
||||||
|
=== 说话人4 ===
|
||||||
|
|
||||||
|
[2516.8s-2518.9s] 这笔伴的笔伴
|
||||||
|
[2518.9s-2519.5s] 那个笔伴
|
||||||
|
|
||||||
|
=== 说话人1 ===
|
||||||
|
|
||||||
|
[2519.5s-2520.2s] 没有看见
|
||||||
|
[2520.2s-2521.1s] 没有看见
|
||||||
|
[2522.8s-2523.7s] 他会到了
|
||||||
|
[2523.7s-2524.2s] 把笔伴
|
||||||
|
|
||||||
|
=== 说话人6 ===
|
||||||
|
|
||||||
|
[2528.5s-2530.4s] 接下音过找
|
||||||
|
|
||||||
|
=== 说话人8 ===
|
||||||
|
|
||||||
|
[2531.3s-2534.1s] 他一aps
|
||||||
|
|
||||||
|
=== 说话人6 ===
|
||||||
|
|
||||||
|
[2534.1s-2535.3s] 差不多
|
||||||
|
[2535.3s-2537.6s] 他上学
|
||||||
|
|
||||||
|
=== 说话人5 ===
|
||||||
|
|
||||||
|
[2537.9s-2540.0s] 我们整体加上
|
||||||
|
|
||||||
|
=== 说话人4 ===
|
||||||
|
|
||||||
|
[2540.0s-2540.8s] 那个笔伴
|
||||||
|
|
||||||
|
=== 说话人2 ===
|
||||||
|
|
||||||
|
[2540.8s-2542.5s] 你看看
|
||||||
|
|
||||||
|
=== 说话人5 ===
|
||||||
|
|
||||||
|
[2542.5s-2557.8s] 他比较感情
|
||||||
|
[4267.8s-4269.6s] 恐慌
|
||||||
|
|
||||||
|
=== 说话人7 ===
|
||||||
|
|
||||||
|
[4277.5s-4285.7s] 那国内主要我们给こちら
|
||||||
|
[4285.7s-4286.8s] 就是宪治 Show
|
||||||
|
|
||||||
|
=== 说话人3 ===
|
||||||
|
|
||||||
|
[4286.8s-4290.6s] 这是就是宪治 Audio
|
||||||
|
|
||||||
|
=== 说话人1 ===
|
||||||
|
|
||||||
|
[4290.6s-4292.5s] 他觉得他在来兜了
|
||||||
|
|
||||||
|
=== 说话人7 ===
|
||||||
|
|
||||||
|
[4292.5s-4296.2s] 不是这个话的志明
|
||||||
|
[4296.8s-4299.3s] 我来看你这种词的看法
|
||||||
|
|
||||||
|
=== 说话人4 ===
|
||||||
|
|
||||||
|
[4299.3s-4300.1s] 这个是一个人的
|
||||||
|
|
||||||
|
=== 说话人7 ===
|
||||||
|
|
||||||
|
[4301.4s-4302.8s] 所以我说一下
|
||||||
|
[4302.8s-4304.3s] 我就是诸个看法的问题
|
||||||
|
|
||||||
|
=== 说话人4 ===
|
||||||
|
|
||||||
|
[4304.3s-4306.2s] 这体的问题是一个诸个
|
||||||
|
|
||||||
|
=== 说话人5 ===
|
||||||
|
|
||||||
|
[4306.7s-4308.5s] 我听过我这口误的语语
|
||||||
|
[4308.5s-4310.5s] 这种不害怕怪怪
|
||||||
|
[4310.5s-4313.0s] 我诸了太太的语语语语
|
||||||
|
|
||||||
|
=== 说话人7 ===
|
||||||
|
|
||||||
|
[4313.0s-4314.7s] 我当时诸个不道理的
|
||||||
|
|
||||||
|
=== 说话人5 ===
|
||||||
|
|
||||||
|
[4315.8s-4318.7s] 这种不道理的语语语语语语语语语语
|
||||||
|
[4318.7s-4320.2s] 假如还有语语语语
|
||||||
|
[4320.2s-4321.7s] 这个语语语语语语语
|
||||||
|
[4321.7s-4326.1s] 这种不道理语语语语
|
||||||
|
|
||||||
|
=== 说话人2 ===
|
||||||
|
|
||||||
|
[4326.1s-4328.0s] 假如那进到了
|
||||||
|
|
||||||
|
=== 说话人7 ===
|
||||||
|
|
||||||
|
[4328.0s-4330.4s] 代表人做什么
|
||||||
|
|
||||||
|
=== 说话人5 ===
|
||||||
|
|
||||||
|
[4330.4s-4331.7s] 菠 çıkt
|
||||||
|
|
||||||
|
=== 说话人2 ===
|
||||||
|
|
||||||
|
[4331.7s-4349.8s] 摇灶的独援
|
||||||
88
Outputs/项目能力综述_数据维度与技术体系.md
Normal file
88
Outputs/项目能力综述_数据维度与技术体系.md
Normal file
@@ -0,0 +1,88 @@
|
|||||||
|
# 儿童呼吸道感染空间流行病学的方法、数据与可视化:文献综述
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
儿童呼吸道感染是全球5岁以下儿童死亡的第二大原因。在中国城市地区,其年发病数长期居于儿科门诊病种的首位。Wu等(2025)在*Communications Earth & Environment*报告了一项横跨江苏省多城市的学校环境研究,纳入265,076例儿童肺炎与气管炎病例,采用时空分层设计估计了PM₁各化学组分与因病缺课率的剂量-反应关系。结果显示,SO₄²⁻的风险比最高(RR=1.119),且低温对每一种污染物组分的健康效应均表现出显著的放大作用,城乡儿童在温度-污染复合暴露下的敏感性方向相反。同年发表于*PLOS Medicine*的全国尺度研究将501个城市、跨越2008至2019年的面板数据嵌入时间分层病例交叉模型,估计野火来源PM₂.₅每升高5μg/m³,儿童呼吸道传染病发病率上升6.8%。值得留意的一个数字是:野火PM₂.₅在总PM₂.₅中仅占2.7%,却贡献了10.8%的相关病例。
|
||||||
|
|
||||||
|
尽管如此,该领域在数据配置与方法部署两个维度上均远未饱和。
|
||||||
|
|
||||||
|
环境与地理空间变量在空间流行病学中的缺席,是反复被指出却极少被回应的问题。2025年发表在*Geospatial Health*的一篇系统综述遵循PRISMA 2020指南,从35项纳入研究中统计出一个醒目的比例:仅2.9%的研究认真纳入了环境、地理或物理空间因子。儿童呼吸系统疾病恰恰处于这一方法论盲区的核心位置——其发病不但与每日吸入的污染物组分直接相关,还与绿地覆盖对微气候的调节效应、建成环境对通风条件的改变、地形对污染扩散路径的约束等多个环境过程的交互作用相关联。另一维度上,近三年间GCN-Transformer混合架构(Zheng等, 2025)、流行病学知识嵌入的图神经网络(EISTGNN, Han等, 2025)以及双通道时空图卷积网络(Luo等, 2025)在COVID-19与流感预测任务中取得了国家级R²达0.966、ILI相关度达0.82的性能表现,但上述方法尚未被迁移至儿童呼吸道感染这一疾病场景。对此,文献中呈现的是一种近乎沉默的状态——既非技术障碍所致,也非数据可及性不足所能解释,更像流行病学建模社区与深度学习社区在研究议程上的长期分离。另一个更具工程属性的问题是,绝大多数空间流行病学研究的产出形式仍然是一篇论文附以离线分析脚本,缺乏一套能够持续接收新数据、自动更新风险估计并实时输出可视化与预警信息的运行中的系统。
|
||||||
|
|
||||||
|
本文旨在回答两个简单的问题:当前空间流行病学领域可用的统计工具有哪些,每种方法产出何种类型的图;目前可获取的数据字段涵盖哪些维度。综述范围不限于已有研究者整合过的变量,也包括分散在各数据源中、技术上完全可调用但尚未被人放到同一模型里的字段。目的是为后续研究提供一个方法—图表—数据的完整参照系。
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 空间统计方法与图表产出
|
||||||
|
|
||||||
|
空间自相关检验是大多数空间流行病学分析的起点。Global Moran's I的运算和使用并不复杂,但理解其局限比掌握其运算本身更为重要:它回答"全局上是否存在空间聚集",但不对"聚集的确切位置"提供任何信息。在不同疾病场景中,该统计量的量级存在显著差异:马拉维疟疾研究的Moran's I为0.044至0.074(p<0.05),埃塞俄比亚HIV血清阳性率的Moran's I则达到0.16(p<0.001)。这些I值的绝对值普遍偏低,但p值表明所检测到的空间结构并非随机过程生成。其标准化输出为Moran散点图:横轴为标准化观测值,纵轴为空间滞后值,四个象限分别对应高-高、低-低、高-低、低-高四种空间关联模式。非空间统计背景的研究者对Moran散点图的初始反应往往是困惑——图中没有任何一个点落在可识别的地图位置上。然而,它比任何等值域地图都更直接地回应一个核心问题:观测值在空间维度上是否存在被其邻近值牵引的现象。
|
||||||
|
|
||||||
|
Moran散点图无法指出聚集发生在哪些具体位置。这一问题的解决需要借助局部空间统计量。Getis-Ord Gi*为每个空间单元输出一个z-score与对应的p-value,z-score需要多大绝对值才能被判定为热点或冷点,取决于所采用的多重比较校正策略。前端输出为分级热点地图,以红-蓝连续色阶渲染:红色端对应高值聚集区(热点),蓝色端对应低值聚集区(冷点)。该方法已在中国猩红热的北部与东北部高风险区定位、埃塞俄比亚HIV亚的斯亚贝巴及周边热点区识别,以及儿童急性呼吸道感染风险的空间分析中被反复验证。Anselin Local Moran's I在Gi*的基础上进一步区分聚类的子类型:高值被高值包围(HH)、低值被低值包围(LL)、高值被低值包围(HL)、低值被高值包围(LH)。其标准输出为LISA聚集图,各区划单元的聚类类别与统计显著性以差异化的符号标注于图上。HL与LH两类通常在后续分析中被视作空间异常值;在环境暴露的空间研究中,它们往往比HH聚类更具分析价值,因为在同一暴露梯度面上出现反直觉的低风险区,通常暗示着值得深入追问的局地保护因素或未观测的混杂结构。
|
||||||
|
|
||||||
|
空间维度与时间维度的联合扫描问题,由Kulldorff(1997)提出的时空扫描统计量所解决。实现上多以SaTScan软件包为工具载体,核心算法逻辑是在研究区内移动大小与形状可变的圆柱形扫描窗口(底面为空间圆或椭圆,高度为时间区间),对每个窗口进行泊松或伯努利概率假设下的似然比检验,判定窗口内观测病例数是否显著超出随机期望。该方法已在多项研究中取得了明确的实证验证:马拉维疟疾的时空聚集区集中于国土中部与东南部,RR显著升高;加纳疟疾扫描出一个RR=4.66(p<0.001)的时空聚集簇;中国猩红热在全国范围内识别出9个彼此离散的时空聚类。结果在地图上的呈现形式为一系列聚类椭圆,叠置于底图之上,直观指示聚集区的地理边界、时间跨度与相对风险量级。
|
||||||
|
|
||||||
|
监测站点的空间分布是不均匀的。在站点间存在显著覆盖空白的情况下,暴露估计需要通过空间插值方法进行补充。Kriging插值利用半变异函数拟合已知点的空间依赖结构,据此对未采样位置做出最优无偏预测。其关键统计假设是二阶平稳或内蕴平稳——实践中该假设很少被严格满足,但偏离假定的严重程度是否实际影响插值面的可用性,取决于半变异函数的拟合质量。若半变异函数在特定方向出现明显漂移,则需要考虑趋势剔除或采用泛Kriging替代。输出为连续风险面或污染物浓度面的渲染图层。
|
||||||
|
|
||||||
|
全局回归估计将暴露-健康关联的效应量假定为整个研究区上的常数,这一假定在空间流行病学中几乎总被拒绝。地理加权回归(GWR)将回归系数从全局空间释放至局部,各位置以自身带宽内的邻近点加权估计一套局域参数。其时空扩展形式(GTWR)进一步将系数沿时间轴展开,使效应量同时随空间位置和时间变动,适用于具备充足年份跨度的面板数据分析。Wang与杨等(2023)发表在《中国血吸虫病防治杂志》的综述已对GTWR在流行病学中的应用场景做了系统阐述。两种方法的输出形式均为回归系数的空间分布面——从该图上可直接判读,哪些地理单元内PM₂.₅单位浓度变动对发病率的边际效应显著高于或低于其他区域。
|
||||||
|
|
||||||
|
上述方法均属于统计推断的范畴。预测问题则在方法论上另有归属。
|
||||||
|
|
||||||
|
近三年间,图神经网络及其时空变体在传染病预测领域取得了可观的性能提升。Zheng等(2025)在*Physica A*提出了GCN-PEFormer架构,将基于航空旅行与地理邻近性的动态图与阶段感知的Transformer编码器融合,60天预测的RMSE较LSTM、GCN-LSTM、Transformer和PatchTST基线平均降低34.5%。Han等(2025)的EISTGNN模型走了一条不同的技术路线——将Spatio-Contact SIR仓室模型的动力学方程直接嵌入图神经网络的损失函数,以有效再生数Rt约束预测方向,在维持预测精度的同时获得了显著高于纯黑箱模型的可解释性。Luo等(2025)提出的双通道STGCN在工程设计上更为务实:一条通道沿道路网络邻接矩阵传导,另一条沿社会经济相关矩阵传导,在台湾ILI监测数据上的预测相关度达到0.82。这一结果揭示了一个重要的经验发现:两个空间上相隔甚远但社会经济结构相似的区域之间,疾病传播的信号强度未必弱于地理位置相邻的区域。上述模型产出的可视化形式高度一致:预测值与观测值在同一时间轴上叠置的序列曲线图,下方通常配以残差分布的直方图或Q-Q图,部分研究附加特征重要性的排序条形图。
|
||||||
|
|
||||||
|
贝叶斯时空层次模型在方法论取向上与前几类方法存在结构性的差异。其处理空间依赖时不依赖预先定义的邻接结构——空间条件自回归先验允许数据自行估计空间依赖的强度与空间范围。Si等(2025)在*Atmospheric Environment*上运用该模型,对北京、广州与武汉三个城市的PM₂.₅各组分与流感传播的关联进行了估计。武汉PM₂.₅的相对风险为1.18(95% CrI: 1.10-1.26),且时空层次模型的拟合效果在DIC准则下显著优于不包含空间随机效应的广义线性模型。其输出形式为后验概率风险图:每个网格的估计值携带完整的后验分布信息,不同位置的不确定性带宽可直接在地图上进行比较,这一点在面向风险沟通与决策支持的场景中具有不可替代的价值。
|
||||||
|
|
||||||
|
将上述统计方法、研究问题与图表产出关系汇总如下:
|
||||||
|
|
||||||
|
| 统计方法 | 研究问题 | 图表产出 |
|
||||||
|
|---------|---------|---------|
|
||||||
|
| Global Moran's I | 全局空间自相关是否存在 | Moran散点图 |
|
||||||
|
| Getis-Ord Gi* | 热点与冷点的空间分布位置 | 分级热点地图(红-蓝色阶) |
|
||||||
|
| Anselin Local Moran's I | 局部聚类的具体类型(HH/HL/LH/LL) | LISA聚集图,附显著性标记 |
|
||||||
|
| Kulldorff时空扫描 | 时空聚集区的时空边界与相对风险 | 时空聚类椭圆,附RR与p值 |
|
||||||
|
| Kriging插值 | 监测空白区的风险面或暴露面估计 | 连续插值面渲染 |
|
||||||
|
| GWR / GTWR | 暴露效应空间异质性的量化 | 回归系数空间分布面 |
|
||||||
|
| GCN / STGCN | 多步长风险预测(1–7天) | 预测-观测对比曲线,残差分布图 |
|
||||||
|
| SIR/SEIR动力学模型 | 传播速率与趋势估计 | 有效再生数Rt时序曲线 |
|
||||||
|
| 贝叶斯时空层次模型 | 空间依赖结构与不确定性量化 | 后验概率风险图 |
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 数据字段
|
||||||
|
|
||||||
|
传统空间流行病学研究的数据视野局限于病例计数、人口分母以及少量行政级别的协变量。这一现状并非由数据可及性所限制——2026年的实际情形是,大量环境遥感、路网拓扑、兴趣点分布与建筑环境数据已在公开渠道或经轻量级申请后可供获取,真正稀缺的并非数据本身,而是将多模态异源数据纳入同一分析框架的研究实践。
|
||||||
|
|
||||||
|
以下按数据模态组织分类,对当前可用与可获取的数据字段做系统性梳理。
|
||||||
|
|
||||||
|
**气象与大气环境。** 基础来源为地面国控监测站网络的逐小时观测记录。核心字段为六项大气污染物浓度(PM₂.₅、PM₁₀、SO₂、NO₂、O₃、CO)与气象要素(温度、湿度、风速、气压)。原始数据为站点级逐小时记录,需经由空间插值转化至面域格网。上述字段仅构成了暴露特征的初级维度。暴露的健康效应极少是即时的——PM₂.₅吸入后的气道炎症反应可能在其后两至三天才反映为门诊计数的抬升。学界在流行病学建模中的惯常做法是构建多尺度滞后窗口:取1d、2d、3d、5d、7d、14d共六个步长,短滞后窗口用于捕获急性气道反应,长滞后窗口用于捕获累积性组织损伤与继发感染的风险推动效应。24小时滚动均值将暴露剂量的时间分辨率从逐时提升至逐日,O₃单独取8小时滑动均值,系因臭氧的急性健康效应评价标准窗口即为8小时。在此基础之上,AQI空气质量指数、首要污染物标识与空气质量等级作为复合概览指标使用。六项污染物乘以六个滞后步长得出的36个滞后特征变量,构成了暴露特征在时间维度上的主要结构。
|
||||||
|
|
||||||
|
**遥感反演与栅格环境数据。** DEM数字高程模型(ASTER或SRTM,30m分辨率)及其衍生地形因子(高程、坡度、坡向)用于刻画各网格单元在区域通风廊道中的空间位置。高分辨率人口密度栅格(LandScan HD,100m分辨率)提供中国全域内暴露人口的精细空间分布。多光谱遥感方面,Landsat 8/9与Sentinel-2的反演产品提供多项关键变量的连续时间序列:NDVI植被指数的逐月或逐旬序列可反映绿地覆盖的季节性波动幅度;地表温度(LST)用于量化城市热岛强度的时空变化模式;气溶胶光学厚度(AOD)作为地面PM₂.₅监测的空间补充,在监测站点稀疏的城市远郊区域尤为有用。土地覆盖分类将地表划分为建筑区、道路、水体、绿地与裸地五个类别,逐网格计算各类别的面积占比,从而将建成环境比例转化为一个连续数值变量进入模型。
|
||||||
|
|
||||||
|
**医疗就诊记录。** 分为门诊与住院两条线数据。单条记录的字段结构明确但信息密度不低:就诊(入院)日期、ICD-10诊断编码、年龄分层(0–14、15–44、45–64、65岁以上四组)、性别与现住址。地址文本经由地理编码引擎转化为经纬度坐标,匹配过程采用两级策略——街道级别匹配,匹配失败则回退至行政区级别——匹配成功的记录附带置信度标记。聚合方向有两个:空间上可以向格网或行政区汇总,时间上可以向逐日、逐周或逐月汇总。门诊权重取1、住院权重取3,是一种将住院事件所代表的更高疾病严重程度以简单数值权重形式纳入统计建模的实用化处理。
|
||||||
|
|
||||||
|
**道路网络、兴趣点与建成环境。** 道路网络数据基于OpenStreetMap提取,覆盖motorway至tertiary五个等级,道路属性包含路段长度、限速与车道数。路网经拓扑检查与简化处理后构建为空间邻接图,其邻接矩阵是图神经网络模型的空间输入骨架。兴趣点数据涵盖三类关键设施:学校与幼儿园的位置及办学规模(影响各网格内儿童白天暴露人口密度的估计)、医疗机构的位置、等级与核定床位数(影响就医可及性与病例记录的完整程度——距离医疗机构较远的网格,轻症病例被记录的概率会系统性地偏低)、以及公园与绿地的空间边界(提供NDVI之外的绿地可达性信息)。公共交通可达性以公交站点与地铁站出入口的空间密度栅格表征。人口结构数据落至街道或社区级别,包含常住人口数、0–14岁儿童人口数与性别比。建筑环境数据涵盖建筑基底面积比例、平均层数与建成年代,这些变量在分析城市内部暴露异质性时,比任何宏观行政区级别的指标都更直接地反映建成环境对呼吸健康的影响机制。
|
||||||
|
|
||||||
|
**空间参考系。** 多级行政边界(省、市、区/县三级,街道级视数据源而可及)提供空间统计的行政分区基准。全域标准格网索引(100m或更大尺度)为多源数据聚合的基本空间单元。每个网格携带唯一标识码、中心经纬度坐标、WKT几何表达,以及到各级行政区的归属映射。所有的环境暴露估计、病例计数与设施密度指标最终均在统一的格网坐标参考系中对齐。
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 现有研究缺口
|
||||||
|
|
||||||
|
数据维度的可及性与方法学工具箱的成熟度均已达到一个可以支撑更高整合度分析的阶段,但以下三个层面的整合尚未在现有文献中实现。
|
||||||
|
|
||||||
|
其一,多源异构数据在单个研究中的并发使用量始终偏低。大多数空间流行病学文章仅包含两到三类数据源——气象污染物加医疗就诊计数,至多再纳入人口密度作为分母变量。将遥感栅格产品、建筑环境要素、道路网络拓扑与POI设施密度同时纳入同一分析框架的研究,在文献中所占比例极低。此处并非是数据的可获取性限制了研究设计,而是数据整合的工程成本与研究者的学科边界共同造成了这一局面。其二,深度学习时空预测方法在COVID-19与流感建模中已积累了充分的性能验证(R²>0.96,相关度>0.82),但将其迁移至儿童呼吸道感染这一疾病场景的文献几乎无法检索到。这一沉默并非源自技术不可行,而更接近流行病学建模社区与深度学习社区在研究议程上的长期分离所导致的学科盲区。其三,从研究产出到可运行系统的转化步骤尚未被跨越。回顾性关联分析、风险地图绘制与学术论文发表构成了目前文献的主要产出形态。然而,一个能够持续接收数据流、按日更新网格级风险估计并自动将热点聚类推送至交互式地图与周期报告的运行中的系统组件,在现有文献中几乎不见踪迹。这严格来说并非学术问题,而是工程化部署问题,但医学地理学与公共卫生中相当比例的具有应用价值的工作,恰恰落在学术研究与工程实现的间隙中。
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 参考文献
|
||||||
|
|
||||||
|
1. Wu et al. Extreme temperatures amplify air pollution risks to childhood respiratory health in school environment in Jiangsu province, China. *Communications Earth & Environment*, 2025. DOI: 10.1038/s43247-025-02409-8.
|
||||||
|
2. Wildfire-related PM₂.₅ and respiratory transmitted disease among Chinese children and adolescents from 2008 to 2019: A retrospective study. *PLOS Medicine*, 2025.
|
||||||
|
3. Factors associated with the spatial distribution of leprosy: a systematic review of the published literature. *Geospatial Health*, 2025. DOI: 10.4081/gh.2025.1394. PMID: 40964901.
|
||||||
|
4. Zheng Z, Zheng X, Liu D, et al. GCN-PEFormer: A mobility-aware and patch-based transformer for predicting spatiotemporal transmission patterns of epidemics. *Physica A*, 2025. DOI: 10.1016/j.physa.2025.130721.
|
||||||
|
5. Han et al. EISTGNN: Epidemiology-informed Spatiotemporal Graph Neural Network for heterogeneity-driven interpretable epidemic forecasting. *Engineering Applications of Artificial Intelligence*, 2025, Vol. 162.
|
||||||
|
6. Luo J, Wang X, Fan X, et al. A novel graph neural network based approach for influenza-like illness nowcasting: exploring the interplay of temporal, geographical, and functional spatial features. *BMC Public Health*, 2025, 25:408.
|
||||||
|
7. Si et al. Particulate matters 2.5 and its organic constituent on seasonal influenza transmission: Insights from Bayesian spatiotemporal modelling in mainland China. *Atmospheric Environment*, 2025.
|
||||||
|
8. Wang Y, et al. 空间流行病学贝叶斯时空模型综述. *ISPRS International Journal of Geo-Information*, 2024.
|
||||||
|
9. 王喆, 杨坤等. 时空地理加权回归模型在流行病学中的应用综述. *中国血吸虫病防治杂志*, 2023, 35(2): 199.
|
||||||
|
10. 夏彦恺等. 一种基于大气环境监测与深度学习模型的儿童呼吸道疾病预警方法及装置. 中国专利: CN120236784A, 2025.
|
||||||
|
11. Galvan MC, Galvan JL. *Writing Literature Reviews*, 7th Edition. Routledge (Taylor & Francis), 2024. ISBN: 978-1-032-60826-6.
|
||||||
|
12. Pan ML, Craven JS. *Preparing Literature Reviews*, 6th Edition. Routledge (Taylor & Francis), 2024. ISBN: 978-1-032-60839-6.
|
||||||
1
backend/__init__.py
Normal file
1
backend/__init__.py
Normal file
@@ -0,0 +1 @@
|
|||||||
|
# Backend package
|
||||||
42
backend/auth/CLAUDE.md
Normal file
42
backend/auth/CLAUDE.md
Normal file
@@ -0,0 +1,42 @@
|
|||||||
|
# Auth — JWT Authentication
|
||||||
|
|
||||||
|
## Stack
|
||||||
|
|
||||||
|
python-jose (JWT signing/verification) + passlib (bcrypt password hashing). Token-based, stateless.
|
||||||
|
|
||||||
|
## Structure
|
||||||
|
|
||||||
|
```
|
||||||
|
auth/
|
||||||
|
models.py # Pydantic models: UserCreate, UserLogin, Token, UserOut
|
||||||
|
service.py # Business logic: authenticate_user, create_user, create_access_token
|
||||||
|
dependencies.py # FastAPI Depends: get_current_user, require_admin
|
||||||
|
middleware.py # ASGI middleware (if any global auth checks)
|
||||||
|
router.py # APIRouter: /login, /register, /whoami
|
||||||
|
```
|
||||||
|
|
||||||
|
## Patterns
|
||||||
|
|
||||||
|
- Passwords hashed with bcrypt via `passlib` — never store plaintext
|
||||||
|
- JWT tokens signed with `python-jose`, include `sub` (username) and `exp`
|
||||||
|
- `get_current_user()` is the standard `Depends()` to inject user into endpoints
|
||||||
|
- Auth endpoints return Pydantic models: `Token(access_token=...)`, `UserOut(username=...)`
|
||||||
|
- HTTP status codes: 401 for bad credentials, 409 for duplicate user
|
||||||
|
|
||||||
|
## Usage in Routers
|
||||||
|
|
||||||
|
```python
|
||||||
|
from auth.dependencies import get_current_user
|
||||||
|
|
||||||
|
@router.get("/protected")
|
||||||
|
async def protected_route(current_user = Depends(get_current_user)):
|
||||||
|
...
|
||||||
|
```
|
||||||
|
|
||||||
|
## Anti-Patterns
|
||||||
|
|
||||||
|
- Don't hardcode secret keys — use `Settings` from environment
|
||||||
|
- Don't store tokens client-side without HttpOnly cookies
|
||||||
|
- Don't skip `response_model` on auth endpoints
|
||||||
|
- Don't leak whether username or password was wrong — always "incorrect username or password"
|
||||||
|
- Don't bypass `Depends(get_current_user)` for protected routes
|
||||||
@@ -8,7 +8,13 @@ from passlib.context import CryptContext
|
|||||||
|
|
||||||
logger = logging.getLogger("cbpoa.auth")
|
logger = logging.getLogger("cbpoa.auth")
|
||||||
|
|
||||||
SECRET_KEY = os.getenv("AUTH_SECRET_KEY", "cbpoa-dev-secret-change-in-production")
|
_DEFAULT_SECRET = "cbpoa-dev-secret-change-in-production"
|
||||||
|
SECRET_KEY = os.getenv("AUTH_SECRET_KEY", _DEFAULT_SECRET)
|
||||||
|
if SECRET_KEY == _DEFAULT_SECRET:
|
||||||
|
logger.warning(
|
||||||
|
"AUTH_SECRET_KEY is not set — using the built-in development secret. "
|
||||||
|
"Set AUTH_SECRET_KEY in the environment before deploying; the default is public and allows token forgery."
|
||||||
|
)
|
||||||
ALGORITHM = "HS256"
|
ALGORITHM = "HS256"
|
||||||
ACCESS_TOKEN_EXPIRE_MINUTES = int(os.getenv("AUTH_TOKEN_EXPIRE_MINUTES", "480"))
|
ACCESS_TOKEN_EXPIRE_MINUTES = int(os.getenv("AUTH_TOKEN_EXPIRE_MINUTES", "480"))
|
||||||
|
|
||||||
@@ -61,6 +67,10 @@ def create_access_token(data: dict) -> str:
|
|||||||
def decode_access_token(token: str) -> dict | None:
|
def decode_access_token(token: str) -> dict | None:
|
||||||
try:
|
try:
|
||||||
payload = jwt.decode(token, SECRET_KEY, algorithms=[ALGORITHM])
|
payload = jwt.decode(token, SECRET_KEY, algorithms=[ALGORITHM])
|
||||||
|
# python-jose ignores PyJWT's options={"require": [...]}, so enforce exp manually:
|
||||||
|
# a token with no exp claim would otherwise never expire.
|
||||||
|
if "exp" not in payload:
|
||||||
|
return None
|
||||||
return payload
|
return payload
|
||||||
except JWTError:
|
except JWTError:
|
||||||
return None
|
return None
|
||||||
|
|||||||
@@ -3,22 +3,64 @@ Shared data-loading module for case data (outpatient + inpatient).
|
|||||||
|
|
||||||
Extracted from routers/cases.py so both cases and reports routers can use
|
Extracted from routers/cases.py so both cases and reports routers can use
|
||||||
the same cached data without circular imports.
|
the same cached data without circular imports.
|
||||||
|
|
||||||
|
Performance: reads from pre-generated Parquet files (~0.1s) instead of
|
||||||
|
Excel (~10s). Falls back to Excel if parquet files are missing.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
import re
|
import re
|
||||||
|
import logging
|
||||||
|
import threading
|
||||||
|
from typing import Optional, cast
|
||||||
import pandas as pd
|
import pandas as pd
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
|
|
||||||
|
logger = logging.getLogger("cbpoa.case_loader")
|
||||||
|
|
||||||
DATE_PATTERN = re.compile(r"^\d{4}-\d{2}-\d{2}$")
|
DATE_PATTERN = re.compile(r"^\d{4}-\d{2}-\d{2}$")
|
||||||
|
|
||||||
# Data cache
|
# Data cache
|
||||||
_cache = {
|
_cache: dict[str, Optional[pd.DataFrame | datetime]] = {
|
||||||
"outpatient": None,
|
"outpatient": None,
|
||||||
"inpatient": None,
|
"inpatient": None,
|
||||||
|
"combined": None,
|
||||||
|
"cases_by_district_daily": None,
|
||||||
"loaded_at": None,
|
"loaded_at": None,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
# Guards the lazy build so concurrent callers don't duplicate the load/concat.
|
||||||
|
_load_lock = threading.RLock()
|
||||||
|
|
||||||
|
# Canonical Wuhan administrative districts (13), matching the `name` field in
|
||||||
|
# Datas/武汉市.geojson. All district roll-ups must collapse to exactly these.
|
||||||
|
CANONICAL_DISTRICTS = [
|
||||||
|
'江岸区', '江汉区', '硚口区', '汉阳区', '武昌区', '青山区', '洪山区',
|
||||||
|
'东西湖区', '汉南区', '蔡甸区', '江夏区', '黄陂区', '新洲区',
|
||||||
|
]
|
||||||
|
# Bare (suffix-less) base name -> canonical 区-suffixed name.
|
||||||
|
_DISTRICT_BASE_TO_CANONICAL = {d[:-1]: d for d in CANONICAL_DISTRICTS}
|
||||||
|
_DISTRICT_SUFFIXES = ('区', '县', '市')
|
||||||
|
|
||||||
|
|
||||||
|
def normalize_district(name: str) -> str:
|
||||||
|
"""Map a district label to its canonical 区-suffixed form.
|
||||||
|
|
||||||
|
The case parquet carries both bare ("武昌") and suffixed ("武昌区") spellings
|
||||||
|
of the same district, which double-counts in any roll-up. This collapses
|
||||||
|
them: known bare names map to their canonical form; already-suffixed names
|
||||||
|
pass through unchanged; anything else gets a "区" appended.
|
||||||
|
"""
|
||||||
|
if name is None:
|
||||||
|
return name
|
||||||
|
name = str(name).strip()
|
||||||
|
if name in _DISTRICT_BASE_TO_CANONICAL:
|
||||||
|
return _DISTRICT_BASE_TO_CANONICAL[name]
|
||||||
|
if name.endswith(_DISTRICT_SUFFIXES):
|
||||||
|
return name
|
||||||
|
return f"{name}区"
|
||||||
|
|
||||||
|
|
||||||
# Wuhan district mapping
|
# Wuhan district mapping
|
||||||
WUHAN_DISTRICTS = {
|
WUHAN_DISTRICTS = {
|
||||||
'江岸区': ['江岸'],
|
'江岸区': ['江岸'],
|
||||||
@@ -40,6 +82,7 @@ WUHAN_DISTRICTS = {
|
|||||||
|
|
||||||
PROJECT_ROOT = Path(__file__).parent.parent.parent
|
PROJECT_ROOT = Path(__file__).parent.parent.parent
|
||||||
DATA_DIR = PROJECT_ROOT / "Datas"
|
DATA_DIR = PROJECT_ROOT / "Datas"
|
||||||
|
PROCESSED_DIR = PROJECT_ROOT / "processed"
|
||||||
|
|
||||||
|
|
||||||
def _extract_district(addr: str) -> str:
|
def _extract_district(addr: str) -> str:
|
||||||
@@ -54,52 +97,143 @@ def _extract_district(addr: str) -> str:
|
|||||||
return '其他'
|
return '其他'
|
||||||
|
|
||||||
|
|
||||||
|
def _load_from_parquet() -> bool:
|
||||||
|
"""Try to load data from pre-generated Parquet files. Returns True on success."""
|
||||||
|
outpatient_path = PROCESSED_DIR / "cases_outpatient.parquet"
|
||||||
|
inpatient_path = PROCESSED_DIR / "cases_inpatient.parquet"
|
||||||
|
|
||||||
|
if not outpatient_path.exists() or not inpatient_path.exists():
|
||||||
|
logger.info("Parquet files not found, falling back to Excel")
|
||||||
|
return False
|
||||||
|
|
||||||
|
try:
|
||||||
|
_cache["outpatient"] = pd.read_parquet(outpatient_path)
|
||||||
|
_cache["inpatient"] = pd.read_parquet(inpatient_path)
|
||||||
|
_cache["loaded_at"] = datetime.now()
|
||||||
|
logger.info("Loaded case data from Parquet (%d outpatient, %d inpatient)",
|
||||||
|
len(_cache["outpatient"]), len(_cache["inpatient"]))
|
||||||
|
return True
|
||||||
|
except Exception as e:
|
||||||
|
logger.warning("Parquet load failed (%s), falling back to Excel", e)
|
||||||
|
return False
|
||||||
|
|
||||||
|
|
||||||
|
def _load_from_excel():
|
||||||
|
"""Load data from Excel files (slow fallback)."""
|
||||||
|
df_out = pd.read_excel(DATA_DIR / "view_门诊.xlsx")
|
||||||
|
df_out['date'] = pd.to_datetime(df_out['门诊日期_re'])
|
||||||
|
df_out['district'] = df_out['现住址区'].fillna('未知')
|
||||||
|
_cache["outpatient"] = df_out
|
||||||
|
|
||||||
|
df_in = pd.read_excel(DATA_DIR / "view_住院.xlsx")
|
||||||
|
df_in['date'] = pd.to_datetime(df_in['入院日期_re'])
|
||||||
|
df_in['district'] = df_in['现住址_脱敏'].apply(_extract_district)
|
||||||
|
_cache["inpatient"] = df_in
|
||||||
|
|
||||||
|
_cache["loaded_at"] = datetime.now()
|
||||||
|
|
||||||
|
|
||||||
def load_data():
|
def load_data():
|
||||||
"""Load and cache outpatient + inpatient data from Excel files"""
|
"""Load and cache outpatient + inpatient data.
|
||||||
|
|
||||||
|
Uses pre-generated Parquet files for fast loading (~0.1s).
|
||||||
|
Falls back to Excel files (~10s) if Parquet is unavailable.
|
||||||
|
"""
|
||||||
if _cache["loaded_at"] is not None:
|
if _cache["loaded_at"] is not None:
|
||||||
return
|
return
|
||||||
|
|
||||||
try:
|
with _load_lock:
|
||||||
# Load outpatient data
|
if _cache["loaded_at"] is not None: # another thread loaded while we waited
|
||||||
df_out = pd.read_excel(DATA_DIR / "view_门诊.xlsx")
|
return
|
||||||
df_out['date'] = pd.to_datetime(df_out['门诊日期_re'])
|
if not _load_from_parquet():
|
||||||
df_out['district'] = df_out['现住址区'].fillna('未知')
|
try:
|
||||||
_cache["outpatient"] = df_out
|
_load_from_excel()
|
||||||
|
except Exception as e:
|
||||||
# Load inpatient data
|
raise RuntimeError(f"Data loading failed: {str(e)}")
|
||||||
df_in = pd.read_excel(DATA_DIR / "view_住院.xlsx")
|
|
||||||
df_in['date'] = pd.to_datetime(df_in['入院日期_re'])
|
|
||||||
df_in['district'] = df_in['现住址_脱敏'].apply(_extract_district)
|
|
||||||
_cache["inpatient"] = df_in
|
|
||||||
|
|
||||||
_cache["loaded_at"] = datetime.now()
|
|
||||||
except Exception as e:
|
|
||||||
raise RuntimeError(f"Data loading failed: {str(e)}")
|
|
||||||
|
|
||||||
|
|
||||||
def get_combined_data():
|
def get_combined_data() -> pd.DataFrame:
|
||||||
"""Return merged outpatient + inpatient data with unified diagnosis column"""
|
"""Return merged outpatient + inpatient data with unified diagnosis column.
|
||||||
|
|
||||||
|
Caches the result in memory after first call (~0.1s on cached hit).
|
||||||
|
"""
|
||||||
|
if _cache["combined"] is not None:
|
||||||
|
return cast(pd.DataFrame, _cache["combined"])
|
||||||
|
|
||||||
|
with _load_lock:
|
||||||
|
if _cache["combined"] is not None: # built while we waited for the lock
|
||||||
|
return cast(pd.DataFrame, _cache["combined"])
|
||||||
|
|
||||||
|
load_data()
|
||||||
|
|
||||||
|
df_out = cast(pd.DataFrame, _cache["outpatient"])
|
||||||
|
df_in = cast(pd.DataFrame, _cache["inpatient"])
|
||||||
|
|
||||||
|
df_out = df_out[['date', 'district', '初诊', '主诉']].copy()
|
||||||
|
df_out['type'] = 'outpatient'
|
||||||
|
df_out['diagnosis'] = df_out['初诊']
|
||||||
|
|
||||||
|
df_in = df_in[['date', 'district', '诊断名称']].copy()
|
||||||
|
df_in['type'] = 'inpatient'
|
||||||
|
df_in['diagnosis'] = df_in['诊断名称']
|
||||||
|
df_in['主诉'] = None
|
||||||
|
|
||||||
|
_cache["combined"] = pd.concat([df_out, df_in], ignore_index=True) # type: ignore[assignment]
|
||||||
|
return cast(pd.DataFrame, _cache["combined"])
|
||||||
|
|
||||||
|
|
||||||
|
def get_diagnoses() -> list[str]:
|
||||||
|
"""Return sorted list of unique diagnosis names (fast: reads from cached DataFrames)."""
|
||||||
load_data()
|
load_data()
|
||||||
|
out_diag = cast(pd.DataFrame, _cache["outpatient"])['初诊'].dropna().unique()
|
||||||
df_out = _cache["outpatient"][['date', 'district', '初诊', '主诉']].copy()
|
in_diag = cast(pd.DataFrame, _cache["inpatient"])['诊断名称'].dropna().unique()
|
||||||
df_out['type'] = 'outpatient'
|
return sorted(set(out_diag.tolist() + in_diag.tolist()))
|
||||||
df_out['diagnosis'] = df_out['初诊']
|
|
||||||
|
|
||||||
df_in = _cache["inpatient"][['date', 'district', '诊断名称']].copy()
|
|
||||||
df_in['type'] = 'inpatient'
|
|
||||||
df_in['diagnosis'] = df_in['诊断名称']
|
|
||||||
df_in['主诉'] = None
|
|
||||||
|
|
||||||
return pd.concat([df_out, df_in], ignore_index=True)
|
|
||||||
|
|
||||||
|
|
||||||
def get_outpatient_data():
|
def get_outpatient_data() -> pd.DataFrame:
|
||||||
"""Return the cached outpatient dataframe"""
|
"""Return the cached outpatient dataframe"""
|
||||||
load_data()
|
load_data()
|
||||||
return _cache["outpatient"]
|
return _cache["outpatient"] # type: ignore[return-value]
|
||||||
|
|
||||||
|
|
||||||
def get_inpatient_data():
|
def get_inpatient_data() -> pd.DataFrame:
|
||||||
"""Return the cached inpatient dataframe"""
|
"""Return the cached inpatient dataframe"""
|
||||||
load_data()
|
load_data()
|
||||||
return _cache["inpatient"]
|
return _cache["inpatient"] # type: ignore[return-value]
|
||||||
|
|
||||||
|
|
||||||
|
def load_cases_by_district_daily() -> pd.DataFrame:
|
||||||
|
"""Load processed/cases_by_district_daily.parquet with districts normalized.
|
||||||
|
|
||||||
|
The on-disk parquet carries both bare and 区-suffixed spellings of each
|
||||||
|
district (26 labels = 13 districts × 2 spellings), so any groupby on the
|
||||||
|
raw `district` column double-counts. This is the single data-access
|
||||||
|
boundary: it normalizes labels to the canonical 13 and re-aggregates
|
||||||
|
(sum of outpatient_count / inpatient_count / total_cases per
|
||||||
|
normalized district + date), so every downstream consumer
|
||||||
|
(analysis / grid / insights) sees clean, deduped 13-district data.
|
||||||
|
|
||||||
|
Returns a copy with columns [date, district, outpatient_count,
|
||||||
|
inpatient_count, total_cases]. Raises FileNotFoundError if the parquet
|
||||||
|
is missing (callers handle this as they did before).
|
||||||
|
"""
|
||||||
|
path = PROCESSED_DIR / "cases_by_district_daily.parquet"
|
||||||
|
cached = _cache.get("cases_by_district_daily")
|
||||||
|
if cached is not None:
|
||||||
|
return cast(pd.DataFrame, cached).copy()
|
||||||
|
|
||||||
|
with _load_lock:
|
||||||
|
cached = _cache.get("cases_by_district_daily")
|
||||||
|
if cached is not None:
|
||||||
|
return cast(pd.DataFrame, cached).copy()
|
||||||
|
|
||||||
|
df = pd.read_parquet(path)
|
||||||
|
df["district"] = df["district"].map(normalize_district)
|
||||||
|
agg = (
|
||||||
|
df.groupby(["date", "district"], as_index=False)[
|
||||||
|
["outpatient_count", "inpatient_count", "total_cases"]
|
||||||
|
]
|
||||||
|
.sum()
|
||||||
|
)
|
||||||
|
_cache["cases_by_district_daily"] = agg # type: ignore[assignment]
|
||||||
|
return agg.copy()
|
||||||
|
|||||||
@@ -17,7 +17,7 @@ class Settings(BaseSettings):
|
|||||||
POSTGRES_USER: str = ""
|
POSTGRES_USER: str = ""
|
||||||
POSTGRES_PASSWORD: str = ""
|
POSTGRES_PASSWORD: str = ""
|
||||||
POSTGRES_DB: str = ""
|
POSTGRES_DB: str = ""
|
||||||
|
|
||||||
class Config:
|
class Config:
|
||||||
env_file = ".env"
|
env_file = ".env"
|
||||||
|
|
||||||
@@ -33,10 +33,10 @@ if not settings.POSTGRES_USER or not settings.POSTGRES_PASSWORD or not settings.
|
|||||||
|
|
||||||
class Database:
|
class Database:
|
||||||
"""Async database connection pool manager"""
|
"""Async database connection pool manager"""
|
||||||
|
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
self.pool: Optional[asyncpg.Pool] = None
|
self.pool: Optional[asyncpg.Pool] = None
|
||||||
|
|
||||||
async def connect(self):
|
async def connect(self):
|
||||||
"""Initialize database connection pool"""
|
"""Initialize database connection pool"""
|
||||||
if self.pool is None:
|
if self.pool is None:
|
||||||
@@ -48,29 +48,29 @@ class Database:
|
|||||||
command_timeout=60
|
command_timeout=60
|
||||||
)
|
)
|
||||||
logger.info("Database connection pool created successfully")
|
logger.info("Database connection pool created successfully")
|
||||||
|
|
||||||
async def disconnect(self):
|
async def disconnect(self):
|
||||||
"""Close database connection pool"""
|
"""Close database connection pool"""
|
||||||
if self.pool:
|
if self.pool:
|
||||||
await self.pool.close()
|
await self.pool.close()
|
||||||
self.pool = None
|
self.pool = None
|
||||||
logger.info("Database connection pool closed")
|
logger.info("Database connection pool closed")
|
||||||
|
|
||||||
@asynccontextmanager
|
@asynccontextmanager
|
||||||
async def get_connection(self):
|
async def get_connection(self):
|
||||||
"""Get a connection from the pool"""
|
"""Get a connection from the pool"""
|
||||||
if self.pool is None:
|
if self.pool is None:
|
||||||
await self.connect()
|
await self.connect()
|
||||||
|
assert self.pool is not None
|
||||||
async with self.pool.acquire() as connection:
|
async with self.pool.acquire() as connection:
|
||||||
yield connection
|
yield connection
|
||||||
|
|
||||||
@asynccontextmanager
|
@asynccontextmanager
|
||||||
async def get_transaction(self):
|
async def get_transaction(self):
|
||||||
"""Get a transaction context"""
|
"""Get a transaction context"""
|
||||||
if self.pool is None:
|
if self.pool is None:
|
||||||
await self.connect()
|
await self.connect()
|
||||||
|
assert self.pool is not None
|
||||||
async with self.pool.acquire() as connection:
|
async with self.pool.acquire() as connection:
|
||||||
async with connection.transaction():
|
async with connection.transaction():
|
||||||
yield connection
|
yield connection
|
||||||
|
|||||||
@@ -13,7 +13,7 @@ from logging_config import setup_logging
|
|||||||
from middleware.request_logger import RequestLoggerMiddleware
|
from middleware.request_logger import RequestLoggerMiddleware
|
||||||
from auth.router import router as auth_router
|
from auth.router import router as auth_router
|
||||||
from auth.service import seed_default_admin
|
from auth.service import seed_default_admin
|
||||||
from routers import risk, alerts, analysis, insights, reports, cases, geocoded, grid, chat
|
from routers import risk, alerts, analysis, insights, reports, cases, geocoded, grid, chat, environment, statistics
|
||||||
|
|
||||||
|
|
||||||
setup_logging()
|
setup_logging()
|
||||||
@@ -27,7 +27,7 @@ app = FastAPI(
|
|||||||
)
|
)
|
||||||
|
|
||||||
app.add_middleware(RequestLoggerMiddleware)
|
app.add_middleware(RequestLoggerMiddleware)
|
||||||
app.add_middleware(GZipMiddleware, minimum_size=1000)
|
app.add_middleware(GZipMiddleware, minimum_size=1000, compresslevel=1)
|
||||||
|
|
||||||
logger = logging.getLogger("cbpoa.main")
|
logger = logging.getLogger("cbpoa.main")
|
||||||
|
|
||||||
@@ -58,6 +58,8 @@ app.include_router(cases.router)
|
|||||||
app.include_router(geocoded.router)
|
app.include_router(geocoded.router)
|
||||||
app.include_router(grid.router)
|
app.include_router(grid.router)
|
||||||
app.include_router(chat.router)
|
app.include_router(chat.router)
|
||||||
|
app.include_router(environment.router)
|
||||||
|
app.include_router(statistics.router)
|
||||||
|
|
||||||
|
|
||||||
@app.get("/")
|
@app.get("/")
|
||||||
|
|||||||
@@ -2,7 +2,7 @@
|
|||||||
Pydantic models for CBPOA risk assessment API
|
Pydantic models for CBPOA risk assessment API
|
||||||
Aligned with frontend types from CBPOA/frontend/src/types/index.ts
|
Aligned with frontend types from CBPOA/frontend/src/types/index.ts
|
||||||
"""
|
"""
|
||||||
from pydantic import BaseModel, Field
|
from pydantic import BaseModel, ConfigDict, Field
|
||||||
from typing import Optional, List, Literal
|
from typing import Optional, List, Literal
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
|
|
||||||
@@ -280,6 +280,9 @@ class MultiDayPredictionRequest(BaseModel):
|
|||||||
|
|
||||||
class MultiDayPredictionResponse(BaseModel):
|
class MultiDayPredictionResponse(BaseModel):
|
||||||
"""Response for multi-day grid predictions"""
|
"""Response for multi-day grid predictions"""
|
||||||
|
# `model_version` collides with Pydantic's protected `model_` namespace; opt out.
|
||||||
|
model_config = ConfigDict(protected_namespaces=())
|
||||||
|
|
||||||
predictions: List[GridPrediction] = Field(..., description="Grid predictions")
|
predictions: List[GridPrediction] = Field(..., description="Grid predictions")
|
||||||
total_grids: int = Field(..., description="Total grids predicted")
|
total_grids: int = Field(..., description="Total grids predicted")
|
||||||
date_range: tuple[str, str] = Field(..., description="Prediction date range")
|
date_range: tuple[str, str] = Field(..., description="Prediction date range")
|
||||||
|
|||||||
@@ -16,3 +16,4 @@ pandas>=2.0.0
|
|||||||
numpy>=1.24.0
|
numpy>=1.24.0
|
||||||
pyarrow>=14.0.0
|
pyarrow>=14.0.0
|
||||||
openpyxl>=3.1.0
|
openpyxl>=3.1.0
|
||||||
|
Pillow>=10.0.0
|
||||||
|
|||||||
47
backend/routers/CLAUDE.md
Normal file
47
backend/routers/CLAUDE.md
Normal file
@@ -0,0 +1,47 @@
|
|||||||
|
# Routers — API Endpoints
|
||||||
|
|
||||||
|
## Pattern
|
||||||
|
|
||||||
|
Each router file defines one `APIRouter(prefix=..., tags=[...])` with typed endpoints.
|
||||||
|
|
||||||
|
```python
|
||||||
|
from fastapi import APIRouter
|
||||||
|
from models import SomeResponse
|
||||||
|
|
||||||
|
router = APIRouter(prefix="/api/domain", tags=["domain"])
|
||||||
|
|
||||||
|
@router.get("/endpoint", response_model=SomeResponse)
|
||||||
|
async def get_something(...):
|
||||||
|
...
|
||||||
|
```
|
||||||
|
|
||||||
|
## Conventions
|
||||||
|
|
||||||
|
- Return Pydantic models (`response_model=`), never raw dicts
|
||||||
|
- Use `Annotated[Type, Query(...)]` / `Path(...)` for request params
|
||||||
|
- Spatial queries use `scipy.spatial.KDTree` for nearest-neighbor lookups
|
||||||
|
- GeoJSON parsing is delegated to `utils/geojson.py`
|
||||||
|
- Risk level mapping is in `utils/risk.py` — use `risk_value_to_level()` not inline thresholds
|
||||||
|
- Large repeated queries use `@lru_cache` (from `functools`)
|
||||||
|
- Date helpers from `utils/date_helpers.py` — always `get_latest_date()`, never guess
|
||||||
|
|
||||||
|
## File Map
|
||||||
|
|
||||||
|
| File | Domain |
|
||||||
|
|------|--------|
|
||||||
|
| `risk.py` | Risk maps, grid detail, history (largest router, ~42 file reads) |
|
||||||
|
| `alerts.py` | Alert feed, stats |
|
||||||
|
| `cases.py` | Medical case queries (age, disease, district filters) |
|
||||||
|
| `analysis.py` | Trend analysis, statistics |
|
||||||
|
| `grid.py` | Grid metadata, elevation, population |
|
||||||
|
| `reports.py` | Report generation, export |
|
||||||
|
| `insights.py` | AI-generated insights |
|
||||||
|
| `chat.py` | Chatbot endpoint |
|
||||||
|
| `geocoded.py` | Geocoded case data |
|
||||||
|
|
||||||
|
## Anti-Patterns
|
||||||
|
|
||||||
|
- Don't use sync I/O in `async def` — use `async with db.get_connection()` for DB
|
||||||
|
- Don't catch bare `Exception` — use specific HTTPException or let it propagate to middleware
|
||||||
|
- Don't return raw GeoJSON dicts without Pydantic validation
|
||||||
|
- Don't inline risk thresholds — use `utils/risk.risk_value_to_level()`
|
||||||
@@ -5,6 +5,8 @@ Generates alerts from high-risk grids in GeoJSON files
|
|||||||
from fastapi import APIRouter, HTTPException, Query
|
from fastapi import APIRouter, HTTPException, Query
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
from typing import List
|
from typing import List
|
||||||
|
from functools import lru_cache
|
||||||
|
import asyncio
|
||||||
import json
|
import json
|
||||||
|
|
||||||
from config import DATA_DIR, ALERT_P1_RISK, ALERT_P2_RISK, WUHAN_BOUNDS, LAT_STEP, LON_STEP, MAX_ALERTS
|
from config import DATA_DIR, ALERT_P1_RISK, ALERT_P2_RISK, WUHAN_BOUNDS, LAT_STEP, LON_STEP, MAX_ALERTS
|
||||||
@@ -32,8 +34,13 @@ def grid_id_to_center(grid_id: str) -> tuple[float, float]:
|
|||||||
return lat, lon
|
return lat, lon
|
||||||
|
|
||||||
|
|
||||||
def generate_alerts_for_date(date: str) -> List[Alert]:
|
@lru_cache(maxsize=8)
|
||||||
"""Generate alerts for high-risk grids on a specific date.
|
def _generate_alerts_cached(date: str) -> List[Alert]:
|
||||||
|
"""Heavy synchronous worker: parse the ~45MB GeoJSON and build alerts.
|
||||||
|
|
||||||
|
Cached by date so the file is parsed once per date. This runs blocking
|
||||||
|
json.load + per-feature loops, so callers must invoke it off the event
|
||||||
|
loop (see generate_alerts_for_date).
|
||||||
|
|
||||||
Phase 1: iterate features, aggregate max risk per 100m grid cell.
|
Phase 1: iterate features, aggregate max risk per 100m grid cell.
|
||||||
Phase 2: build Alert objects from aggregated grid cells.
|
Phase 2: build Alert objects from aggregated grid cells.
|
||||||
@@ -111,6 +118,16 @@ def generate_alerts_for_date(date: str) -> List[Alert]:
|
|||||||
return alerts[:MAX_ALERTS]
|
return alerts[:MAX_ALERTS]
|
||||||
|
|
||||||
|
|
||||||
|
async def generate_alerts_for_date(date: str) -> List[Alert]:
|
||||||
|
"""Async accessor: run the cached heavy parser in a thread pool.
|
||||||
|
|
||||||
|
Offloading the blocking json.load + per-feature aggregation keeps the
|
||||||
|
event loop free. The lru_cache lives on the worker, so warm dates return
|
||||||
|
near-instantly without re-parsing.
|
||||||
|
"""
|
||||||
|
return await asyncio.to_thread(_generate_alerts_cached, date)
|
||||||
|
|
||||||
|
|
||||||
@router.get("", response_model=AlertResponse)
|
@router.get("", response_model=AlertResponse)
|
||||||
async def list_alerts(date: str | None = None, priority: str | None = None, min_risk: float | None = None):
|
async def list_alerts(date: str | None = None, priority: str | None = None, min_risk: float | None = None):
|
||||||
if date is not None and not validate_date_format(date):
|
if date is not None and not validate_date_format(date):
|
||||||
@@ -118,7 +135,7 @@ async def list_alerts(date: str | None = None, priority: str | None = None, min_
|
|||||||
if date is None:
|
if date is None:
|
||||||
date = get_latest_date()
|
date = get_latest_date()
|
||||||
|
|
||||||
alerts = generate_alerts_for_date(date)
|
alerts = await generate_alerts_for_date(date)
|
||||||
|
|
||||||
if priority:
|
if priority:
|
||||||
alerts = [a for a in alerts if a.priority == priority]
|
alerts = [a for a in alerts if a.priority == priority]
|
||||||
@@ -140,7 +157,7 @@ async def get_alert(alert_id: str, date: str | None = None):
|
|||||||
if date is None:
|
if date is None:
|
||||||
date = get_latest_date()
|
date = get_latest_date()
|
||||||
|
|
||||||
alerts = generate_alerts_for_date(date)
|
alerts = await generate_alerts_for_date(date)
|
||||||
|
|
||||||
for alert in alerts:
|
for alert in alerts:
|
||||||
if alert.alert_id == alert_id:
|
if alert.alert_id == alert_id:
|
||||||
@@ -156,7 +173,7 @@ async def get_p1_alerts(date: str | None = None):
|
|||||||
if date is None:
|
if date is None:
|
||||||
date = get_latest_date()
|
date = get_latest_date()
|
||||||
|
|
||||||
alerts = generate_alerts_for_date(date)
|
alerts = await generate_alerts_for_date(date)
|
||||||
p1_alerts = [a for a in alerts if a.priority == "P1"]
|
p1_alerts = [a for a in alerts if a.priority == "P1"]
|
||||||
|
|
||||||
return AlertResponse(
|
return AlertResponse(
|
||||||
@@ -173,7 +190,7 @@ async def get_p2_alerts(date: str | None = None):
|
|||||||
if date is None:
|
if date is None:
|
||||||
date = get_latest_date()
|
date = get_latest_date()
|
||||||
|
|
||||||
alerts = generate_alerts_for_date(date)
|
alerts = await generate_alerts_for_date(date)
|
||||||
p2_alerts = [a for a in alerts if a.priority == "P2"]
|
p2_alerts = [a for a in alerts if a.priority == "P2"]
|
||||||
|
|
||||||
return AlertResponse(
|
return AlertResponse(
|
||||||
@@ -190,7 +207,7 @@ async def get_grid_alerts(grid_id: str, date: str | None = None):
|
|||||||
if date is None:
|
if date is None:
|
||||||
date = get_latest_date()
|
date = get_latest_date()
|
||||||
|
|
||||||
alerts = generate_alerts_for_date(date)
|
alerts = await generate_alerts_for_date(date)
|
||||||
grid_alerts = [a for a in alerts if a.grid_id == grid_id]
|
grid_alerts = [a for a in alerts if a.grid_id == grid_id]
|
||||||
|
|
||||||
return AlertResponse(
|
return AlertResponse(
|
||||||
|
|||||||
@@ -5,11 +5,14 @@ Time series trends, district aggregation, and weather-health correlations
|
|||||||
from fastapi import APIRouter, HTTPException, Query
|
from fastapi import APIRouter, HTTPException, Query
|
||||||
from datetime import datetime, timedelta
|
from datetime import datetime, timedelta
|
||||||
from typing import List, Literal
|
from typing import List, Literal
|
||||||
|
from functools import lru_cache
|
||||||
import random
|
import random
|
||||||
|
|
||||||
|
import pandas as pd
|
||||||
from pydantic import BaseModel, Field
|
from pydantic import BaseModel, Field
|
||||||
|
|
||||||
from config import DATA_DIR, RISK_HIGH
|
from config import DATA_DIR, RISK_HIGH, PROJECT_ROOT, WUHAN_BOUNDS, LAT_STEP, LON_STEP
|
||||||
|
from data.case_loader import load_cases_by_district_daily
|
||||||
from utils.date_helpers import get_latest_date
|
from utils.date_helpers import get_latest_date
|
||||||
from utils.geojson import parse_geojson_file, load_districts
|
from utils.geojson import parse_geojson_file, load_districts
|
||||||
from utils.geo import point_in_polygon
|
from utils.geo import point_in_polygon
|
||||||
@@ -27,11 +30,13 @@ class TrendResponse(BaseModel):
|
|||||||
|
|
||||||
class DistrictRisk(BaseModel):
|
class DistrictRisk(BaseModel):
|
||||||
"""District-level risk aggregation"""
|
"""District-level risk aggregation"""
|
||||||
name: str = Field(..., description="District name")
|
district: str = Field(..., description="District name")
|
||||||
avg_risk: float = Field(..., description="Average risk value")
|
avg_risk: float = Field(..., description="Average risk value")
|
||||||
|
avg_aqi: float = Field(..., description="Average AQI from weather stations in this district")
|
||||||
|
population: int = Field(..., description="Population (sum of 100m LandScan cells in district)")
|
||||||
high_risk_count: int = Field(..., description="Count of high risk grids")
|
high_risk_count: int = Field(..., description="Count of high risk grids")
|
||||||
total_grids: int = Field(..., description="Total grids in district")
|
total_grids: int = Field(..., description="Total grids in district")
|
||||||
total_cases: int = Field(..., description="Estimated total cases")
|
total_cases: int = Field(..., description="Total recorded cases (real, from cases_by_district_daily)")
|
||||||
|
|
||||||
|
|
||||||
class DistrictsResponse(BaseModel):
|
class DistrictsResponse(BaseModel):
|
||||||
@@ -91,12 +96,9 @@ async def get_trend(days: int = Query(default=7, ge=1, le=30)):
|
|||||||
values.append(0)
|
values.append(0)
|
||||||
dates.append(date.strftime("%Y-%m-%d"))
|
dates.append(date.strftime("%Y-%m-%d"))
|
||||||
|
|
||||||
# Filter out zero values
|
# Preserve the full requested date range: a "7天" request must return 7
|
||||||
valid_data = [(d, v) for d, v in zip(dates, values) if v > 0]
|
# contiguous points. Days with no geojson (or empty grids) stay 0 rather
|
||||||
if valid_data:
|
# than being dropped, which previously produced fewer, non-contiguous points.
|
||||||
dates, values = zip(*valid_data)
|
|
||||||
dates, values = list(dates), list(values)
|
|
||||||
|
|
||||||
trend_direction = calculate_trend(values)
|
trend_direction = calculate_trend(values)
|
||||||
|
|
||||||
return TrendResponse(
|
return TrendResponse(
|
||||||
@@ -106,101 +108,151 @@ async def get_trend(days: int = Query(default=7, ge=1, le=30)):
|
|||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
@lru_cache(maxsize=1)
|
||||||
|
def _grid_district_lookup() -> dict:
|
||||||
|
"""Map precomputed r{row}_c{col} grid id -> district name (loaded once)."""
|
||||||
|
path = PROJECT_ROOT / "processed" / "grid_district_mapping.parquet"
|
||||||
|
if not path.exists():
|
||||||
|
return {}
|
||||||
|
df = pd.read_parquet(path)
|
||||||
|
# Some grids have a null district_name; drop them so the lookup only ever
|
||||||
|
# returns valid strings (missing keys fall back to "其他").
|
||||||
|
df = df.dropna(subset=["district_name"])
|
||||||
|
return dict(zip(df["grid_id"].astype(str), df["district_name"].astype(str)))
|
||||||
|
|
||||||
|
|
||||||
|
@lru_cache(maxsize=1)
|
||||||
|
def _district_population() -> dict:
|
||||||
|
"""Real population per district.
|
||||||
|
|
||||||
|
Sums the LandScan-derived population_density of every 100m cell
|
||||||
|
(grid_100m_with_dem_pop.parquet) grouped by district via the
|
||||||
|
grid->district mapping. Returns {district_name: total_population}.
|
||||||
|
"""
|
||||||
|
pop_path = PROJECT_ROOT / "processed" / "grid_100m_with_dem_pop.parquet"
|
||||||
|
map_path = PROJECT_ROOT / "processed" / "grid_district_mapping.parquet"
|
||||||
|
if not pop_path.exists() or not map_path.exists():
|
||||||
|
return {}
|
||||||
|
pop = pd.read_parquet(pop_path, columns=["grid_id", "population_density"])
|
||||||
|
mapping = pd.read_parquet(map_path).dropna(subset=["district_name"])
|
||||||
|
joined = pop.merge(mapping, on="grid_id", how="inner")
|
||||||
|
by_d = joined.groupby("district_name")["population_density"].sum()
|
||||||
|
return {str(k): int(round(v)) for k, v in by_d.items()}
|
||||||
|
|
||||||
|
|
||||||
|
@lru_cache(maxsize=1)
|
||||||
|
def _district_avg_aqi() -> dict:
|
||||||
|
"""Real average AQI per district from weather station daily data.
|
||||||
|
|
||||||
|
Each station (with lat/lon) is assigned to a district using the same
|
||||||
|
grid->district mapping (100m grid spacing of 1/1110 deg, the convention
|
||||||
|
the mapping was built with), then AQI is averaged per district across
|
||||||
|
all daily observations. Returns {district_name: avg_aqi}. Districts with
|
||||||
|
no station fall back to the city-wide mean in the caller.
|
||||||
|
"""
|
||||||
|
map_path = PROJECT_ROOT / "processed" / "grid_district_mapping.parquet"
|
||||||
|
station_path = PROJECT_ROOT / "processed" / "weather" / "station_daily_2022.parquet"
|
||||||
|
if not map_path.exists() or not station_path.exists():
|
||||||
|
return {}
|
||||||
|
mapping = pd.read_parquet(map_path).dropna(subset=["district_name"])
|
||||||
|
lookup = dict(zip(mapping["grid_id"].astype(str), mapping["district_name"].astype(str)))
|
||||||
|
station = pd.read_parquet(station_path, columns=["station_id", "lat", "lon", "AQI"])
|
||||||
|
|
||||||
|
step = 1.0 / 1110.0 # mapping grid spacing in degrees
|
||||||
|
min_lat = WUHAN_BOUNDS["min_lat"]
|
||||||
|
min_lon = WUHAN_BOUNDS["min_lon"]
|
||||||
|
|
||||||
|
coords = station[["station_id", "lat", "lon"]].drop_duplicates()
|
||||||
|
station_to_district = {}
|
||||||
|
for _, r in coords.iterrows():
|
||||||
|
row = int((r["lat"] - min_lat) / step)
|
||||||
|
col = int((r["lon"] - min_lon) / step)
|
||||||
|
station_to_district[r["station_id"]] = lookup.get(f"r{row}_c{col}", "其他")
|
||||||
|
|
||||||
|
station = station.copy()
|
||||||
|
station["district"] = station["station_id"].map(station_to_district)
|
||||||
|
in_district = station[station["district"] != "其他"]
|
||||||
|
by_d = in_district.groupby("district")["AQI"].mean()
|
||||||
|
return {str(k): round(float(v), 1) for k, v in by_d.items()}
|
||||||
|
|
||||||
|
|
||||||
|
@lru_cache(maxsize=1)
|
||||||
|
def _district_total_cases() -> dict:
|
||||||
|
"""Real total recorded cases per district from cases_by_district_daily.
|
||||||
|
|
||||||
|
District labels are normalized to the canonical 13 区-suffixed names at the
|
||||||
|
data-access boundary (data.case_loader), so this is a plain per-district
|
||||||
|
sum. Returns {district: cases}.
|
||||||
|
"""
|
||||||
|
try:
|
||||||
|
df = load_cases_by_district_daily()
|
||||||
|
except FileNotFoundError:
|
||||||
|
return {}
|
||||||
|
by_district = df.groupby("district")["total_cases"].sum()
|
||||||
|
return {str(d): int(v) for d, v in by_district.items()}
|
||||||
|
|
||||||
|
|
||||||
|
@lru_cache(maxsize=8)
|
||||||
|
def _aggregate_districts(date: str) -> list:
|
||||||
|
"""Aggregate per-district risk for a date.
|
||||||
|
|
||||||
|
Assigns each 100m risk grid to a district via the precomputed
|
||||||
|
grid->district mapping (O(1) dict lookup per grid) instead of per-grid
|
||||||
|
point-in-polygon (which is ~100x slower over 140k grids). Cached by date.
|
||||||
|
"""
|
||||||
|
grids = parse_geojson_file(DATA_DIR / f"risk_{date}.geojson")
|
||||||
|
lookup = _grid_district_lookup()
|
||||||
|
|
||||||
|
agg: dict = {}
|
||||||
|
for g in grids:
|
||||||
|
row = int((g["latitude"] - WUHAN_BOUNDS["min_lat"]) / LAT_STEP)
|
||||||
|
col = int((g["longitude"] - WUHAN_BOUNDS["min_lon"]) / LON_STEP)
|
||||||
|
name = lookup.get(f"r{row}_c{col}", "其他")
|
||||||
|
a = agg.setdefault(name, {"sum": 0.0, "count": 0, "high": 0})
|
||||||
|
risk = g["risk_value"]
|
||||||
|
a["sum"] += risk
|
||||||
|
a["count"] += 1
|
||||||
|
if risk >= RISK_HIGH:
|
||||||
|
a["high"] += 1
|
||||||
|
|
||||||
|
pop_by_district = _district_population()
|
||||||
|
aqi_by_district = _district_avg_aqi()
|
||||||
|
cases_by_district = _district_total_cases()
|
||||||
|
# City-wide mean AQI as fallback for districts without a weather station.
|
||||||
|
city_avg_aqi = round(sum(aqi_by_district.values()) / len(aqi_by_district), 1) if aqi_by_district else 0.0
|
||||||
|
|
||||||
|
result = []
|
||||||
|
for name, a in agg.items():
|
||||||
|
if a["count"] == 0:
|
||||||
|
continue
|
||||||
|
avg = a["sum"] / a["count"]
|
||||||
|
result.append({
|
||||||
|
"district": name,
|
||||||
|
"avg_risk": round(avg, 4),
|
||||||
|
"avg_aqi": aqi_by_district.get(name, city_avg_aqi),
|
||||||
|
"population": pop_by_district.get(name, 0),
|
||||||
|
"high_risk_count": a["high"],
|
||||||
|
"total_grids": a["count"],
|
||||||
|
"total_cases": cases_by_district.get(name, 0),
|
||||||
|
})
|
||||||
|
# '其他' (unassigned) last, otherwise by descending risk
|
||||||
|
result.sort(key=lambda d: (d["district"] == "其他", -d["avg_risk"]))
|
||||||
|
return result
|
||||||
|
|
||||||
|
|
||||||
@router.get("/districts", response_model=DistrictsResponse)
|
@router.get("/districts", response_model=DistrictsResponse)
|
||||||
async def get_districts():
|
async def get_districts():
|
||||||
"""
|
"""Get district-level risk aggregation (cached per date)."""
|
||||||
Get district-level risk aggregation
|
|
||||||
|
|
||||||
Returns:
|
|
||||||
District-level risk data with averages and counts
|
|
||||||
"""
|
|
||||||
latest_date = get_latest_date()
|
latest_date = get_latest_date()
|
||||||
filepath = DATA_DIR / f"risk_{latest_date}.geojson"
|
filepath = DATA_DIR / f"risk_{latest_date}.geojson"
|
||||||
|
|
||||||
if not filepath.exists():
|
if not filepath.exists():
|
||||||
raise HTTPException(status_code=404, detail=f"No data found for date {latest_date}")
|
raise HTTPException(status_code=404, detail=f"No data found for date {latest_date}")
|
||||||
|
|
||||||
grids = parse_geojson_file(filepath)
|
districts = [DistrictRisk(**d) for d in _aggregate_districts(latest_date)]
|
||||||
districts = load_districts()
|
|
||||||
|
|
||||||
if not districts:
|
|
||||||
# Fallback: return city-wide aggregation
|
|
||||||
avg_risk = sum(g["risk_value"] for g in grids) / len(grids) if grids else 0
|
|
||||||
high_risk_count = sum(1 for g in grids if g["risk_value"] >= RISK_HIGH)
|
|
||||||
|
|
||||||
return DistrictsResponse(
|
|
||||||
districts=[
|
|
||||||
DistrictRisk(
|
|
||||||
name="武汉市",
|
|
||||||
avg_risk=round(avg_risk, 4),
|
|
||||||
high_risk_count=high_risk_count,
|
|
||||||
total_grids=len(grids),
|
|
||||||
total_cases=int(len(grids) * avg_risk * 0.1) # Mock case rate
|
|
||||||
)
|
|
||||||
],
|
|
||||||
timestamp=datetime.now().isoformat()
|
|
||||||
)
|
|
||||||
|
|
||||||
# Aggregate grids by district using point-in-polygon
|
|
||||||
district_data = {d["name"]: {"grids": [], "high_risk": 0} for d in districts}
|
|
||||||
unassigned = {"grids": [], "high_risk": 0}
|
|
||||||
|
|
||||||
for grid in grids:
|
|
||||||
assigned = False
|
|
||||||
for district in districts:
|
|
||||||
if point_in_polygon(grid["latitude"], grid["longitude"], district["coordinates"]):
|
|
||||||
district_data[district["name"]]["grids"].append(grid)
|
|
||||||
if grid["risk_value"] >= RISK_HIGH:
|
|
||||||
district_data[district["name"]]["high_risk"] += 1
|
|
||||||
assigned = True
|
|
||||||
break
|
|
||||||
|
|
||||||
if not assigned:
|
|
||||||
unassigned["grids"].append(grid)
|
|
||||||
if grid["risk_value"] >= RISK_HIGH:
|
|
||||||
unassigned["high_risk"] += 1
|
|
||||||
|
|
||||||
# Build response
|
|
||||||
result = []
|
|
||||||
for district in districts:
|
|
||||||
name = district["name"]
|
|
||||||
grids_in_district = district_data[name]["grids"]
|
|
||||||
|
|
||||||
if not grids_in_district:
|
|
||||||
continue
|
|
||||||
|
|
||||||
avg_risk = sum(g["risk_value"] for g in grids_in_district) / len(grids_in_district)
|
|
||||||
high_risk_count = district_data[name]["high_risk"]
|
|
||||||
|
|
||||||
# Mock total cases based on risk and grid count
|
|
||||||
total_cases = int(len(grids_in_district) * avg_risk * 0.1)
|
|
||||||
|
|
||||||
result.append(
|
|
||||||
DistrictRisk(
|
|
||||||
name=name,
|
|
||||||
avg_risk=round(avg_risk, 4),
|
|
||||||
high_risk_count=high_risk_count,
|
|
||||||
total_grids=len(grids_in_district),
|
|
||||||
total_cases=total_cases
|
|
||||||
)
|
|
||||||
)
|
|
||||||
|
|
||||||
# Add unassigned as "其他" if significant
|
|
||||||
if unassigned["grids"]:
|
|
||||||
avg_risk = sum(g["risk_value"] for g in unassigned["grids"]) / len(unassigned["grids"])
|
|
||||||
result.append(
|
|
||||||
DistrictRisk(
|
|
||||||
name="其他",
|
|
||||||
avg_risk=round(avg_risk, 4),
|
|
||||||
high_risk_count=unassigned["high_risk"],
|
|
||||||
total_grids=len(unassigned["grids"]),
|
|
||||||
total_cases=int(len(unassigned["grids"]) * avg_risk * 0.1)
|
|
||||||
)
|
|
||||||
)
|
|
||||||
|
|
||||||
return DistrictsResponse(
|
return DistrictsResponse(
|
||||||
districts=result,
|
districts=districts,
|
||||||
timestamp=datetime.now().isoformat()
|
timestamp=datetime.now().isoformat(),
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -96,9 +96,10 @@ async def get_cases_stats(
|
|||||||
total_outpatient = len(df_out)
|
total_outpatient = len(df_out)
|
||||||
total_inpatient = len(df_in)
|
total_inpatient = len(df_in)
|
||||||
|
|
||||||
# 日期范围
|
# 日期范围(过滤后可能为空,需防御 NaT)
|
||||||
min_date = min(df_out['date'].min(), df_in['date'].min())
|
all_dates = pd.concat([df_out['date'], df_in['date']]).dropna()
|
||||||
max_date = max(df_out['date'].max(), df_in['date'].max())
|
date_start = all_dates.min().strftime("%Y-%m-%d") if len(all_dates) else ""
|
||||||
|
date_end = all_dates.max().strftime("%Y-%m-%d") if len(all_dates) else ""
|
||||||
|
|
||||||
# 区域统计
|
# 区域统计
|
||||||
out_districts = df_out[df_out['district'] != '未知']['district'].value_counts().head(10)
|
out_districts = df_out[df_out['district'] != '未知']['district'].value_counts().head(10)
|
||||||
@@ -120,8 +121,8 @@ async def get_cases_stats(
|
|||||||
total_outpatient=total_outpatient,
|
total_outpatient=total_outpatient,
|
||||||
total_inpatient=total_inpatient,
|
total_inpatient=total_inpatient,
|
||||||
date_range={
|
date_range={
|
||||||
"start": min_date.strftime("%Y-%m-%d"),
|
"start": date_start,
|
||||||
"end": max_date.strftime("%Y-%m-%d")
|
"end": date_end
|
||||||
},
|
},
|
||||||
top_districts=top_districts,
|
top_districts=top_districts,
|
||||||
top_diagnoses=top_diagnoses
|
top_diagnoses=top_diagnoses
|
||||||
@@ -147,7 +148,7 @@ async def get_cases_trend(
|
|||||||
if end_date and not DATE_PATTERN.match(end_date):
|
if end_date and not DATE_PATTERN.match(end_date):
|
||||||
raise HTTPException(status_code=400, detail="Invalid end_date format. Use YYYY-MM-DD")
|
raise HTTPException(status_code=400, detail="Invalid end_date format. Use YYYY-MM-DD")
|
||||||
|
|
||||||
df = get_combined_data()
|
df = get_combined_data().copy()
|
||||||
|
|
||||||
# 日期过滤
|
# 日期过滤
|
||||||
if start_date:
|
if start_date:
|
||||||
@@ -203,6 +204,8 @@ def _compute_cases_districts(
|
|||||||
case_type: Optional[str],
|
case_type: Optional[str],
|
||||||
min_count: int,
|
min_count: int,
|
||||||
diagnosis: Optional[str],
|
diagnosis: Optional[str],
|
||||||
|
start_date: Optional[str] = None,
|
||||||
|
end_date: Optional[str] = None,
|
||||||
) -> DistrictsResponse:
|
) -> DistrictsResponse:
|
||||||
"""Run the full pandas aggregation pipeline (called in thread pool)."""
|
"""Run the full pandas aggregation pipeline (called in thread pool)."""
|
||||||
df = get_combined_data()
|
df = get_combined_data()
|
||||||
@@ -211,6 +214,12 @@ def _compute_cases_districts(
|
|||||||
if diagnosis:
|
if diagnosis:
|
||||||
df = df[df['diagnosis'].str.contains(diagnosis, na=False, case=False)]
|
df = df[df['diagnosis'].str.contains(diagnosis, na=False, case=False)]
|
||||||
|
|
||||||
|
# 日期过滤
|
||||||
|
if start_date:
|
||||||
|
df = df[df['date'] >= pd.to_datetime(start_date)]
|
||||||
|
if end_date:
|
||||||
|
df = df[df['date'] <= pd.to_datetime(end_date)]
|
||||||
|
|
||||||
# 类型过滤
|
# 类型过滤
|
||||||
if case_type == "outpatient":
|
if case_type == "outpatient":
|
||||||
df = df[df['type'] == 'outpatient']
|
df = df[df['type'] == 'outpatient']
|
||||||
@@ -255,18 +264,21 @@ async def get_cases_districts(
|
|||||||
case_type: Optional[str] = Query(None, description="病例类型:outpatient, inpatient, all"),
|
case_type: Optional[str] = Query(None, description="病例类型:outpatient, inpatient, all"),
|
||||||
min_count: int = Query(10, description="最小病例数过滤"),
|
min_count: int = Query(10, description="最小病例数过滤"),
|
||||||
diagnosis: Optional[str] = Query(None, description="Filter by diagnosis name"),
|
diagnosis: Optional[str] = Query(None, description="Filter by diagnosis name"),
|
||||||
|
start_date: Optional[str] = Query(None, description="开始日期 (YYYY-MM-DD)"),
|
||||||
|
end_date: Optional[str] = Query(None, description="结束日期 (YYYY-MM-DD)"),
|
||||||
):
|
):
|
||||||
"""
|
"""
|
||||||
获取病例区域分布数据
|
获取病例区域分布数据
|
||||||
|
|
||||||
- 支持按病例类型筛选
|
- 支持按病例类型筛选
|
||||||
- 可设置最小病例数过滤
|
- 可设置最小病例数过滤
|
||||||
|
- 支持日期范围过滤
|
||||||
- 返回各区门诊、住院量及占比
|
- 返回各区门诊、住院量及占比
|
||||||
|
|
||||||
Pandas processing runs in a thread pool to avoid blocking the async event loop.
|
Pandas processing runs in a thread pool to avoid blocking the async event loop.
|
||||||
"""
|
"""
|
||||||
return await asyncio.to_thread(
|
return await asyncio.to_thread(
|
||||||
_compute_cases_districts, case_type, min_count, diagnosis
|
_compute_cases_districts, case_type, min_count, diagnosis, start_date, end_date
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
@@ -413,10 +425,19 @@ class DiseaseSeasonalityPoint(BaseModel):
|
|||||||
total: int
|
total: int
|
||||||
|
|
||||||
|
|
||||||
|
class DiseaseDistrictItem(BaseModel):
|
||||||
|
"""单个诊断的区域分布(按病例数排序的前若干区)"""
|
||||||
|
diagnosis: str
|
||||||
|
district: str
|
||||||
|
total: int
|
||||||
|
|
||||||
|
|
||||||
class DiseaseSeasonalityResponse(BaseModel):
|
class DiseaseSeasonalityResponse(BaseModel):
|
||||||
"""疾病季节性响应"""
|
"""疾病季节性响应"""
|
||||||
seasonality: list[DiseaseSeasonalityPoint]
|
seasonality: list[DiseaseSeasonalityPoint]
|
||||||
diagnoses: list[str]
|
diagnoses: list[str]
|
||||||
|
# 每个诊断的真实区域分布(按区聚合),使前端可为每个诊断显示其各自的"主要区域"
|
||||||
|
diagnosis_districts: list[DiseaseDistrictItem]
|
||||||
|
|
||||||
|
|
||||||
@router.get("/seasonal", response_model=SeasonalResponse, summary="获取季节性月度聚合数据")
|
@router.get("/seasonal", response_model=SeasonalResponse, summary="获取季节性月度聚合数据")
|
||||||
@@ -562,16 +583,22 @@ async def get_cases_demographics():
|
|||||||
# ============== Disease Seasonality Endpoint ==============
|
# ============== Disease Seasonality Endpoint ==============
|
||||||
|
|
||||||
@router.get("/disease-seasonality", response_model=DiseaseSeasonalityResponse, summary="获取疾病季节性数据")
|
@router.get("/disease-seasonality", response_model=DiseaseSeasonalityResponse, summary="获取疾病季节性数据")
|
||||||
async def get_disease_seasonality():
|
async def get_disease_seasonality(
|
||||||
|
diagnosis: Optional[str] = Query(None, description="Filter by diagnosis name"),
|
||||||
|
):
|
||||||
"""
|
"""
|
||||||
获取各诊断的月度季节性分布数据
|
获取各诊断的月度季节性分布数据
|
||||||
|
|
||||||
- 基于门诊+住院合并数据
|
- 基于门诊+住院合并数据
|
||||||
- 按月聚合所有年份,返回 top 10 诊断的月度分布
|
- 按月聚合所有年份,返回 top 10 诊断的月度分布
|
||||||
|
- 支持可选诊断过滤
|
||||||
- 用于疾病季节性热力图、雷达图等
|
- 用于疾病季节性热力图、雷达图等
|
||||||
"""
|
"""
|
||||||
df = get_combined_data()
|
df = get_combined_data()
|
||||||
|
|
||||||
|
if diagnosis:
|
||||||
|
df = df[df['diagnosis'].str.contains(diagnosis, na=False, case=False)]
|
||||||
|
|
||||||
# Extract month
|
# Extract month
|
||||||
df = df.copy()
|
df = df.copy()
|
||||||
df['month'] = df['date'].dt.month
|
df['month'] = df['date'].dt.month
|
||||||
@@ -608,7 +635,25 @@ async def get_disease_seasonality():
|
|||||||
total=out_count + in_count,
|
total=out_count + in_count,
|
||||||
))
|
))
|
||||||
|
|
||||||
|
# Per-diagnosis district distribution (real aggregation by diagnosis × district).
|
||||||
|
# Previously the frontend showed the same "主要区域" for every diagnosis because
|
||||||
|
# no per-diagnosis district data was exposed. Top 3 districts per diagnosis.
|
||||||
|
df_districts = df_top[(df_top['district'] != '未知') & (df_top['district'] != '其他')]
|
||||||
|
diag_district_counts = df_districts.groupby(['diagnosis', 'district']).size()
|
||||||
|
diagnosis_districts: list[DiseaseDistrictItem] = []
|
||||||
|
for diag in top_diagnoses:
|
||||||
|
if diag not in diag_district_counts.index.get_level_values('diagnosis'):
|
||||||
|
continue
|
||||||
|
top_d = diag_district_counts.loc[diag].sort_values(ascending=False).head(3)
|
||||||
|
for district_name, count in top_d.items():
|
||||||
|
diagnosis_districts.append(DiseaseDistrictItem(
|
||||||
|
diagnosis=str(diag),
|
||||||
|
district=str(district_name),
|
||||||
|
total=int(count),
|
||||||
|
))
|
||||||
|
|
||||||
return DiseaseSeasonalityResponse(
|
return DiseaseSeasonalityResponse(
|
||||||
seasonality=seasonality,
|
seasonality=seasonality,
|
||||||
diagnoses=[str(d) for d in top_diagnoses],
|
diagnoses=[str(d) for d in top_diagnoses],
|
||||||
|
diagnosis_districts=diagnosis_districts,
|
||||||
)
|
)
|
||||||
|
|||||||
325
backend/routers/environment.py
Normal file
325
backend/routers/environment.py
Normal file
@@ -0,0 +1,325 @@
|
|||||||
|
"""
|
||||||
|
环境数据 API 路由
|
||||||
|
|
||||||
|
提供空气污染物时间序列和滞后相关性分析接口
|
||||||
|
"""
|
||||||
|
|
||||||
|
import logging
|
||||||
|
from pathlib import Path
|
||||||
|
from typing import Optional
|
||||||
|
|
||||||
|
import pandas as pd
|
||||||
|
from fastapi import APIRouter, HTTPException, Query
|
||||||
|
from pydantic import BaseModel
|
||||||
|
from scipy.stats import pearsonr
|
||||||
|
|
||||||
|
logger = logging.getLogger("cbpoa.environment")
|
||||||
|
|
||||||
|
PROJECT_ROOT = Path(__file__).parent.parent.parent
|
||||||
|
|
||||||
|
router = APIRouter(prefix="/api/environment", tags=["environment"])
|
||||||
|
|
||||||
|
# 污染物列表(CO 无基值列,仅存在于 lag_features.parquet 的滞后列中)
|
||||||
|
POLLUTANTS = ["AQI", "PM2.5", "PM10", "SO2", "NO2", "O3"]
|
||||||
|
LAGS = [1, 2, 3, 5, 7, 14]
|
||||||
|
|
||||||
|
# station_daily 列名映射(PM25 无点号 -> PM2.5 带点号)
|
||||||
|
_STATION_COL_MAP: dict[str, str] | None = None
|
||||||
|
# lag_features 基值列映射
|
||||||
|
_LAG_BASE_MAP: dict[str, str] | None = None
|
||||||
|
|
||||||
|
|
||||||
|
# ============== Response Models ==============
|
||||||
|
|
||||||
|
|
||||||
|
class LagCorrelationItem(BaseModel):
|
||||||
|
pollutant: str # "AQI", "PM2.5", "PM10", "SO2", "NO2", "O3"
|
||||||
|
lag_days: int # 1, 2, 3, 5, 7, 14
|
||||||
|
correlation: float # Pearson r
|
||||||
|
|
||||||
|
|
||||||
|
class LagCorrelationResponse(BaseModel):
|
||||||
|
correlations: list[LagCorrelationItem]
|
||||||
|
data_note: str # "CO excluded - no base column in lag_features.parquet"
|
||||||
|
|
||||||
|
|
||||||
|
class PollutantPoint(BaseModel):
|
||||||
|
date: str
|
||||||
|
AQI: float
|
||||||
|
PM25: float
|
||||||
|
PM10: float
|
||||||
|
SO2: float
|
||||||
|
NO2: float
|
||||||
|
O3: float
|
||||||
|
CO: float
|
||||||
|
|
||||||
|
|
||||||
|
class PollutantResponse(BaseModel):
|
||||||
|
data: list[PollutantPoint]
|
||||||
|
station_count: int
|
||||||
|
date_range: dict # {start, end}
|
||||||
|
|
||||||
|
|
||||||
|
# ============== Helper Functions ==============
|
||||||
|
|
||||||
|
|
||||||
|
def _get_station_col_map() -> dict[str, str]:
|
||||||
|
"""返回 station_daily parquet 中实际列名到标准名称的映射。
|
||||||
|
|
||||||
|
station_daily 文件中 PM2.5 列名为 "PM25"(无点号),
|
||||||
|
需要映射到前端期望的 "PM2.5"。
|
||||||
|
"""
|
||||||
|
global _STATION_COL_MAP
|
||||||
|
if _STATION_COL_MAP is not None:
|
||||||
|
return _STATION_COL_MAP
|
||||||
|
|
||||||
|
path = PROJECT_ROOT / "processed" / "weather" / "station_daily_2022.parquet"
|
||||||
|
df = pd.read_parquet(path)
|
||||||
|
cols = set(df.columns)
|
||||||
|
|
||||||
|
col_map = {}
|
||||||
|
for standard in POLLUTANTS:
|
||||||
|
if standard in cols:
|
||||||
|
col_map[standard] = standard
|
||||||
|
elif "PM25" in cols and standard == "PM2.5":
|
||||||
|
col_map[standard] = "PM25"
|
||||||
|
else:
|
||||||
|
col_map[standard] = standard # fallback
|
||||||
|
|
||||||
|
# CO is in station_daily but not in POLLUTANTS
|
||||||
|
if "CO" in cols:
|
||||||
|
col_map["CO"] = "CO"
|
||||||
|
|
||||||
|
_STATION_COL_MAP = col_map
|
||||||
|
return _STATION_COL_MAP
|
||||||
|
|
||||||
|
|
||||||
|
def _get_lag_base_map() -> dict[str, str]:
|
||||||
|
"""返回 lag_features parquet 中基值列名到标准名称的映射。
|
||||||
|
|
||||||
|
lag_features 中 PM2.5 列名为 "PM2.5"(带点号),与标准名称一致。
|
||||||
|
此函数在运行时验证实际列名。
|
||||||
|
"""
|
||||||
|
global _LAG_BASE_MAP
|
||||||
|
if _LAG_BASE_MAP is not None:
|
||||||
|
return _LAG_BASE_MAP
|
||||||
|
|
||||||
|
path = PROJECT_ROOT / "processed" / "weather" / "lag_features.parquet"
|
||||||
|
df = pd.read_parquet(path)
|
||||||
|
cols = set(df.columns)
|
||||||
|
|
||||||
|
base_map = {}
|
||||||
|
for p in POLLUTANTS:
|
||||||
|
if p in cols:
|
||||||
|
base_map[p] = p
|
||||||
|
elif p == "PM2.5" and "PM25" in cols:
|
||||||
|
base_map[p] = "PM25"
|
||||||
|
else:
|
||||||
|
base_map[p] = p # will be checked later
|
||||||
|
|
||||||
|
_LAG_BASE_MAP = base_map
|
||||||
|
return _LAG_BASE_MAP
|
||||||
|
|
||||||
|
|
||||||
|
def _load_lag_features() -> pd.DataFrame:
|
||||||
|
"""加载 lag_features.parquet 并转换日期列。"""
|
||||||
|
path = PROJECT_ROOT / "processed" / "weather" / "lag_features.parquet"
|
||||||
|
if not path.exists():
|
||||||
|
raise FileNotFoundError(f"lag_features.parquet not found at {path}")
|
||||||
|
df = pd.read_parquet(path)
|
||||||
|
df["date"] = pd.to_datetime(df["date"])
|
||||||
|
return df
|
||||||
|
|
||||||
|
|
||||||
|
def _load_cases_daily() -> pd.DataFrame:
|
||||||
|
"""加载 cases_combined.parquet 并按日期汇总每日总病例数。"""
|
||||||
|
path = PROJECT_ROOT / "processed" / "cases_combined.parquet"
|
||||||
|
if not path.exists():
|
||||||
|
raise FileNotFoundError(f"cases_combined.parquet not found at {path}")
|
||||||
|
df = pd.read_parquet(path)
|
||||||
|
df["date"] = pd.to_datetime(df["date"])
|
||||||
|
daily = df.groupby("date").size().reset_index(name="total_cases")
|
||||||
|
return daily
|
||||||
|
|
||||||
|
|
||||||
|
def _load_station_daily() -> pd.DataFrame:
|
||||||
|
"""加载并合并 station_daily_2022.parquet 和 station_daily_2023.parquet。"""
|
||||||
|
dfs = []
|
||||||
|
for year in [2022, 2023]:
|
||||||
|
path = PROJECT_ROOT / "processed" / "weather" / f"station_daily_{year}.parquet"
|
||||||
|
if not path.exists():
|
||||||
|
logger.warning("station_daily_%s.parquet not found at %s", year, path)
|
||||||
|
continue
|
||||||
|
df = pd.read_parquet(path)
|
||||||
|
dfs.append(df)
|
||||||
|
if not dfs:
|
||||||
|
raise FileNotFoundError("No station_daily parquet files found")
|
||||||
|
combined = pd.concat(dfs, ignore_index=True)
|
||||||
|
combined["date"] = pd.to_datetime(combined["date"])
|
||||||
|
return combined
|
||||||
|
|
||||||
|
|
||||||
|
# ============== Endpoints ==============
|
||||||
|
|
||||||
|
|
||||||
|
@router.get("/lag-correlations", response_model=LagCorrelationResponse)
|
||||||
|
async def get_lag_correlations():
|
||||||
|
"""获取污染物滞后相关性分析数据。
|
||||||
|
|
||||||
|
计算各污染物在不同滞后天数(1, 2, 3, 5, 7, 14 天)下
|
||||||
|
与每日病例总数之间的 Pearson 相关系数。
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
LagCorrelationResponse: 包含 36 个相关系数(6 种污染物 × 6 个滞后天数)
|
||||||
|
"""
|
||||||
|
try:
|
||||||
|
lag_df = _load_lag_features()
|
||||||
|
cases_daily = _load_cases_daily()
|
||||||
|
except FileNotFoundError as e:
|
||||||
|
logger.warning("Data file not found for lag-correlations: %s", e)
|
||||||
|
return LagCorrelationResponse(
|
||||||
|
correlations=[],
|
||||||
|
data_note="CO excluded - no base column in lag_features.parquet",
|
||||||
|
)
|
||||||
|
|
||||||
|
# 计算每日全市均值(按日期聚合,对 23 个站点取平均)
|
||||||
|
base_map = _get_lag_base_map()
|
||||||
|
mean_cols = {p: base_map.get(p, p) for p in POLLUTANTS}
|
||||||
|
daily_mean = lag_df.groupby("date")[list(mean_cols.values())].mean().reset_index()
|
||||||
|
|
||||||
|
# 重命名列为标准名称以便一致访问
|
||||||
|
rename_map = {v: k for k, v in mean_cols.items() if v != k}
|
||||||
|
if rename_map:
|
||||||
|
daily_mean = daily_mean.rename(columns=rename_map)
|
||||||
|
|
||||||
|
# 对每种污染物计算每日均值
|
||||||
|
# 对齐污染物时间序列与病例数据
|
||||||
|
merged = daily_mean.merge(cases_daily, on="date", how="inner")
|
||||||
|
merged = merged.sort_values("date")
|
||||||
|
|
||||||
|
results: list[LagCorrelationItem] = []
|
||||||
|
|
||||||
|
for pollutant in POLLUTANTS:
|
||||||
|
# 构建滞后列映射(lag_features 文件名用点号 "PM2.5")
|
||||||
|
lag_base_name = base_map.get(pollutant, pollutant)
|
||||||
|
|
||||||
|
for lag in LAGS:
|
||||||
|
lag_col = f"{lag_base_name}_lag{lag}"
|
||||||
|
if lag_col not in lag_df.columns:
|
||||||
|
logger.debug("Lag column %s not found, skipping", lag_col)
|
||||||
|
continue
|
||||||
|
|
||||||
|
# 从原始 lag_features 提取该污染物的滞后数据(按日期取全市均值)
|
||||||
|
lag_series = lag_df.groupby("date")[lag_col].mean().reset_index()
|
||||||
|
lag_series = lag_series.rename(columns={lag_col: f"{pollutant}_lag{lag}"})
|
||||||
|
|
||||||
|
# 将滞后污染物数据与病例数据对齐
|
||||||
|
# 滞后列的值代表的是 t-lag 时刻的污染物,病例是 t 时刻
|
||||||
|
# 所以将 lag 列的时间向后平移 lag 天,使其与病例时间对齐
|
||||||
|
lag_series["align_date"] = lag_series["date"] + pd.Timedelta(days=lag)
|
||||||
|
combined = lag_series.merge(
|
||||||
|
cases_daily, left_on="align_date", right_on="date", how="inner"
|
||||||
|
)
|
||||||
|
|
||||||
|
if len(combined) < 10:
|
||||||
|
logger.debug(
|
||||||
|
"Insufficient data for %s lag%d: %d rows, skipping",
|
||||||
|
pollutant,
|
||||||
|
lag,
|
||||||
|
len(combined),
|
||||||
|
)
|
||||||
|
continue
|
||||||
|
|
||||||
|
# 计算 Pearson 相关系数
|
||||||
|
r, _ = pearsonr(
|
||||||
|
combined[f"{pollutant}_lag{lag}"], combined["total_cases"]
|
||||||
|
)
|
||||||
|
results.append(
|
||||||
|
LagCorrelationItem(
|
||||||
|
pollutant=pollutant, lag_days=lag, correlation=round(float(r), 4)
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
|
return LagCorrelationResponse(
|
||||||
|
correlations=results,
|
||||||
|
data_note="CO excluded - no base column in lag_features.parquet",
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
@router.get("/pollutants", response_model=PollutantResponse)
|
||||||
|
async def get_pollutants(
|
||||||
|
days: Optional[int] = Query(default=30, ge=1, le=730, description="返回最近 N 天的数据"),
|
||||||
|
start_date: Optional[str] = Query(default=None, description="开始日期 YYYY-MM-DD"),
|
||||||
|
end_date: Optional[str] = Query(default=None, description="结束日期 YYYY-MM-DD"),
|
||||||
|
):
|
||||||
|
"""获取每日全市均值污染物时间序列。
|
||||||
|
|
||||||
|
合并 2022/2023 两个年度的站点日数据,按日期聚合所有站点取均值。
|
||||||
|
|
||||||
|
可通过 ?days=N(默认 30)或 ?start_date=YYYY-MM-DD&end_date=YYYY-MM-DD 筛选日期范围。
|
||||||
|
"""
|
||||||
|
try:
|
||||||
|
df = _load_station_daily()
|
||||||
|
except FileNotFoundError as e:
|
||||||
|
logger.warning("Data file not found for pollutants: %s", e)
|
||||||
|
return PollutantResponse(data=[], station_count=0, date_range={})
|
||||||
|
|
||||||
|
station_count = df["station_id"].nunique()
|
||||||
|
|
||||||
|
# 获取列名映射
|
||||||
|
col_map = _get_station_col_map()
|
||||||
|
|
||||||
|
# 选择污染物列
|
||||||
|
poll_cols = [col_map.get(p, p) for p in POLLUTANTS] + ["CO"]
|
||||||
|
# 确保需要的列都存在
|
||||||
|
available_cols = [c for c in poll_cols if c in df.columns]
|
||||||
|
|
||||||
|
# 按日期聚合取均值
|
||||||
|
daily_mean = df.groupby("date")[available_cols].mean().reset_index()
|
||||||
|
|
||||||
|
# 重命名为标准名称
|
||||||
|
rename_map = {}
|
||||||
|
for std_name, actual_name in col_map.items():
|
||||||
|
if actual_name != std_name and actual_name in daily_mean.columns:
|
||||||
|
rename_map[actual_name] = std_name
|
||||||
|
if rename_map:
|
||||||
|
daily_mean = daily_mean.rename(columns=rename_map)
|
||||||
|
|
||||||
|
# 日期筛选
|
||||||
|
if start_date and end_date:
|
||||||
|
start_dt = pd.to_datetime(start_date)
|
||||||
|
end_dt = pd.to_datetime(end_date)
|
||||||
|
date_mask = (daily_mean["date"] >= start_dt) & (
|
||||||
|
daily_mean["date"] <= end_dt
|
||||||
|
)
|
||||||
|
daily_mean = daily_mean[date_mask].copy()
|
||||||
|
else:
|
||||||
|
daily_mean = daily_mean.sort_values("date").tail(days)
|
||||||
|
|
||||||
|
daily_mean = daily_mean.sort_values("date")
|
||||||
|
|
||||||
|
# 构建响应
|
||||||
|
data: list[PollutantPoint] = []
|
||||||
|
for _, row in daily_mean.iterrows():
|
||||||
|
pt = PollutantPoint(
|
||||||
|
date=row["date"].strftime("%Y-%m-%d"),
|
||||||
|
AQI=round(float(row.get("AQI", 0)), 2),
|
||||||
|
PM25=round(float(row.get("PM2.5", 0)), 2),
|
||||||
|
PM10=round(float(row.get("PM10", 0)), 2),
|
||||||
|
SO2=round(float(row.get("SO2", 0)), 2),
|
||||||
|
NO2=round(float(row.get("NO2", 0)), 2),
|
||||||
|
O3=round(float(row.get("O3", 0)), 2),
|
||||||
|
CO=round(float(row.get("CO", 0)), 2),
|
||||||
|
)
|
||||||
|
data.append(pt)
|
||||||
|
|
||||||
|
date_range = {}
|
||||||
|
if daily_mean.shape[0] > 0:
|
||||||
|
date_range = {
|
||||||
|
"start": daily_mean["date"].iloc[0].strftime("%Y-%m-%d"),
|
||||||
|
"end": daily_mean["date"].iloc[-1].strftime("%Y-%m-%d"),
|
||||||
|
}
|
||||||
|
|
||||||
|
return PollutantResponse(
|
||||||
|
data=data, station_count=station_count, date_range=date_range
|
||||||
|
)
|
||||||
@@ -17,7 +17,7 @@ PROJECT_ROOT = Path(__file__).parent.parent.parent
|
|||||||
DATA_DIR = PROJECT_ROOT / "outputs"
|
DATA_DIR = PROJECT_ROOT / "outputs"
|
||||||
|
|
||||||
|
|
||||||
@lru_cache(maxsize=1)
|
@lru_cache(maxsize=4)
|
||||||
def _load_csv(path: Path) -> pd.DataFrame:
|
def _load_csv(path: Path) -> pd.DataFrame:
|
||||||
return pd.read_csv(path)
|
return pd.read_csv(path)
|
||||||
|
|
||||||
@@ -109,19 +109,21 @@ async def get_grid_cases():
|
|||||||
async def get_geocoded_cases(
|
async def get_geocoded_cases(
|
||||||
limit: int = 1000,
|
limit: int = 1000,
|
||||||
district: Optional[str] = None,
|
district: Optional[str] = None,
|
||||||
|
date: Optional[str] = Query(None, description="Filter by date (YYYY-MM-DD)"),
|
||||||
):
|
):
|
||||||
"""
|
"""
|
||||||
Get individual geocoded case data.
|
Get individual geocoded case data.
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
limit: Maximum number of cases to return (for performance)
|
limit: Maximum number of cases to return (for performance)
|
||||||
district: Filter by district name
|
district: Filter by district name
|
||||||
|
date: Filter by specific date
|
||||||
"""
|
"""
|
||||||
cases_file = DATA_DIR / "geocoded_all_cases.csv"
|
cases_file = DATA_DIR / "geocoded_all_cases.csv"
|
||||||
|
|
||||||
if not cases_file.exists():
|
if not cases_file.exists():
|
||||||
raise HTTPException(status_code=404, detail="Geocoded data not found")
|
raise HTTPException(status_code=404, detail="Geocoded data not found")
|
||||||
|
|
||||||
try:
|
try:
|
||||||
df = _load_csv(cases_file)
|
df = _load_csv(cases_file)
|
||||||
|
|
||||||
@@ -132,6 +134,11 @@ async def get_geocoded_cases(
|
|||||||
swapped = df['latitude'] > 50 # longitude values are >113
|
swapped = df['latitude'] > 50 # longitude values are >113
|
||||||
df.loc[swapped, ['latitude', 'longitude']] = df.loc[swapped, ['longitude', 'latitude']].values
|
df.loc[swapped, ['latitude', 'longitude']] = df.loc[swapped, ['longitude', 'latitude']].values
|
||||||
|
|
||||||
|
# Filter by date if specified
|
||||||
|
if date and 'date' in df.columns:
|
||||||
|
df['date_str'] = pd.to_datetime(df['date']).dt.strftime('%Y-%m-%d')
|
||||||
|
df = df[df['date_str'] == date]
|
||||||
|
|
||||||
# Filter by district if specified
|
# Filter by district if specified
|
||||||
if district:
|
if district:
|
||||||
df = df[df['district'] == district]
|
df = df[df['district'] == district]
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
from fastapi import APIRouter, HTTPException, Query
|
import asyncio
|
||||||
|
from fastapi import APIRouter, HTTPException, Query, Response
|
||||||
from datetime import datetime, timedelta
|
from datetime import datetime, timedelta
|
||||||
from functools import lru_cache
|
from functools import lru_cache
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
@@ -6,6 +7,7 @@ from typing import Optional
|
|||||||
import logging
|
import logging
|
||||||
import sys
|
import sys
|
||||||
import math
|
import math
|
||||||
|
import pandas as pd
|
||||||
|
|
||||||
PROJECT_ROOT = Path(__file__).parent.parent.parent
|
PROJECT_ROOT = Path(__file__).parent.parent.parent
|
||||||
sys.path.insert(0, str(PROJECT_ROOT))
|
sys.path.insert(0, str(PROJECT_ROOT))
|
||||||
@@ -19,49 +21,35 @@ from models import (
|
|||||||
MultiDayPredictionRequest,
|
MultiDayPredictionRequest,
|
||||||
MultiDayPredictionResponse,
|
MultiDayPredictionResponse,
|
||||||
)
|
)
|
||||||
|
from data.case_loader import load_cases_by_district_daily
|
||||||
|
|
||||||
router = APIRouter(prefix="/api", tags=["grid"])
|
router = APIRouter(prefix="/api", tags=["grid"])
|
||||||
|
|
||||||
|
logger = logging.getLogger("cbpoa.grid")
|
||||||
|
|
||||||
_parquet_cache: dict[str, "pd.DataFrame"] = {}
|
_parquet_cache: dict[str, pd.DataFrame] = {}
|
||||||
|
|
||||||
def _load_parquet(path: Path) -> "pd.DataFrame":
|
def _load_parquet(path: Path) -> pd.DataFrame:
|
||||||
import pandas as pd
|
|
||||||
key = str(path)
|
key = str(path)
|
||||||
if key not in _parquet_cache:
|
if key not in _parquet_cache:
|
||||||
_parquet_cache[key] = pd.read_parquet(path)
|
_parquet_cache[key] = pd.read_parquet(path)
|
||||||
return _parquet_cache[key]
|
return _parquet_cache[key]
|
||||||
|
|
||||||
|
|
||||||
@router.get("/history/aggregated", response_model=HistoricalAggregationResponse)
|
def _compute_historical_aggregation(
|
||||||
async def get_historical_aggregated(
|
start: datetime,
|
||||||
start_date: str = Query(..., description="Start date (YYYY-MM-DD)"),
|
end: datetime,
|
||||||
end_date: str = Query(..., description="End date (YYYY-MM-DD)"),
|
aggregation: str,
|
||||||
aggregation: str = Query("daily", description="Aggregation level: daily, weekly, monthly"),
|
district: Optional[str],
|
||||||
district: Optional[str] = Query(None, description="Filter by district name"),
|
) -> HistoricalAggregationResponse:
|
||||||
):
|
"""Run the full pandas aggregation pipeline (called in thread pool)."""
|
||||||
"""
|
|
||||||
Historical data aggregation API.
|
|
||||||
|
|
||||||
Returns aggregated case and weather data by district and date.
|
|
||||||
"""
|
|
||||||
try:
|
try:
|
||||||
start = datetime.strptime(start_date, "%Y-%m-%d")
|
cases_df = load_cases_by_district_daily()
|
||||||
end = datetime.strptime(end_date, "%Y-%m-%d")
|
|
||||||
except ValueError:
|
|
||||||
raise HTTPException(status_code=400, detail="Invalid date format. Use YYYY-MM-DD")
|
|
||||||
|
|
||||||
if (end - start).days > 365:
|
|
||||||
raise HTTPException(status_code=400, detail="Date range exceeds 365 days")
|
|
||||||
|
|
||||||
import pandas as pd
|
|
||||||
|
|
||||||
try:
|
|
||||||
cases_df = _load_parquet(PROJECT_ROOT / "processed" / "cases_by_district_daily.parquet")
|
|
||||||
except FileNotFoundError:
|
except FileNotFoundError:
|
||||||
return HistoricalAggregationResponse(
|
return HistoricalAggregationResponse(
|
||||||
aggregations=[], total_records=0,
|
aggregations=[], total_records=0,
|
||||||
date_range=(start_date, end_date), timestamp=datetime.now().isoformat(),
|
date_range=(start.strftime("%Y-%m-%d"), end.strftime("%Y-%m-%d")),
|
||||||
|
timestamp=datetime.now().isoformat(),
|
||||||
)
|
)
|
||||||
cases_df['date'] = pd.to_datetime(cases_df['date'])
|
cases_df['date'] = pd.to_datetime(cases_df['date'])
|
||||||
|
|
||||||
@@ -76,6 +64,7 @@ async def get_historical_aggregated(
|
|||||||
]
|
]
|
||||||
|
|
||||||
if aggregation == "weekly":
|
if aggregation == "weekly":
|
||||||
|
filtered_cases = filtered_cases.copy()
|
||||||
filtered_cases['period'] = filtered_cases['date'].dt.to_period('W').astype(str)
|
filtered_cases['period'] = filtered_cases['date'].dt.to_period('W').astype(str)
|
||||||
grouped = filtered_cases.groupby(['period', 'district']).agg({
|
grouped = filtered_cases.groupby(['period', 'district']).agg({
|
||||||
'total_cases': 'sum',
|
'total_cases': 'sum',
|
||||||
@@ -84,6 +73,7 @@ async def get_historical_aggregated(
|
|||||||
}).reset_index()
|
}).reset_index()
|
||||||
grouped['date'] = grouped['period']
|
grouped['date'] = grouped['period']
|
||||||
elif aggregation == "monthly":
|
elif aggregation == "monthly":
|
||||||
|
filtered_cases = filtered_cases.copy()
|
||||||
filtered_cases['period'] = filtered_cases['date'].dt.to_period('M').astype(str)
|
filtered_cases['period'] = filtered_cases['date'].dt.to_period('M').astype(str)
|
||||||
grouped = filtered_cases.groupby(['period', 'district']).agg({
|
grouped = filtered_cases.groupby(['period', 'district']).agg({
|
||||||
'total_cases': 'sum',
|
'total_cases': 'sum',
|
||||||
@@ -98,7 +88,8 @@ async def get_historical_aggregated(
|
|||||||
try:
|
try:
|
||||||
weather_df = _load_parquet(PROJECT_ROOT / "processed" / "weather" / "station_daily_2022.parquet")
|
weather_df = _load_parquet(PROJECT_ROOT / "processed" / "weather" / "station_daily_2022.parquet")
|
||||||
except FileNotFoundError:
|
except FileNotFoundError:
|
||||||
weather_df = pd.DataFrame(columns=['date', 'AQI', 'PM25', 'PM10'])
|
weather_df = pd.DataFrame({'date': pd.Series(dtype='str'), 'AQI': pd.Series(dtype='float64'), 'PM25': pd.Series(dtype='float64'), 'PM10': pd.Series(dtype='float64')})
|
||||||
|
weather_df = weather_df.copy()
|
||||||
weather_df['date'] = pd.to_datetime(weather_df['date']).dt.strftime('%Y-%m-%d')
|
weather_df['date'] = pd.to_datetime(weather_df['date']).dt.strftime('%Y-%m-%d')
|
||||||
|
|
||||||
# Weather data doesn't have district - aggregate by date only
|
# Weather data doesn't have district - aggregate by date only
|
||||||
@@ -114,135 +105,173 @@ async def get_historical_aggregated(
|
|||||||
aggregations = []
|
aggregations = []
|
||||||
for _, row in merged.iterrows():
|
for _, row in merged.iterrows():
|
||||||
aggregations.append(DistrictAggregation(
|
aggregations.append(DistrictAggregation(
|
||||||
district=row['district'],
|
district=str(row['district']),
|
||||||
date=str(row['date']),
|
date=str(row['date']),
|
||||||
total_cases=int(row['total_cases']),
|
total_cases=int(row['total_cases']),
|
||||||
outpatient_count=int(row['outpatient_count']),
|
outpatient_count=int(row['outpatient_count']),
|
||||||
inpatient_count=int(row['inpatient_count']),
|
inpatient_count=int(row['inpatient_count']),
|
||||||
avg_AQI=float(row['AQI']) if pd.notna(row['AQI']) else 0.0,
|
avg_AQI=float(row['AQI']) if bool(pd.notna(row['AQI'])) else 0.0,
|
||||||
avg_PM25=float(row['PM25']) if pd.notna(row['PM25']) else 0.0,
|
avg_PM25=float(row['PM25']) if bool(pd.notna(row['PM25'])) else 0.0,
|
||||||
avg_PM10=float(row['PM10']) if pd.notna(row['PM10']) else 0.0,
|
avg_PM10=float(row['PM10']) if bool(pd.notna(row['PM10'])) else 0.0,
|
||||||
))
|
))
|
||||||
|
|
||||||
return HistoricalAggregationResponse(
|
return HistoricalAggregationResponse(
|
||||||
aggregations=aggregations,
|
aggregations=aggregations,
|
||||||
total_records=len(aggregations),
|
total_records=len(aggregations),
|
||||||
date_range=(start_date, end_date),
|
date_range=(start.strftime("%Y-%m-%d"), end.strftime("%Y-%m-%d")),
|
||||||
timestamp=datetime.now().isoformat(),
|
timestamp=datetime.now().isoformat(),
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
@router.get("/history/aggregated", response_model=HistoricalAggregationResponse)
|
||||||
|
async def get_historical_aggregated(
|
||||||
|
start_date: str = Query(..., description="Start date (YYYY-MM-DD)"),
|
||||||
|
end_date: str = Query(..., description="End date (YYYY-MM-DD)"),
|
||||||
|
aggregation: str = Query("daily", description="Aggregation level: daily, weekly, monthly"),
|
||||||
|
district: Optional[str] = Query(None, description="Filter by district name"),
|
||||||
|
):
|
||||||
|
"""
|
||||||
|
Historical data aggregation API.
|
||||||
|
|
||||||
|
Returns aggregated case and weather data by district and date.
|
||||||
|
Pandas processing runs in a thread pool to avoid blocking the async event loop.
|
||||||
|
"""
|
||||||
|
try:
|
||||||
|
start = datetime.strptime(start_date, "%Y-%m-%d")
|
||||||
|
end = datetime.strptime(end_date, "%Y-%m-%d")
|
||||||
|
except ValueError:
|
||||||
|
raise HTTPException(status_code=400, detail="Invalid date format. Use YYYY-MM-DD")
|
||||||
|
|
||||||
|
if (end - start).days > 365:
|
||||||
|
raise HTTPException(status_code=400, detail="Date range exceeds 365 days")
|
||||||
|
|
||||||
|
# Offload all pandas I/O and processing to a thread pool
|
||||||
|
# to prevent blocking the async event loop
|
||||||
|
return await asyncio.to_thread(
|
||||||
|
_compute_historical_aggregation, start, end, aggregation, district
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
@lru_cache(maxsize=1)
|
||||||
|
def _grid_geojson_base():
|
||||||
|
"""Date-independent base merge: grid centroid + district + real population.
|
||||||
|
|
||||||
|
Merged once and cached (the source frames are ~1M rows each, so the join
|
||||||
|
must not run per request). Raises FileNotFoundError if the core grid files
|
||||||
|
are missing (caller handles it).
|
||||||
|
"""
|
||||||
|
import pandas as pd
|
||||||
|
grid_df = _load_parquet(PROJECT_ROOT / "processed" / "grid_100m_index.parquet")
|
||||||
|
district_map = _load_parquet(PROJECT_ROOT / "processed" / "grid_district_mapping.parquet")
|
||||||
|
base = grid_df.merge(district_map, on='grid_id', how='left')
|
||||||
|
try:
|
||||||
|
pop_df = _load_parquet(PROJECT_ROOT / "processed" / "grid_100m_with_dem_pop.parquet")
|
||||||
|
base = base.merge(pop_df[['grid_id', 'population_density']], on='grid_id', how='left')
|
||||||
|
except FileNotFoundError:
|
||||||
|
base['population_density'] = 0.0
|
||||||
|
base['population_density'] = base['population_density'].fillna(0.0)
|
||||||
|
return base
|
||||||
|
|
||||||
|
|
||||||
|
def _risk_level_of(v: float) -> str:
|
||||||
|
if v >= 0.7:
|
||||||
|
return "high"
|
||||||
|
if v >= 0.5:
|
||||||
|
return "medium"
|
||||||
|
if v >= 0.3:
|
||||||
|
return "medium_low"
|
||||||
|
return "low"
|
||||||
|
|
||||||
|
|
||||||
|
@lru_cache(maxsize=32)
|
||||||
|
def _grids_geojson_body(date: str, district: Optional[str], risk_level: Optional[str]) -> str:
|
||||||
|
"""Build + serialize the grid GeoJSON once per (date, district, risk_level).
|
||||||
|
|
||||||
|
Risk is computed vectorised over the full grid (no per-row Python loop) and
|
||||||
|
the highest-risk grids are returned as hotspots, so the map shows real
|
||||||
|
high→low variation. Cached, so warm calls are near-instant. Raises
|
||||||
|
FileNotFoundError if the core grid files are missing.
|
||||||
|
"""
|
||||||
|
merged = _grid_geojson_base()
|
||||||
|
if district:
|
||||||
|
merged = merged[merged['district_name'].str.contains(district.replace('区', ''), na=False, regex=False)]
|
||||||
|
|
||||||
|
cases_df = load_cases_by_district_daily()
|
||||||
|
cases_df['date'] = pd.to_datetime(cases_df['date']).dt.strftime('%Y-%m-%d')
|
||||||
|
cases_df = cases_df[cases_df['date'] == date]
|
||||||
|
|
||||||
|
# Normalise district case load to 0..1 across districts for this date.
|
||||||
|
max_district_cases = float(cases_df['total_cases'].max()) if len(cases_df) else 0.0
|
||||||
|
if max_district_cases <= 0:
|
||||||
|
max_district_cases = 1.0
|
||||||
|
|
||||||
|
merged = merged.merge(cases_df[['district', 'total_cases']], left_on='district_name', right_on='district', how='left')
|
||||||
|
merged = merged.copy()
|
||||||
|
merged['total_cases'] = merged['total_cases'].fillna(0).astype(int)
|
||||||
|
merged['center_lon'] = pd.to_numeric(merged['center_lon'], errors='coerce').fillna(0.0)
|
||||||
|
merged['center_lat'] = pd.to_numeric(merged['center_lat'], errors='coerce').fillna(0.0)
|
||||||
|
merged['population_density'] = merged['population_density'].fillna(0.0).clip(lower=0.0)
|
||||||
|
|
||||||
|
# Drop grids without coordinates.
|
||||||
|
merged = merged[(merged['center_lon'] != 0.0) | (merged['center_lat'] != 0.0)]
|
||||||
|
|
||||||
|
# Demo risk model (vectorised): a district's relative case load × each grid's
|
||||||
|
# own population exposure. Sparse cells stay low; densely-populated cells in
|
||||||
|
# high-case districts rise toward 1.0.
|
||||||
|
district_load = (merged['total_cases'] / max_district_cases).clip(upper=1.0)
|
||||||
|
pop_factor = (merged['population_density'] / 50.0).clip(upper=1.0)
|
||||||
|
merged['risk_value'] = (0.1 + 0.85 * district_load * pop_factor).clip(upper=1.0).round(3)
|
||||||
|
|
||||||
|
# Show the highest-risk grids (hotspots), not arbitrary cells.
|
||||||
|
merged = merged.nlargest(10000, 'risk_value')
|
||||||
|
|
||||||
|
features = []
|
||||||
|
for rec in merged.to_dict('records'):
|
||||||
|
rv = float(rec['risk_value'])
|
||||||
|
lvl = _risk_level_of(rv)
|
||||||
|
if risk_level and lvl != risk_level:
|
||||||
|
continue
|
||||||
|
name = rec.get('district_name')
|
||||||
|
if not isinstance(name, str):
|
||||||
|
name = "未知"
|
||||||
|
lon = round(float(rec['center_lon']), 6)
|
||||||
|
lat = round(float(rec['center_lat']), 6)
|
||||||
|
features.append({
|
||||||
|
"type": "Feature",
|
||||||
|
"geometry": {"type": "Point", "coordinates": [lon, lat]},
|
||||||
|
"properties": {
|
||||||
|
"grid_id": str(rec.get('grid_id', '')),
|
||||||
|
"latitude": lat,
|
||||||
|
"longitude": lon,
|
||||||
|
"district": name,
|
||||||
|
"total_cases": int(rec.get('total_cases', 0)),
|
||||||
|
"population_density": round(float(rec.get('population_density', 0.0)), 2),
|
||||||
|
"risk_value": rv,
|
||||||
|
"risk_level": lvl,
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
return GridGeoJSONResponse(
|
||||||
|
type="FeatureCollection",
|
||||||
|
features=features,
|
||||||
|
timestamp=datetime.now().isoformat(),
|
||||||
|
).model_dump_json()
|
||||||
|
|
||||||
|
|
||||||
@router.get("/grids/geojson", response_model=GridGeoJSONResponse)
|
@router.get("/grids/geojson", response_model=GridGeoJSONResponse)
|
||||||
async def get_grids_geojson(
|
async def get_grids_geojson(
|
||||||
date: str = Query(..., description="Date (YYYY-MM-DD)"),
|
date: str = Query(..., description="Date (YYYY-MM-DD)"),
|
||||||
district: Optional[str] = Query(None, description="Filter by district"),
|
district: Optional[str] = Query(None, description="Filter by district"),
|
||||||
risk_level: Optional[str] = Query(None, description="Filter by risk level"),
|
risk_level: Optional[str] = Query(None, description="Filter by risk level"),
|
||||||
):
|
):
|
||||||
"""
|
"""Get grid data as GeoJSON for map visualization (cached per query)."""
|
||||||
Get grid data as GeoJSON for map visualization.
|
|
||||||
"""
|
|
||||||
import pandas as pd
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
grid_df = _load_parquet(PROJECT_ROOT / "processed" / "grid_100m_index.parquet")
|
# Offload the parquet merges + vectorised compute to a thread so the
|
||||||
|
# cold-cache build doesn't block the event loop.
|
||||||
|
body = await asyncio.to_thread(_grids_geojson_body, date, district, risk_level)
|
||||||
except FileNotFoundError:
|
except FileNotFoundError:
|
||||||
return GridGeoJSONResponse(type="FeatureCollection", features=[], timestamp=datetime.now().isoformat())
|
return GridGeoJSONResponse(type="FeatureCollection", features=[], timestamp=datetime.now().isoformat())
|
||||||
|
return Response(content=body, media_type="application/json")
|
||||||
try:
|
|
||||||
district_map = _load_parquet(PROJECT_ROOT / "processed" / "grid_district_mapping.parquet")
|
|
||||||
except FileNotFoundError:
|
|
||||||
return GridGeoJSONResponse(type="FeatureCollection", features=[], timestamp=datetime.now().isoformat())
|
|
||||||
|
|
||||||
merged = grid_df.merge(district_map, on='grid_id', how='left')
|
|
||||||
|
|
||||||
if district:
|
|
||||||
merged = merged[merged['district_name'].str.contains(district.replace('区', ''), na=False, regex=False)]
|
|
||||||
|
|
||||||
try:
|
|
||||||
cases_df = _load_parquet(PROJECT_ROOT / "processed" / "cases_by_district_daily.parquet")
|
|
||||||
except FileNotFoundError:
|
|
||||||
return GridGeoJSONResponse(type="FeatureCollection", features=[], timestamp=datetime.now().isoformat())
|
|
||||||
cases_df['date'] = pd.to_datetime(cases_df['date']).dt.strftime('%Y-%m-%d')
|
|
||||||
|
|
||||||
cases_df = cases_df[cases_df['date'] == date]
|
|
||||||
|
|
||||||
merged = merged.merge(cases_df, left_on='district_name', right_on='district', how='left')
|
|
||||||
merged['total_cases'] = merged['total_cases'].fillna(0).astype(int)
|
|
||||||
|
|
||||||
def safe_float(val, default=0.0):
|
|
||||||
try:
|
|
||||||
v = float(val)
|
|
||||||
return default if math.isnan(v) or math.isinf(v) else v
|
|
||||||
except (TypeError, ValueError):
|
|
||||||
return default
|
|
||||||
|
|
||||||
def sanitize(obj):
|
|
||||||
"""Replace NaN/Inf with None for JSON serialization."""
|
|
||||||
if isinstance(obj, float):
|
|
||||||
if math.isnan(obj) or math.isinf(obj):
|
|
||||||
return None
|
|
||||||
return obj
|
|
||||||
if isinstance(obj, dict):
|
|
||||||
return {k: sanitize(v) for k, v in obj.items()}
|
|
||||||
if isinstance(obj, list):
|
|
||||||
return [sanitize(v) for v in obj]
|
|
||||||
return obj
|
|
||||||
|
|
||||||
features = []
|
|
||||||
for _, row in merged.iterrows():
|
|
||||||
lon = safe_float(row.get('center_lon'))
|
|
||||||
lat = safe_float(row.get('center_lat'))
|
|
||||||
if lon == 0.0 and lat == 0.0:
|
|
||||||
continue
|
|
||||||
|
|
||||||
# MVP: Simple risk calculation based on cases and population density
|
|
||||||
total_cases = safe_float(row.get('total_cases', 0), 0)
|
|
||||||
total_cases = int(total_cases)
|
|
||||||
pop_density = safe_float(row.get('population_density', 0))
|
|
||||||
|
|
||||||
# Risk formula: cases per 10k population + baseline
|
|
||||||
risk_value = min(1.0, (total_cases / max(pop_density, 1)) * 10 + 0.1)
|
|
||||||
|
|
||||||
if risk_value >= 0.7:
|
|
||||||
risk_level = "high"
|
|
||||||
elif risk_value >= 0.5:
|
|
||||||
risk_level = "medium"
|
|
||||||
elif risk_value >= 0.3:
|
|
||||||
risk_level = "medium_low"
|
|
||||||
else:
|
|
||||||
risk_level = "low"
|
|
||||||
|
|
||||||
district = row.get('district_name')
|
|
||||||
if isinstance(district, float) and (math.isnan(district) or math.isinf(district)):
|
|
||||||
district = "未知"
|
|
||||||
|
|
||||||
feature = {
|
|
||||||
"type": "Feature",
|
|
||||||
"geometry": {
|
|
||||||
"type": "Point",
|
|
||||||
"coordinates": [lon, lat]
|
|
||||||
},
|
|
||||||
"properties": {
|
|
||||||
"grid_id": str(row.get('grid_id', '')),
|
|
||||||
"latitude": lat,
|
|
||||||
"longitude": lon,
|
|
||||||
"district": district,
|
|
||||||
"total_cases": total_cases,
|
|
||||||
"population_density": pop_density,
|
|
||||||
"risk_value": round(risk_value, 3),
|
|
||||||
"risk_level": risk_level,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
features.append(feature)
|
|
||||||
|
|
||||||
if len(features) >= 10000:
|
|
||||||
break
|
|
||||||
|
|
||||||
return GridGeoJSONResponse(
|
|
||||||
type="FeatureCollection",
|
|
||||||
features=features,
|
|
||||||
timestamp=datetime.now().isoformat(),
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
@router.post("/predict/multi-day", response_model=MultiDayPredictionResponse)
|
@router.post("/predict/multi-day", response_model=MultiDayPredictionResponse)
|
||||||
@@ -278,9 +307,9 @@ async def predict_multi_day(request: MultiDayPredictionRequest):
|
|||||||
]
|
]
|
||||||
|
|
||||||
for _, row in features_df.iterrows():
|
for _, row in features_df.iterrows():
|
||||||
risk_1d = float(row.get('risk_1day', 0.5))
|
risk_1d = float(row.get('risk_1day', 0.5)) # type: ignore[arg-type]
|
||||||
risk_3d = float(row.get('risk_3day', 0.5))
|
risk_3d = float(row.get('risk_3day', 0.5)) # type: ignore[arg-type]
|
||||||
risk_7d = float(row.get('risk_7day', 0.5))
|
risk_7d = float(row.get('risk_7day', 0.5)) # type: ignore[arg-type]
|
||||||
|
|
||||||
if risk_1d >= 0.8:
|
if risk_1d >= 0.8:
|
||||||
risk_level = "high"
|
risk_level = "high"
|
||||||
@@ -323,14 +352,8 @@ async def predict_multi_day(request: MultiDayPredictionRequest):
|
|||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
@router.get("/grids/{grid_id}/history")
|
def _compute_grid_history(grid_id: str, days: int) -> dict:
|
||||||
async def get_grid_history(
|
"""Heavy synchronous parquet reads + per-row loop (called in thread pool)."""
|
||||||
grid_id: str,
|
|
||||||
days: int = Query(30, ge=1, le=365, description="Number of days of history"),
|
|
||||||
):
|
|
||||||
"""
|
|
||||||
Get historical data for a specific grid cell.
|
|
||||||
"""
|
|
||||||
import pandas as pd
|
import pandas as pd
|
||||||
|
|
||||||
district_map = _load_parquet(PROJECT_ROOT / "processed" / "grid_district_mapping.parquet")
|
district_map = _load_parquet(PROJECT_ROOT / "processed" / "grid_district_mapping.parquet")
|
||||||
@@ -341,7 +364,7 @@ async def get_grid_history(
|
|||||||
|
|
||||||
district = grid_info.iloc[0]['district_name']
|
district = grid_info.iloc[0]['district_name']
|
||||||
|
|
||||||
cases_df = _load_parquet(PROJECT_ROOT / "processed" / "cases_by_district_daily.parquet")
|
cases_df = load_cases_by_district_daily()
|
||||||
cases_df['date'] = pd.to_datetime(cases_df['date'])
|
cases_df['date'] = pd.to_datetime(cases_df['date'])
|
||||||
|
|
||||||
end_date = datetime.now()
|
end_date = datetime.now()
|
||||||
@@ -367,4 +390,18 @@ async def get_grid_history(
|
|||||||
"district": district,
|
"district": district,
|
||||||
"history": history,
|
"history": history,
|
||||||
"timestamp": datetime.now().isoformat(),
|
"timestamp": datetime.now().isoformat(),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@router.get("/grids/{grid_id}/history")
|
||||||
|
async def get_grid_history(
|
||||||
|
grid_id: str,
|
||||||
|
days: int = Query(30, ge=1, le=365, description="Number of days of history"),
|
||||||
|
):
|
||||||
|
"""
|
||||||
|
Get historical data for a specific grid cell.
|
||||||
|
|
||||||
|
Parquet reads + aggregation run in a thread pool to avoid blocking the
|
||||||
|
async event loop.
|
||||||
|
"""
|
||||||
|
return await asyncio.to_thread(_compute_grid_history, grid_id, days)
|
||||||
|
|||||||
@@ -4,12 +4,14 @@ Provides comprehensive analytics, trends, hotspots, and correlations
|
|||||||
"""
|
"""
|
||||||
from fastapi import APIRouter, HTTPException, Query
|
from fastapi import APIRouter, HTTPException, Query
|
||||||
from datetime import datetime, timedelta
|
from datetime import datetime, timedelta
|
||||||
|
from functools import lru_cache
|
||||||
import random
|
import random
|
||||||
|
|
||||||
from pydantic import BaseModel, Field
|
from pydantic import BaseModel, Field
|
||||||
from typing import Dict, List, Literal
|
from typing import Dict, List, Literal
|
||||||
|
|
||||||
from config import DATA_DIR, RISK_HIGH, PROJECT_ROOT
|
from config import DATA_DIR, RISK_HIGH, PROJECT_ROOT
|
||||||
|
from data.case_loader import load_cases_by_district_daily
|
||||||
from models import (
|
from models import (
|
||||||
InsightsResponse,
|
InsightsResponse,
|
||||||
InsightTrend,
|
InsightTrend,
|
||||||
@@ -20,6 +22,13 @@ from models import (
|
|||||||
)
|
)
|
||||||
from utils.date_helpers import get_latest_date
|
from utils.date_helpers import get_latest_date
|
||||||
from utils.geojson import parse_geojson_file, load_districts
|
from utils.geojson import parse_geojson_file, load_districts
|
||||||
|
|
||||||
|
|
||||||
|
@lru_cache(maxsize=8)
|
||||||
|
def _cached_parquet(path_str: str):
|
||||||
|
"""Load a parquet file once and reuse it (read-only) across requests."""
|
||||||
|
import pandas as pd
|
||||||
|
return pd.read_parquet(path_str)
|
||||||
from utils.geo import point_in_polygon
|
from utils.geo import point_in_polygon
|
||||||
from utils.risk import calculate_trend as calculate_trend_direction
|
from utils.risk import calculate_trend as calculate_trend_direction
|
||||||
|
|
||||||
@@ -41,6 +50,7 @@ class InsightCardResponse(BaseModel):
|
|||||||
warning_count: int
|
warning_count: int
|
||||||
info_count: int
|
info_count: int
|
||||||
success_count: int
|
success_count: int
|
||||||
|
danger_count: int
|
||||||
cards: list[InsightCardItem]
|
cards: list[InsightCardItem]
|
||||||
|
|
||||||
|
|
||||||
@@ -482,22 +492,22 @@ async def get_insights_cards():
|
|||||||
|
|
||||||
cases_path = PROJECT_ROOT / "processed" / "cases_by_district_daily.parquet"
|
cases_path = PROJECT_ROOT / "processed" / "cases_by_district_daily.parquet"
|
||||||
if cases_path.exists():
|
if cases_path.exists():
|
||||||
cases_df = pd.read_parquet(cases_path)
|
# Districts already normalized to the canonical 13 区-suffixed names.
|
||||||
|
cases_df = load_cases_by_district_daily()
|
||||||
|
cases_df["date"] = pd.to_datetime(cases_df["date"])
|
||||||
latest_case_date = cases_df["date"].max()
|
latest_case_date = cases_df["date"].max()
|
||||||
latest_cases = cases_df[cases_df["date"] == latest_case_date].copy()
|
latest_cases = cases_df[cases_df["date"] == latest_case_date]
|
||||||
latest_cases["base_district"] = latest_cases["district"].str.replace("区", "")
|
district_daily = latest_cases.groupby("district")["total_cases"].sum().sort_values(ascending=False)
|
||||||
district_daily = latest_cases.groupby("base_district")["total_cases"].sum().sort_values(ascending=False)
|
|
||||||
total_daily = int(district_daily.sum())
|
total_daily = int(district_daily.sum())
|
||||||
top_name = district_daily.index[0]
|
top_name = district_daily.index[0]
|
||||||
top_val = int(district_daily.iloc[0])
|
top_val = int(district_daily.iloc[0])
|
||||||
num_districts = len(district_daily)
|
num_districts = len(district_daily)
|
||||||
|
|
||||||
week_ago = latest_case_date - pd.Timedelta(days=6)
|
week_ago = latest_case_date - pd.Timedelta(days=6)
|
||||||
week_cases = cases_df[cases_df["date"] >= week_ago].copy()
|
week_cases = cases_df[cases_df["date"] >= week_ago]
|
||||||
week_cases["base_district"] = week_cases["district"].str.replace("区", "")
|
|
||||||
daily_totals = week_cases.groupby("date")["total_cases"].sum()
|
daily_totals = week_cases.groupby("date")["total_cases"].sum()
|
||||||
avg_daily = int(daily_totals.mean())
|
avg_daily = int(daily_totals.mean())
|
||||||
week_district = week_cases.groupby("base_district")["total_cases"].sum().sort_values(ascending=False)
|
week_district = week_cases.groupby("district")["total_cases"].sum().sort_values(ascending=False)
|
||||||
week_top_val = int(week_district.iloc[0])
|
week_top_val = int(week_district.iloc[0])
|
||||||
|
|
||||||
date_str = latest_case_date.strftime("%m月%d日")
|
date_str = latest_case_date.strftime("%m月%d日")
|
||||||
@@ -506,8 +516,8 @@ async def get_insights_cards():
|
|||||||
title=f"日病例统计 ({date_str})",
|
title=f"日病例统计 ({date_str})",
|
||||||
description=(
|
description=(
|
||||||
f"最近统计日({date_str})全市{num_districts}个区共记录{total_daily}例儿童呼吸道疾病病例,"
|
f"最近统计日({date_str})全市{num_districts}个区共记录{total_daily}例儿童呼吸道疾病病例,"
|
||||||
f"{top_name}区{top_val}例为当日最高。近7日日均{avg_daily}例,"
|
f"{top_name}{top_val}例为当日最高。近7日日均{avg_daily}例,"
|
||||||
f"{week_district.index[0]}区累计{week_top_val}例居首。"
|
f"{week_district.index[0]}累计{week_top_val}例居首。"
|
||||||
),
|
),
|
||||||
type="warning",
|
type="warning",
|
||||||
metric="日病例",
|
metric="日病例",
|
||||||
@@ -533,7 +543,7 @@ async def get_insights_cards():
|
|||||||
grids_df["col"] = ((grids_df["longitude"] - MIN_LON) / STEP).astype(int)
|
grids_df["col"] = ((grids_df["longitude"] - MIN_LON) / STEP).astype(int)
|
||||||
grids_df["grid_id"] = "r" + grids_df["row"].astype(str) + "_c" + grids_df["col"].astype(str)
|
grids_df["grid_id"] = "r" + grids_df["row"].astype(str) + "_c" + grids_df["col"].astype(str)
|
||||||
|
|
||||||
mapping = pd.read_parquet(mapping_path)
|
mapping = _cached_parquet(str(mapping_path))
|
||||||
merged = grids_df.merge(mapping, on="grid_id", how="inner")
|
merged = grids_df.merge(mapping, on="grid_id", how="inner")
|
||||||
|
|
||||||
if len(merged) > 0:
|
if len(merged) > 0:
|
||||||
@@ -574,7 +584,7 @@ async def get_insights_cards():
|
|||||||
|
|
||||||
weather_path = PROJECT_ROOT / "processed" / "weather" / "station_daily_2022.parquet"
|
weather_path = PROJECT_ROOT / "processed" / "weather" / "station_daily_2022.parquet"
|
||||||
if weather_path.exists():
|
if weather_path.exists():
|
||||||
weather_df = pd.read_parquet(weather_path)
|
weather_df = _cached_parquet(str(weather_path))
|
||||||
daily_wx = weather_df.groupby("date").agg(
|
daily_wx = weather_df.groupby("date").agg(
|
||||||
AQI=("AQI", "mean"), PM25=("PM25", "mean"), PM10=("PM10", "mean"),
|
AQI=("AQI", "mean"), PM25=("PM25", "mean"), PM10=("PM10", "mean"),
|
||||||
).reset_index()
|
).reset_index()
|
||||||
@@ -664,11 +674,13 @@ async def get_insights_cards():
|
|||||||
warning_count = sum(1 for c in cards if c.type == "warning")
|
warning_count = sum(1 for c in cards if c.type == "warning")
|
||||||
info_count = sum(1 for c in cards if c.type == "info")
|
info_count = sum(1 for c in cards if c.type == "info")
|
||||||
success_count = sum(1 for c in cards if c.type == "success")
|
success_count = sum(1 for c in cards if c.type == "success")
|
||||||
|
danger_count = sum(1 for c in cards if c.type == "danger")
|
||||||
|
|
||||||
return InsightCardResponse(
|
return InsightCardResponse(
|
||||||
total_insights=len(cards),
|
total_insights=len(cards),
|
||||||
warning_count=warning_count,
|
warning_count=warning_count,
|
||||||
info_count=info_count,
|
info_count=info_count,
|
||||||
success_count=success_count,
|
success_count=success_count,
|
||||||
|
danger_count=danger_count,
|
||||||
cards=cards,
|
cards=cards,
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -2,10 +2,10 @@
|
|||||||
Router for CBPOA risk assessment endpoints
|
Router for CBPOA risk assessment endpoints
|
||||||
Reads from GeoJSON files in outputs/daily/ directory
|
Reads from GeoJSON files in outputs/daily/ directory
|
||||||
"""
|
"""
|
||||||
from fastapi import APIRouter, HTTPException, Path, Query
|
from fastapi import APIRouter, HTTPException, Path, Query, Response
|
||||||
from datetime import datetime, timedelta
|
from datetime import datetime, timedelta
|
||||||
from pathlib import Path
|
|
||||||
from typing import Annotated, List, Literal
|
from typing import Annotated, List, Literal
|
||||||
|
import asyncio
|
||||||
import json
|
import json
|
||||||
import glob
|
import glob
|
||||||
import re
|
import re
|
||||||
@@ -28,8 +28,12 @@ from utils.risk import risk_value_to_level
|
|||||||
|
|
||||||
router = APIRouter(prefix="/api/risk", tags=["risk"])
|
router = APIRouter(prefix="/api/risk", tags=["risk"])
|
||||||
|
|
||||||
|
# Cells with risk at/below this are culled from LOD responses; the frontend
|
||||||
|
# discards risk==0 cells anyway, so emitting them only bloats the payload.
|
||||||
|
LOD_RISK_EPSILON = 1e-6
|
||||||
|
|
||||||
@lru_cache(maxsize=3)
|
|
||||||
|
@lru_cache(maxsize=8)
|
||||||
def get_risk_data(date: str) -> tuple[list[list], dict]:
|
def get_risk_data(date: str) -> tuple[list[list], dict]:
|
||||||
filepath = DATA_DIR / f"risk_{date}.geojson"
|
filepath = DATA_DIR / f"risk_{date}.geojson"
|
||||||
if not filepath.exists():
|
if not filepath.exists():
|
||||||
@@ -55,7 +59,7 @@ def get_risk_data(date: str) -> tuple[list[list], dict]:
|
|||||||
return grids, grid_map
|
return grids, grid_map
|
||||||
|
|
||||||
|
|
||||||
@lru_cache(maxsize=3)
|
@lru_cache(maxsize=8)
|
||||||
def get_kdtree_and_risks(date: str):
|
def get_kdtree_and_risks(date: str):
|
||||||
grids, _ = get_risk_data(date)
|
grids, _ = get_risk_data(date)
|
||||||
if not grids:
|
if not grids:
|
||||||
@@ -70,6 +74,8 @@ def generate_lod_grid(zoom: int, forecast_day: Literal[1, 3, 7] = 1,
|
|||||||
bounds: dict | None = None) -> dict:
|
bounds: dict | None = None) -> dict:
|
||||||
date = get_latest_date()
|
date = get_latest_date()
|
||||||
kdtree, risk_values = get_kdtree_and_risks(date)
|
kdtree, risk_values = get_kdtree_and_risks(date)
|
||||||
|
if kdtree is None or risk_values is None:
|
||||||
|
return {"lod": "empty", "zoom": zoom, "aggregate": 1, "grids": [], "total_count": 0, "bounds": bounds or WUHAN_BOUNDS}
|
||||||
|
|
||||||
risk_idx = forecast_day - 1
|
risk_idx = forecast_day - 1
|
||||||
|
|
||||||
@@ -117,7 +123,12 @@ def generate_lod_grid(zoom: int, forecast_day: Literal[1, 3, 7] = 1,
|
|||||||
risks = risk_array[indices]
|
risks = risk_array[indices]
|
||||||
risks[dists > LOD_MAX_RADIUS] = 0.0
|
risks[dists > LOD_MAX_RADIUS] = 0.0
|
||||||
|
|
||||||
lod_grids = np.column_stack([lat_grid.ravel(), lon_grid.ravel(), risks]).tolist()
|
# Drop zero-risk cells (incl. out-of-radius). The frontend discards
|
||||||
|
# them anyway, so culling here shrinks the payload substantially.
|
||||||
|
flat_lat = lat_grid.ravel()
|
||||||
|
flat_lon = lon_grid.ravel()
|
||||||
|
keep = risks > LOD_RISK_EPSILON
|
||||||
|
lod_grids = np.column_stack([flat_lat[keep], flat_lon[keep], risks[keep]]).tolist()
|
||||||
|
|
||||||
return {
|
return {
|
||||||
"lod": lod_name,
|
"lod": lod_name,
|
||||||
@@ -143,8 +154,9 @@ def generate_lod_grid(zoom: int, forecast_day: Literal[1, 3, 7] = 1,
|
|||||||
cell_lat = (WUHAN_BOUNDS["max_lat"] - WUHAN_BOUNDS["min_lat"]) / lat_count
|
cell_lat = (WUHAN_BOUNDS["max_lat"] - WUHAN_BOUNDS["min_lat"]) / lat_count
|
||||||
cell_lon = (WUHAN_BOUNDS["max_lon"] - WUHAN_BOUNDS["min_lon"]) / lon_count
|
cell_lon = (WUHAN_BOUNDS["max_lon"] - WUHAN_BOUNDS["min_lon"]) / lon_count
|
||||||
|
|
||||||
# Apply viewport bounds filtering for zoom >= 10
|
# Apply viewport bounds filtering at all zoom levels when bounds are given,
|
||||||
if bounds and zoom >= 10:
|
# so even the coarse zoom<=9 lod1 grid is clipped to the viewport.
|
||||||
|
if bounds:
|
||||||
b_min_lat = max(bounds["min_lat"], WUHAN_BOUNDS["min_lat"])
|
b_min_lat = max(bounds["min_lat"], WUHAN_BOUNDS["min_lat"])
|
||||||
b_max_lat = min(bounds["max_lat"], WUHAN_BOUNDS["max_lat"])
|
b_max_lat = min(bounds["max_lat"], WUHAN_BOUNDS["max_lat"])
|
||||||
b_min_lon = max(bounds["min_lon"], WUHAN_BOUNDS["min_lon"])
|
b_min_lon = max(bounds["min_lon"], WUHAN_BOUNDS["min_lon"])
|
||||||
@@ -176,7 +188,11 @@ def generate_lod_grid(zoom: int, forecast_day: Literal[1, 3, 7] = 1,
|
|||||||
|
|
||||||
risks[dists > LOD_MAX_RADIUS] = 0.0
|
risks[dists > LOD_MAX_RADIUS] = 0.0
|
||||||
|
|
||||||
lod_grids = np.column_stack([lat_grid.ravel(), lon_grid.ravel(), risks]).tolist()
|
# Drop zero-risk cells (incl. out-of-radius) before serialization.
|
||||||
|
flat_lat = lat_grid.ravel()
|
||||||
|
flat_lon = lon_grid.ravel()
|
||||||
|
keep = risks > LOD_RISK_EPSILON
|
||||||
|
lod_grids = np.column_stack([flat_lat[keep], flat_lon[keep], risks[keep]]).tolist()
|
||||||
|
|
||||||
return {
|
return {
|
||||||
"lod": lod_name,
|
"lod": lod_name,
|
||||||
@@ -188,6 +204,21 @@ def generate_lod_grid(zoom: int, forecast_day: Literal[1, 3, 7] = 1,
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@lru_cache(maxsize=8)
|
||||||
|
def _risk_map_body(date: str) -> str:
|
||||||
|
"""Serialize the full ~140k-grid risk map for a date once (cached).
|
||||||
|
|
||||||
|
The 140k-element response costs ~0.5s of Pydantic validation + JSON
|
||||||
|
serialization; caching the serialized body makes warm calls ~instant.
|
||||||
|
"""
|
||||||
|
grids = parse_geojson_file(DATA_DIR / f"risk_{date}.geojson")
|
||||||
|
return RiskMapResponse(
|
||||||
|
grids=grids,
|
||||||
|
total_count=len(grids),
|
||||||
|
timestamp=datetime.now().isoformat(),
|
||||||
|
).model_dump_json()
|
||||||
|
|
||||||
|
|
||||||
@router.get("/map", response_model=RiskMapResponse)
|
@router.get("/map", response_model=RiskMapResponse)
|
||||||
async def get_risk_map(date: str | None = None):
|
async def get_risk_map(date: str | None = None):
|
||||||
if date is None:
|
if date is None:
|
||||||
@@ -197,13 +228,7 @@ async def get_risk_map(date: str | None = None):
|
|||||||
if not filepath.exists():
|
if not filepath.exists():
|
||||||
raise HTTPException(status_code=404, detail=f"No data found for date {date}")
|
raise HTTPException(status_code=404, detail=f"No data found for date {date}")
|
||||||
|
|
||||||
grids = parse_geojson_file(filepath)
|
return Response(content=_risk_map_body(date), media_type="application/json")
|
||||||
|
|
||||||
return RiskMapResponse(
|
|
||||||
grids=grids,
|
|
||||||
total_count=len(grids),
|
|
||||||
timestamp=datetime.now().isoformat()
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
@router.get("/current", response_model=RiskMapResponse)
|
@router.get("/current", response_model=RiskMapResponse)
|
||||||
@@ -214,28 +239,7 @@ async def get_current_risk():
|
|||||||
if not filepath.exists():
|
if not filepath.exists():
|
||||||
raise HTTPException(status_code=404, detail=f"No data found for date {date}")
|
raise HTTPException(status_code=404, detail=f"No data found for date {date}")
|
||||||
|
|
||||||
with open(filepath, 'r', encoding='utf-8') as f:
|
return Response(content=_risk_map_body(date), media_type="application/json")
|
||||||
geojson = json.load(f)
|
|
||||||
|
|
||||||
grids: list[dict[str, str | float]] = []
|
|
||||||
for feature in geojson.get("features", []):
|
|
||||||
props = feature.get("properties", {})
|
|
||||||
coords = feature.get("geometry", {}).get("coordinates", [0, 0])
|
|
||||||
|
|
||||||
risk_value = props.get("risk_1d", 0)
|
|
||||||
grids.append({
|
|
||||||
"grid_id": str(props.get("node_id", "")),
|
|
||||||
"latitude": props.get("lat", coords[1] if len(coords) > 1 else 0),
|
|
||||||
"longitude": props.get("lon", coords[0] if len(coords) > 0 else 0),
|
|
||||||
"risk_value": risk_value,
|
|
||||||
"risk_level": risk_value_to_level(risk_value),
|
|
||||||
})
|
|
||||||
|
|
||||||
return RiskMapResponse(
|
|
||||||
grids=grids,
|
|
||||||
total_count=len(grids),
|
|
||||||
timestamp=datetime.now().isoformat()
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
@router.get("/precomputed", response_model=RiskMapResponse)
|
@router.get("/precomputed", response_model=RiskMapResponse)
|
||||||
@@ -247,7 +251,7 @@ async def get_precomputed_risk():
|
|||||||
|
|
||||||
grids = []
|
grids = []
|
||||||
for _, row in df.iterrows():
|
for _, row in df.iterrows():
|
||||||
risk_index = float(row.get('risk_index', 0))
|
risk_index = float(row.get('risk_index', 0)) # type: ignore[arg-type]
|
||||||
grids.append({
|
grids.append({
|
||||||
"grid_id": str(row['grid_id']),
|
"grid_id": str(row['grid_id']),
|
||||||
"latitude": float(row['center_y']),
|
"latitude": float(row['center_y']),
|
||||||
@@ -263,6 +267,18 @@ async def get_precomputed_risk():
|
|||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
@lru_cache(maxsize=8)
|
||||||
|
def _fullgrid_body(date: str) -> str:
|
||||||
|
"""Serialize the compact full-grid payload once (cached)."""
|
||||||
|
grids, _ = get_risk_data(date)
|
||||||
|
return json.dumps({
|
||||||
|
"date": date,
|
||||||
|
"total_count": len(grids),
|
||||||
|
"columns": ["lat", "lon", "risk_1d", "risk_3d", "risk_7d"],
|
||||||
|
"grids": grids,
|
||||||
|
})
|
||||||
|
|
||||||
|
|
||||||
@router.get("/fullgrid")
|
@router.get("/fullgrid")
|
||||||
async def get_full_grid(date: str | None = None):
|
async def get_full_grid(date: str | None = None):
|
||||||
if date is None:
|
if date is None:
|
||||||
@@ -272,26 +288,7 @@ async def get_full_grid(date: str | None = None):
|
|||||||
if not filepath.exists():
|
if not filepath.exists():
|
||||||
raise HTTPException(status_code=404, detail=f"No data found for date {date}")
|
raise HTTPException(status_code=404, detail=f"No data found for date {date}")
|
||||||
|
|
||||||
with open(filepath, 'r', encoding='utf-8') as f:
|
return Response(content=_fullgrid_body(date), media_type="application/json")
|
||||||
geojson = json.load(f)
|
|
||||||
|
|
||||||
grids = []
|
|
||||||
for feature in geojson.get("features", []):
|
|
||||||
props = feature.get("properties", {})
|
|
||||||
grids.append([
|
|
||||||
round(props.get("lat", 0), 6),
|
|
||||||
round(props.get("lon", 0), 6),
|
|
||||||
round(props.get("risk_1d", 0), 4),
|
|
||||||
round(props.get("risk_3d", 0), 4),
|
|
||||||
round(props.get("risk_7d", 0), 4),
|
|
||||||
])
|
|
||||||
|
|
||||||
return {
|
|
||||||
"date": date,
|
|
||||||
"total_count": len(grids),
|
|
||||||
"columns": ["lat", "lon", "risk_1d", "risk_3d", "risk_7d"],
|
|
||||||
"grids": grids,
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
@router.get("/lod-grid")
|
@router.get("/lod-grid")
|
||||||
@@ -366,50 +363,44 @@ async def get_lod_tile(
|
|||||||
|
|
||||||
|
|
||||||
@router.get("/history/{grid_id}", response_model=RiskHistoryResponse)
|
@router.get("/history/{grid_id}", response_model=RiskHistoryResponse)
|
||||||
async def get_risk_history(grid_id: str, days: int = 7):
|
async def get_risk_history(grid_id: str, days: Annotated[int, Query(ge=1, le=30)] = 7):
|
||||||
date = get_latest_date()
|
date = get_latest_date()
|
||||||
filepath = DATA_DIR / f"risk_{date}.geojson"
|
filepath = DATA_DIR / f"risk_{date}.geojson"
|
||||||
|
|
||||||
if not filepath.exists():
|
if not filepath.exists():
|
||||||
raise HTTPException(status_code=404, detail=f"No data found for date {date}")
|
raise HTTPException(status_code=404, detail=f"No data found for date {date}")
|
||||||
|
|
||||||
with open(filepath, 'r', encoding='utf-8') as f:
|
# Use cached parsed grids + cached KDTree instead of re-reading the ~44MB file.
|
||||||
geojson = json.load(f)
|
grids = parse_geojson_file(filepath)
|
||||||
|
if not grids:
|
||||||
|
raise HTTPException(status_code=404, detail=f"No data found for date {date}")
|
||||||
|
|
||||||
target_feature = None
|
base_risk = None
|
||||||
for feature in geojson.get("features", []):
|
# Exact node_id match
|
||||||
props = feature.get("properties", {})
|
for g in grids:
|
||||||
if str(props.get("node_id", "")) == grid_id:
|
if g["grid_id"] == grid_id:
|
||||||
target_feature = feature
|
base_risk = g["risk_value"]
|
||||||
break
|
break
|
||||||
|
|
||||||
if not target_feature and re.match(r'r\d+_c\d+', grid_id):
|
# Fallback: nearest grid for r{row}_c{col} ids
|
||||||
|
if base_risk is None and re.match(r'r\d+_c\d+', grid_id):
|
||||||
parts = grid_id.replace("r", "").split("_c")
|
parts = grid_id.replace("r", "").split("_c")
|
||||||
row, col = int(parts[0]), int(parts[1])
|
row, col = int(parts[0]), int(parts[1])
|
||||||
center_lat = WUHAN_BOUNDS["min_lat"] + (row + 0.5) * LAT_STEP
|
center_lat = WUHAN_BOUNDS["min_lat"] + (row + 0.5) * LAT_STEP
|
||||||
center_lon = WUHAN_BOUNDS["min_lon"] + (col + 0.5) * LON_STEP
|
center_lon = WUHAN_BOUNDS["min_lon"] + (col + 0.5) * LON_STEP
|
||||||
points = []
|
kdtree, risk_values = get_kdtree_and_risks(date)
|
||||||
features_list = []
|
if kdtree is not None and risk_values is not None:
|
||||||
for feature in geojson.get("features", []):
|
_, idx = kdtree.query([center_lat, center_lon])
|
||||||
props = feature.get("properties", {})
|
base_risk = risk_values[idx][0] # risk_1d
|
||||||
points.append([props.get("lat", 0), props.get("lon", 0)])
|
|
||||||
features_list.append(feature)
|
|
||||||
if points:
|
|
||||||
tree = KDTree(points)
|
|
||||||
_, idx = tree.query([center_lat, center_lon])
|
|
||||||
target_feature = features_list[idx]
|
|
||||||
|
|
||||||
if not target_feature:
|
if base_risk is None:
|
||||||
raise HTTPException(status_code=404, detail=f"Grid {grid_id} not found")
|
raise HTTPException(status_code=404, detail=f"Grid {grid_id} not found")
|
||||||
|
|
||||||
props = target_feature.get("properties", {})
|
|
||||||
base_risk = props.get("risk_1d", 0)
|
|
||||||
|
|
||||||
history = []
|
history = []
|
||||||
for i in range(days):
|
for i in range(days):
|
||||||
history.append({
|
history.append({
|
||||||
"date": (datetime.now() - timedelta(days=i)).strftime("%Y-%m-%d"),
|
"date": (datetime.now() - timedelta(days=i)).strftime("%Y-%m-%d"),
|
||||||
"risk_value": base_risk * (1 - i * 0.05)
|
"risk_value": max(0.0, base_risk * (1 - i * 0.05))
|
||||||
})
|
})
|
||||||
|
|
||||||
return RiskHistoryResponse(
|
return RiskHistoryResponse(
|
||||||
@@ -432,7 +423,7 @@ async def get_forecast_map(
|
|||||||
|
|
||||||
if not filepath.exists():
|
if not filepath.exists():
|
||||||
# Fall back to current data
|
# Fall back to current data
|
||||||
return await get_current_risk_map()
|
return await get_current_risk()
|
||||||
|
|
||||||
grids = parse_geojson_file(filepath)
|
grids = parse_geojson_file(filepath)
|
||||||
if not grids:
|
if not grids:
|
||||||
@@ -495,3 +486,71 @@ async def get_stats(date: str | None = None):
|
|||||||
high_risk_count=distribution["high"],
|
high_risk_count=distribution["high"],
|
||||||
timestamp=datetime.now().isoformat()
|
timestamp=datetime.now().isoformat()
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
# ---------------------------------------------------------------------------
|
||||||
|
# Raster LOD tiles — full-Wuhan 100m risk grid served as XYZ map tiles.
|
||||||
|
# The browser loads PNG images (cached by Leaflet); no per-cell JS work.
|
||||||
|
# See utils/risk_raster.py for the rendering engine.
|
||||||
|
# ---------------------------------------------------------------------------
|
||||||
|
from utils import risk_raster # noqa: E402 (kept local to this feature block)
|
||||||
|
|
||||||
|
_VALID_DAYS = {1, 3, 7}
|
||||||
|
|
||||||
|
|
||||||
|
@router.get("/tiles/{z}/{x}/{y}.png")
|
||||||
|
async def get_risk_tile(
|
||||||
|
z: Annotated[int, Path(ge=0, le=22)],
|
||||||
|
x: Annotated[int, Path(ge=0)],
|
||||||
|
y: Annotated[int, Path(ge=0)],
|
||||||
|
date: str | None = None,
|
||||||
|
day: Annotated[int, Query()] = 1,
|
||||||
|
):
|
||||||
|
"""Render one web-mercator risk tile (256x256 PNG) for the 100m grid."""
|
||||||
|
if day not in _VALID_DAYS:
|
||||||
|
raise HTTPException(status_code=400, detail="day must be 1, 3, or 7")
|
||||||
|
if date is None:
|
||||||
|
date = get_latest_date()
|
||||||
|
if not (DATA_DIR / f"risk_{date}.geojson").exists():
|
||||||
|
raise HTTPException(status_code=404, detail=f"No data found for date {date}")
|
||||||
|
|
||||||
|
png = await asyncio.to_thread(risk_raster.render_tile, z, x, y, date, day)
|
||||||
|
return Response(
|
||||||
|
content=png,
|
||||||
|
media_type="image/png",
|
||||||
|
headers={"Cache-Control": "public, max-age=3600"},
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
@router.get("/grid-stats")
|
||||||
|
async def get_risk_grid_stats(
|
||||||
|
date: str | None = None,
|
||||||
|
day: Annotated[int, Query()] = 1,
|
||||||
|
):
|
||||||
|
"""Aggregate stats over the in-boundary 100m grid (cell count / avg / max / high)."""
|
||||||
|
if day not in _VALID_DAYS:
|
||||||
|
raise HTTPException(status_code=400, detail="day must be 1, 3, or 7")
|
||||||
|
if date is None:
|
||||||
|
date = get_latest_date()
|
||||||
|
if not (DATA_DIR / f"risk_{date}.geojson").exists():
|
||||||
|
raise HTTPException(status_code=404, detail=f"No data found for date {date}")
|
||||||
|
|
||||||
|
return await asyncio.to_thread(risk_raster.grid_stats, date, day)
|
||||||
|
|
||||||
|
|
||||||
|
@router.get("/cell")
|
||||||
|
async def get_risk_cell(
|
||||||
|
lat: Annotated[float, Query(ge=-90, le=90)],
|
||||||
|
lon: Annotated[float, Query(ge=-180, le=180)],
|
||||||
|
date: str | None = None,
|
||||||
|
day: Annotated[int, Query()] = 1,
|
||||||
|
):
|
||||||
|
"""Risk at the 100m cell containing (lat, lon) — used for click-to-inspect."""
|
||||||
|
if day not in _VALID_DAYS:
|
||||||
|
raise HTTPException(status_code=400, detail="day must be 1, 3, or 7")
|
||||||
|
if date is None:
|
||||||
|
date = get_latest_date()
|
||||||
|
if not (DATA_DIR / f"risk_{date}.geojson").exists():
|
||||||
|
raise HTTPException(status_code=404, detail=f"No data found for date {date}")
|
||||||
|
|
||||||
|
return await asyncio.to_thread(risk_raster.query_cell, lat, lon, date, day)
|
||||||
|
|||||||
566
backend/routers/statistics.py
Normal file
566
backend/routers/statistics.py
Normal file
@@ -0,0 +1,566 @@
|
|||||||
|
"""
|
||||||
|
统计分析 API 路由 (prefix /api/stats)
|
||||||
|
|
||||||
|
为前端统计仪表盘提供聚合后的临床、症状、发病率、环境相关性和时序数据。
|
||||||
|
所有数据从 processed/*.parquet 计算得出(文件型后端,无数据库)。
|
||||||
|
|
||||||
|
设计原则:
|
||||||
|
- 模块级缓存载入的 parquet(与其他路由一致)。
|
||||||
|
- 仅返回聚合结果,绝不直接 dump 原始行,保持 payload 小。
|
||||||
|
- 每个端点用 try/except 包裹,失败时返回合法的空结构(绝不让 UI 收到 500)。
|
||||||
|
- pandas 计算放入线程池 (asyncio.to_thread),避免阻塞事件循环。
|
||||||
|
"""
|
||||||
|
|
||||||
|
import asyncio
|
||||||
|
import glob
|
||||||
|
import logging
|
||||||
|
import threading
|
||||||
|
from pathlib import Path
|
||||||
|
from typing import Optional, cast
|
||||||
|
|
||||||
|
import numpy as np
|
||||||
|
import pandas as pd
|
||||||
|
from fastapi import APIRouter, Query
|
||||||
|
from pydantic import BaseModel
|
||||||
|
|
||||||
|
from data.case_loader import (
|
||||||
|
get_inpatient_data,
|
||||||
|
get_outpatient_data,
|
||||||
|
get_combined_data,
|
||||||
|
load_cases_by_district_daily,
|
||||||
|
normalize_district,
|
||||||
|
)
|
||||||
|
|
||||||
|
logger = logging.getLogger("cbpoa.statistics")
|
||||||
|
|
||||||
|
router = APIRouter(prefix="/api/stats", tags=["statistics"])
|
||||||
|
|
||||||
|
PROJECT_ROOT = Path(__file__).parent.parent.parent
|
||||||
|
PROCESSED_DIR = PROJECT_ROOT / "processed"
|
||||||
|
|
||||||
|
# ============== Module-level caches ==============
|
||||||
|
|
||||||
|
_cache: dict[str, object] = {}
|
||||||
|
_cache_lock = threading.RLock()
|
||||||
|
|
||||||
|
# 7 个污染物(与 feature snapshot 列名一致)
|
||||||
|
POLLUTANTS = ["AQI", "PM25", "PM10", "SO2", "NO2", "O3", "CO"]
|
||||||
|
|
||||||
|
# 主诉症状关键词(固定列表,子串匹配)。注意顺序:更具体的在前避免被宽泛词吞掉,
|
||||||
|
# 但因为是独立子串计数,顺序不影响结果,仅为可读性分组。
|
||||||
|
SYMPTOM_KEYWORDS = [
|
||||||
|
"发热", "咳嗽", "咳", "喘息", "喘", "流涕", "鼻塞", "咽痛", "咽喉",
|
||||||
|
"痰", "气促", "呼吸困难", "肺炎", "复诊", "随诊", "复查",
|
||||||
|
"腹泻", "呕吐", "头痛", "乏力", "胸闷", "鼻涕", "发烧", "感冒",
|
||||||
|
]
|
||||||
|
REVISIT_KEYWORDS = ["复诊", "随诊", "复查"]
|
||||||
|
|
||||||
|
|
||||||
|
def _district_population() -> pd.Series:
|
||||||
|
"""各区人口(population_density 求和,按 grid_district_mapping 归属)。
|
||||||
|
|
||||||
|
返回 index 为规范化区名(13)、值为人口的 Series。结果缓存。
|
||||||
|
"""
|
||||||
|
cached = _cache.get("district_population")
|
||||||
|
if cached is not None:
|
||||||
|
return cast(pd.Series, cached)
|
||||||
|
with _cache_lock:
|
||||||
|
cached = _cache.get("district_population")
|
||||||
|
if cached is not None:
|
||||||
|
return cast(pd.Series, cached)
|
||||||
|
mapping = pd.read_parquet(PROCESSED_DIR / "grid_district_mapping.parquet")
|
||||||
|
grid = pd.read_parquet(
|
||||||
|
PROCESSED_DIR / "grid_100m_with_dem_pop.parquet",
|
||||||
|
columns=["grid_id", "population_density"],
|
||||||
|
)
|
||||||
|
joined = mapping.merge(grid, on="grid_id", how="inner")
|
||||||
|
joined = joined.dropna(subset=["district_name"])
|
||||||
|
joined["district_name"] = joined["district_name"].map(normalize_district)
|
||||||
|
pop = joined.groupby("district_name")["population_density"].sum()
|
||||||
|
_cache["district_population"] = pop
|
||||||
|
return pop
|
||||||
|
|
||||||
|
|
||||||
|
def _feature_snapshots() -> pd.DataFrame:
|
||||||
|
"""合并所有可用的 features_*.parquet 快照(缓存)。
|
||||||
|
|
||||||
|
用于污染物 vs 病例的相关性分析。每个快照按格点给出污染物 + 病例计数 + 区。
|
||||||
|
"""
|
||||||
|
cached = _cache.get("features")
|
||||||
|
if cached is not None:
|
||||||
|
return cast(pd.DataFrame, cached)
|
||||||
|
with _cache_lock:
|
||||||
|
cached = _cache.get("features")
|
||||||
|
if cached is not None:
|
||||||
|
return cast(pd.DataFrame, cached)
|
||||||
|
paths = sorted(glob.glob(str(PROCESSED_DIR / "features_*.parquet")))
|
||||||
|
if not paths:
|
||||||
|
df = pd.DataFrame(
|
||||||
|
columns=POLLUTANTS + ["outpatient_count", "inpatient_count", "total_cases", "district"]
|
||||||
|
)
|
||||||
|
else:
|
||||||
|
frames = [pd.read_parquet(p) for p in paths]
|
||||||
|
df = pd.concat(frames, ignore_index=True)
|
||||||
|
_cache["features"] = df
|
||||||
|
return df
|
||||||
|
|
||||||
|
|
||||||
|
# ============== Response Models ==============
|
||||||
|
|
||||||
|
|
||||||
|
class KeyValueCount(BaseModel):
|
||||||
|
bin_label: str
|
||||||
|
count: int
|
||||||
|
|
||||||
|
|
||||||
|
class InpatientKpis(BaseModel):
|
||||||
|
total_admissions: int
|
||||||
|
median_los_days: float
|
||||||
|
cure_rate: float
|
||||||
|
emergency_admit_ratio: float
|
||||||
|
|
||||||
|
|
||||||
|
class LosByDisease(BaseModel):
|
||||||
|
diagnosis: str
|
||||||
|
p25: float
|
||||||
|
median: float
|
||||||
|
p75: float
|
||||||
|
n: int
|
||||||
|
|
||||||
|
|
||||||
|
class LabelCount(BaseModel):
|
||||||
|
outcome: Optional[str] = None
|
||||||
|
route: Optional[str] = None
|
||||||
|
count: int
|
||||||
|
|
||||||
|
|
||||||
|
class OutcomeCount(BaseModel):
|
||||||
|
outcome: str
|
||||||
|
count: int
|
||||||
|
|
||||||
|
|
||||||
|
class RouteCount(BaseModel):
|
||||||
|
route: str
|
||||||
|
count: int
|
||||||
|
|
||||||
|
|
||||||
|
class BmiByAge(BaseModel):
|
||||||
|
age_band: str
|
||||||
|
p25: float
|
||||||
|
median: float
|
||||||
|
p75: float
|
||||||
|
n: int
|
||||||
|
|
||||||
|
|
||||||
|
class InpatientClinicalResponse(BaseModel):
|
||||||
|
kpis: InpatientKpis
|
||||||
|
los_histogram: list[KeyValueCount]
|
||||||
|
los_by_disease: list[LosByDisease]
|
||||||
|
outcome_counts: list[OutcomeCount]
|
||||||
|
admission_route_counts: list[RouteCount]
|
||||||
|
bmi_by_age_band: list[BmiByAge]
|
||||||
|
|
||||||
|
|
||||||
|
class SymptomItem(BaseModel):
|
||||||
|
keyword: str
|
||||||
|
count: int
|
||||||
|
|
||||||
|
|
||||||
|
class SymptomsResponse(BaseModel):
|
||||||
|
symptoms: list[SymptomItem]
|
||||||
|
revisit_ratio: float
|
||||||
|
|
||||||
|
|
||||||
|
class IncidenceItem(BaseModel):
|
||||||
|
district: str
|
||||||
|
total_cases: int
|
||||||
|
population: float
|
||||||
|
rate_per_10k: float
|
||||||
|
|
||||||
|
|
||||||
|
class IncidenceResponse(BaseModel):
|
||||||
|
districts: list[IncidenceItem]
|
||||||
|
|
||||||
|
|
||||||
|
class CorrItem(BaseModel):
|
||||||
|
pollutant: str
|
||||||
|
corr_with_cases: float
|
||||||
|
|
||||||
|
|
||||||
|
class ScatterPoint(BaseModel):
|
||||||
|
pm25: float
|
||||||
|
aqi: float
|
||||||
|
cases: float
|
||||||
|
|
||||||
|
|
||||||
|
class PairwiseCorr(BaseModel):
|
||||||
|
a: str
|
||||||
|
b: str
|
||||||
|
corr: float
|
||||||
|
|
||||||
|
|
||||||
|
class EnvCorrelationResponse(BaseModel):
|
||||||
|
correlation_matrix: list[CorrItem]
|
||||||
|
scatter: list[ScatterPoint]
|
||||||
|
pollutant_pairwise: list[PairwiseCorr]
|
||||||
|
|
||||||
|
|
||||||
|
class WeekdayPoint(BaseModel):
|
||||||
|
weekday: str
|
||||||
|
outpatient: int
|
||||||
|
inpatient: int
|
||||||
|
total: int
|
||||||
|
|
||||||
|
|
||||||
|
class MonthYearPoint(BaseModel):
|
||||||
|
year: int
|
||||||
|
month: int
|
||||||
|
total: int
|
||||||
|
|
||||||
|
|
||||||
|
class YoYPoint(BaseModel):
|
||||||
|
period: str
|
||||||
|
current: int
|
||||||
|
previous: int
|
||||||
|
growth_pct: float
|
||||||
|
|
||||||
|
|
||||||
|
class TemporalResponse(BaseModel):
|
||||||
|
weekday: list[WeekdayPoint]
|
||||||
|
month_year: list[MonthYearPoint]
|
||||||
|
yoy: list[YoYPoint]
|
||||||
|
|
||||||
|
|
||||||
|
# ============== Helpers ==============
|
||||||
|
|
||||||
|
|
||||||
|
def _empty_inpatient_clinical() -> InpatientClinicalResponse:
|
||||||
|
return InpatientClinicalResponse(
|
||||||
|
kpis=InpatientKpis(
|
||||||
|
total_admissions=0, median_los_days=0.0,
|
||||||
|
cure_rate=0.0, emergency_admit_ratio=0.0,
|
||||||
|
),
|
||||||
|
los_histogram=[], los_by_disease=[], outcome_counts=[],
|
||||||
|
admission_route_counts=[], bmi_by_age_band=[],
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def _safe_float(v) -> float:
|
||||||
|
try:
|
||||||
|
f = float(v)
|
||||||
|
if np.isnan(f) or np.isinf(f):
|
||||||
|
return 0.0
|
||||||
|
return round(f, 4)
|
||||||
|
except (TypeError, ValueError):
|
||||||
|
return 0.0
|
||||||
|
|
||||||
|
|
||||||
|
# ============== Endpoint 1: inpatient clinical ==============
|
||||||
|
|
||||||
|
|
||||||
|
def _compute_inpatient_clinical() -> InpatientClinicalResponse:
|
||||||
|
df = get_inpatient_data().copy()
|
||||||
|
if df.empty:
|
||||||
|
return _empty_inpatient_clinical()
|
||||||
|
|
||||||
|
# LOS = (出院日期 - 入院日期).days, valid 0-60
|
||||||
|
in_date = pd.to_datetime(df["入院日期"], errors="coerce")
|
||||||
|
out_date = pd.to_datetime(df["出院日期"], errors="coerce")
|
||||||
|
df["los"] = (out_date - in_date).dt.days
|
||||||
|
df_los = df[(df["los"] >= 0) & (df["los"] <= 60)]
|
||||||
|
|
||||||
|
total = len(df)
|
||||||
|
median_los = float(df_los["los"].median()) if len(df_los) else 0.0
|
||||||
|
|
||||||
|
outcome = df["出院情况"].fillna("未知")
|
||||||
|
cure_n = int(outcome.isin(["治愈", "好转"]).sum())
|
||||||
|
cure_rate = cure_n / total if total else 0.0
|
||||||
|
|
||||||
|
route = df["入院途径"].fillna("未知")
|
||||||
|
emerg_n = int((route == "急诊").sum())
|
||||||
|
emerg_ratio = emerg_n / total if total else 0.0
|
||||||
|
|
||||||
|
kpis = InpatientKpis(
|
||||||
|
total_admissions=total,
|
||||||
|
median_los_days=round(median_los, 2),
|
||||||
|
cure_rate=round(cure_rate, 4),
|
||||||
|
emergency_admit_ratio=round(emerg_ratio, 4),
|
||||||
|
)
|
||||||
|
|
||||||
|
# LOS histogram bins: 0,1,2,3,4,5,6,7,8-14,15+
|
||||||
|
los_histogram: list[KeyValueCount] = []
|
||||||
|
los_vals = df_los["los"]
|
||||||
|
for b in range(0, 8):
|
||||||
|
los_histogram.append(KeyValueCount(bin_label=str(b), count=int((los_vals == b).sum())))
|
||||||
|
los_histogram.append(KeyValueCount(bin_label="8-14", count=int(((los_vals >= 8) & (los_vals <= 14)).sum())))
|
||||||
|
los_histogram.append(KeyValueCount(bin_label="15+", count=int((los_vals >= 15).sum())))
|
||||||
|
|
||||||
|
# LOS by disease (top 8 diagnoses by n)
|
||||||
|
los_by_disease: list[LosByDisease] = []
|
||||||
|
if len(df_los):
|
||||||
|
top_diag = df_los["诊断名称"].value_counts().head(8).index.tolist()
|
||||||
|
for d in top_diag:
|
||||||
|
grp = df_los[df_los["诊断名称"] == d]["los"]
|
||||||
|
los_by_disease.append(LosByDisease(
|
||||||
|
diagnosis=str(d),
|
||||||
|
p25=round(float(grp.quantile(0.25)), 2),
|
||||||
|
median=round(float(grp.median()), 2),
|
||||||
|
p75=round(float(grp.quantile(0.75)), 2),
|
||||||
|
n=int(len(grp)),
|
||||||
|
))
|
||||||
|
|
||||||
|
# outcome counts
|
||||||
|
outcome_counts = [
|
||||||
|
OutcomeCount(outcome=str(k), count=int(v))
|
||||||
|
for k, v in outcome.value_counts().items()
|
||||||
|
]
|
||||||
|
|
||||||
|
# admission route counts
|
||||||
|
admission_route_counts = [
|
||||||
|
RouteCount(route=str(k), count=int(v))
|
||||||
|
for k, v in route.value_counts().items()
|
||||||
|
]
|
||||||
|
|
||||||
|
# BMI by age band. BMI = 体重kg / (身高m)^2; plausible 8-40.
|
||||||
|
bmi_by_age_band: list[BmiByAge] = []
|
||||||
|
h = pd.to_numeric(df["身高"], errors="coerce") # cm
|
||||||
|
w = pd.to_numeric(df["体重"], errors="coerce") # kg
|
||||||
|
age = pd.to_numeric(df["年龄"], errors="coerce")
|
||||||
|
bmi = w / ((h / 100.0) ** 2)
|
||||||
|
bmi_df = pd.DataFrame({"age": age, "bmi": bmi})
|
||||||
|
bmi_df = bmi_df[(bmi_df["bmi"] >= 8) & (bmi_df["bmi"] <= 40) & bmi_df["age"].notna()]
|
||||||
|
age_bands = [(0, 3, "0-2"), (3, 6, "3-5"), (6, 9, "6-8"),
|
||||||
|
(9, 12, "9-11"), (12, 15, "12-14"), (15, 19, "15-18")]
|
||||||
|
for lo, hi, label in age_bands:
|
||||||
|
grp = bmi_df[(bmi_df["age"] >= lo) & (bmi_df["age"] < hi)]["bmi"]
|
||||||
|
if len(grp) == 0:
|
||||||
|
continue
|
||||||
|
bmi_by_age_band.append(BmiByAge(
|
||||||
|
age_band=label,
|
||||||
|
p25=round(float(grp.quantile(0.25)), 2),
|
||||||
|
median=round(float(grp.median()), 2),
|
||||||
|
p75=round(float(grp.quantile(0.75)), 2),
|
||||||
|
n=int(len(grp)),
|
||||||
|
))
|
||||||
|
|
||||||
|
return InpatientClinicalResponse(
|
||||||
|
kpis=kpis,
|
||||||
|
los_histogram=los_histogram,
|
||||||
|
los_by_disease=los_by_disease,
|
||||||
|
outcome_counts=outcome_counts,
|
||||||
|
admission_route_counts=admission_route_counts,
|
||||||
|
bmi_by_age_band=bmi_by_age_band,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
@router.get("/inpatient-clinical", response_model=InpatientClinicalResponse, summary="住院临床统计")
|
||||||
|
async def inpatient_clinical():
|
||||||
|
"""住院临床概览:KPI、住院天数(LOS)分布、费用分布、转归、入院途径、BMI 分布。"""
|
||||||
|
try:
|
||||||
|
return await asyncio.to_thread(_compute_inpatient_clinical)
|
||||||
|
except Exception:
|
||||||
|
logger.exception("inpatient-clinical failed")
|
||||||
|
return _empty_inpatient_clinical()
|
||||||
|
|
||||||
|
|
||||||
|
# ============== Endpoint 2: symptoms ==============
|
||||||
|
|
||||||
|
|
||||||
|
def _compute_symptoms(top: int) -> SymptomsResponse:
|
||||||
|
df = get_outpatient_data()
|
||||||
|
if df.empty or "主诉" not in df.columns:
|
||||||
|
return SymptomsResponse(symptoms=[], revisit_ratio=0.0)
|
||||||
|
chief = df["主诉"].dropna().astype(str)
|
||||||
|
total = len(chief)
|
||||||
|
if total == 0:
|
||||||
|
return SymptomsResponse(symptoms=[], revisit_ratio=0.0)
|
||||||
|
|
||||||
|
counts: list[SymptomItem] = []
|
||||||
|
for kw in SYMPTOM_KEYWORDS:
|
||||||
|
c = int(chief.str.contains(kw, regex=False).sum())
|
||||||
|
if c > 0:
|
||||||
|
counts.append(SymptomItem(keyword=kw, count=c))
|
||||||
|
counts.sort(key=lambda x: x.count, reverse=True)
|
||||||
|
counts = counts[:top]
|
||||||
|
|
||||||
|
revisit_mask = chief.str.contains("|".join(REVISIT_KEYWORDS), regex=True)
|
||||||
|
revisit_ratio = float(revisit_mask.sum()) / total if total else 0.0
|
||||||
|
|
||||||
|
return SymptomsResponse(symptoms=counts, revisit_ratio=round(revisit_ratio, 4))
|
||||||
|
|
||||||
|
|
||||||
|
@router.get("/symptoms", response_model=SymptomsResponse, summary="门诊主诉症状词频")
|
||||||
|
async def symptoms(top: int = Query(20, ge=1, le=50, description="返回前 N 个症状词")):
|
||||||
|
"""从门诊主诉中提取固定症状关键词的出现频次,并计算复诊比例。"""
|
||||||
|
try:
|
||||||
|
return await asyncio.to_thread(_compute_symptoms, top)
|
||||||
|
except Exception:
|
||||||
|
logger.exception("symptoms failed")
|
||||||
|
return SymptomsResponse(symptoms=[], revisit_ratio=0.0)
|
||||||
|
|
||||||
|
|
||||||
|
# ============== Endpoint 3: incidence rate ==============
|
||||||
|
|
||||||
|
|
||||||
|
def _compute_incidence() -> IncidenceResponse:
|
||||||
|
daily = load_cases_by_district_daily()
|
||||||
|
if daily.empty:
|
||||||
|
return IncidenceResponse(districts=[])
|
||||||
|
case_totals = daily.groupby("district")["total_cases"].sum()
|
||||||
|
pop = _district_population()
|
||||||
|
|
||||||
|
items: list[IncidenceItem] = []
|
||||||
|
for district in case_totals.index:
|
||||||
|
total_cases = int(case_totals.get(district, 0))
|
||||||
|
population = float(pop.get(district, 0.0))
|
||||||
|
rate = (total_cases / population * 10000) if population > 0 else 0.0
|
||||||
|
items.append(IncidenceItem(
|
||||||
|
district=str(district),
|
||||||
|
total_cases=total_cases,
|
||||||
|
population=round(population, 1),
|
||||||
|
rate_per_10k=round(rate, 2),
|
||||||
|
))
|
||||||
|
items.sort(key=lambda x: x.rate_per_10k, reverse=True)
|
||||||
|
return IncidenceResponse(districts=items)
|
||||||
|
|
||||||
|
|
||||||
|
@router.get("/incidence-rate", response_model=IncidenceResponse, summary="各区发病率")
|
||||||
|
async def incidence_rate():
|
||||||
|
"""各区病例总数 / 区人口 * 10000,得到每万人发病率(13 区)。"""
|
||||||
|
try:
|
||||||
|
return await asyncio.to_thread(_compute_incidence)
|
||||||
|
except Exception:
|
||||||
|
logger.exception("incidence-rate failed")
|
||||||
|
return IncidenceResponse(districts=[])
|
||||||
|
|
||||||
|
|
||||||
|
# ============== Endpoint 4: env correlation ==============
|
||||||
|
|
||||||
|
|
||||||
|
def _compute_env_correlation() -> EnvCorrelationResponse:
|
||||||
|
df = _feature_snapshots()
|
||||||
|
if df.empty or "total_cases" not in df.columns:
|
||||||
|
return EnvCorrelationResponse(correlation_matrix=[], scatter=[], pollutant_pairwise=[])
|
||||||
|
|
||||||
|
# 污染物 vs 病例 的 Pearson 相关(按格点,汇集所有快照)
|
||||||
|
correlation_matrix: list[CorrItem] = []
|
||||||
|
cases = pd.to_numeric(df["total_cases"], errors="coerce")
|
||||||
|
for p in POLLUTANTS:
|
||||||
|
if p not in df.columns:
|
||||||
|
continue
|
||||||
|
series = pd.to_numeric(df[p], errors="coerce")
|
||||||
|
valid = series.notna() & cases.notna()
|
||||||
|
if valid.sum() < 2 or series[valid].std() == 0 or cases[valid].std() == 0:
|
||||||
|
corr = 0.0
|
||||||
|
else:
|
||||||
|
corr = float(series[valid].corr(cases[valid]))
|
||||||
|
correlation_matrix.append(CorrItem(pollutant=p, corr_with_cases=_safe_float(corr)))
|
||||||
|
|
||||||
|
# scatter: 采样 cases>0 的格点(up to 500)
|
||||||
|
scatter: list[ScatterPoint] = []
|
||||||
|
has_cols = all(c in df.columns for c in ["PM25", "AQI", "total_cases"])
|
||||||
|
if has_cols:
|
||||||
|
sdf = df[["PM25", "AQI", "total_cases"]].copy()
|
||||||
|
sdf = sdf[pd.to_numeric(sdf["total_cases"], errors="coerce") > 0].dropna()
|
||||||
|
if len(sdf) > 500:
|
||||||
|
sdf = sdf.sample(n=500, random_state=42)
|
||||||
|
for _, r in sdf.iterrows():
|
||||||
|
scatter.append(ScatterPoint(
|
||||||
|
pm25=_safe_float(r["PM25"]),
|
||||||
|
aqi=_safe_float(r["AQI"]),
|
||||||
|
cases=_safe_float(r["total_cases"]),
|
||||||
|
))
|
||||||
|
|
||||||
|
# pollutant pairwise (upper triangle)
|
||||||
|
pollutant_pairwise: list[PairwiseCorr] = []
|
||||||
|
present = [p for p in POLLUTANTS if p in df.columns]
|
||||||
|
pol_df = df[present].apply(pd.to_numeric, errors="coerce")
|
||||||
|
corr_mat = pol_df.corr()
|
||||||
|
for i, a in enumerate(present):
|
||||||
|
for b in present[i + 1:]:
|
||||||
|
try:
|
||||||
|
v = corr_mat.loc[a, b]
|
||||||
|
except KeyError:
|
||||||
|
v = 0.0
|
||||||
|
pollutant_pairwise.append(PairwiseCorr(a=a, b=b, corr=_safe_float(v)))
|
||||||
|
|
||||||
|
return EnvCorrelationResponse(
|
||||||
|
correlation_matrix=correlation_matrix,
|
||||||
|
scatter=scatter,
|
||||||
|
pollutant_pairwise=pollutant_pairwise,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
@router.get("/env-correlation", response_model=EnvCorrelationResponse, summary="环境-病例相关性")
|
||||||
|
async def env_correlation():
|
||||||
|
"""污染物与病例的相关矩阵、PM2.5/AQI 散点、污染物两两相关(热力图)。"""
|
||||||
|
try:
|
||||||
|
return await asyncio.to_thread(_compute_env_correlation)
|
||||||
|
except Exception:
|
||||||
|
logger.exception("env-correlation failed")
|
||||||
|
return EnvCorrelationResponse(correlation_matrix=[], scatter=[], pollutant_pairwise=[])
|
||||||
|
|
||||||
|
|
||||||
|
# ============== Endpoint 5: temporal ==============
|
||||||
|
|
||||||
|
_WEEKDAY_LABELS = ["周一", "周二", "周三", "周四", "周五", "周六", "周日"]
|
||||||
|
|
||||||
|
|
||||||
|
def _compute_temporal() -> TemporalResponse:
|
||||||
|
df = get_combined_data().copy()
|
||||||
|
if df.empty:
|
||||||
|
return TemporalResponse(weekday=[], month_year=[], yoy=[])
|
||||||
|
df["date"] = pd.to_datetime(df["date"], errors="coerce")
|
||||||
|
df = df[df["date"].notna()]
|
||||||
|
if df.empty:
|
||||||
|
return TemporalResponse(weekday=[], month_year=[], yoy=[])
|
||||||
|
|
||||||
|
# weekday (0=周一..6=周日)
|
||||||
|
df["wd"] = df["date"].dt.weekday
|
||||||
|
weekday: list[WeekdayPoint] = []
|
||||||
|
for wd in range(7):
|
||||||
|
sub = df[df["wd"] == wd]
|
||||||
|
out = int((sub["type"] == "outpatient").sum())
|
||||||
|
inp = int((sub["type"] == "inpatient").sum())
|
||||||
|
weekday.append(WeekdayPoint(
|
||||||
|
weekday=_WEEKDAY_LABELS[wd], outpatient=out, inpatient=inp, total=out + inp,
|
||||||
|
))
|
||||||
|
|
||||||
|
# month_year (seasonality grid)
|
||||||
|
df["year"] = df["date"].dt.year
|
||||||
|
df["month"] = df["date"].dt.month
|
||||||
|
my = df.groupby(["year", "month"]).size()
|
||||||
|
month_year = [
|
||||||
|
MonthYearPoint(year=int(y), month=int(m), total=int(c))
|
||||||
|
for (y, m), c in my.items()
|
||||||
|
]
|
||||||
|
month_year.sort(key=lambda x: (x.year, x.month))
|
||||||
|
|
||||||
|
# yoy: monthly current vs same-month-prior-year (only if multiple years exist)
|
||||||
|
yoy: list[YoYPoint] = []
|
||||||
|
years = sorted(df["year"].unique().tolist())
|
||||||
|
if len(years) > 1:
|
||||||
|
monthly_totals = {(int(y), int(m)): int(c) for (y, m), c in my.items()}
|
||||||
|
for (y, m), cur in sorted(monthly_totals.items()):
|
||||||
|
prev = monthly_totals.get((y - 1, m))
|
||||||
|
if prev is None:
|
||||||
|
continue
|
||||||
|
growth = ((cur - prev) / prev * 100) if prev else 0.0
|
||||||
|
yoy.append(YoYPoint(
|
||||||
|
period=f"{y}-{m:02d}",
|
||||||
|
current=cur,
|
||||||
|
previous=prev,
|
||||||
|
growth_pct=round(growth, 2),
|
||||||
|
))
|
||||||
|
|
||||||
|
return TemporalResponse(weekday=weekday, month_year=month_year, yoy=yoy)
|
||||||
|
|
||||||
|
|
||||||
|
@router.get("/temporal", response_model=TemporalResponse, summary="时序统计")
|
||||||
|
async def temporal():
|
||||||
|
"""按星期、年-月(季节性网格)聚合,以及同比(YoY)增长(若有多年数据)。"""
|
||||||
|
try:
|
||||||
|
return await asyncio.to_thread(_compute_temporal)
|
||||||
|
except Exception:
|
||||||
|
logger.exception("temporal failed")
|
||||||
|
return TemporalResponse(weekday=[], month_year=[], yoy=[])
|
||||||
46
backend/tests/CLAUDE.md
Normal file
46
backend/tests/CLAUDE.md
Normal file
@@ -0,0 +1,46 @@
|
|||||||
|
# Backend Tests
|
||||||
|
|
||||||
|
## Framework
|
||||||
|
|
||||||
|
pytest + FastAPI `TestClient` (sync, in-process). No database mocking needed — tests hit real endpoints with real data files.
|
||||||
|
|
||||||
|
## Structure
|
||||||
|
|
||||||
|
- `conftest.py` — shared fixtures (`client`, `auth_headers`, test data)
|
||||||
|
- `test_api.py` — endpoint integration tests, organized by router class
|
||||||
|
- `test_auth.py` — authentication flow tests
|
||||||
|
- `test_error_handling.py` — edge cases, error responses
|
||||||
|
- `test_utils.py` — pure utility function tests
|
||||||
|
|
||||||
|
## Patterns
|
||||||
|
|
||||||
|
- Tests organized in classes: `class TestRiskEndpoints:`
|
||||||
|
- One test method per scenario: `test_current_risk_map()`, `test_risk_map_with_date()`
|
||||||
|
- Fixture naming: `client: TestClient`, `auth_headers: dict`
|
||||||
|
- Assert response status, then JSON structure, then field types/values
|
||||||
|
|
||||||
|
```python
|
||||||
|
class TestSomeRouter:
|
||||||
|
def test_something(self, client: TestClient):
|
||||||
|
resp = client.get("/api/some/endpoint")
|
||||||
|
assert resp.status_code == 200
|
||||||
|
data = resp.json()
|
||||||
|
assert "key" in data
|
||||||
|
assert isinstance(data["key"], list)
|
||||||
|
```
|
||||||
|
|
||||||
|
## Running
|
||||||
|
|
||||||
|
```bash
|
||||||
|
cd backend
|
||||||
|
source venv/bin/activate
|
||||||
|
pytest tests/ -v
|
||||||
|
pytest tests/test_api.py -v -k "test_risk"
|
||||||
|
```
|
||||||
|
|
||||||
|
## Anti-Patterns
|
||||||
|
|
||||||
|
- Don't mock endpoints you can test with real data
|
||||||
|
- Don't hardcode test dates that will go stale
|
||||||
|
- Don't skip assertions on response structure just because status is 200
|
||||||
|
- Don't share mutable state between test classes — use fixtures
|
||||||
82
backend/tests/test_district_normalization.py
Normal file
82
backend/tests/test_district_normalization.py
Normal file
@@ -0,0 +1,82 @@
|
|||||||
|
"""Tests for district label normalization at the case-loader boundary.
|
||||||
|
|
||||||
|
The processed/cases_by_district_daily.parquet carries both bare ("武昌") and
|
||||||
|
区-suffixed ("武昌区") spellings of each district (26 labels = 13 districts × 2
|
||||||
|
spellings), which double-counts in any roll-up. data.case_loader normalizes
|
||||||
|
these to the canonical 13 区-suffixed names and re-aggregates. These tests pin
|
||||||
|
that behavior.
|
||||||
|
"""
|
||||||
|
import sys
|
||||||
|
from pathlib import Path
|
||||||
|
|
||||||
|
import pandas as pd
|
||||||
|
import pytest
|
||||||
|
|
||||||
|
# Ensure the backend package root is importable at collection time (mirrors the
|
||||||
|
# sys.path handling other modules rely on once the app is imported).
|
||||||
|
BACKEND_ROOT = Path(__file__).parent.parent
|
||||||
|
if str(BACKEND_ROOT) not in sys.path:
|
||||||
|
sys.path.insert(0, str(BACKEND_ROOT))
|
||||||
|
|
||||||
|
from data.case_loader import ( # noqa: E402
|
||||||
|
CANONICAL_DISTRICTS,
|
||||||
|
normalize_district,
|
||||||
|
load_cases_by_district_daily,
|
||||||
|
)
|
||||||
|
|
||||||
|
PROJECT_ROOT = Path(__file__).parent.parent.parent
|
||||||
|
RAW_PARQUET = PROJECT_ROOT / "processed" / "cases_by_district_daily.parquet"
|
||||||
|
|
||||||
|
|
||||||
|
def test_normalize_district_known_bare_forms():
|
||||||
|
"""Every known bare form maps to its canonical 区-suffixed name."""
|
||||||
|
cases = {
|
||||||
|
"武昌": "武昌区", "汉阳": "汉阳区", "江岸": "江岸区", "硚口": "硚口区",
|
||||||
|
"青山": "青山区", "洪山": "洪山区", "东西湖": "东西湖区", "汉南": "汉南区",
|
||||||
|
"蔡甸": "蔡甸区", "江夏": "江夏区", "黄陂": "黄陂区", "新洲": "新洲区",
|
||||||
|
"江汉": "江汉区",
|
||||||
|
}
|
||||||
|
for bare, canonical in cases.items():
|
||||||
|
assert normalize_district(bare) == canonical
|
||||||
|
|
||||||
|
|
||||||
|
def test_normalize_district_already_suffixed_passes_through():
|
||||||
|
for d in CANONICAL_DISTRICTS:
|
||||||
|
assert normalize_district(d) == d
|
||||||
|
|
||||||
|
|
||||||
|
def test_canonical_set_is_exactly_thirteen():
|
||||||
|
assert len(CANONICAL_DISTRICTS) == 13
|
||||||
|
assert len(set(CANONICAL_DISTRICTS)) == 13
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.skipif(not RAW_PARQUET.exists(), reason="case parquet not present")
|
||||||
|
def test_loader_collapses_to_thirteen_canonical_districts():
|
||||||
|
df = load_cases_by_district_daily()
|
||||||
|
districts = set(df["district"].unique())
|
||||||
|
|
||||||
|
# (a) exactly 13 unique districts, all canonical
|
||||||
|
assert len(districts) == 13, f"expected 13 districts, got {len(districts)}: {sorted(districts)}"
|
||||||
|
assert districts == set(CANONICAL_DISTRICTS)
|
||||||
|
|
||||||
|
# (b) no bare / unsuffixed duplicates remain
|
||||||
|
for name in districts:
|
||||||
|
assert name.endswith(("区", "县", "市")), f"unsuffixed district leaked: {name}"
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.skipif(not RAW_PARQUET.exists(), reason="case parquet not present")
|
||||||
|
def test_loader_preserves_totals_no_rows_dropped_or_double_counted():
|
||||||
|
"""Sum integrity: normalized total == raw parquet total."""
|
||||||
|
raw = pd.read_parquet(RAW_PARQUET)
|
||||||
|
normalized = load_cases_by_district_daily()
|
||||||
|
|
||||||
|
assert int(normalized["total_cases"].sum()) == int(raw["total_cases"].sum())
|
||||||
|
assert int(normalized["outpatient_count"].sum()) == int(raw["outpatient_count"].sum())
|
||||||
|
assert int(normalized["inpatient_count"].sum()) == int(raw["inpatient_count"].sum())
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.skipif(not RAW_PARQUET.exists(), reason="case parquet not present")
|
||||||
|
def test_raw_parquet_actually_has_dirty_labels():
|
||||||
|
"""Sanity: the raw file really has the 26-label problem we are fixing."""
|
||||||
|
raw = pd.read_parquet(RAW_PARQUET)
|
||||||
|
assert raw["district"].nunique() > 13
|
||||||
@@ -37,8 +37,11 @@ class TestMissingResources:
|
|||||||
|
|
||||||
class TestInvalidForecastDay:
|
class TestInvalidForecastDay:
|
||||||
def test_forecast_out_of_range(self, client: TestClient):
|
def test_forecast_out_of_range(self, client: TestClient):
|
||||||
|
# days=0 violates the ge=1 bound on /forecast/{days}, so FastAPI returns 422.
|
||||||
|
# (This previously returned 200 because `Path` was shadowed by `pathlib.Path`,
|
||||||
|
# silently disabling validation — fixed by the risk.py import correction.)
|
||||||
resp = client.get("/api/risk/forecast/0")
|
resp = client.get("/api/risk/forecast/0")
|
||||||
assert resp.status_code in (200, 404)
|
assert resp.status_code == 422
|
||||||
|
|
||||||
def test_forecast_too_large(self, client: TestClient):
|
def test_forecast_too_large(self, client: TestClient):
|
||||||
resp = client.get("/api/risk/forecast/999")
|
resp = client.get("/api/risk/forecast/999")
|
||||||
|
|||||||
@@ -9,7 +9,7 @@ def point_in_polygon(lat: float, lon: float, polygon_coords: list) -> bool:
|
|||||||
return False
|
return False
|
||||||
|
|
||||||
# MultiPolygon: check each polygon
|
# MultiPolygon: check each polygon
|
||||||
if isinstance(polygon_coords[0], list) and isinstance(polygon_coords[0][0], list):
|
if isinstance(polygon_coords[0], list) and polygon_coords[0] and isinstance(polygon_coords[0][0], list):
|
||||||
for polygon in polygon_coords:
|
for polygon in polygon_coords:
|
||||||
if polygon and isinstance(polygon[0], list):
|
if polygon and isinstance(polygon[0], list):
|
||||||
ring = polygon[0] if isinstance(polygon[0][0], list) else polygon
|
ring = polygon[0] if isinstance(polygon[0][0], list) else polygon
|
||||||
@@ -25,6 +25,8 @@ def point_in_polygon(lat: float, lon: float, polygon_coords: list) -> bool:
|
|||||||
def point_in_ring(lat: float, lon: float, ring: list) -> bool:
|
def point_in_ring(lat: float, lon: float, ring: list) -> bool:
|
||||||
"""Ray casting algorithm for point-in-ring test."""
|
"""Ray casting algorithm for point-in-ring test."""
|
||||||
n = len(ring)
|
n = len(ring)
|
||||||
|
if n < 3:
|
||||||
|
return False
|
||||||
inside = False
|
inside = False
|
||||||
|
|
||||||
x, y = lon, lat
|
x, y = lon, lat
|
||||||
|
|||||||
@@ -13,7 +13,7 @@ from utils.risk import risk_value_to_level
|
|||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
@lru_cache(maxsize=8)
|
@lru_cache(maxsize=16)
|
||||||
def parse_geojson_file(filepath: Path) -> list[dict[str, Any]]:
|
def parse_geojson_file(filepath: Path) -> list[dict[str, Any]]:
|
||||||
"""Parse GeoJSON file and extract grid data with standard fields."""
|
"""Parse GeoJSON file and extract grid data with standard fields."""
|
||||||
try:
|
try:
|
||||||
|
|||||||
375
backend/utils/risk_raster.py
Normal file
375
backend/utils/risk_raster.py
Normal file
@@ -0,0 +1,375 @@
|
|||||||
|
"""
|
||||||
|
Risk raster tile engine — renders the full-Wuhan 100m risk grid as XYZ map tiles.
|
||||||
|
|
||||||
|
Why this exists
|
||||||
|
---------------
|
||||||
|
The model emits risk at ~140k GCN nodes per day. The product needs to display this
|
||||||
|
over the full Wuhan 100m grid (~1.5M in-boundary cells) with smooth LOD. Shipping
|
||||||
|
that many cells to the browser as vectors is impossible, so we rasterize server-side:
|
||||||
|
|
||||||
|
1. Build a dense per-cell risk raster R[row, col] once per (date, forecast_day):
|
||||||
|
scatter each node's risk onto its 100m cell (max per cell), then nearest-fill
|
||||||
|
empty cells via a Euclidean distance transform (Voronoi over nodes, quantized
|
||||||
|
to the 100m grid). Cells outside the Wuhan boundary are masked out.
|
||||||
|
2. Build a max-pooled pyramid for clean LOD at low zoom.
|
||||||
|
3. Render standard 256x256 web-mercator PNG tiles by sampling the pyramid level
|
||||||
|
that matches the tile's zoom. Tiles are cached; the browser just loads images.
|
||||||
|
|
||||||
|
Coordinate conventions (calibrated from processed/grid_100m_index.parquet):
|
||||||
|
lat = MIN_LAT + (row + 0.5) * LAT_STEP -> row 0 is SOUTH, row increases north
|
||||||
|
lon = MIN_LON + (col + 0.5) * LON_STEP -> col 0 is WEST, col increases east
|
||||||
|
Everything below maps lat/lon -> (row, col) the same way, so orientation is coherent
|
||||||
|
end to end. Tile pixel py=0 is north (high lat -> high row); we build the RGBA array
|
||||||
|
with py as the first axis so north ends up at the top of the PNG.
|
||||||
|
"""
|
||||||
|
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
import io
|
||||||
|
import json
|
||||||
|
import math
|
||||||
|
from functools import lru_cache
|
||||||
|
from pathlib import Path
|
||||||
|
|
||||||
|
import numpy as np
|
||||||
|
from PIL import Image, ImageDraw
|
||||||
|
from scipy.ndimage import distance_transform_edt
|
||||||
|
|
||||||
|
from config import DATA_DIR, WUHAN_BOUNDS, PROJECT_ROOT
|
||||||
|
|
||||||
|
# --- Grid definition (geographically-correct ~100m grid over the Wuhan bbox) ---
|
||||||
|
MIN_LON = WUHAN_BOUNDS["min_lon"]
|
||||||
|
MAX_LON = WUHAN_BOUNDS["max_lon"]
|
||||||
|
MIN_LAT = WUHAN_BOUNDS["min_lat"]
|
||||||
|
MAX_LAT = WUHAN_BOUNDS["max_lat"]
|
||||||
|
|
||||||
|
NROWS = 1550 # matches processed/grid_100m_with_dem_pop.parquet row extent
|
||||||
|
NCOLS = 1336 # matches its col extent
|
||||||
|
LAT_STEP = (MAX_LAT - MIN_LAT) / NROWS
|
||||||
|
LON_STEP = (MAX_LON - MIN_LON) / NCOLS
|
||||||
|
|
||||||
|
BOUNDARY_GEOJSON = PROJECT_ROOT / "Datas" / "武汉市.geojson"
|
||||||
|
|
||||||
|
# Forecast-day -> property suffix on the risk geojson features.
|
||||||
|
_DAY_TO_KEY = {1: "risk_1d", 3: "risk_3d", 7: "risk_7d"}
|
||||||
|
|
||||||
|
MAX_PYRAMID_LEVEL = 7 # full-res + 7 downsamples covers world zoom range
|
||||||
|
TILE_PX = 256
|
||||||
|
|
||||||
|
|
||||||
|
# ----------------------------------------------------------------------------
|
||||||
|
# Affine helpers (lat/lon <-> grid row/col)
|
||||||
|
# ----------------------------------------------------------------------------
|
||||||
|
def latlon_to_rowcol(lat: float, lon: float) -> tuple[int, int]:
|
||||||
|
row = int((lat - MIN_LAT) / LAT_STEP)
|
||||||
|
col = int((lon - MIN_LON) / LON_STEP)
|
||||||
|
row = max(0, min(NROWS - 1, row))
|
||||||
|
col = max(0, min(NCOLS - 1, col))
|
||||||
|
return row, col
|
||||||
|
|
||||||
|
|
||||||
|
# ----------------------------------------------------------------------------
|
||||||
|
# Boundary mask (rasterized once)
|
||||||
|
# ----------------------------------------------------------------------------
|
||||||
|
@lru_cache(maxsize=1)
|
||||||
|
def _boundary_mask() -> np.ndarray:
|
||||||
|
"""Boolean (NROWS, NCOLS) mask, True for cells inside the Wuhan boundary."""
|
||||||
|
img = Image.new("1", (NCOLS, NROWS), 0)
|
||||||
|
draw = ImageDraw.Draw(img)
|
||||||
|
|
||||||
|
if not BOUNDARY_GEOJSON.exists():
|
||||||
|
# No boundary file -> color the whole bbox rather than nothing.
|
||||||
|
return np.ones((NROWS, NCOLS), dtype=bool)
|
||||||
|
|
||||||
|
with open(BOUNDARY_GEOJSON, "r", encoding="utf-8") as f:
|
||||||
|
gj = json.load(f)
|
||||||
|
|
||||||
|
def _draw_ring(ring):
|
||||||
|
pts = []
|
||||||
|
for lon, lat in ring:
|
||||||
|
col = (lon - MIN_LON) / LON_STEP
|
||||||
|
row = (lat - MIN_LAT) / LAT_STEP
|
||||||
|
pts.append((col, row))
|
||||||
|
if len(pts) >= 3:
|
||||||
|
draw.polygon(pts, fill=1)
|
||||||
|
|
||||||
|
def _walk(geom):
|
||||||
|
gtype = geom.get("type")
|
||||||
|
coords = geom.get("coordinates", [])
|
||||||
|
if gtype == "Polygon":
|
||||||
|
for ring in coords:
|
||||||
|
_draw_ring(ring)
|
||||||
|
elif gtype == "MultiPolygon":
|
||||||
|
for poly in coords:
|
||||||
|
for ring in poly:
|
||||||
|
_draw_ring(ring)
|
||||||
|
|
||||||
|
if gj.get("type") == "FeatureCollection":
|
||||||
|
for feat in gj.get("features", []):
|
||||||
|
_walk(feat.get("geometry", {}))
|
||||||
|
elif gj.get("type") == "Feature":
|
||||||
|
_walk(gj.get("geometry", {}))
|
||||||
|
else:
|
||||||
|
_walk(gj)
|
||||||
|
|
||||||
|
return np.array(img, dtype=bool)
|
||||||
|
|
||||||
|
|
||||||
|
# ----------------------------------------------------------------------------
|
||||||
|
# Node loading (cached per date)
|
||||||
|
# ----------------------------------------------------------------------------
|
||||||
|
@lru_cache(maxsize=8)
|
||||||
|
def _load_nodes(date: str) -> tuple[np.ndarray, np.ndarray, np.ndarray]:
|
||||||
|
"""Return (rows, cols, risks[3]) arrays for all nodes on a date.
|
||||||
|
|
||||||
|
risks is shape (n_nodes, 3) for [risk_1d, risk_3d, risk_7d].
|
||||||
|
"""
|
||||||
|
filepath = DATA_DIR / f"risk_{date}.geojson"
|
||||||
|
if not filepath.exists():
|
||||||
|
raise FileNotFoundError(f"No risk data for date {date}")
|
||||||
|
|
||||||
|
with open(filepath, "r", encoding="utf-8") as f:
|
||||||
|
gj = json.load(f)
|
||||||
|
|
||||||
|
feats = gj.get("features", [])
|
||||||
|
n = len(feats)
|
||||||
|
rows = np.empty(n, dtype=np.int32)
|
||||||
|
cols = np.empty(n, dtype=np.int32)
|
||||||
|
risks = np.zeros((n, 3), dtype=np.float32)
|
||||||
|
|
||||||
|
for i, feat in enumerate(feats):
|
||||||
|
p = feat.get("properties", {})
|
||||||
|
lat = p.get("lat", 0.0)
|
||||||
|
lon = p.get("lon", 0.0)
|
||||||
|
r, c = latlon_to_rowcol(lat, lon)
|
||||||
|
rows[i] = r
|
||||||
|
cols[i] = c
|
||||||
|
risks[i, 0] = p.get("risk_1d", 0.0)
|
||||||
|
risks[i, 1] = p.get("risk_3d", 0.0)
|
||||||
|
risks[i, 2] = p.get("risk_7d", 0.0)
|
||||||
|
|
||||||
|
return rows, cols, risks
|
||||||
|
|
||||||
|
|
||||||
|
# ----------------------------------------------------------------------------
|
||||||
|
# Risk raster + pyramid (cached per date+day)
|
||||||
|
# ----------------------------------------------------------------------------
|
||||||
|
def _maxpool2(a: np.ndarray) -> np.ndarray:
|
||||||
|
"""Downsample by 2 taking the NaN-aware max of each 2x2 block."""
|
||||||
|
h, w = a.shape
|
||||||
|
h2, w2 = (h + 1) // 2, (w + 1) // 2
|
||||||
|
out = np.full((h2, w2), np.nan, dtype=np.float32)
|
||||||
|
# Pad to even dims with NaN so the reshape is clean.
|
||||||
|
ph, pw = h2 * 2, w2 * 2
|
||||||
|
pad = np.full((ph, pw), np.nan, dtype=np.float32)
|
||||||
|
pad[:h, :w] = a
|
||||||
|
blocks = pad.reshape(h2, 2, w2, 2)
|
||||||
|
# np.nanmax over the 2x2 block axes; suppress all-NaN warnings.
|
||||||
|
with np.errstate(invalid="ignore"):
|
||||||
|
out = np.nanmax(blocks, axis=(1, 3))
|
||||||
|
return out.astype(np.float32)
|
||||||
|
|
||||||
|
|
||||||
|
@lru_cache(maxsize=6)
|
||||||
|
def _risk_pyramid(date: str, day: int) -> tuple[np.ndarray, ...]:
|
||||||
|
"""Build the nearest-filled, boundary-masked risk raster and its LOD pyramid.
|
||||||
|
|
||||||
|
Returns a tuple of arrays, level 0 = full res (NROWS, NCOLS), each subsequent
|
||||||
|
level downsampled 2x. NaN marks "no data / outside boundary".
|
||||||
|
"""
|
||||||
|
if day not in _DAY_TO_KEY:
|
||||||
|
raise ValueError(f"invalid forecast day {day}")
|
||||||
|
day_idx = {1: 0, 3: 1, 7: 2}[day]
|
||||||
|
|
||||||
|
rows, cols, risks = _load_nodes(date)
|
||||||
|
vals = risks[:, day_idx]
|
||||||
|
|
||||||
|
# Scatter nodes onto the grid, taking the max risk per cell.
|
||||||
|
R = np.full((NROWS, NCOLS), -np.inf, dtype=np.float32)
|
||||||
|
np.maximum.at(R, (rows, cols), vals)
|
||||||
|
known = np.isfinite(R)
|
||||||
|
|
||||||
|
# Nearest-fill empty cells (Voronoi over nodes, quantized to the 100m grid).
|
||||||
|
if known.any():
|
||||||
|
idx = distance_transform_edt(~known, return_distances=False, return_indices=True)
|
||||||
|
R = R[tuple(idx)]
|
||||||
|
R = R.astype(np.float32)
|
||||||
|
|
||||||
|
# Mask out everything outside the Wuhan boundary.
|
||||||
|
mask = _boundary_mask()
|
||||||
|
R[~mask] = np.nan
|
||||||
|
|
||||||
|
pyramid = [R]
|
||||||
|
for _ in range(MAX_PYRAMID_LEVEL):
|
||||||
|
nxt = _maxpool2(pyramid[-1])
|
||||||
|
pyramid.append(nxt)
|
||||||
|
if nxt.shape[0] <= 2 or nxt.shape[1] <= 2:
|
||||||
|
break
|
||||||
|
return tuple(pyramid)
|
||||||
|
|
||||||
|
|
||||||
|
# ----------------------------------------------------------------------------
|
||||||
|
# Colormap (risk 0..1 -> RGBA), built once as a 256-entry LUT
|
||||||
|
# ----------------------------------------------------------------------------
|
||||||
|
@lru_cache(maxsize=1)
|
||||||
|
def _color_lut() -> np.ndarray:
|
||||||
|
"""256x4 uint8 LUT. Green -> yellow -> orange -> red, alpha grows with risk.
|
||||||
|
|
||||||
|
Risk below ~0.25 is rendered transparent to keep the map readable.
|
||||||
|
"""
|
||||||
|
lut = np.zeros((256, 4), dtype=np.uint8)
|
||||||
|
# control points: (risk, R, G, B)
|
||||||
|
stops = [
|
||||||
|
(0.00, 56, 176, 0), # green (low)
|
||||||
|
(0.40, 250, 204, 21), # yellow (medium)
|
||||||
|
(0.60, 249, 115, 22), # orange (high)
|
||||||
|
(0.80, 239, 68, 68), # red (critical)
|
||||||
|
(1.00, 153, 27, 27), # dark red (extreme)
|
||||||
|
]
|
||||||
|
xs = [s[0] for s in stops]
|
||||||
|
for i in range(256):
|
||||||
|
t = i / 255.0
|
||||||
|
# piecewise-linear RGB interpolation
|
||||||
|
for k in range(len(stops) - 1):
|
||||||
|
if xs[k] <= t <= xs[k + 1]:
|
||||||
|
f = (t - xs[k]) / (xs[k + 1] - xs[k] + 1e-9)
|
||||||
|
r = stops[k][1] + f * (stops[k + 1][1] - stops[k][1])
|
||||||
|
g = stops[k][2] + f * (stops[k + 1][2] - stops[k][2])
|
||||||
|
b = stops[k][3] + f * (stops[k + 1][3] - stops[k][3])
|
||||||
|
break
|
||||||
|
else:
|
||||||
|
r, g, b = stops[-1][1:]
|
||||||
|
# alpha: transparent below 0.25, then ramp 90 -> 235
|
||||||
|
if t < 0.25:
|
||||||
|
a = 0.0
|
||||||
|
else:
|
||||||
|
a = 90 + (t - 0.25) / 0.75 * (235 - 90)
|
||||||
|
lut[i] = (int(r), int(g), int(b), int(a))
|
||||||
|
return lut
|
||||||
|
|
||||||
|
|
||||||
|
# ----------------------------------------------------------------------------
|
||||||
|
# Tile rendering
|
||||||
|
# ----------------------------------------------------------------------------
|
||||||
|
def _tile_pixel_latlon(z: int, x: int, y: int) -> tuple[np.ndarray, np.ndarray]:
|
||||||
|
"""Return (lat[256,256], lon[256,256]) for each pixel center of a tile."""
|
||||||
|
n = 2.0 ** z
|
||||||
|
px = (np.arange(TILE_PX) + 0.5) / TILE_PX
|
||||||
|
# longitude is linear in tile-x
|
||||||
|
X = (x + px) / n
|
||||||
|
lon = X * 360.0 - 180.0 # shape (256,)
|
||||||
|
# latitude via inverse web-mercator (nonlinear in tile-y)
|
||||||
|
Y = (y + px) / n
|
||||||
|
lat = np.degrees(np.arctan(np.sinh(np.pi * (1.0 - 2.0 * Y)))) # shape (256,)
|
||||||
|
lon2d = np.broadcast_to(lon, (TILE_PX, TILE_PX)) # varies along axis 1 (px)
|
||||||
|
lat2d = np.broadcast_to(lat[:, None], (TILE_PX, TILE_PX)) # varies along axis 0 (py)
|
||||||
|
return lat2d, lon2d
|
||||||
|
|
||||||
|
|
||||||
|
def _level_for_zoom(z: int) -> int:
|
||||||
|
"""Pick the pyramid level so ~1 source cell maps to ~1 screen pixel."""
|
||||||
|
# meters/pixel at lat ~30.6: 156543.03 * cos(lat) / 2^z ; /100m per cell
|
||||||
|
cells_per_px = (156543.03 * math.cos(math.radians(30.6)) / (2.0 ** z)) / 100.0
|
||||||
|
if cells_per_px <= 1.0:
|
||||||
|
return 0
|
||||||
|
return max(0, min(MAX_PYRAMID_LEVEL, int(math.floor(math.log2(cells_per_px)))))
|
||||||
|
|
||||||
|
|
||||||
|
def render_tile(z: int, x: int, y: int, date: str, day: int = 1) -> bytes:
|
||||||
|
"""Render a single XYZ tile to PNG bytes. Fully transparent tiles return a
|
||||||
|
tiny cached blank PNG. Result is cached per (z,x,y,date,day)."""
|
||||||
|
return _render_tile_cached(z, x, y, date, day)
|
||||||
|
|
||||||
|
|
||||||
|
@lru_cache(maxsize=1024)
|
||||||
|
def _render_tile_cached(z: int, x: int, y: int, date: str, day: int) -> bytes:
|
||||||
|
pyramid = _risk_pyramid(date, day)
|
||||||
|
level = _level_for_zoom(z)
|
||||||
|
level = min(level, len(pyramid) - 1)
|
||||||
|
R = pyramid[level]
|
||||||
|
factor = 2 ** level
|
||||||
|
lh, lw = R.shape
|
||||||
|
|
||||||
|
lat2d, lon2d = _tile_pixel_latlon(z, x, y)
|
||||||
|
|
||||||
|
# lat/lon -> full-res row/col -> level row/col
|
||||||
|
row = ((lat2d - MIN_LAT) / LAT_STEP).astype(np.int32) // factor
|
||||||
|
col = ((lon2d - MIN_LON) / LON_STEP).astype(np.int32) // factor
|
||||||
|
|
||||||
|
inside = (row >= 0) & (row < lh) & (col >= 0) & (col < lw)
|
||||||
|
rc = np.clip(row, 0, lh - 1)
|
||||||
|
cc = np.clip(col, 0, lw - 1)
|
||||||
|
sampled = R[rc, cc] # (256,256) float32, NaN where no data
|
||||||
|
valid = inside & np.isfinite(sampled)
|
||||||
|
|
||||||
|
# Map risk -> LUT index (NaN cells become 0 then are zeroed-out below)
|
||||||
|
lut = _color_lut()
|
||||||
|
safe = np.nan_to_num(sampled, nan=0.0)
|
||||||
|
idx = np.clip((safe * 255.0), 0, 255).astype(np.uint8)
|
||||||
|
rgba = lut[idx] # (256,256,4)
|
||||||
|
rgba[~valid] = (0, 0, 0, 0) # transparent outside data/boundary
|
||||||
|
|
||||||
|
img = Image.fromarray(rgba, mode="RGBA")
|
||||||
|
buf = io.BytesIO()
|
||||||
|
img.save(buf, format="PNG", optimize=False)
|
||||||
|
return buf.getvalue()
|
||||||
|
|
||||||
|
|
||||||
|
# ----------------------------------------------------------------------------
|
||||||
|
# Point query (for click-to-inspect)
|
||||||
|
# ----------------------------------------------------------------------------
|
||||||
|
@lru_cache(maxsize=16)
|
||||||
|
def grid_stats(date: str, day: int = 1) -> dict:
|
||||||
|
"""Lightweight aggregate stats over the in-boundary 100m grid for a date/day.
|
||||||
|
|
||||||
|
Computed from the cached raster, so this is cheap after the first tile build.
|
||||||
|
Replaces the old heavy per-viewport LOD fetch the overlay used to do.
|
||||||
|
"""
|
||||||
|
R = _risk_pyramid(date, day)[0]
|
||||||
|
finite = np.isfinite(R)
|
||||||
|
n = int(finite.sum())
|
||||||
|
if n == 0:
|
||||||
|
return {"cell_count": 0, "avg_risk": 0.0, "max_risk": 0.0,
|
||||||
|
"high_risk_count": 0, "forecast_day": day, "date": date}
|
||||||
|
vals = R[finite]
|
||||||
|
return {
|
||||||
|
"cell_count": n,
|
||||||
|
"avg_risk": round(float(vals.mean()), 4),
|
||||||
|
"max_risk": round(float(vals.max()), 4),
|
||||||
|
"high_risk_count": int((vals >= 0.8).sum()),
|
||||||
|
"forecast_day": day,
|
||||||
|
"date": date,
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
def query_cell(lat: float, lon: float, date: str, day: int = 1) -> dict:
|
||||||
|
"""Return the 100m cell risk at a lat/lon for the given date.
|
||||||
|
|
||||||
|
Includes all three forecast horizons (1d/3d/7d) so the click panel can show
|
||||||
|
them without a separate heavy grid fetch. `risk_value` is the requested day.
|
||||||
|
"""
|
||||||
|
row, col = latlon_to_rowcol(lat, lon)
|
||||||
|
|
||||||
|
def _sample(d: int) -> tuple[float, bool]:
|
||||||
|
v = _risk_pyramid(date, d)[0][row, col]
|
||||||
|
ok = bool(np.isfinite(v))
|
||||||
|
return (round(float(v), 4) if ok else 0.0), ok
|
||||||
|
|
||||||
|
r1, in_b = _sample(1)
|
||||||
|
r3, _ = _sample(3)
|
||||||
|
r7, _ = _sample(7)
|
||||||
|
current = {1: r1, 3: r3, 7: r7}[day]
|
||||||
|
return {
|
||||||
|
"grid_id": f"r{row}_c{col}",
|
||||||
|
"row": row,
|
||||||
|
"col": col,
|
||||||
|
"center_lat": round(MIN_LAT + (row + 0.5) * LAT_STEP, 6),
|
||||||
|
"center_lon": round(MIN_LON + (col + 0.5) * LON_STEP, 6),
|
||||||
|
"risk_value": current,
|
||||||
|
"risk_1d": r1,
|
||||||
|
"risk_3d": r3,
|
||||||
|
"risk_7d": r7,
|
||||||
|
"in_boundary": in_b,
|
||||||
|
"forecast_day": day,
|
||||||
|
"date": date,
|
||||||
|
}
|
||||||
44
deploy/CLAUDE.md
Normal file
44
deploy/CLAUDE.md
Normal file
@@ -0,0 +1,44 @@
|
|||||||
|
# Deploy — Docker Compose
|
||||||
|
|
||||||
|
## Stack
|
||||||
|
|
||||||
|
- Docker Compose with 3 services: `api` (FastAPI), `frontend` (nginx/React), `mlflow`
|
||||||
|
- Multi-stage Dockerfiles: build stage → production stage
|
||||||
|
- Env vars via `.env` file (see `.env.example` for template)
|
||||||
|
|
||||||
|
## Files
|
||||||
|
|
||||||
|
```
|
||||||
|
deploy/
|
||||||
|
docker-compose.yml # Main: api + frontend + PostgreSQL/PostGIS
|
||||||
|
docker-compose.mlflow.yml # MLflow tracking server
|
||||||
|
.env.example # Required env vars template
|
||||||
|
backend/Dockerfile # FastAPI app image
|
||||||
|
backend/.dockerignore
|
||||||
|
frontend/Dockerfile # nginx serving built React app
|
||||||
|
frontend/.dockerignore
|
||||||
|
```
|
||||||
|
|
||||||
|
## Running
|
||||||
|
|
||||||
|
```bash
|
||||||
|
# Full stack
|
||||||
|
docker compose -f deploy/docker-compose.yml up -d
|
||||||
|
|
||||||
|
# With MLflow
|
||||||
|
docker compose -f deploy/docker-compose.yml -f deploy/docker-compose.mlflow.yml up -d
|
||||||
|
```
|
||||||
|
|
||||||
|
## Conventions
|
||||||
|
|
||||||
|
- Never commit `.env` — use `.env.example` as template
|
||||||
|
- Dockerfiles use multi-stage builds to minimize image size
|
||||||
|
- Frontend is built during Docker build, served by nginx
|
||||||
|
- Backend runs uvicorn with `--host 0.0.0.0` inside container
|
||||||
|
|
||||||
|
## Anti-Patterns
|
||||||
|
|
||||||
|
- Don't hardcode ports in docker-compose — use env vars
|
||||||
|
- Don't commit secrets to `.env.example` — placeholder values only
|
||||||
|
- Don't run as root in Dockerfiles — create non-root user
|
||||||
|
- Don't skip `.dockerignore` — keeps build context small
|
||||||
90
frontend/e2e/clinical.spec.ts
Normal file
90
frontend/e2e/clinical.spec.ts
Normal file
@@ -0,0 +1,90 @@
|
|||||||
|
/**
|
||||||
|
* 住院临床分析页(/analysis/clinical)验收测试。
|
||||||
|
* 与 user-flows.spec.ts 一致的鉴权策略:addInitScript 注入 cbpoa_token,
|
||||||
|
* 用 page.route 拦截 /api/**,对 inpatient-clinical 返回合法小样本,其余返回 {}。
|
||||||
|
*/
|
||||||
|
import { test, expect, Page } from '@playwright/test';
|
||||||
|
import { TESTIDS } from '../src/utils/testids';
|
||||||
|
|
||||||
|
const CLINICAL_FIXTURE = {
|
||||||
|
kpis: {
|
||||||
|
total_admissions: 5822,
|
||||||
|
median_los_days: 4,
|
||||||
|
cure_rate: 0.991,
|
||||||
|
emergency_admit_ratio: 0.47,
|
||||||
|
},
|
||||||
|
los_histogram: [
|
||||||
|
{ bin_label: '1-2', count: 1200 },
|
||||||
|
{ bin_label: '3-4', count: 2100 },
|
||||||
|
{ bin_label: '5-7', count: 1500 },
|
||||||
|
],
|
||||||
|
los_by_disease: [
|
||||||
|
{ diagnosis: '肺炎', p25: 3, median: 5, p75: 7, n: 800 },
|
||||||
|
{ diagnosis: '支气管炎', p25: 2, median: 4, p75: 6, n: 600 },
|
||||||
|
],
|
||||||
|
outcome_counts: [
|
||||||
|
{ outcome: '治愈', count: 3474 },
|
||||||
|
{ outcome: '好转', count: 2298 },
|
||||||
|
{ outcome: '其他', count: 35 },
|
||||||
|
{ outcome: '未愈', count: 12 },
|
||||||
|
{ outcome: '死亡', count: 3 },
|
||||||
|
],
|
||||||
|
admission_route_counts: [
|
||||||
|
{ route: '急诊', count: 2700 },
|
||||||
|
{ route: '门诊', count: 3122 },
|
||||||
|
],
|
||||||
|
bmi_by_age_band: [
|
||||||
|
{ age_band: '0-2', p25: 14, median: 16, p75: 18, n: 400 },
|
||||||
|
{ age_band: '3-6', p25: 15, median: 17, p75: 19, n: 500 },
|
||||||
|
],
|
||||||
|
};
|
||||||
|
|
||||||
|
async function seedAuthAndMockApi(page: Page) {
|
||||||
|
await page.addInitScript(() => {
|
||||||
|
localStorage.setItem('cbpoa_token', 'e2e-test-token');
|
||||||
|
});
|
||||||
|
|
||||||
|
await page.route('/api/**', (route) => {
|
||||||
|
const url = route.request().url();
|
||||||
|
|
||||||
|
if (url.includes('/stats/inpatient-clinical')) {
|
||||||
|
route.fulfill({
|
||||||
|
status: 200,
|
||||||
|
contentType: 'application/json',
|
||||||
|
body: JSON.stringify(CLINICAL_FIXTURE),
|
||||||
|
});
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 其余接口返回空对象,本页不依赖。
|
||||||
|
route.fulfill({
|
||||||
|
status: 200,
|
||||||
|
contentType: 'application/json',
|
||||||
|
body: JSON.stringify({}),
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
test.describe('住院临床分析页', () => {
|
||||||
|
test.beforeEach(async ({ page }) => {
|
||||||
|
await seedAuthAndMockApi(page);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('deep-link /analysis/clinical mounts page-clinical + clinical-kpis', async ({ page }) => {
|
||||||
|
await page.goto('/analysis/clinical');
|
||||||
|
await expect(page.locator(`[data-testid="${TESTIDS.pageClinical}"]`)).toBeVisible();
|
||||||
|
await expect(page.locator(`[data-testid="${TESTIDS.clinicalKpis}"]`)).toBeVisible();
|
||||||
|
});
|
||||||
|
|
||||||
|
test('no horizontal scroll at 375px', async ({ page }) => {
|
||||||
|
await page.setViewportSize({ width: 375, height: 812 });
|
||||||
|
await page.goto('/analysis/clinical');
|
||||||
|
await expect(page.locator(`[data-testid="${TESTIDS.pageClinical}"]`)).toBeVisible();
|
||||||
|
await expect(page.locator(`[data-testid="${TESTIDS.clinicalKpis}"]`)).toBeVisible();
|
||||||
|
|
||||||
|
const noHorizontalScroll = await page.evaluate(
|
||||||
|
() => document.documentElement.scrollWidth <= document.documentElement.clientWidth
|
||||||
|
);
|
||||||
|
expect(noHorizontalScroll).toBe(true);
|
||||||
|
});
|
||||||
|
});
|
||||||
143
frontend/e2e/doctor-view.spec.ts
Normal file
143
frontend/e2e/doctor-view.spec.ts
Normal file
@@ -0,0 +1,143 @@
|
|||||||
|
/**
|
||||||
|
* Phase-3 acceptance tests: role-aware 预警 (alerts) view.
|
||||||
|
*
|
||||||
|
* Two view-preset invariants (D2 — frontend presets, NOT access control):
|
||||||
|
*
|
||||||
|
* 1. PRIVACY INVARIANT (doctor / ?view=cluster): the doctor sees ONLY the aggregated
|
||||||
|
* density raster + the disease filter — ZERO individual patient/case point markers.
|
||||||
|
* The page mirrors every individual marker it would actually render into a hidden
|
||||||
|
* data-testid="patient-point" element (the live Leaflet CircleMarkers are canvas/SVG
|
||||||
|
* objects with no testid and can't be counted directly). In cluster mode the page
|
||||||
|
* forces showAlertMarkers=false, so that mirror set is empty → patient-point count 0.
|
||||||
|
*
|
||||||
|
* 2. 官员 (official) GRID-HIDE: the 100m 网格 is meaningless for leadership, so the grid
|
||||||
|
* toggle wrapper (data-testid="grid-layer-wrapper") is not rendered at all.
|
||||||
|
*
|
||||||
|
* Auth + API mocking mirror e2e/user-flows.spec.ts so the suite runs hermetically
|
||||||
|
* (no live :8000 backend). Role is seeded via localStorage['cbpoa_role'].
|
||||||
|
*/
|
||||||
|
import { test, expect, Page } from '@playwright/test';
|
||||||
|
import { TESTIDS } from '../src/utils/testids';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Seed auth token (+ optional role) and mock all /api/** calls before page load.
|
||||||
|
* Response shapes copied from user-flows.spec.ts.
|
||||||
|
*/
|
||||||
|
async function seedAuthAndMockApi(page: Page, role?: string) {
|
||||||
|
await page.addInitScript((r) => {
|
||||||
|
localStorage.setItem('cbpoa_token', 'e2e-test-token');
|
||||||
|
if (r) localStorage.setItem('cbpoa_role', r);
|
||||||
|
}, role ?? '');
|
||||||
|
|
||||||
|
await page.route('/api/**', (route) => {
|
||||||
|
const url = route.request().url();
|
||||||
|
|
||||||
|
if (url.includes('/alerts')) {
|
||||||
|
route.fulfill({
|
||||||
|
status: 200,
|
||||||
|
contentType: 'application/json',
|
||||||
|
body: JSON.stringify({ alerts: [], total: 0 }),
|
||||||
|
});
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (url.includes('/history/aggregated')) {
|
||||||
|
route.fulfill({
|
||||||
|
status: 200,
|
||||||
|
contentType: 'application/json',
|
||||||
|
body: JSON.stringify({ aggregations: [], total_records: 0 }),
|
||||||
|
});
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (url.includes('/grids')) {
|
||||||
|
route.fulfill({
|
||||||
|
status: 200,
|
||||||
|
contentType: 'application/json',
|
||||||
|
body: JSON.stringify({ type: 'FeatureCollection', features: [] }),
|
||||||
|
});
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (url.includes('/cases/demographics')) {
|
||||||
|
route.fulfill({
|
||||||
|
status: 200,
|
||||||
|
contentType: 'application/json',
|
||||||
|
body: JSON.stringify({
|
||||||
|
age_distribution: [],
|
||||||
|
gender_split: { male: { outpatient: 0, inpatient: 0 }, female: { outpatient: 0, inpatient: 0 } },
|
||||||
|
age_diagnosis_matrix: [],
|
||||||
|
}),
|
||||||
|
});
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (url.includes('/cases/diagnosis-distribution') || url.includes('/cases/disease-seasonality')) {
|
||||||
|
route.fulfill({ status: 200, contentType: 'application/json', body: JSON.stringify([]) });
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (url.includes('/diagnoses') || url.includes('/diagnosis-list')) {
|
||||||
|
route.fulfill({ status: 200, contentType: 'application/json', body: JSON.stringify([]) });
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (url.includes('/cases/districts') || url.includes('/districts')) {
|
||||||
|
route.fulfill({ status: 200, contentType: 'application/json', body: JSON.stringify([]) });
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (url.includes('/cases/trend') || url.includes('/cases')) {
|
||||||
|
route.fulfill({
|
||||||
|
status: 200,
|
||||||
|
contentType: 'application/json',
|
||||||
|
body: JSON.stringify({ data: [], total: 0 }),
|
||||||
|
});
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
route.fulfill({
|
||||||
|
status: 200,
|
||||||
|
contentType: 'application/json',
|
||||||
|
body: JSON.stringify({ data: [], items: [], total: 0 }),
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
test.describe('role-aware 预警 view (Phase 3)', () => {
|
||||||
|
test.use({ viewport: { width: 1280, height: 800 } });
|
||||||
|
|
||||||
|
test('医生 /alerts?view=cluster: cluster-view mounts, disease filter present, ZERO patient points', async ({
|
||||||
|
page,
|
||||||
|
}) => {
|
||||||
|
await seedAuthAndMockApi(page, 'doctor');
|
||||||
|
await page.goto('/alerts?view=cluster');
|
||||||
|
|
||||||
|
// Page + the aggregated density (cluster) map both mount.
|
||||||
|
await expect(page.locator(`[data-testid="${TESTIDS.pageAlerts}"]`)).toBeVisible();
|
||||||
|
await expect(page.locator(`[data-testid="${TESTIDS.clusterView}"]`)).toBeVisible();
|
||||||
|
|
||||||
|
// The disease filter is the doctor's core tool — it must be on the page.
|
||||||
|
await expect(page.getByText('按病种筛选')).toBeVisible();
|
||||||
|
|
||||||
|
// PRIVACY INVARIANT: not a single individual patient/case point may be rendered.
|
||||||
|
// Asserted at the data level (the mirrored DOM set), independent of Leaflet internals.
|
||||||
|
await expect(page.getByTestId(TESTIDS.patientPoint)).toHaveCount(0);
|
||||||
|
|
||||||
|
// The 预警标记 toggle (which would turn individual markers on) must be absent,
|
||||||
|
// so there is no way for the doctor to opt out of the privacy invariant.
|
||||||
|
await expect(page.getByRole('button', { name: '预警标记' })).toHaveCount(0);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('官员 /alerts: 100m grid hidden — grid-layer-wrapper not rendered', async ({ page }) => {
|
||||||
|
await seedAuthAndMockApi(page, 'official');
|
||||||
|
await page.goto('/alerts');
|
||||||
|
|
||||||
|
await expect(page.locator(`[data-testid="${TESTIDS.pageAlerts}"]`)).toBeVisible();
|
||||||
|
|
||||||
|
// The grid toggle wrapper must be entirely absent for leadership.
|
||||||
|
await expect(page.getByTestId(TESTIDS.gridLayerWrapper)).toHaveCount(0);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('admin /alerts: full behavior — grid toggle present, no forced cluster view', async ({ page }) => {
|
||||||
|
await seedAuthAndMockApi(page, 'admin');
|
||||||
|
await page.goto('/alerts');
|
||||||
|
|
||||||
|
await expect(page.locator(`[data-testid="${TESTIDS.pageAlerts}"]`)).toBeVisible();
|
||||||
|
// Admin keeps the grid toggle and is NOT forced into cluster view.
|
||||||
|
await expect(page.getByTestId(TESTIDS.gridLayerWrapper)).toHaveCount(1);
|
||||||
|
await expect(page.locator(`[data-testid="${TESTIDS.clusterView}"]`)).toHaveCount(0);
|
||||||
|
});
|
||||||
|
});
|
||||||
78
frontend/e2e/granularity.spec.ts
Normal file
78
frontend/e2e/granularity.spec.ts
Normal file
@@ -0,0 +1,78 @@
|
|||||||
|
import { test, expect, Page } from '@playwright/test';
|
||||||
|
import { TESTIDS } from '../src/utils/testids';
|
||||||
|
|
||||||
|
// 与 user-flows.spec.ts 一致的鉴权策略:注入 token 绕过登录门,并 mock /api/**,
|
||||||
|
// 让用例脱离活的后端 hermetic 运行。
|
||||||
|
async function seedAuthAndMockApi(page: Page) {
|
||||||
|
await page.addInitScript(() => {
|
||||||
|
localStorage.setItem('cbpoa_token', 'e2e-test-token');
|
||||||
|
});
|
||||||
|
await page.route('/api/**', (route) => {
|
||||||
|
const url = route.request().url();
|
||||||
|
if (url.includes('/history/aggregated')) {
|
||||||
|
route.fulfill({ status: 200, contentType: 'application/json', body: JSON.stringify({ aggregations: [], total_records: 0 }) });
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (url.includes('/grids')) {
|
||||||
|
route.fulfill({ status: 200, contentType: 'application/json', body: JSON.stringify({ type: 'FeatureCollection', features: [] }) });
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (url.includes('/streets')) {
|
||||||
|
route.fulfill({ status: 200, contentType: 'application/json', body: JSON.stringify({ streets: [] }) });
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (url.includes('/cases/districts') || url.includes('/districts')) {
|
||||||
|
route.fulfill({ status: 200, contentType: 'application/json', body: JSON.stringify([]) });
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (url.includes('/cases/trend') || url.includes('/cases')) {
|
||||||
|
route.fulfill({ status: 200, contentType: 'application/json', body: JSON.stringify({ data: [], trend: [], total: 0 }) });
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
route.fulfill({ status: 200, contentType: 'application/json', body: JSON.stringify({ data: [], items: [], total: 0 }) });
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
// 粒度(granularity)以 URL query 参数为真相来源(source of truth)。
|
||||||
|
// 监测页通过 useSearchParams 读取它,drilldownStore 单向派生。
|
||||||
|
test.describe('monitoring granularity URL source-of-truth', () => {
|
||||||
|
test.beforeEach(async ({ page }) => {
|
||||||
|
await seedAuthAndMockApi(page);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('deep-link granularity=street mounts page and reflects street in control', async ({ page }) => {
|
||||||
|
await page.goto('/monitoring?granularity=street');
|
||||||
|
|
||||||
|
await expect(page.getByTestId(TESTIDS.pageMonitoring)).toBeVisible();
|
||||||
|
|
||||||
|
const control = page.getByTestId(TESTIDS.granularityControl);
|
||||||
|
await expect(control).toBeVisible();
|
||||||
|
// 街道分段为激活态(Segmented 给激活按钮加 bg-primary)。
|
||||||
|
const streetBtn = page.getByTestId(`${TESTIDS.granularityControl}-street`);
|
||||||
|
await expect(streetBtn).toHaveClass(/bg-primary/);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('clicking a granularity control updates the URL granularity param', async ({ page }) => {
|
||||||
|
await page.goto('/monitoring?granularity=street');
|
||||||
|
await expect(page.getByTestId(TESTIDS.pageMonitoring)).toBeVisible();
|
||||||
|
|
||||||
|
// 切到「区域」应把 URL 写为 granularity=district。
|
||||||
|
await page.getByTestId(`${TESTIDS.granularityControl}-district`).click();
|
||||||
|
await expect(page).toHaveURL(/granularity=district/);
|
||||||
|
|
||||||
|
// 切到「全市」应把 URL 写为 granularity=city。
|
||||||
|
await page.getByTestId(`${TESTIDS.granularityControl}-city`).click();
|
||||||
|
await expect(page).toHaveURL(/granularity=city/);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('deep-link granularity=district survives a reload', async ({ page }) => {
|
||||||
|
await page.goto('/monitoring?granularity=district');
|
||||||
|
await expect(page.getByTestId(TESTIDS.pageMonitoring)).toBeVisible();
|
||||||
|
await expect(page).toHaveURL(/granularity=district/);
|
||||||
|
|
||||||
|
await page.reload();
|
||||||
|
await expect(page.getByTestId(TESTIDS.pageMonitoring)).toBeVisible();
|
||||||
|
await expect(page).toHaveURL(/granularity=district/);
|
||||||
|
await expect(page.getByTestId(`${TESTIDS.granularityControl}-district`)).toHaveClass(/bg-primary/);
|
||||||
|
});
|
||||||
|
});
|
||||||
119
frontend/e2e/overview.spec.ts
Normal file
119
frontend/e2e/overview.spec.ts
Normal file
@@ -0,0 +1,119 @@
|
|||||||
|
/**
|
||||||
|
* 综合概览大屏 (/overview) 验收测试。
|
||||||
|
*
|
||||||
|
* 与 user-flows.spec.ts 一致:用 addInitScript 注入 cbpoa_token 绕过登录门,
|
||||||
|
* page.route 拦截 /api/** 使套件 hermetic(无需 :8000)。/wuhan_districts.geojson
|
||||||
|
* 走真实静态资源(dev server 提供),由 Leaflet 取用。
|
||||||
|
*/
|
||||||
|
import { test, expect, Page } from '@playwright/test';
|
||||||
|
import { TESTIDS } from '../src/utils/testids';
|
||||||
|
|
||||||
|
/** 13 区里造两条数据,断言 choropleth 能着色、toggle 能切换。 */
|
||||||
|
function districtPayload() {
|
||||||
|
return {
|
||||||
|
districts: [
|
||||||
|
{ district: '武昌区', outpatient: 120, inpatient: 30, total: 150, outpatient_ratio: 0.8, inpatient_ratio: 0.2 },
|
||||||
|
{ district: '江岸', outpatient: 60, inpatient: 10, total: 70, outpatient_ratio: 0.86, inpatient_ratio: 0.14 },
|
||||||
|
],
|
||||||
|
total: 220,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
async function seedAuthAndMockApi(page: Page) {
|
||||||
|
await page.addInitScript(() => {
|
||||||
|
localStorage.setItem('cbpoa_token', 'e2e-test-token');
|
||||||
|
});
|
||||||
|
|
||||||
|
await page.route('/api/**', (route) => {
|
||||||
|
const url = route.request().url();
|
||||||
|
|
||||||
|
const json = (body: unknown) =>
|
||||||
|
route.fulfill({ status: 200, contentType: 'application/json', body: JSON.stringify(body) });
|
||||||
|
|
||||||
|
if (url.includes('/alerts')) {
|
||||||
|
return json({ alerts: [], total: 0 });
|
||||||
|
}
|
||||||
|
if (url.includes('/cases/stats')) {
|
||||||
|
return json({
|
||||||
|
total_outpatient: 1000,
|
||||||
|
total_inpatient: 200,
|
||||||
|
date_range: { start: '2023-01-01', end: '2023-12-01' },
|
||||||
|
top_districts: [],
|
||||||
|
top_diagnoses: [
|
||||||
|
{ diagnosis: '上呼吸道感染', outpatient: 300, inpatient: 40 },
|
||||||
|
{ diagnosis: '肺炎', outpatient: 120, inpatient: 80 },
|
||||||
|
],
|
||||||
|
});
|
||||||
|
}
|
||||||
|
if (url.includes('/cases/trend')) {
|
||||||
|
return json({
|
||||||
|
trend: [
|
||||||
|
{ date: '2023-11-01', outpatient: 10, inpatient: 2, total: 12 },
|
||||||
|
{ date: '2023-11-02', outpatient: 14, inpatient: 3, total: 17 },
|
||||||
|
],
|
||||||
|
summary: {
|
||||||
|
total_outpatient: 24,
|
||||||
|
total_inpatient: 5,
|
||||||
|
period_count: 2,
|
||||||
|
avg_daily_outpatient: 12,
|
||||||
|
avg_daily_inpatient: 2.5,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
}
|
||||||
|
if (url.includes('/cases/districts')) {
|
||||||
|
return json(districtPayload());
|
||||||
|
}
|
||||||
|
if (url.includes('/risk/stats')) {
|
||||||
|
return json({ high_risk_count: 7, total_grids: 100, avg_risk: 0.4 });
|
||||||
|
}
|
||||||
|
if (url.includes('/environment/pollutants')) {
|
||||||
|
return json({ data: [{ date: '2023-11-01', AQI: 80 }, { date: '2023-11-02', AQI: 95 }] });
|
||||||
|
}
|
||||||
|
|
||||||
|
return json({ data: [], items: [], total: 0 });
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
test.describe('Overview 大屏', () => {
|
||||||
|
test.use({ viewport: { width: 1280, height: 900 } });
|
||||||
|
|
||||||
|
test.beforeEach(async ({ page }) => {
|
||||||
|
await seedAuthAndMockApi(page);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('renders kpi-row, choropleth, as-of badge and metric toggle', async ({ page }) => {
|
||||||
|
await page.goto('/overview');
|
||||||
|
|
||||||
|
await expect(page.locator(`[data-testid="${TESTIDS.pageOverview}"]`)).toBeVisible();
|
||||||
|
await expect(page.locator(`[data-testid="${TESTIDS.kpiRow}"]`)).toBeVisible();
|
||||||
|
await expect(page.locator(`[data-testid="${TESTIDS.choroplethWrapper}"]`)).toBeVisible();
|
||||||
|
|
||||||
|
// Literal honesty badge — exact text.
|
||||||
|
const badge = page.locator(`[data-testid="${TESTIDS.asofBadge}"]`);
|
||||||
|
await expect(badge).toBeVisible();
|
||||||
|
await expect(badge).toHaveText('数据截至2023-12');
|
||||||
|
});
|
||||||
|
|
||||||
|
test('门诊/住院 toggle switches active segment without error', async ({ page }) => {
|
||||||
|
await page.goto('/overview');
|
||||||
|
await expect(page.locator(`[data-testid="${TESTIDS.choroplethWrapper}"]`)).toBeVisible();
|
||||||
|
|
||||||
|
const outBtn = page.locator(`[data-testid="${TESTIDS.outinpatientToggle}-outpatient"]`);
|
||||||
|
const inBtn = page.locator(`[data-testid="${TESTIDS.outinpatientToggle}-inpatient"]`);
|
||||||
|
const allBtn = page.locator(`[data-testid="${TESTIDS.outinpatientToggle}-all"]`);
|
||||||
|
|
||||||
|
// Default: 全部 active (primary background).
|
||||||
|
await expect(allBtn).toHaveClass(/bg-primary/);
|
||||||
|
|
||||||
|
await outBtn.click();
|
||||||
|
await expect(outBtn).toHaveClass(/bg-primary/);
|
||||||
|
await expect(allBtn).not.toHaveClass(/bg-primary/);
|
||||||
|
|
||||||
|
await inBtn.click();
|
||||||
|
await expect(inBtn).toHaveClass(/bg-primary/);
|
||||||
|
await expect(outBtn).not.toHaveClass(/bg-primary/);
|
||||||
|
|
||||||
|
// Wrapper still mounted after toggling — no render crash.
|
||||||
|
await expect(page.locator(`[data-testid="${TESTIDS.choroplethWrapper}"]`)).toBeVisible();
|
||||||
|
});
|
||||||
|
});
|
||||||
232
frontend/e2e/perf.spec.ts
Normal file
232
frontend/e2e/perf.spec.ts
Normal file
@@ -0,0 +1,232 @@
|
|||||||
|
/**
|
||||||
|
* Performance-measurement harness for the leadership 大屏 (/overview).
|
||||||
|
*
|
||||||
|
* Runs ONLY in the dedicated `perf` Playwright project (see playwright.config.ts
|
||||||
|
* testMatch) so emulated network throttling never pollutes the functional suite.
|
||||||
|
*
|
||||||
|
* What it measures:
|
||||||
|
* 1. LCP (Largest Contentful Paint) of /overview under emulated Fast 3G.
|
||||||
|
* 2. Client-side route-transition time from /overview → /monitoring.
|
||||||
|
*
|
||||||
|
* Throttling model: /api/** is mocked to resolve INSTANTLY (see seedAuthAndMockApi),
|
||||||
|
* so the backend contributes ~0ms. That is deliberate — it isolates the realistic
|
||||||
|
* SPA cost on a slow link: the *static asset graph* (app JS/CSS bundle + the
|
||||||
|
* /wuhan_districts.geojson choropleth payload, which is served by the real dev
|
||||||
|
* server, not mocked). Fast 3G therefore shapes exactly the bytes a cold-cache
|
||||||
|
* leadership client must pull before first paint, which is what LCP should reflect.
|
||||||
|
*
|
||||||
|
* Assertion policy (per the UX-modernization plan): LCP and route-transition
|
||||||
|
* targets (2500ms LCP / 800ms transition) are REPORTED, not hard CI gates — a
|
||||||
|
* miss under throttle on a loaded CI box must not fail the build. We therefore
|
||||||
|
* record each number against its target as a test annotation + console line and
|
||||||
|
* let the test PASS regardless of the target. (Note: `expect.soft` would still
|
||||||
|
* mark the test failed at teardown, so it's the wrong tool for a report-only
|
||||||
|
* target — annotations are.) Hard assertions guard ONLY that the measurement
|
||||||
|
* machinery worked: LCP was observed (> 0) and the nav actually landed.
|
||||||
|
*
|
||||||
|
* Caveat on absolute values: this runs against the Vite DEV server (unbundled,
|
||||||
|
* unminified ESM with per-module requests). Dev LCP under Fast 3G is therefore
|
||||||
|
* far higher than a production build would be — these numbers are a relative
|
||||||
|
* regression signal for this harness, not a production SLA.
|
||||||
|
*/
|
||||||
|
import { test, expect, Page } from '@playwright/test';
|
||||||
|
import { TESTIDS } from '../src/utils/testids';
|
||||||
|
|
||||||
|
// Reported (soft) targets — see file header.
|
||||||
|
const LCP_TARGET_MS = 2500;
|
||||||
|
const ROUTE_TRANSITION_TARGET_MS = 800;
|
||||||
|
|
||||||
|
// Emulated "Fast 3G" network conditions (Chrome DevTools preset).
|
||||||
|
const FAST_3G = {
|
||||||
|
offline: false,
|
||||||
|
downloadThroughput: (1.6 * 1024 * 1024) / 8, // 1.6 Mbps
|
||||||
|
uploadThroughput: (750 * 1024) / 8, // 750 Kbps
|
||||||
|
latency: 150, // ms RTT
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Seed auth + mock /api/** so the page renders hermetically. Mirrors the helper
|
||||||
|
* in user-flows.spec.ts, with one deliberate difference: /wuhan_districts.geojson
|
||||||
|
* is a real static asset and is NOT under /api, so page.route('/api/**') already
|
||||||
|
* lets it pass through to the dev server (the realistic, throttled payload).
|
||||||
|
*/
|
||||||
|
async function seedAuthAndMockApi(page: Page) {
|
||||||
|
await page.addInitScript(() => {
|
||||||
|
localStorage.setItem('cbpoa_token', 'e2e-test-token');
|
||||||
|
});
|
||||||
|
|
||||||
|
// Mock backend responses instantly so Fast-3G shapes only the static asset
|
||||||
|
// graph (JS/CSS + geojson), not API latency.
|
||||||
|
await page.route('/api/**', (route) => {
|
||||||
|
const url = route.request().url();
|
||||||
|
|
||||||
|
if (url.includes('/alerts')) {
|
||||||
|
route.fulfill({
|
||||||
|
status: 200,
|
||||||
|
contentType: 'application/json',
|
||||||
|
body: JSON.stringify({ alerts: [], total: 0 }),
|
||||||
|
});
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (url.includes('/history/aggregated')) {
|
||||||
|
route.fulfill({
|
||||||
|
status: 200,
|
||||||
|
contentType: 'application/json',
|
||||||
|
body: JSON.stringify({ aggregations: [], total_records: 0 }),
|
||||||
|
});
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (url.includes('/grids')) {
|
||||||
|
route.fulfill({
|
||||||
|
status: 200,
|
||||||
|
contentType: 'application/json',
|
||||||
|
body: JSON.stringify({ type: 'FeatureCollection', features: [] }),
|
||||||
|
});
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (url.includes('/cases/demographics')) {
|
||||||
|
route.fulfill({
|
||||||
|
status: 200,
|
||||||
|
contentType: 'application/json',
|
||||||
|
body: JSON.stringify({
|
||||||
|
age_distribution: [],
|
||||||
|
gender_split: { male: { outpatient: 0, inpatient: 0 }, female: { outpatient: 0, inpatient: 0 } },
|
||||||
|
age_diagnosis_matrix: [],
|
||||||
|
}),
|
||||||
|
});
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (url.includes('/cases/diagnosis-distribution') || url.includes('/cases/disease-seasonality')) {
|
||||||
|
route.fulfill({ status: 200, contentType: 'application/json', body: JSON.stringify([]) });
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (url.includes('/cases/districts') || url.includes('/districts')) {
|
||||||
|
route.fulfill({ status: 200, contentType: 'application/json', body: JSON.stringify([]) });
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (url.includes('/cases/trend') || url.includes('/cases')) {
|
||||||
|
route.fulfill({
|
||||||
|
status: 200,
|
||||||
|
contentType: 'application/json',
|
||||||
|
body: JSON.stringify({ data: [], total: 0 }),
|
||||||
|
});
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Default fallback — safe empty shape.
|
||||||
|
route.fulfill({
|
||||||
|
status: 200,
|
||||||
|
contentType: 'application/json',
|
||||||
|
body: JSON.stringify({ data: [], items: [], total: 0 }),
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
test.describe('Performance — /overview under emulated Fast 3G', () => {
|
||||||
|
test('LCP and route-transition are measured and reported', async ({ page }, testInfo) => {
|
||||||
|
// Fast-3G throttling makes the cold asset-graph download slow; the default 30s
|
||||||
|
// test budget can be eaten by the initial /overview load alone. Give the whole
|
||||||
|
// measurement flow generous headroom — this bounds the harness, not the metrics.
|
||||||
|
test.setTimeout(120_000);
|
||||||
|
await seedAuthAndMockApi(page);
|
||||||
|
|
||||||
|
// Install the LCP observer BEFORE any navigation so it captures the very
|
||||||
|
// first paint. buffered:true also replays entries emitted before observe().
|
||||||
|
await page.addInitScript(() => {
|
||||||
|
(window as unknown as { __lcp: number }).__lcp = 0;
|
||||||
|
new PerformanceObserver((list) => {
|
||||||
|
const entries = list.getEntries();
|
||||||
|
(window as unknown as { __lcp: number }).__lcp = entries[entries.length - 1].startTime;
|
||||||
|
}).observe({ type: 'largest-contentful-paint', buffered: true });
|
||||||
|
});
|
||||||
|
|
||||||
|
// Apply Fast 3G throttling via CDP before navigating.
|
||||||
|
const client = await page.context().newCDPSession(page);
|
||||||
|
await client.send('Network.enable');
|
||||||
|
await client.send('Network.emulateNetworkConditions', FAST_3G);
|
||||||
|
|
||||||
|
// --- LCP measurement -----------------------------------------------------
|
||||||
|
// Generous wait: under Fast 3G the throttled JS bundle download dominates, so
|
||||||
|
// first meaningful paint legitimately exceeds the 5s default expect timeout.
|
||||||
|
// The LCP NUMBER we read is the real measured value — this timeout only bounds
|
||||||
|
// how long we'll wait for the asset graph to arrive before failing the harness.
|
||||||
|
await page.goto('/overview');
|
||||||
|
await expect(page.locator(`[data-testid="${TESTIDS.kpiRow}"]`)).toBeVisible({ timeout: 30_000 });
|
||||||
|
|
||||||
|
// LCP finalizes on the last contentful paint; give the observer a beat to flush
|
||||||
|
// the entry for the kpi-row we just saw before reading it.
|
||||||
|
await page.waitForTimeout(200);
|
||||||
|
const lcp = await page.evaluate(() => (window as unknown as { __lcp: number }).__lcp);
|
||||||
|
|
||||||
|
// --- Route-transition measurement ---------------------------------------
|
||||||
|
// Expand the 监测 module if its NavLink is collapsed, then click it.
|
||||||
|
const railSel = `[data-testid="${TESTIDS.sidebarRail}"]`;
|
||||||
|
const navMonitoring = page.locator(`${railSel} [data-testid="${TESTIDS.navMonitoring}"]`);
|
||||||
|
if (!(await navMonitoring.isVisible())) {
|
||||||
|
await page.locator(`${railSel} button`).filter({ hasText: '监测' }).first().click();
|
||||||
|
}
|
||||||
|
await expect(navMonitoring).toBeVisible();
|
||||||
|
|
||||||
|
const t0 = await page.evaluate(() => performance.now());
|
||||||
|
await navMonitoring.click();
|
||||||
|
await expect(page.locator(`[data-testid="${TESTIDS.pageMonitoring}"]`)).toBeVisible({
|
||||||
|
timeout: 30_000,
|
||||||
|
});
|
||||||
|
const t1 = await page.evaluate(() => performance.now());
|
||||||
|
const routeTransitionMs = t1 - t0;
|
||||||
|
|
||||||
|
// --- Report --------------------------------------------------------------
|
||||||
|
// eslint-disable-next-line no-console
|
||||||
|
console.log(`[perf] /overview LCP (Fast 3G): ${lcp.toFixed(0)} ms (target < ${LCP_TARGET_MS})`);
|
||||||
|
// eslint-disable-next-line no-console
|
||||||
|
console.log(
|
||||||
|
`[perf] /overview → /monitoring route transition: ${routeTransitionMs.toFixed(0)} ms (target < ${ROUTE_TRANSITION_TARGET_MS})`
|
||||||
|
);
|
||||||
|
await testInfo.attach('perf-metrics', {
|
||||||
|
contentType: 'application/json',
|
||||||
|
body: JSON.stringify(
|
||||||
|
{
|
||||||
|
lcpMs: Math.round(lcp),
|
||||||
|
lcpTargetMs: LCP_TARGET_MS,
|
||||||
|
routeTransitionMs: Math.round(routeTransitionMs),
|
||||||
|
routeTransitionTargetMs: ROUTE_TRANSITION_TARGET_MS,
|
||||||
|
network: 'Fast 3G (emulated via CDP)',
|
||||||
|
},
|
||||||
|
null,
|
||||||
|
2
|
||||||
|
),
|
||||||
|
});
|
||||||
|
|
||||||
|
// --- Reported targets (NOT gates) ---------------------------------------
|
||||||
|
// Record each metric vs. its target as a passing/over annotation. A miss is
|
||||||
|
// visible in the report and console but does NOT fail the test.
|
||||||
|
const lcpVerdict = lcp < LCP_TARGET_MS ? 'within' : 'over';
|
||||||
|
const routeVerdict = routeTransitionMs < ROUTE_TRANSITION_TARGET_MS ? 'within' : 'over';
|
||||||
|
testInfo.annotations.push({
|
||||||
|
type: 'perf-lcp',
|
||||||
|
description: `${Math.round(lcp)}ms (target ${LCP_TARGET_MS}ms — ${lcpVerdict})`,
|
||||||
|
});
|
||||||
|
testInfo.annotations.push({
|
||||||
|
type: 'perf-route-transition',
|
||||||
|
description: `${Math.round(routeTransitionMs)}ms (target ${ROUTE_TRANSITION_TARGET_MS}ms — ${routeVerdict})`,
|
||||||
|
});
|
||||||
|
if (lcpVerdict === 'over' || routeVerdict === 'over') {
|
||||||
|
// eslint-disable-next-line no-console
|
||||||
|
console.warn(
|
||||||
|
`[perf] target exceeded (LCP ${lcpVerdict}, route ${routeVerdict}) — reported, not gated (dev-server throttled run).`
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
// --- Hard assertions (gates) --------------------------------------------
|
||||||
|
// Only the measurement machinery is gated: the observer fired and the nav
|
||||||
|
// landed (page-monitoring visibility is already hard-asserted above).
|
||||||
|
expect(lcp, 'LCP observer should have recorded a paint').toBeGreaterThan(0);
|
||||||
|
expect(routeTransitionMs, 'route transition should elapse measurable time').toBeGreaterThan(0);
|
||||||
|
});
|
||||||
|
});
|
||||||
135
frontend/e2e/responsive.spec.ts
Normal file
135
frontend/e2e/responsive.spec.ts
Normal file
@@ -0,0 +1,135 @@
|
|||||||
|
/**
|
||||||
|
* Phase-4 responsive acceptance: every analysis page must be usable at 375px
|
||||||
|
* (the narrowest mobile viewport in D4) with NO horizontal scroll.
|
||||||
|
*
|
||||||
|
* Auth + backend mocking mirror e2e/user-flows.spec.ts (seedAuthAndMockApi):
|
||||||
|
* seed localStorage['cbpoa_token'] so the login gate is skipped, then mock all
|
||||||
|
* /api/** calls so the suite runs hermetically without a live :8000 backend.
|
||||||
|
*/
|
||||||
|
import { test, expect, Page } from '@playwright/test';
|
||||||
|
import { TESTIDS } from '../src/utils/testids';
|
||||||
|
|
||||||
|
// Each analysis route paired with its page-* mount testid.
|
||||||
|
const ANALYSIS_PAGES: Array<{ route: string; testid: string }> = [
|
||||||
|
{ route: '/analysis/trend', testid: TESTIDS.pageTrend },
|
||||||
|
{ route: '/analysis/district', testid: TESTIDS.pageDistrict },
|
||||||
|
{ route: '/analysis/insights', testid: TESTIDS.pageInsights },
|
||||||
|
{ route: '/analysis/reports', testid: TESTIDS.pageReports },
|
||||||
|
{ route: '/analysis/demographics', testid: TESTIDS.pageDemographics },
|
||||||
|
{ route: '/analysis/disease', testid: TESTIDS.pageDisease },
|
||||||
|
{ route: '/analysis/environment', testid: TESTIDS.pageEnvironment },
|
||||||
|
];
|
||||||
|
|
||||||
|
/** Seed auth token and mock all /api/** calls before each page load. */
|
||||||
|
async function seedAuthAndMockApi(page: Page) {
|
||||||
|
await page.addInitScript(() => {
|
||||||
|
localStorage.setItem('cbpoa_token', 'e2e-test-token');
|
||||||
|
});
|
||||||
|
|
||||||
|
// Mock backend responses so the suite is hermetic — no live :8000 required.
|
||||||
|
// Each response must match the TypeScript interface shape; returning {} causes
|
||||||
|
// pages to throw when accessing expected array properties.
|
||||||
|
await page.route('/api/**', (route) => {
|
||||||
|
const url = route.request().url();
|
||||||
|
|
||||||
|
if (url.includes('/alerts')) {
|
||||||
|
route.fulfill({
|
||||||
|
status: 200,
|
||||||
|
contentType: 'application/json',
|
||||||
|
body: JSON.stringify({ alerts: [], total: 0 }),
|
||||||
|
});
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (url.includes('/history/aggregated')) {
|
||||||
|
route.fulfill({
|
||||||
|
status: 200,
|
||||||
|
contentType: 'application/json',
|
||||||
|
body: JSON.stringify({ aggregations: [], total_records: 0 }),
|
||||||
|
});
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (url.includes('/grids')) {
|
||||||
|
route.fulfill({
|
||||||
|
status: 200,
|
||||||
|
contentType: 'application/json',
|
||||||
|
body: JSON.stringify({ type: 'FeatureCollection', features: [] }),
|
||||||
|
});
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (url.includes('/cases/demographics')) {
|
||||||
|
route.fulfill({
|
||||||
|
status: 200,
|
||||||
|
contentType: 'application/json',
|
||||||
|
body: JSON.stringify({
|
||||||
|
age_distribution: [],
|
||||||
|
gender_split: { male: { outpatient: 0, inpatient: 0 }, female: { outpatient: 0, inpatient: 0 } },
|
||||||
|
age_diagnosis_matrix: [],
|
||||||
|
}),
|
||||||
|
});
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (url.includes('/cases/diagnosis-distribution') || url.includes('/cases/disease-seasonality')) {
|
||||||
|
route.fulfill({
|
||||||
|
status: 200,
|
||||||
|
contentType: 'application/json',
|
||||||
|
body: JSON.stringify([]),
|
||||||
|
});
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (url.includes('/cases/districts') || url.includes('/districts')) {
|
||||||
|
route.fulfill({
|
||||||
|
status: 200,
|
||||||
|
contentType: 'application/json',
|
||||||
|
body: JSON.stringify([]),
|
||||||
|
});
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (url.includes('/cases/trend') || url.includes('/cases')) {
|
||||||
|
route.fulfill({
|
||||||
|
status: 200,
|
||||||
|
contentType: 'application/json',
|
||||||
|
body: JSON.stringify({ data: [], total: 0 }),
|
||||||
|
});
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Default fallback — return a safe empty object.
|
||||||
|
route.fulfill({
|
||||||
|
status: 200,
|
||||||
|
contentType: 'application/json',
|
||||||
|
body: JSON.stringify({ data: [], items: [], total: 0 }),
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
test.describe('Responsive — analysis pages @375px', () => {
|
||||||
|
test.use({ viewport: { width: 375, height: 812 } });
|
||||||
|
|
||||||
|
test.beforeEach(async ({ page }) => {
|
||||||
|
await seedAuthAndMockApi(page);
|
||||||
|
});
|
||||||
|
|
||||||
|
for (const { route, testid } of ANALYSIS_PAGES) {
|
||||||
|
test(`${route} mounts and has no horizontal scroll at 375px`, async ({ page }) => {
|
||||||
|
await page.goto(route);
|
||||||
|
|
||||||
|
// Page must mount.
|
||||||
|
await expect(page.locator(`[data-testid="${testid}"]`)).toBeVisible();
|
||||||
|
|
||||||
|
// No horizontal overflow: scrollWidth must not exceed clientWidth (+1px slack
|
||||||
|
// for sub-pixel rounding).
|
||||||
|
const noHorizontalScroll = await page.evaluate(
|
||||||
|
() =>
|
||||||
|
document.documentElement.scrollWidth <=
|
||||||
|
document.documentElement.clientWidth + 1
|
||||||
|
);
|
||||||
|
expect(noHorizontalScroll, `${route} overflows horizontally at 375px`).toBe(true);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
});
|
||||||
133
frontend/e2e/roles.spec.ts
Normal file
133
frontend/e2e/roles.spec.ts
Normal file
@@ -0,0 +1,133 @@
|
|||||||
|
/**
|
||||||
|
* Phase-3 acceptance tests: 视角/perspective switcher (D2 — frontend view-presets only).
|
||||||
|
*
|
||||||
|
* Roles are NOT access control: the switcher only changes the default landing page +
|
||||||
|
* granularity/filter presets. This suite verifies the switcher renders, selecting a role
|
||||||
|
* navigates to that role's default landing URL (with its query params), and the choice
|
||||||
|
* survives a reload (persisted to localStorage['cbpoa_role']).
|
||||||
|
*
|
||||||
|
* Auth + API mocking mirror e2e/user-flows.spec.ts so the suite runs hermetically.
|
||||||
|
*/
|
||||||
|
import { test, expect, Page } from '@playwright/test';
|
||||||
|
import { TESTIDS } from '../src/utils/testids';
|
||||||
|
|
||||||
|
/** Seed auth token and mock all /api/** calls (shapes copied from user-flows.spec.ts). */
|
||||||
|
async function seedAuthAndMockApi(page: Page) {
|
||||||
|
await page.addInitScript(() => {
|
||||||
|
localStorage.setItem('cbpoa_token', 'e2e-test-token');
|
||||||
|
});
|
||||||
|
|
||||||
|
await page.route('/api/**', (route) => {
|
||||||
|
const url = route.request().url();
|
||||||
|
|
||||||
|
if (url.includes('/alerts')) {
|
||||||
|
route.fulfill({
|
||||||
|
status: 200,
|
||||||
|
contentType: 'application/json',
|
||||||
|
body: JSON.stringify({ alerts: [], total: 0 }),
|
||||||
|
});
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (url.includes('/history/aggregated')) {
|
||||||
|
route.fulfill({
|
||||||
|
status: 200,
|
||||||
|
contentType: 'application/json',
|
||||||
|
body: JSON.stringify({ aggregations: [], total_records: 0 }),
|
||||||
|
});
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (url.includes('/grids')) {
|
||||||
|
route.fulfill({
|
||||||
|
status: 200,
|
||||||
|
contentType: 'application/json',
|
||||||
|
body: JSON.stringify({ type: 'FeatureCollection', features: [] }),
|
||||||
|
});
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (url.includes('/cases/demographics')) {
|
||||||
|
route.fulfill({
|
||||||
|
status: 200,
|
||||||
|
contentType: 'application/json',
|
||||||
|
body: JSON.stringify({
|
||||||
|
age_distribution: [],
|
||||||
|
gender_split: { male: { outpatient: 0, inpatient: 0 }, female: { outpatient: 0, inpatient: 0 } },
|
||||||
|
age_diagnosis_matrix: [],
|
||||||
|
}),
|
||||||
|
});
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (url.includes('/cases/diagnosis-distribution') || url.includes('/cases/disease-seasonality')) {
|
||||||
|
route.fulfill({ status: 200, contentType: 'application/json', body: JSON.stringify([]) });
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (url.includes('/cases/districts') || url.includes('/districts')) {
|
||||||
|
route.fulfill({ status: 200, contentType: 'application/json', body: JSON.stringify([]) });
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (url.includes('/cases/trend') || url.includes('/cases')) {
|
||||||
|
route.fulfill({
|
||||||
|
status: 200,
|
||||||
|
contentType: 'application/json',
|
||||||
|
body: JSON.stringify({ data: [], total: 0 }),
|
||||||
|
});
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
route.fulfill({
|
||||||
|
status: 200,
|
||||||
|
contentType: 'application/json',
|
||||||
|
body: JSON.stringify({ data: [], items: [], total: 0 }),
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
test.describe('视角/perspective switcher (Phase 3)', () => {
|
||||||
|
// Use a desktop viewport so the top bar renders the switcher inline.
|
||||||
|
test.use({ viewport: { width: 1280, height: 800 } });
|
||||||
|
|
||||||
|
test.beforeEach(async ({ page }) => {
|
||||||
|
await seedAuthAndMockApi(page);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('perspective-switcher is visible in the top bar', async ({ page }) => {
|
||||||
|
await page.goto('/monitoring');
|
||||||
|
await expect(page.locator(`[data-testid="${TESTIDS.perspectiveSwitcher}"]`)).toBeVisible();
|
||||||
|
});
|
||||||
|
|
||||||
|
test('selecting 厅领导 (official) navigates to /overview?granularity=district', async ({ page }) => {
|
||||||
|
await page.goto('/monitoring');
|
||||||
|
const switcher = page.locator(`[data-testid="${TESTIDS.perspectiveSwitcher}"]`);
|
||||||
|
await expect(switcher).toBeVisible();
|
||||||
|
|
||||||
|
await switcher.selectOption('official');
|
||||||
|
|
||||||
|
await expect(page).toHaveURL(/\/overview/);
|
||||||
|
await expect(page).toHaveURL(/granularity=district/);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('selecting 医生 (doctor) navigates to /alerts?view=cluster', async ({ page }) => {
|
||||||
|
await page.goto('/monitoring');
|
||||||
|
const switcher = page.locator(`[data-testid="${TESTIDS.perspectiveSwitcher}"]`);
|
||||||
|
await expect(switcher).toBeVisible();
|
||||||
|
|
||||||
|
await switcher.selectOption('doctor');
|
||||||
|
|
||||||
|
await expect(page).toHaveURL(/\/alerts/);
|
||||||
|
await expect(page).toHaveURL(/view=cluster/);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('selected role persists across reload (localStorage cbpoa_role)', async ({ page }) => {
|
||||||
|
await page.goto('/monitoring');
|
||||||
|
const switcher = page.locator(`[data-testid="${TESTIDS.perspectiveSwitcher}"]`);
|
||||||
|
await switcher.selectOption('doctor');
|
||||||
|
await expect(page).toHaveURL(/\/alerts/);
|
||||||
|
|
||||||
|
// localStorage should now hold the chosen role.
|
||||||
|
const stored = await page.evaluate(() => localStorage.getItem('cbpoa_role'));
|
||||||
|
expect(stored).toBe('doctor');
|
||||||
|
|
||||||
|
await page.reload();
|
||||||
|
|
||||||
|
// After reload the switcher reflects the persisted role.
|
||||||
|
await expect(page.locator(`[data-testid="${TESTIDS.perspectiveSwitcher}"]`)).toHaveValue('doctor');
|
||||||
|
});
|
||||||
|
});
|
||||||
@@ -1,242 +1,291 @@
|
|||||||
/**
|
/**
|
||||||
* US-007 + US-008: E2E user flow and UI state tests.
|
* Phase-1 acceptance tests: URL-based navigation, responsive layout, and core user flows.
|
||||||
* Simulates real user workflows through the CBPOA system.
|
* Rewrites the previous click-nav suite for react-router v6 URL navigation.
|
||||||
|
*
|
||||||
|
* Auth strategy: seed localStorage['cbpoa_token'] via addInitScript (App.tsx gates on
|
||||||
|
* token presence only; no server validation). Backend is mocked via page.route so the
|
||||||
|
* suite runs hermetically without a live :8000 backend.
|
||||||
*/
|
*/
|
||||||
import { test, expect } from '@playwright/test';
|
import { test, expect, Page } from '@playwright/test';
|
||||||
|
import { TESTIDS } from '../src/utils/testids';
|
||||||
|
|
||||||
const BASE_URL = 'http://localhost:3000';
|
// ---------------------------------------------------------------------------
|
||||||
|
// Helpers
|
||||||
|
// ---------------------------------------------------------------------------
|
||||||
|
|
||||||
test.describe('认证流程 (Authentication Flow)', () => {
|
/** Seed auth token and mock all /api/** calls before each page load. */
|
||||||
test('显示登录页面', async ({ page }) => {
|
async function seedAuthAndMockApi(page: Page) {
|
||||||
await page.goto(BASE_URL);
|
// Prevent login gate from appearing.
|
||||||
await page.waitForTimeout(1000);
|
await page.addInitScript(() => {
|
||||||
// Should see login form or app (if cached token)
|
localStorage.setItem('cbpoa_token', 'e2e-test-token');
|
||||||
const isLogin = await page.locator('input').count();
|
|
||||||
const isApp = await page.locator('nav').count();
|
|
||||||
expect(isLogin > 0 || isApp > 0).toBeTruthy();
|
|
||||||
});
|
});
|
||||||
|
|
||||||
test('登录表单可交互', async ({ page }) => {
|
// Mock backend responses so the suite is hermetic — no live :8000 required.
|
||||||
await page.goto(BASE_URL);
|
// Each response must match the TypeScript interface shape; returning {} causes
|
||||||
await page.waitForTimeout(1000);
|
// pages to throw when accessing expected array properties.
|
||||||
|
await page.route('/api/**', (route) => {
|
||||||
|
const url = route.request().url();
|
||||||
|
|
||||||
const inputs = page.locator('input');
|
if (url.includes('/alerts')) {
|
||||||
const count = await inputs.count();
|
route.fulfill({
|
||||||
|
status: 200,
|
||||||
if (count >= 2) {
|
contentType: 'application/json',
|
||||||
// Login page is shown
|
body: JSON.stringify({ alerts: [], total: 0 }),
|
||||||
await inputs.first().fill('admin');
|
});
|
||||||
await inputs.nth(1).fill('admin123');
|
return;
|
||||||
|
|
||||||
const loginBtn = page.locator('button[type="submit"], button:has-text("登录"), button:has-text("Login")');
|
|
||||||
const btnCount = await loginBtn.count();
|
|
||||||
if (btnCount > 0) {
|
|
||||||
await loginBtn.first().click();
|
|
||||||
await page.waitForTimeout(2000);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
// If no inputs, user is already logged in (token in localStorage)
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
test.describe('监测面板 (Monitoring Dashboard)', () => {
|
if (url.includes('/history/aggregated')) {
|
||||||
test('面板加载并显示统计卡片', async ({ page }) => {
|
route.fulfill({
|
||||||
await page.goto(BASE_URL);
|
status: 200,
|
||||||
await page.waitForTimeout(3000);
|
contentType: 'application/json',
|
||||||
|
body: JSON.stringify({ aggregations: [], total_records: 0 }),
|
||||||
// Should show monitoring page by default
|
});
|
||||||
const statCards = page.locator('[class*="stat"], [class*="card"], [class*="Stat"]');
|
return;
|
||||||
const cardsCount = await statCards.count();
|
|
||||||
|
|
||||||
// Should see some content
|
|
||||||
const bodyText = await page.textContent('body');
|
|
||||||
expect(bodyText).toBeTruthy();
|
|
||||||
});
|
|
||||||
|
|
||||||
test('时间线控件可交互', async ({ page }) => {
|
|
||||||
await page.goto(BASE_URL);
|
|
||||||
await page.waitForTimeout(3000);
|
|
||||||
|
|
||||||
// Look for timeline controls
|
|
||||||
const playButton = page.locator('button:has-text("播放"), button[title*="play" i], button[class*="play" i]');
|
|
||||||
const prevButton = page.locator('button:has-text("前一天"), button[title*="prev" i]');
|
|
||||||
const nextButton = page.locator('button:has-text("后一天"), button[title*="next" i]');
|
|
||||||
|
|
||||||
if (await playButton.count() > 0) {
|
|
||||||
await playButton.first().click();
|
|
||||||
await page.waitForTimeout(1000);
|
|
||||||
}
|
}
|
||||||
});
|
|
||||||
|
|
||||||
test('疾病筛选器可用', async ({ page }) => {
|
if (url.includes('/grids')) {
|
||||||
await page.goto(BASE_URL);
|
route.fulfill({
|
||||||
await page.waitForTimeout(3000);
|
status: 200,
|
||||||
|
contentType: 'application/json',
|
||||||
const selects = page.locator('select, [role="combobox"], [class*="select" i], [class*="filter" i]');
|
body: JSON.stringify({ type: 'FeatureCollection', features: [] }),
|
||||||
const count = await selects.count();
|
});
|
||||||
expect(count >= 0).toBeTruthy();
|
return;
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
test.describe('预警面板 (Alerts Dashboard)', () => {
|
|
||||||
test('导航到预警面板', async ({ page }) => {
|
|
||||||
await page.goto(BASE_URL);
|
|
||||||
await page.waitForTimeout(2000);
|
|
||||||
|
|
||||||
// Navigate to alerts - click sidebar link
|
|
||||||
const alertsLink = page.locator('a[href*="alert" i], button:has-text("预警"), button:has-text("告警"), span:has-text("预警"), span:has-text("告警")');
|
|
||||||
if (await alertsLink.count() > 0) {
|
|
||||||
await alertsLink.first().click();
|
|
||||||
await page.waitForTimeout(2000);
|
|
||||||
}
|
}
|
||||||
});
|
|
||||||
|
|
||||||
test('预警列表加载', async ({ page }) => {
|
// DemographicsResponse — used by DemographicAnalysis page.
|
||||||
await page.goto(BASE_URL);
|
if (url.includes('/cases/demographics')) {
|
||||||
await page.waitForTimeout(2000);
|
route.fulfill({
|
||||||
|
status: 200,
|
||||||
const alertsLink = page.locator('a[href*="alert" i], button:has-text("预警"), span:has-text("预警")');
|
contentType: 'application/json',
|
||||||
if (await alertsLink.count() > 0) {
|
body: JSON.stringify({
|
||||||
await alertsLink.first().click();
|
age_distribution: [],
|
||||||
await page.waitForTimeout(3000);
|
gender_split: { male: { outpatient: 0, inpatient: 0 }, female: { outpatient: 0, inpatient: 0 } },
|
||||||
|
age_diagnosis_matrix: [],
|
||||||
const bodyText = await page.textContent('body');
|
}),
|
||||||
expect(bodyText).toBeTruthy();
|
});
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
test.describe('趋势分析 (Trend Analysis)', () => {
|
// DiseaseAnalysis calls: diagnosis-distribution, seasonality, districts.
|
||||||
test('导航到趋势分析页面', async ({ page }) => {
|
if (url.includes('/cases/diagnosis-distribution') || url.includes('/cases/disease-seasonality')) {
|
||||||
await page.goto(BASE_URL);
|
route.fulfill({
|
||||||
await page.waitForTimeout(2000);
|
status: 200,
|
||||||
|
contentType: 'application/json',
|
||||||
const trendLink = page.locator('button:has-text("趋势"), span:has-text("趋势"), a[href*="trend" i]');
|
body: JSON.stringify([]),
|
||||||
if (await trendLink.count() > 0) {
|
});
|
||||||
await trendLink.first().click();
|
return;
|
||||||
await page.waitForTimeout(2000);
|
|
||||||
}
|
}
|
||||||
});
|
|
||||||
|
|
||||||
test('趋势图渲染', async ({ page }) => {
|
if (url.includes('/cases/districts') || url.includes('/districts')) {
|
||||||
await page.goto(BASE_URL);
|
route.fulfill({
|
||||||
await page.waitForTimeout(2000);
|
status: 200,
|
||||||
|
contentType: 'application/json',
|
||||||
const trendLink = page.locator('button:has-text("趋势"), span:has-text("趋势")');
|
body: JSON.stringify([]),
|
||||||
if (await trendLink.count() > 0) {
|
});
|
||||||
await trendLink.first().click();
|
return;
|
||||||
await page.waitForTimeout(3000);
|
|
||||||
|
|
||||||
// Recharts renders SVG charts
|
|
||||||
const svgCharts = page.locator('svg.recharts-surface');
|
|
||||||
const chartCount = await svgCharts.count();
|
|
||||||
expect(chartCount >= 0).toBeTruthy();
|
|
||||||
}
|
}
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
test.describe('区县对比 (District Comparison)', () => {
|
// Trend / time-series endpoints.
|
||||||
test('导航到区县对比页面', async ({ page }) => {
|
if (url.includes('/cases/trend') || url.includes('/cases')) {
|
||||||
await page.goto(BASE_URL);
|
route.fulfill({
|
||||||
await page.waitForTimeout(2000);
|
status: 200,
|
||||||
|
contentType: 'application/json',
|
||||||
const districtLink = page.locator('button:has-text("区县"), button:has-text("对比"), span:has-text("区县")');
|
body: JSON.stringify({ data: [], total: 0 }),
|
||||||
if (await districtLink.count() > 0) {
|
});
|
||||||
await districtLink.first().click();
|
return;
|
||||||
await page.waitForTimeout(2000);
|
|
||||||
}
|
}
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
test.describe('报告中心 (Reports Center)', () => {
|
// Default fallback — return a safe empty object.
|
||||||
test('导航到报告中心', async ({ page }) => {
|
route.fulfill({
|
||||||
await page.goto(BASE_URL);
|
status: 200,
|
||||||
await page.waitForTimeout(2000);
|
contentType: 'application/json',
|
||||||
|
body: JSON.stringify({ data: [], items: [], total: 0 }),
|
||||||
const reportsLink = page.locator('button:has-text("报告"), span:has-text("报告"), a[href*="report" i]');
|
|
||||||
if (await reportsLink.count() > 0) {
|
|
||||||
await reportsLink.first().click();
|
|
||||||
await page.waitForTimeout(2000);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
test('报告列表加载', async ({ page }) => {
|
|
||||||
await page.goto(BASE_URL);
|
|
||||||
await page.waitForTimeout(2000);
|
|
||||||
|
|
||||||
const reportsLink = page.locator('button:has-text("报告"), span:has-text("报告")');
|
|
||||||
if (await reportsLink.count() > 0) {
|
|
||||||
await reportsLink.first().click();
|
|
||||||
await page.waitForTimeout(3000);
|
|
||||||
|
|
||||||
const bodyText = await page.textContent('body');
|
|
||||||
expect(bodyText).toBeTruthy();
|
|
||||||
}
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
test.describe('UI 状态与错误处理 (UI States & Error Handling)', () => {
|
|
||||||
test('页面加载显示加载指示器而非白屏', async ({ page }) => {
|
|
||||||
await page.goto(BASE_URL);
|
|
||||||
await page.waitForTimeout(500);
|
|
||||||
|
|
||||||
const bodyHTML = await page.innerHTML('body');
|
|
||||||
// Should have some content, even during loading
|
|
||||||
expect(bodyHTML.length).toBeGreaterThan(0);
|
|
||||||
});
|
|
||||||
|
|
||||||
test('侧边栏导航切换页面正常', async ({ page }) => {
|
|
||||||
await page.goto(BASE_URL);
|
|
||||||
await page.waitForTimeout(2000);
|
|
||||||
|
|
||||||
const navLinks = page.locator('nav a, nav button, [class*="side" i] a, [class*="side" i] button');
|
|
||||||
const count = await navLinks.count();
|
|
||||||
|
|
||||||
if (count >= 2) {
|
|
||||||
await navLinks.first().click();
|
|
||||||
await page.waitForTimeout(1000);
|
|
||||||
await navLinks.nth(1).click();
|
|
||||||
await page.waitForTimeout(1000);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
test('未出现明显 console 报错', async ({ page }) => {
|
|
||||||
const errors: string[] = [];
|
|
||||||
page.on('console', (msg) => {
|
|
||||||
if (msg.type() === 'error') {
|
|
||||||
errors.push(msg.text());
|
|
||||||
}
|
|
||||||
});
|
|
||||||
page.on('pageerror', (err) => {
|
|
||||||
errors.push(err.message);
|
|
||||||
});
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
await page.goto(BASE_URL);
|
// ---------------------------------------------------------------------------
|
||||||
await page.waitForTimeout(3000);
|
// URL-based navigation (react-router v6)
|
||||||
|
// ---------------------------------------------------------------------------
|
||||||
|
|
||||||
const filtered = errors.filter(
|
test.describe('URL-based navigation', () => {
|
||||||
(e) => !e.includes('favicon') && !e.includes('404') && !e.includes('OLMap')
|
test.beforeEach(async ({ page }) => {
|
||||||
|
await seedAuthAndMockApi(page);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('deep-link: /analysis/disease mounts page-disease directly', async ({ page }) => {
|
||||||
|
await page.goto('/analysis/disease');
|
||||||
|
await expect(page.locator(`[data-testid="${TESTIDS.pageDisease}"]`)).toBeVisible();
|
||||||
|
});
|
||||||
|
|
||||||
|
test('refresh preserves page: reload on /analysis/disease keeps URL and mounts page-disease', async ({
|
||||||
|
page,
|
||||||
|
}) => {
|
||||||
|
await page.goto('/analysis/disease');
|
||||||
|
await expect(page.locator(`[data-testid="${TESTIDS.pageDisease}"]`)).toBeVisible();
|
||||||
|
|
||||||
|
await page.reload();
|
||||||
|
|
||||||
|
await expect(page).toHaveURL(/\/analysis\/disease/);
|
||||||
|
await expect(page.locator(`[data-testid="${TESTIDS.pageDisease}"]`)).toBeVisible();
|
||||||
|
});
|
||||||
|
|
||||||
|
test('browser back: from /analysis/trend back to /monitoring restores page-monitoring', async ({
|
||||||
|
page,
|
||||||
|
}) => {
|
||||||
|
await page.goto('/monitoring');
|
||||||
|
await expect(page.locator(`[data-testid="${TESTIDS.pageMonitoring}"]`)).toBeVisible();
|
||||||
|
|
||||||
|
await page.goto('/analysis/trend');
|
||||||
|
await expect(page.locator(`[data-testid="${TESTIDS.pageTrend}"]`)).toBeVisible();
|
||||||
|
|
||||||
|
await page.goBack();
|
||||||
|
|
||||||
|
await expect(page).toHaveURL(/\/monitoring/);
|
||||||
|
await expect(page.locator(`[data-testid="${TESTIDS.pageMonitoring}"]`)).toBeVisible();
|
||||||
|
});
|
||||||
|
|
||||||
|
test('NavLink click updates URL to /alerts and mounts page-alerts', async ({ page }) => {
|
||||||
|
// Start on /monitoring. The SideNav collapses all modules except the active one,
|
||||||
|
// so nav-alerts (inside the "预警" module) is hidden behind a collapsed section.
|
||||||
|
// We must expand the "预警" module first by clicking its header button.
|
||||||
|
await page.goto('/monitoring');
|
||||||
|
await expect(page.locator(`[data-testid="${TESTIDS.pageMonitoring}"]`)).toBeVisible();
|
||||||
|
|
||||||
|
// Expand the "预警" module section so nav-alerts NavLink becomes visible.
|
||||||
|
// Both sidebar-rail and app-drawer render a SideNav; scope to sidebar-rail to avoid
|
||||||
|
// strict-mode ambiguity (the app-drawer's copy is also in the DOM but off-screen).
|
||||||
|
await page
|
||||||
|
.locator(`[data-testid="${TESTIDS.sidebarRail}"] button`)
|
||||||
|
.filter({ hasText: '预警' })
|
||||||
|
.click();
|
||||||
|
await expect(
|
||||||
|
page.locator(`[data-testid="${TESTIDS.sidebarRail}"] [data-testid="${TESTIDS.navAlerts}"]`)
|
||||||
|
).toBeVisible();
|
||||||
|
|
||||||
|
await page
|
||||||
|
.locator(`[data-testid="${TESTIDS.sidebarRail}"] [data-testid="${TESTIDS.navAlerts}"]`)
|
||||||
|
.click();
|
||||||
|
|
||||||
|
await expect(page).toHaveURL(/\/alerts/);
|
||||||
|
await expect(page.locator(`[data-testid="${TESTIDS.pageAlerts}"]`)).toBeVisible();
|
||||||
|
});
|
||||||
|
|
||||||
|
test('root / redirects to /monitoring', async ({ page }) => {
|
||||||
|
await page.goto('/');
|
||||||
|
await expect(page).toHaveURL(/\/monitoring/);
|
||||||
|
await expect(page.locator(`[data-testid="${TESTIDS.pageMonitoring}"]`)).toBeVisible();
|
||||||
|
});
|
||||||
|
|
||||||
|
test('unknown path redirects to /monitoring', async ({ page }) => {
|
||||||
|
await page.goto('/does-not-exist');
|
||||||
|
await expect(page).toHaveURL(/\/monitoring/);
|
||||||
|
await expect(page.locator(`[data-testid="${TESTIDS.pageMonitoring}"]`)).toBeVisible();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
// ---------------------------------------------------------------------------
|
||||||
|
// Responsive layout
|
||||||
|
// ---------------------------------------------------------------------------
|
||||||
|
|
||||||
|
test.describe('Responsive layout — mobile @375px', () => {
|
||||||
|
test.use({ viewport: { width: 375, height: 812 } });
|
||||||
|
|
||||||
|
test.beforeEach(async ({ page }) => {
|
||||||
|
await seedAuthAndMockApi(page);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('hamburger is visible and sidebar-rail is hidden at 375px', async ({ page }) => {
|
||||||
|
await page.goto('/monitoring');
|
||||||
|
await expect(page.locator(`[data-testid="${TESTIDS.pageMonitoring}"]`)).toBeVisible();
|
||||||
|
|
||||||
|
await expect(page.locator(`[data-testid="${TESTIDS.hamburger}"]`)).toBeVisible();
|
||||||
|
await expect(page.locator(`[data-testid="${TESTIDS.sidebarRail}"]`)).not.toBeVisible();
|
||||||
|
});
|
||||||
|
|
||||||
|
test('tapping hamburger slides app-drawer into viewport', async ({ page }) => {
|
||||||
|
await page.goto('/monitoring');
|
||||||
|
await expect(page.locator(`[data-testid="${TESTIDS.pageMonitoring}"]`)).toBeVisible();
|
||||||
|
|
||||||
|
// Drawer should be off-screen (translate-x-full) before toggle.
|
||||||
|
const drawer = page.locator(`[data-testid="${TESTIDS.appDrawer}"]`);
|
||||||
|
await expect(drawer).not.toBeInViewport();
|
||||||
|
|
||||||
|
await page.locator(`[data-testid="${TESTIDS.hamburger}"]`).click();
|
||||||
|
|
||||||
|
// After toggle, drawer slides in and becomes visible in viewport.
|
||||||
|
await expect(drawer).toBeInViewport();
|
||||||
|
});
|
||||||
|
|
||||||
|
test('no horizontal scroll on default route at 375px', async ({ page }) => {
|
||||||
|
await page.goto('/monitoring');
|
||||||
|
await expect(page.locator(`[data-testid="${TESTIDS.pageMonitoring}"]`)).toBeVisible();
|
||||||
|
|
||||||
|
const noHorizontalScroll = await page.evaluate(
|
||||||
|
() => document.documentElement.scrollWidth <= document.documentElement.clientWidth
|
||||||
);
|
);
|
||||||
expect(filtered).toHaveLength(0);
|
expect(noHorizontalScroll).toBe(true);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
test.describe('响应式布局 (Responsive Layout)', () => {
|
test.describe('Responsive layout — desktop @1280px', () => {
|
||||||
test('移动端视口下不崩溃', async ({ page }) => {
|
test.use({ viewport: { width: 1280, height: 800 } });
|
||||||
await page.setViewportSize({ width: 375, height: 812 });
|
|
||||||
await page.goto(BASE_URL);
|
|
||||||
await page.waitForTimeout(2000);
|
|
||||||
|
|
||||||
const bodyText = await page.textContent('body');
|
test.beforeEach(async ({ page }) => {
|
||||||
expect(bodyText).toBeTruthy();
|
await seedAuthAndMockApi(page);
|
||||||
});
|
});
|
||||||
|
|
||||||
test('平板视口下正常显示', async ({ page }) => {
|
test('sidebar-rail is visible and hamburger is hidden at 1280px', async ({ page }) => {
|
||||||
await page.setViewportSize({ width: 768, height: 1024 });
|
await page.goto('/monitoring');
|
||||||
await page.goto(BASE_URL);
|
await expect(page.locator(`[data-testid="${TESTIDS.pageMonitoring}"]`)).toBeVisible();
|
||||||
await page.waitForTimeout(2000);
|
|
||||||
|
|
||||||
const bodyText = await page.textContent('body');
|
await expect(page.locator(`[data-testid="${TESTIDS.sidebarRail}"]`)).toBeVisible();
|
||||||
expect(bodyText).toBeTruthy();
|
await expect(page.locator(`[data-testid="${TESTIDS.hamburger}"]`)).not.toBeVisible();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
// ---------------------------------------------------------------------------
|
||||||
|
// Core page loading
|
||||||
|
// ---------------------------------------------------------------------------
|
||||||
|
|
||||||
|
test.describe('Core pages load via URL nav', () => {
|
||||||
|
test.beforeEach(async ({ page }) => {
|
||||||
|
await seedAuthAndMockApi(page);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('/monitoring loads page-monitoring', async ({ page }) => {
|
||||||
|
await page.goto('/monitoring');
|
||||||
|
await expect(page.locator(`[data-testid="${TESTIDS.pageMonitoring}"]`)).toBeVisible();
|
||||||
|
});
|
||||||
|
|
||||||
|
test('/alerts loads page-alerts', async ({ page }) => {
|
||||||
|
await page.goto('/alerts');
|
||||||
|
await expect(page.locator(`[data-testid="${TESTIDS.pageAlerts}"]`)).toBeVisible();
|
||||||
|
});
|
||||||
|
|
||||||
|
test('/analysis/trend loads page-trend', async ({ page }) => {
|
||||||
|
await page.goto('/analysis/trend');
|
||||||
|
await expect(page.locator(`[data-testid="${TESTIDS.pageTrend}"]`)).toBeVisible();
|
||||||
|
});
|
||||||
|
|
||||||
|
test('/analysis/district loads page-district', async ({ page }) => {
|
||||||
|
await page.goto('/analysis/district');
|
||||||
|
await expect(page.locator(`[data-testid="${TESTIDS.pageDistrict}"]`)).toBeVisible();
|
||||||
|
});
|
||||||
|
|
||||||
|
test('/analysis/reports loads page-reports', async ({ page }) => {
|
||||||
|
await page.goto('/analysis/reports');
|
||||||
|
await expect(page.locator(`[data-testid="${TESTIDS.pageReports}"]`)).toBeVisible();
|
||||||
|
});
|
||||||
|
|
||||||
|
test('/analysis/demographics loads page-demographics', async ({ page }) => {
|
||||||
|
await page.goto('/analysis/demographics');
|
||||||
|
await expect(page.locator(`[data-testid="${TESTIDS.pageDemographics}"]`)).toBeVisible();
|
||||||
|
});
|
||||||
|
|
||||||
|
test('/analysis/environment loads page-environment', async ({ page }) => {
|
||||||
|
await page.goto('/analysis/environment');
|
||||||
|
await expect(page.locator(`[data-testid="${TESTIDS.pageEnvironment}"]`)).toBeVisible();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -15,6 +15,7 @@
|
|||||||
"react": "^18.2.0",
|
"react": "^18.2.0",
|
||||||
"react-dom": "^18.2.0",
|
"react-dom": "^18.2.0",
|
||||||
"react-leaflet": "^4.2.1",
|
"react-leaflet": "^4.2.1",
|
||||||
|
"react-router-dom": "^6.30.4",
|
||||||
"recharts": "^2.12.0",
|
"recharts": "^2.12.0",
|
||||||
"zustand": "^4.5.0"
|
"zustand": "^4.5.0"
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -14,6 +14,16 @@ export default defineConfig({
|
|||||||
projects: [
|
projects: [
|
||||||
{
|
{
|
||||||
name: 'chromium',
|
name: 'chromium',
|
||||||
|
// Functional suite. Exclude the throttled perf spec so emulated Fast-3G
|
||||||
|
// latency never bleeds into (or slows) the normal acceptance run.
|
||||||
|
testIgnore: /perf\.spec\.ts/,
|
||||||
|
use: { ...devices['Desktop Chrome'] },
|
||||||
|
},
|
||||||
|
{
|
||||||
|
// Dedicated perf project — only perf.spec.ts runs here, under CDP network
|
||||||
|
// throttling. Kept separate so functional and perf measurements don't mix.
|
||||||
|
name: 'perf',
|
||||||
|
testMatch: /perf\.spec\.ts/,
|
||||||
use: { ...devices['Desktop Chrome'] },
|
use: { ...devices['Desktop Chrome'] },
|
||||||
},
|
},
|
||||||
],
|
],
|
||||||
|
|||||||
34
frontend/pnpm-lock.yaml
generated
34
frontend/pnpm-lock.yaml
generated
@@ -26,6 +26,9 @@ importers:
|
|||||||
react-leaflet:
|
react-leaflet:
|
||||||
specifier: ^4.2.1
|
specifier: ^4.2.1
|
||||||
version: 4.2.1(leaflet@1.9.4)(react-dom@18.3.1(react@18.3.1))(react@18.3.1)
|
version: 4.2.1(leaflet@1.9.4)(react-dom@18.3.1(react@18.3.1))(react@18.3.1)
|
||||||
|
react-router-dom:
|
||||||
|
specifier: ^6.30.4
|
||||||
|
version: 6.30.4(react-dom@18.3.1(react@18.3.1))(react@18.3.1)
|
||||||
recharts:
|
recharts:
|
||||||
specifier: ^2.12.0
|
specifier: ^2.12.0
|
||||||
version: 2.15.4(react-dom@18.3.1(react@18.3.1))(react@18.3.1)
|
version: 2.15.4(react-dom@18.3.1(react@18.3.1))(react@18.3.1)
|
||||||
@@ -385,6 +388,10 @@ packages:
|
|||||||
react: ^18.0.0
|
react: ^18.0.0
|
||||||
react-dom: ^18.0.0
|
react-dom: ^18.0.0
|
||||||
|
|
||||||
|
'@remix-run/router@1.23.3':
|
||||||
|
resolution: {integrity: sha512-4An71tdz9X8+3sI4Qqqd2LWd9vS39J7sqd9EU4Scw7TJE/qB10Flv/UuqbPVgfQV9XoK8Np6jNquZitnZq5i+Q==}
|
||||||
|
engines: {node: '>=14.0.0'}
|
||||||
|
|
||||||
'@rolldown/pluginutils@1.0.0-beta.27':
|
'@rolldown/pluginutils@1.0.0-beta.27':
|
||||||
resolution: {integrity: sha512-+d0F4MKMCbeVUJwG96uQ4SgAznZNSq93I3V+9NHA4OpvqG8mRCpGdKmK8l/dl02h2CCDHwW2FqilnTyDcAnqjA==}
|
resolution: {integrity: sha512-+d0F4MKMCbeVUJwG96uQ4SgAznZNSq93I3V+9NHA4OpvqG8mRCpGdKmK8l/dl02h2CCDHwW2FqilnTyDcAnqjA==}
|
||||||
|
|
||||||
@@ -1504,6 +1511,19 @@ packages:
|
|||||||
resolution: {integrity: sha512-z6F7K9bV85EfseRCp2bzrpyQ0Gkw1uLoCel9XBVWPg/TjRj94SkJzUTGfOa4bs7iJvBWtQG0Wq7wnI0syw3EBQ==}
|
resolution: {integrity: sha512-z6F7K9bV85EfseRCp2bzrpyQ0Gkw1uLoCel9XBVWPg/TjRj94SkJzUTGfOa4bs7iJvBWtQG0Wq7wnI0syw3EBQ==}
|
||||||
engines: {node: '>=0.10.0'}
|
engines: {node: '>=0.10.0'}
|
||||||
|
|
||||||
|
react-router-dom@6.30.4:
|
||||||
|
resolution: {integrity: sha512-q4HvNl+mmDdkS0g+MqiBZNteQJCuimWoOyHMy4T/RQLAn9Z29+E91QXRaxOujeMl2HTzRSS0KFPd7lxX3PjV0Q==}
|
||||||
|
engines: {node: '>=14.0.0'}
|
||||||
|
peerDependencies:
|
||||||
|
react: '>=16.8'
|
||||||
|
react-dom: '>=16.8'
|
||||||
|
|
||||||
|
react-router@6.30.4:
|
||||||
|
resolution: {integrity: sha512-SVUsDe+DybHM/WmYKIVYhZh1o5Dcuf16yM6WjG02Q9XVFMZIJyHYhwrr6bFBXZkVP6z69kNkMyBCujt8FaFLJA==}
|
||||||
|
engines: {node: '>=14.0.0'}
|
||||||
|
peerDependencies:
|
||||||
|
react: '>=16.8'
|
||||||
|
|
||||||
react-smooth@4.0.4:
|
react-smooth@4.0.4:
|
||||||
resolution: {integrity: sha512-gnGKTpYwqL0Iii09gHobNolvX4Kiq4PKx6eWBCYYix+8cdw+cGo3do906l1NBPKkSWx1DghC1dlWG9L2uGd61Q==}
|
resolution: {integrity: sha512-gnGKTpYwqL0Iii09gHobNolvX4Kiq4PKx6eWBCYYix+8cdw+cGo3do906l1NBPKkSWx1DghC1dlWG9L2uGd61Q==}
|
||||||
peerDependencies:
|
peerDependencies:
|
||||||
@@ -2157,6 +2177,8 @@ snapshots:
|
|||||||
react: 18.3.1
|
react: 18.3.1
|
||||||
react-dom: 18.3.1(react@18.3.1)
|
react-dom: 18.3.1(react@18.3.1)
|
||||||
|
|
||||||
|
'@remix-run/router@1.23.3': {}
|
||||||
|
|
||||||
'@rolldown/pluginutils@1.0.0-beta.27': {}
|
'@rolldown/pluginutils@1.0.0-beta.27': {}
|
||||||
|
|
||||||
'@rollup/rollup-android-arm-eabi@4.60.2':
|
'@rollup/rollup-android-arm-eabi@4.60.2':
|
||||||
@@ -3233,6 +3255,18 @@ snapshots:
|
|||||||
|
|
||||||
react-refresh@0.17.0: {}
|
react-refresh@0.17.0: {}
|
||||||
|
|
||||||
|
react-router-dom@6.30.4(react-dom@18.3.1(react@18.3.1))(react@18.3.1):
|
||||||
|
dependencies:
|
||||||
|
'@remix-run/router': 1.23.3
|
||||||
|
react: 18.3.1
|
||||||
|
react-dom: 18.3.1(react@18.3.1)
|
||||||
|
react-router: 6.30.4(react@18.3.1)
|
||||||
|
|
||||||
|
react-router@6.30.4(react@18.3.1):
|
||||||
|
dependencies:
|
||||||
|
'@remix-run/router': 1.23.3
|
||||||
|
react: 18.3.1
|
||||||
|
|
||||||
react-smooth@4.0.4(react-dom@18.3.1(react@18.3.1))(react@18.3.1):
|
react-smooth@4.0.4(react-dom@18.3.1(react@18.3.1))(react@18.3.1):
|
||||||
dependencies:
|
dependencies:
|
||||||
fast-equals: 5.4.0
|
fast-equals: 5.4.0
|
||||||
|
|||||||
BIN
frontend/public/wuhan_districts.geojson
LFS
Normal file
BIN
frontend/public/wuhan_districts.geojson
LFS
Normal file
Binary file not shown.
@@ -1,16 +1,9 @@
|
|||||||
import { useEffect, useState, Component, ReactNode, Suspense, lazy, useCallback } from 'react';
|
import { useEffect, useState, Component, ReactNode, useCallback } from 'react';
|
||||||
import { TopNav } from '@/components/TopNav';
|
import { BrowserRouter, Routes, Route, useRoutes } from 'react-router-dom';
|
||||||
import { SideNav } from '@/components/SideNav';
|
import { AppShell } from '@/components/AppShell';
|
||||||
import { useRiskStore } from '@/stores';
|
import { useRiskStore } from '@/stores';
|
||||||
import { Login } from '@/pages/Login';
|
import { Login } from '@/pages/Login';
|
||||||
|
import { appRoutes } from '@/routes';
|
||||||
const MonitoringDashboard = lazy(() => import('@/pages/MonitoringDashboard').then(m => ({ default: m.MonitoringDashboard })));
|
|
||||||
const AlertsDashboard = lazy(() => import('@/pages/AlertsDashboard').then(m => ({ default: m.AlertsDashboard })));
|
|
||||||
const TrendAnalysis = lazy(() => import('@/pages/TrendAnalysis').then(m => ({ default: m.TrendAnalysis })));
|
|
||||||
const DistrictComparison = lazy(() => import('@/pages/DistrictComparison').then(m => ({ default: m.DistrictComparison })));
|
|
||||||
const Insights = lazy(() => import('@/pages/Insights').then(m => ({ default: m.Insights })));
|
|
||||||
const ReportsCenter = lazy(() => import('@/pages/ReportsCenter').then(m => ({ default: m.ReportsCenter })));
|
|
||||||
|
|
||||||
|
|
||||||
interface Props {
|
interface Props {
|
||||||
children: ReactNode;
|
children: ReactNode;
|
||||||
@@ -52,28 +45,25 @@ class ErrorBoundary extends Component<Props, State> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function PageLoader() {
|
// 已登录:AppShell 提供布局骨架,子路由表渲染到其 <Outlet/>。
|
||||||
return (
|
function AuthedApp({ onLogout }: { onLogout: () => void }) {
|
||||||
<div className="flex items-center justify-center h-[60vh]">
|
const element = useRoutes([
|
||||||
<div className="text-text-secondary text-[13px]">加载中...</div>
|
{
|
||||||
</div>
|
element: <AppShell onLogout={onLogout} />,
|
||||||
);
|
children: appRoutes,
|
||||||
|
},
|
||||||
|
]);
|
||||||
|
return element;
|
||||||
}
|
}
|
||||||
|
|
||||||
function App() {
|
function App() {
|
||||||
const [activePage, setActivePage] = useState('monitoring');
|
|
||||||
const [token, setToken] = useState<string | null>(() => localStorage.getItem('cbpoa_token'));
|
const [token, setToken] = useState<string | null>(() => localStorage.getItem('cbpoa_token'));
|
||||||
const alerts = useRiskStore((s) => s.alerts);
|
|
||||||
const fetchAlerts = useRiskStore((s) => s.fetchAlerts);
|
const fetchAlerts = useRiskStore((s) => s.fetchAlerts);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (token) fetchAlerts();
|
if (token) fetchAlerts();
|
||||||
}, [fetchAlerts, token]);
|
}, [fetchAlerts, token]);
|
||||||
|
|
||||||
const handlePageChange = useCallback((page: string) => {
|
|
||||||
setActivePage(page);
|
|
||||||
}, []);
|
|
||||||
|
|
||||||
const handleLogin = useCallback((newToken: string) => {
|
const handleLogin = useCallback((newToken: string) => {
|
||||||
setToken(newToken);
|
setToken(newToken);
|
||||||
}, []);
|
}, []);
|
||||||
@@ -83,38 +73,18 @@ function App() {
|
|||||||
setToken(null);
|
setToken(null);
|
||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
if (!token) {
|
|
||||||
return (
|
|
||||||
<ErrorBoundary>
|
|
||||||
<Login onLogin={handleLogin} />
|
|
||||||
</ErrorBoundary>
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<ErrorBoundary>
|
<ErrorBoundary>
|
||||||
<div className="min-h-screen bg-bg-page">
|
<BrowserRouter>
|
||||||
<TopNav onLogout={handleLogout} />
|
{token ? (
|
||||||
|
<AuthedApp onLogout={handleLogout} />
|
||||||
<div className="flex pt-[52px]">
|
) : (
|
||||||
<SideNav
|
// 鉴权门:无 token 时所有路由都进入登录页。
|
||||||
activePage={activePage}
|
<Routes>
|
||||||
onPageChange={handlePageChange}
|
<Route path="*" element={<Login onLogin={handleLogin} />} />
|
||||||
alertCount={alerts.length}
|
</Routes>
|
||||||
/>
|
)}
|
||||||
|
</BrowserRouter>
|
||||||
<main className="flex-1 ml-[200px] p-5">
|
|
||||||
<Suspense fallback={<PageLoader />}>
|
|
||||||
{activePage === 'monitoring' && <MonitoringDashboard />}
|
|
||||||
{activePage === 'alerts' && <AlertsDashboard />}
|
|
||||||
{activePage === 'trend-analysis' && <TrendAnalysis />}
|
|
||||||
{activePage === 'district-comparison' && <DistrictComparison />}
|
|
||||||
{activePage === 'insights' && <Insights />}
|
|
||||||
{activePage === 'reports' && <ReportsCenter />}
|
|
||||||
</Suspense>
|
|
||||||
</main>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
</ErrorBoundary>
|
</ErrorBoundary>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,82 +7,97 @@ import { describe, it, expect } from 'vitest';
|
|||||||
describe('Component exports', () => {
|
describe('Component exports', () => {
|
||||||
it('TopNav 可以被导入', async () => {
|
it('TopNav 可以被导入', async () => {
|
||||||
const mod = await import('@/components/TopNav');
|
const mod = await import('@/components/TopNav');
|
||||||
expect(mod.default || mod.TopNav).toBeDefined();
|
expect((mod as any).default || mod.TopNav).toBeDefined();
|
||||||
});
|
});
|
||||||
|
|
||||||
it('SideNav 可以被导入', async () => {
|
it('SideNav 可以被导入', async () => {
|
||||||
const mod = await import('@/components/SideNav');
|
const mod = await import('@/components/SideNav');
|
||||||
expect(mod.default || mod.SideNav).toBeDefined();
|
expect((mod as any).default || mod.SideNav).toBeDefined();
|
||||||
});
|
});
|
||||||
|
|
||||||
it('StatCard 可以被导入', async () => {
|
it('StatCard 可以被导入', async () => {
|
||||||
const mod = await import('@/components/StatCard');
|
const mod = await import('@/components/StatCard');
|
||||||
expect(mod.default || mod.StatCard).toBeDefined();
|
expect((mod as any).default || mod.StatCard).toBeDefined();
|
||||||
});
|
});
|
||||||
|
|
||||||
it('ErrorBanner 可以被导入', async () => {
|
it('ErrorBanner 可以被导入', async () => {
|
||||||
const mod = await import('@/components/ErrorBanner');
|
const mod = await import('@/components/ErrorBanner');
|
||||||
expect(mod.default || mod.ErrorBanner).toBeDefined();
|
expect((mod as any).default || mod.ErrorBanner).toBeDefined();
|
||||||
});
|
});
|
||||||
|
|
||||||
it('DiseaseFilter 可以被导入', async () => {
|
it('DiseaseFilter 可以被导入', async () => {
|
||||||
const mod = await import('@/components/DiseaseFilter');
|
const mod = await import('@/components/DiseaseFilter');
|
||||||
expect(mod.default || mod.DiseaseFilter).toBeDefined();
|
expect((mod as any).default || mod.DiseaseFilter).toBeDefined();
|
||||||
});
|
});
|
||||||
|
|
||||||
it('ChatBot 可以被导入', async () => {
|
it('ChatBot 可以被导入', async () => {
|
||||||
const mod = await import('@/components/ChatBot');
|
const mod = await import('@/components/ChatBot');
|
||||||
expect(mod.default || mod.ChatBot).toBeDefined();
|
expect((mod as any).default || mod.ChatBot).toBeDefined();
|
||||||
});
|
});
|
||||||
|
|
||||||
it('TimelinePlayer 可以被导入', async () => {
|
it('TimelinePlayer 可以被导入', async () => {
|
||||||
const mod = await import('@/components/TimelinePlayer');
|
const mod = await import('@/components/TimelinePlayer');
|
||||||
expect(mod.default || mod.TimelinePlayer).toBeDefined();
|
expect((mod as any).default || mod.TimelinePlayer).toBeDefined();
|
||||||
});
|
});
|
||||||
|
|
||||||
it('StatisticalCharts 可以被导入', async () => {
|
it('StatisticalCharts 可以被导入', async () => {
|
||||||
const mod = await import('@/components/StatisticalCharts');
|
const mod = await import('@/components/StatisticalCharts');
|
||||||
expect(mod.default || mod.StatisticalCharts).toBeDefined();
|
expect((mod as any).default || mod.StatisticalCharts).toBeDefined();
|
||||||
});
|
});
|
||||||
|
|
||||||
it('RiskMap 可以被导入', async () => {
|
it('RiskMap 可以被导入', async () => {
|
||||||
const mod = await import('@/components/RiskMap');
|
const mod = await import('@/components/RiskMap');
|
||||||
expect(mod.default || mod.RiskMap).toBeDefined();
|
expect((mod as any).default || mod.RiskMap).toBeDefined();
|
||||||
});
|
});
|
||||||
|
|
||||||
it('AlertMap 可以被导入', async () => {
|
it('AlertMap 可以被导入', async () => {
|
||||||
const mod = await import('@/components/AlertMap');
|
const mod = await import('@/components/AlertMap');
|
||||||
expect(mod.default || mod.AlertMap).toBeDefined();
|
expect((mod as any).default || mod.AlertMap).toBeDefined();
|
||||||
});
|
});
|
||||||
|
|
||||||
it('CaseLocationMap 可以被导入', async () => {
|
it('CaseLocationMap 可以被导入', async () => {
|
||||||
const mod = await import('@/components/CaseLocationMap');
|
const mod = await import('@/components/CaseLocationMap');
|
||||||
expect(mod.default || mod.CaseLocationMap).toBeDefined();
|
expect((mod as any).default || mod.CaseLocationMap).toBeDefined();
|
||||||
});
|
});
|
||||||
|
|
||||||
it('CaseMap 可以被导入', async () => {
|
it('CaseMap 可以被导入', async () => {
|
||||||
const mod = await import('@/components/CaseMap');
|
const mod = await import('@/components/CaseMap');
|
||||||
expect(mod.default || mod.CaseMap).toBeDefined();
|
expect((mod as any).default || mod.CaseMap).toBeDefined();
|
||||||
});
|
});
|
||||||
|
|
||||||
it('DistributionChart 可以被导入', async () => {
|
it('DistributionChart 可以被导入', async () => {
|
||||||
const mod = await import('@/components/DistributionChart');
|
const mod = await import('@/components/DistributionChart');
|
||||||
expect(mod.default || mod.DistributionChart).toBeDefined();
|
expect((mod as any).default || mod.DistributionChart).toBeDefined();
|
||||||
});
|
});
|
||||||
|
|
||||||
it('GridStatsOverlay 可以被导入', async () => {
|
it('GridStatsOverlay 可以被导入', async () => {
|
||||||
const mod = await import('@/components/GridStatsOverlay');
|
const mod = await import('@/components/GridStatsOverlay');
|
||||||
expect(mod.default || mod.GridStatsOverlay).toBeDefined();
|
expect((mod as any).default || mod.GridStatsOverlay).toBeDefined();
|
||||||
});
|
});
|
||||||
|
|
||||||
it('LodGridLayer 可以被导入', async () => {
|
it('LodGridLayer 可以被导入', async () => {
|
||||||
const mod = await import('@/components/LodGridLayer');
|
const mod = await import('@/components/LodGridLayer');
|
||||||
expect(mod.default || mod.LodGridLayer).toBeDefined();
|
expect((mod as any).default || mod.LodGridLayer).toBeDefined();
|
||||||
});
|
});
|
||||||
|
|
||||||
it('AdminBreadcrumb 可以被导入', async () => {
|
it('AdminBreadcrumb 可以被导入', async () => {
|
||||||
const mod = await import('@/components/AdminBreadcrumb');
|
const mod = await import('@/components/AdminBreadcrumb');
|
||||||
expect(mod.default || mod.AdminBreadcrumb).toBeDefined();
|
expect((mod as any).default || mod.AdminBreadcrumb).toBeDefined();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('CalendarHeatmap 可以被导入', async () => {
|
||||||
|
const mod = await import('@/components/CalendarHeatmap');
|
||||||
|
expect((mod as any).default || mod.CalendarHeatmap).toBeDefined();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('MetricHeatmapTable 可以被导入', async () => {
|
||||||
|
const mod = await import('@/components/MetricHeatmapTable');
|
||||||
|
expect((mod as any).default || mod.MetricHeatmapTable).toBeDefined();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('AnomalyMarkers 可以被导入', async () => {
|
||||||
|
const mod = await import('@/components/AnomalyMarkers');
|
||||||
|
expect((mod as any).default || mod.AnomalyMarkers).toBeDefined();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
@@ -1,15 +1,18 @@
|
|||||||
import { useEffect, useRef, useState, useCallback, memo } from 'react';
|
import { useEffect, useRef, useState, useCallback, memo } from 'react';
|
||||||
import L from 'leaflet';
|
import L from 'leaflet';
|
||||||
import { useRiskStore } from '@/stores';
|
|
||||||
import { LodGridLayer } from '@/components/LodGridLayer';
|
|
||||||
import { GridStatsOverlay } from '@/components/GridStatsOverlay';
|
import { GridStatsOverlay } from '@/components/GridStatsOverlay';
|
||||||
import { useLodGrid } from '@/hooks/useLodGrid';
|
import { riskApi } from '@/services/api';
|
||||||
import type { Alert, GridRisk } from '@/types';
|
import type { RiskGridStats } from '@/services/api';
|
||||||
|
import type { Alert } from '@/types';
|
||||||
|
|
||||||
export interface CellInfo {
|
export interface CellInfo {
|
||||||
lat: number;
|
lat: number;
|
||||||
lon: number;
|
lon: number;
|
||||||
risk: number;
|
risk: number;
|
||||||
|
grid_id: string;
|
||||||
|
risk_1d: number;
|
||||||
|
risk_3d: number;
|
||||||
|
risk_7d: number;
|
||||||
nearestAlertId: string | null;
|
nearestAlertId: string | null;
|
||||||
nearestAlertDist: number;
|
nearestAlertDist: number;
|
||||||
}
|
}
|
||||||
@@ -22,16 +25,16 @@ interface AlertMapProps {
|
|||||||
showAlertMarkers?: boolean;
|
showAlertMarkers?: boolean;
|
||||||
showGrid?: boolean;
|
showGrid?: boolean;
|
||||||
filteredAlerts?: Alert[];
|
filteredAlerts?: Alert[];
|
||||||
riskRange?: [number, number];
|
|
||||||
isFullscreen?: boolean;
|
isFullscreen?: boolean;
|
||||||
}
|
}
|
||||||
|
|
||||||
const WUHAN_CENTER: [number, number] = [30.59, 114.31];
|
const WUHAN_CENTER: [number, number] = [30.59, 114.31];
|
||||||
|
const GRID_OPACITY = 0.72;
|
||||||
|
|
||||||
const RISK_COLORS: [number, number, string][] = [
|
// Mirrors the server-side colormap in backend/utils/risk_raster.py.
|
||||||
[0.0, 0.2, '#22c55e'],
|
const RISK_LEGEND: [number, number, string][] = [
|
||||||
[0.2, 0.4, '#3b82f6'],
|
[0.25, 0.4, '#38b000'],
|
||||||
[0.4, 0.6, '#eab308'],
|
[0.4, 0.6, '#facc15'],
|
||||||
[0.6, 0.8, '#f97316'],
|
[0.6, 0.8, '#f97316'],
|
||||||
[0.8, 1.0, '#ef4444'],
|
[0.8, 1.0, '#ef4444'],
|
||||||
];
|
];
|
||||||
@@ -40,12 +43,9 @@ function getRiskLabel(value: number): string {
|
|||||||
if (value >= 0.8) return '高风险';
|
if (value >= 0.8) return '高风险';
|
||||||
if (value >= 0.6) return '中高';
|
if (value >= 0.6) return '中高';
|
||||||
if (value >= 0.4) return '中风险';
|
if (value >= 0.4) return '中风险';
|
||||||
if (value >= 0.2) return '中低';
|
|
||||||
return '低风险';
|
return '低风险';
|
||||||
}
|
}
|
||||||
|
|
||||||
const EMPTY_GRIDS: GridRisk[] = [];
|
|
||||||
|
|
||||||
function AlertMapComponent({
|
function AlertMapComponent({
|
||||||
selectedGridId,
|
selectedGridId,
|
||||||
onGridClick,
|
onGridClick,
|
||||||
@@ -54,26 +54,31 @@ function AlertMapComponent({
|
|||||||
showAlertMarkers = true,
|
showAlertMarkers = true,
|
||||||
showGrid = true,
|
showGrid = true,
|
||||||
filteredAlerts = [],
|
filteredAlerts = [],
|
||||||
riskRange,
|
|
||||||
isFullscreen = false,
|
isFullscreen = false,
|
||||||
}: AlertMapProps) {
|
}: AlertMapProps) {
|
||||||
const mapRef = useRef<HTMLDivElement>(null);
|
const mapRef = useRef<HTMLDivElement>(null);
|
||||||
const mapInstanceRef = useRef<L.Map | null>(null);
|
const mapInstanceRef = useRef<L.Map | null>(null);
|
||||||
|
const riskTileRef = useRef<L.TileLayer | null>(null);
|
||||||
const alertLayerRef = useRef<L.LayerGroup | null>(null);
|
const alertLayerRef = useRef<L.LayerGroup | null>(null);
|
||||||
|
const markerMapRef = useRef<Map<string, L.CircleMarker>>(new Map());
|
||||||
const selectedMarkerRef = useRef<L.Rectangle | null>(null);
|
const selectedMarkerRef = useRef<L.Rectangle | null>(null);
|
||||||
const clickHandlerRef = useRef(onGridClick);
|
const clickHandlerRef = useRef(onGridClick);
|
||||||
const [currentZoom, setCurrentZoom] = useState(10);
|
const cellInfoRef = useRef(onCellInfo);
|
||||||
|
const resizeObserverRef = useRef<ResizeObserver | null>(null);
|
||||||
|
// Latest inputs the once-subscribed map handlers read, so we never have to
|
||||||
|
// re-subscribe (and tear down listeners) when prop/callback identities change.
|
||||||
|
const inputsRef = useRef({ filteredAlerts, showAlertMarkers, forecastDay });
|
||||||
|
|
||||||
const grids = useRiskStore((s) => s.grids ?? EMPTY_GRIDS);
|
const [gridStats, setGridStats] = useState<RiskGridStats | null>(null);
|
||||||
|
const [statsLoading, setStatsLoading] = useState(false);
|
||||||
// LOD grid data for stats overlay
|
|
||||||
const { count, avgRisk, maxRisk, loading } = useLodGrid(currentZoom, forecastDay);
|
|
||||||
|
|
||||||
|
useEffect(() => { clickHandlerRef.current = onGridClick; }, [onGridClick]);
|
||||||
|
useEffect(() => { cellInfoRef.current = onCellInfo; }, [onCellInfo]);
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
clickHandlerRef.current = onGridClick;
|
inputsRef.current = { filteredAlerts, showAlertMarkers, forecastDay };
|
||||||
}, [onGridClick]);
|
}, [filteredAlerts, showAlertMarkers, forecastDay]);
|
||||||
|
|
||||||
// Initialize map
|
// --- Initialize map once ---
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (!mapRef.current || mapInstanceRef.current) return;
|
if (!mapRef.current || mapInstanceRef.current) return;
|
||||||
|
|
||||||
@@ -88,63 +93,137 @@ function AlertMapComponent({
|
|||||||
maxZoom: 19,
|
maxZoom: 19,
|
||||||
}).addTo(map);
|
}).addTo(map);
|
||||||
|
|
||||||
map.on('zoomend', () => {
|
// Full-Wuhan 100m risk grid as raster tiles. The browser only fetches PNGs
|
||||||
setCurrentZoom(map.getZoom());
|
// (Leaflet caches them per z/x/y); LOD is inherent in the tile pyramid.
|
||||||
|
const riskTiles = L.tileLayer(riskApi.tileUrlTemplate(forecastDay), {
|
||||||
|
opacity: showGrid ? GRID_OPACITY : 0,
|
||||||
|
maxNativeZoom: 16,
|
||||||
|
maxZoom: 19,
|
||||||
|
updateWhenZooming: false,
|
||||||
|
keepBuffer: 2,
|
||||||
|
zIndex: 200,
|
||||||
|
}).addTo(map);
|
||||||
|
riskTileRef.current = riskTiles;
|
||||||
|
|
||||||
|
const alertLayer = L.layerGroup().addTo(map);
|
||||||
|
alertLayerRef.current = alertLayer;
|
||||||
|
|
||||||
|
// Click-to-inspect: query the 100m cell under the cursor. Clicks on alert
|
||||||
|
// markers are consumed by the marker handler and never reach this.
|
||||||
|
map.on('click', async (e: L.LeafletMouseEvent) => {
|
||||||
|
const { lat, lng } = e.latlng;
|
||||||
|
const { filteredAlerts: alerts, forecastDay: day } = inputsRef.current;
|
||||||
|
try {
|
||||||
|
const cell = await riskApi.getCell(lat, lng, day);
|
||||||
|
// Nearest alert (squared degree distance — cheap, no sqrt).
|
||||||
|
let nearestId: string | null = null;
|
||||||
|
let minSq = Infinity;
|
||||||
|
for (const a of alerts) {
|
||||||
|
const dx = a.latitude - lat;
|
||||||
|
const dy = a.longitude - lng;
|
||||||
|
const d = dx * dx + dy * dy;
|
||||||
|
if (d < minSq) { minSq = d; nearestId = a.grid_id; }
|
||||||
|
}
|
||||||
|
const nearestDist = Math.sqrt(minSq);
|
||||||
|
if (nearestId && nearestDist < 0.01) {
|
||||||
|
clickHandlerRef.current(nearestId);
|
||||||
|
} else if (cellInfoRef.current) {
|
||||||
|
cellInfoRef.current({
|
||||||
|
lat, lon: lng,
|
||||||
|
risk: cell.risk_value,
|
||||||
|
grid_id: cell.grid_id,
|
||||||
|
risk_1d: cell.risk_1d,
|
||||||
|
risk_3d: cell.risk_3d,
|
||||||
|
risk_7d: cell.risk_7d,
|
||||||
|
nearestAlertId: nearestId,
|
||||||
|
nearestAlertDist: nearestDist,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
} catch { /* transient fetch error — ignore the click */ }
|
||||||
});
|
});
|
||||||
|
|
||||||
mapInstanceRef.current = map;
|
mapInstanceRef.current = map;
|
||||||
|
|
||||||
|
const resizeObserver = new ResizeObserver(() => {
|
||||||
|
mapInstanceRef.current?.invalidateSize({ animate: false });
|
||||||
|
});
|
||||||
|
resizeObserver.observe(mapRef.current);
|
||||||
|
resizeObserverRef.current = resizeObserver;
|
||||||
|
|
||||||
return () => {
|
return () => {
|
||||||
|
resizeObserver.disconnect();
|
||||||
|
resizeObserverRef.current = null;
|
||||||
|
markerMapRef.current.clear();
|
||||||
|
alertLayerRef.current = null;
|
||||||
|
riskTileRef.current = null;
|
||||||
map.remove();
|
map.remove();
|
||||||
mapInstanceRef.current = null;
|
mapInstanceRef.current = null;
|
||||||
};
|
};
|
||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
// Render alert markers overlay
|
// --- Update risk tiles + stats when the forecast horizon changes ---
|
||||||
|
useEffect(() => {
|
||||||
|
const layer = riskTileRef.current;
|
||||||
|
if (layer) layer.setUrl(riskApi.tileUrlTemplate(forecastDay));
|
||||||
|
|
||||||
|
let cancelled = false;
|
||||||
|
setStatsLoading(true);
|
||||||
|
riskApi.getGridStats(forecastDay)
|
||||||
|
.then((s) => { if (!cancelled) setGridStats(s); })
|
||||||
|
.catch(() => { if (!cancelled) setGridStats(null); })
|
||||||
|
.finally(() => { if (!cancelled) setStatsLoading(false); });
|
||||||
|
return () => { cancelled = true; };
|
||||||
|
}, [forecastDay]);
|
||||||
|
|
||||||
|
// --- Toggle grid visibility without rebuilding tiles ---
|
||||||
|
useEffect(() => {
|
||||||
|
riskTileRef.current?.setOpacity(showGrid ? GRID_OPACITY : 0);
|
||||||
|
}, [showGrid]);
|
||||||
|
|
||||||
|
// --- Render alert markers via diffing against a persistent layer group ---
|
||||||
const renderAlertMarkers = useCallback(() => {
|
const renderAlertMarkers = useCallback(() => {
|
||||||
const map = mapInstanceRef.current;
|
const map = mapInstanceRef.current;
|
||||||
if (!map) return;
|
const layer = alertLayerRef.current;
|
||||||
|
if (!map || !layer) return;
|
||||||
|
|
||||||
if (alertLayerRef.current) {
|
const markerMap = markerMapRef.current;
|
||||||
try { map.removeLayer(alertLayerRef.current); } catch { /* ok */ }
|
const { filteredAlerts: alerts, showAlertMarkers: showMarkers } = inputsRef.current;
|
||||||
alertLayerRef.current = null;
|
|
||||||
|
if (!showMarkers || !alerts || alerts.length === 0) {
|
||||||
|
if (markerMap.size > 0) { layer.clearLayers(); markerMap.clear(); }
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!showAlertMarkers || !filteredAlerts || filteredAlerts.length === 0) return;
|
const b = map.getBounds();
|
||||||
|
const south = b.getSouth(), north = b.getNorth(), west = b.getWest(), east = b.getEast();
|
||||||
const layer = L.layerGroup();
|
|
||||||
const mapBounds = map.getBounds();
|
|
||||||
const maxMarkers = 500;
|
const maxMarkers = 500;
|
||||||
const step = Math.max(1, Math.floor(filteredAlerts.length / maxMarkers));
|
const step = Math.max(1, Math.floor(alerts.length / maxMarkers));
|
||||||
|
|
||||||
for (let i = 0; i < filteredAlerts.length; i += step) {
|
const desired = new Map<string, Alert>();
|
||||||
const alert = filteredAlerts[i];
|
for (let i = 0; i < alerts.length; i += step) {
|
||||||
|
const alert = alerts[i];
|
||||||
if (!alert.latitude || !alert.longitude) continue;
|
if (!alert.latitude || !alert.longitude) continue;
|
||||||
|
if (alert.latitude < south || alert.latitude > north ||
|
||||||
|
alert.longitude < west || alert.longitude > east) continue;
|
||||||
|
const key = alert.grid_id || `${alert.latitude},${alert.longitude},${i}`;
|
||||||
|
desired.set(key, alert);
|
||||||
|
}
|
||||||
|
|
||||||
// Skip if outside viewport
|
for (const [key, marker] of markerMap) {
|
||||||
if (
|
if (!desired.has(key)) { layer.removeLayer(marker); markerMap.delete(key); }
|
||||||
alert.latitude < mapBounds.getSouth() ||
|
}
|
||||||
alert.latitude > mapBounds.getNorth() ||
|
|
||||||
alert.longitude < mapBounds.getWest() ||
|
|
||||||
alert.longitude > mapBounds.getEast()
|
|
||||||
) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
for (const [key, alert] of desired) {
|
||||||
|
if (markerMap.has(key)) continue;
|
||||||
const isP1 = alert.priority === 'P1';
|
const isP1 = alert.priority === 'P1';
|
||||||
|
const marker = L.circleMarker([alert.latitude, alert.longitude], {
|
||||||
const marker = L.circleMarker(
|
radius: isP1 ? 6 : 4,
|
||||||
[alert.latitude, alert.longitude],
|
fillColor: isP1 ? '#ef4444' : '#f97316',
|
||||||
{
|
fillOpacity: 0.7,
|
||||||
radius: isP1 ? 6 : 4,
|
color: isP1 ? '#ef4444' : '#f97316',
|
||||||
fillColor: isP1 ? '#ef4444' : '#f97316',
|
weight: 2,
|
||||||
fillOpacity: 0.7,
|
dashArray: isP1 ? undefined : '4 2',
|
||||||
color: isP1 ? '#ef4444' : '#f97316',
|
});
|
||||||
weight: 2,
|
|
||||||
dashArray: isP1 ? undefined : '4 2',
|
|
||||||
}
|
|
||||||
);
|
|
||||||
|
|
||||||
marker.bindTooltip(
|
marker.bindTooltip(
|
||||||
`<div style="font-size:12px;">
|
`<div style="font-size:12px;">
|
||||||
<strong>${alert.priority}</strong> · ${(alert.risk_value * 100).toFixed(0)}%<br/>
|
<strong>${alert.priority}</strong> · ${(alert.risk_value * 100).toFixed(0)}%<br/>
|
||||||
@@ -152,106 +231,60 @@ function AlertMapComponent({
|
|||||||
</div>`,
|
</div>`,
|
||||||
{ direction: 'top', offset: [0, -5] }
|
{ direction: 'top', offset: [0, -5] }
|
||||||
);
|
);
|
||||||
|
const gridId = alert.grid_id;
|
||||||
marker.on('click', () => {
|
marker.on('click', () => { if (gridId) clickHandlerRef.current(gridId); });
|
||||||
if (alert.grid_id) clickHandlerRef.current(alert.grid_id);
|
|
||||||
});
|
|
||||||
|
|
||||||
marker.addTo(layer);
|
marker.addTo(layer);
|
||||||
|
markerMap.set(key, marker);
|
||||||
}
|
}
|
||||||
|
}, []);
|
||||||
|
|
||||||
layer.addTo(map);
|
|
||||||
alertLayerRef.current = layer;
|
|
||||||
}, [filteredAlerts, showAlertMarkers]);
|
|
||||||
|
|
||||||
// Re-render alert markers when data changes
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
renderAlertMarkers();
|
renderAlertMarkers();
|
||||||
}, [renderAlertMarkers]);
|
}, [filteredAlerts, showAlertMarkers, renderAlertMarkers]);
|
||||||
|
|
||||||
// Also re-render on map zoom/pan
|
// Re-render markers on pan/zoom, throttled, subscribed once per map instance.
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const map = mapInstanceRef.current;
|
const map = mapInstanceRef.current;
|
||||||
if (!map) return;
|
if (!map) return;
|
||||||
|
let throttle: ReturnType<typeof setTimeout> | null = null;
|
||||||
const handleMove = () => renderAlertMarkers();
|
const handleMove = () => {
|
||||||
|
if (throttle) return;
|
||||||
|
throttle = setTimeout(() => { throttle = null; renderAlertMarkers(); }, 150);
|
||||||
|
};
|
||||||
map.on('moveend', handleMove);
|
map.on('moveend', handleMove);
|
||||||
return () => { map.off('moveend', handleMove); };
|
return () => {
|
||||||
|
map.off('moveend', handleMove);
|
||||||
|
if (throttle) clearTimeout(throttle);
|
||||||
|
};
|
||||||
}, [renderAlertMarkers]);
|
}, [renderAlertMarkers]);
|
||||||
|
|
||||||
// Selected grid highlight
|
// --- Selected alert highlight (located from the alert list, no grid scan) ---
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const map = mapInstanceRef.current;
|
const map = mapInstanceRef.current;
|
||||||
if (!map) return;
|
if (!map) return;
|
||||||
|
|
||||||
if (selectedMarkerRef.current) {
|
if (selectedMarkerRef.current) {
|
||||||
try { map.removeLayer(selectedMarkerRef.current); } catch { /* ok */ }
|
try { map.removeLayer(selectedMarkerRef.current); } catch { /* ok */ }
|
||||||
selectedMarkerRef.current = null;
|
selectedMarkerRef.current = null;
|
||||||
}
|
}
|
||||||
|
if (!selectedGridId) return;
|
||||||
|
const alert = filteredAlerts.find((a) => a.grid_id === selectedGridId);
|
||||||
|
if (!alert) return;
|
||||||
|
const latHalf = 0.00045, lonHalf = 0.00052;
|
||||||
|
const rect = L.rectangle(
|
||||||
|
[[alert.latitude - latHalf, alert.longitude - lonHalf],
|
||||||
|
[alert.latitude + latHalf, alert.longitude + lonHalf]],
|
||||||
|
{ fillColor: '#3b82f6', fillOpacity: 0.3, color: '#3b82f6', weight: 3 }
|
||||||
|
).addTo(map);
|
||||||
|
selectedMarkerRef.current = rect;
|
||||||
|
map.flyTo([alert.latitude, alert.longitude], Math.max(map.getZoom(), 13), { duration: 0.5 });
|
||||||
|
}, [selectedGridId, filteredAlerts]);
|
||||||
|
|
||||||
if (selectedGridId) {
|
// --- Invalidate size after fullscreen toggle (CSS transition ~200ms) ---
|
||||||
let grid = grids.find((g) => g.grid_id === selectedGridId);
|
|
||||||
if (!grid) {
|
|
||||||
const selectedAlertObj = filteredAlerts.find((a) => a.grid_id === selectedGridId);
|
|
||||||
if (selectedAlertObj) {
|
|
||||||
grid = grids.find((g) =>
|
|
||||||
Math.abs(g.latitude - selectedAlertObj.latitude) < 0.001 &&
|
|
||||||
Math.abs(g.longitude - selectedAlertObj.longitude) < 0.001
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (grid) {
|
|
||||||
const latHalf = 0.00045;
|
|
||||||
const lonHalf = 0.00052;
|
|
||||||
const marker = L.rectangle(
|
|
||||||
[
|
|
||||||
[grid.latitude - latHalf, grid.longitude - lonHalf],
|
|
||||||
[grid.latitude + latHalf, grid.longitude + lonHalf],
|
|
||||||
],
|
|
||||||
{
|
|
||||||
fillColor: '#3b82f6',
|
|
||||||
fillOpacity: 0.3,
|
|
||||||
color: '#3b82f6',
|
|
||||||
weight: 3,
|
|
||||||
}
|
|
||||||
).addTo(map);
|
|
||||||
selectedMarkerRef.current = marker;
|
|
||||||
|
|
||||||
map.flyTo([grid.latitude, grid.longitude], Math.max(map.getZoom(), 12), { duration: 0.5 });
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}, [selectedGridId, grids]);
|
|
||||||
|
|
||||||
// Handle LOD grid cell click → find nearest alert
|
|
||||||
const handleCellClick = useCallback(
|
|
||||||
(lat: number, lon: number, risk: number) => {
|
|
||||||
let nearestId: string | null = null;
|
|
||||||
let minDist = Infinity;
|
|
||||||
|
|
||||||
if (filteredAlerts) {
|
|
||||||
for (const a of filteredAlerts) {
|
|
||||||
const d = Math.sqrt((a.latitude - lat) ** 2 + (a.longitude - lon) ** 2);
|
|
||||||
if (d < minDist) {
|
|
||||||
minDist = d;
|
|
||||||
nearestId = a.grid_id;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (nearestId && minDist < 0.01) {
|
|
||||||
clickHandlerRef.current(nearestId);
|
|
||||||
} else if (onCellInfo) {
|
|
||||||
onCellInfo({ lat, lon, risk, nearestAlertId: nearestId, nearestAlertDist: minDist });
|
|
||||||
}
|
|
||||||
},
|
|
||||||
[filteredAlerts, onCellInfo]
|
|
||||||
);
|
|
||||||
|
|
||||||
// Invalidate Leaflet size after fullscreen toggle
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const map = mapInstanceRef.current;
|
const map = mapInstanceRef.current;
|
||||||
if (!map) return;
|
if (!map) return;
|
||||||
const timer = setTimeout(() => map.invalidateSize({ animate: true }), 100);
|
map.invalidateSize({ animate: false });
|
||||||
|
const timer = setTimeout(() => map.invalidateSize({ animate: true }), 200);
|
||||||
return () => clearTimeout(timer);
|
return () => clearTimeout(timer);
|
||||||
}, [isFullscreen]);
|
}, [isFullscreen]);
|
||||||
|
|
||||||
@@ -261,29 +294,18 @@ function AlertMapComponent({
|
|||||||
<div className="relative">
|
<div className="relative">
|
||||||
<div ref={mapRef} className="w-full rounded-lg overflow-hidden" style={{ height: containerHeight }} />
|
<div ref={mapRef} className="w-full rounded-lg overflow-hidden" style={{ height: containerHeight }} />
|
||||||
|
|
||||||
{/* LOD Grid Layer */}
|
|
||||||
<LodGridLayer
|
|
||||||
map={mapInstanceRef.current}
|
|
||||||
forecastDay={forecastDay}
|
|
||||||
visible={showGrid}
|
|
||||||
riskRange={riskRange}
|
|
||||||
onCellClick={handleCellClick}
|
|
||||||
/>
|
|
||||||
|
|
||||||
{/* Stats overlay */}
|
|
||||||
<GridStatsOverlay
|
<GridStatsOverlay
|
||||||
count={count}
|
count={gridStats?.cell_count ?? 0}
|
||||||
avgRisk={avgRisk}
|
avgRisk={gridStats?.avg_risk ?? 0}
|
||||||
maxRisk={maxRisk}
|
maxRisk={gridStats?.max_risk ?? 0}
|
||||||
loading={loading}
|
loading={statsLoading}
|
||||||
forecastDay={forecastDay}
|
forecastDay={forecastDay}
|
||||||
/>
|
/>
|
||||||
|
|
||||||
{/* Legend */}
|
|
||||||
<div className="absolute bottom-4 right-4 bg-bg-card/90 backdrop-blur rounded-lg border border-border-light shadow-sm z-[1000] px-4 py-3">
|
<div className="absolute bottom-4 right-4 bg-bg-card/90 backdrop-blur rounded-lg border border-border-light shadow-sm z-[1000] px-4 py-3">
|
||||||
<div className="text-[11px] font-semibold text-text-secondary mb-2">风险等级</div>
|
<div className="text-[11px] font-semibold text-text-secondary mb-2">风险等级 (100m 网格)</div>
|
||||||
<div className="space-y-1.5">
|
<div className="space-y-1.5">
|
||||||
{RISK_COLORS.slice().reverse().map(([min, max, color]) => (
|
{RISK_LEGEND.slice().reverse().map(([min, max, color]) => (
|
||||||
<div key={color} className="flex items-center gap-2">
|
<div key={color} className="flex items-center gap-2">
|
||||||
<div className="w-4 h-4 rounded" style={{ backgroundColor: color }} />
|
<div className="w-4 h-4 rounded" style={{ backgroundColor: color }} />
|
||||||
<span className="text-[11px] text-text-secondary">
|
<span className="text-[11px] text-text-secondary">
|
||||||
@@ -291,6 +313,10 @@ function AlertMapComponent({
|
|||||||
</span>
|
</span>
|
||||||
</div>
|
</div>
|
||||||
))}
|
))}
|
||||||
|
<div className="flex items-center gap-2">
|
||||||
|
<div className="w-4 h-4 rounded border border-border-light bg-transparent" />
|
||||||
|
<span className="text-[11px] text-text-muted"><25% 不显示</span>
|
||||||
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
76
frontend/src/components/AnomalyMarkers.tsx
Normal file
76
frontend/src/components/AnomalyMarkers.tsx
Normal file
@@ -0,0 +1,76 @@
|
|||||||
|
import React from 'react';
|
||||||
|
|
||||||
|
interface AnomalyMarkersProps {
|
||||||
|
anomalies: Array<{ date: string; value: number; description: string }>;
|
||||||
|
}
|
||||||
|
|
||||||
|
export const AnomalyMarkers = React.memo(function AnomalyMarkers({
|
||||||
|
anomalies,
|
||||||
|
}: AnomalyMarkersProps) {
|
||||||
|
if (anomalies.length === 0) return null;
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div className="absolute inset-0 pointer-events-none" aria-hidden>
|
||||||
|
{anomalies.map((a, i) => {
|
||||||
|
// Parse date to position marker horizontally
|
||||||
|
// This requires the parent to position relative; markers are
|
||||||
|
// positioned via CSS custom properties set by the consumer.
|
||||||
|
// For a data-driven overlay, we expose the anomalies as a
|
||||||
|
// data list that the chart-library integration uses.
|
||||||
|
return (
|
||||||
|
<div
|
||||||
|
key={`${a.date}-${i}`}
|
||||||
|
data-anomaly-date={a.date}
|
||||||
|
data-anomaly-value={a.value}
|
||||||
|
title={`${a.date}: ${a.value} — ${a.description}`}
|
||||||
|
className="absolute w-2.5 h-2.5 rounded-full bg-red-500 border border-red-300 pointer-events-auto cursor-help"
|
||||||
|
style={{
|
||||||
|
// Positioned via CSS custom properties set by parent
|
||||||
|
left: `var(--anomaly-x-${i})`,
|
||||||
|
top: `var(--anomaly-y-${i})`,
|
||||||
|
transform: 'translate(-50%, -50%)',
|
||||||
|
}}
|
||||||
|
/>
|
||||||
|
);
|
||||||
|
})}
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Helper to compute anomaly positions as percentages within a chart area.
|
||||||
|
*
|
||||||
|
* Usage inside a Recharts chart component:
|
||||||
|
* - Import { computeAnomalyPositions } from './AnomalyMarkers'
|
||||||
|
* - Call with bounds: computeAnomalyPositions(chartData, anomalies, dateKey, valueKey, xDomain, yDomain)
|
||||||
|
* - Apply returned style vars on parent container
|
||||||
|
*/
|
||||||
|
export function computeAnomalyPositions(
|
||||||
|
allDates: string[],
|
||||||
|
anomalies: Array<{ date: string; value: number }>,
|
||||||
|
xMin: number,
|
||||||
|
xMax: number,
|
||||||
|
yMin: number,
|
||||||
|
yMax: number,
|
||||||
|
): Record<string, string> {
|
||||||
|
const vars: Record<string, string> = {};
|
||||||
|
const xRange = xMax - xMin || 1;
|
||||||
|
const yRange = yMax - yMin || 1;
|
||||||
|
|
||||||
|
// Build a date→index map once for O(1) lookups instead of indexOf per anomaly.
|
||||||
|
const dateIndex = new Map<string, number>();
|
||||||
|
for (let i = 0; i < allDates.length; i++) {
|
||||||
|
if (!dateIndex.has(allDates[i])) dateIndex.set(allDates[i], i);
|
||||||
|
}
|
||||||
|
|
||||||
|
for (let i = 0; i < anomalies.length; i++) {
|
||||||
|
const a = anomalies[i];
|
||||||
|
const dateIdx = dateIndex.get(a.date) ?? -1;
|
||||||
|
if (dateIdx === -1) continue;
|
||||||
|
const xPct = ((dateIdx - xMin) / xRange) * 100;
|
||||||
|
const yPct = 100 - ((a.value - yMin) / yRange) * 100;
|
||||||
|
vars[`--anomaly-x-${i}`] = `${xPct}%`;
|
||||||
|
vars[`--anomaly-y-${i}`] = `${yPct}%`;
|
||||||
|
}
|
||||||
|
return vars;
|
||||||
|
}
|
||||||
141
frontend/src/components/AppShell.tsx
Normal file
141
frontend/src/components/AppShell.tsx
Normal file
@@ -0,0 +1,141 @@
|
|||||||
|
import { useState, useCallback, useEffect, useRef } from 'react';
|
||||||
|
import { Outlet } from 'react-router-dom';
|
||||||
|
import { TopNav } from '@/components/TopNav';
|
||||||
|
import { SideNav } from '@/components/SideNav';
|
||||||
|
import { RouteErrorBoundary } from '@/components/RouteErrorBoundary';
|
||||||
|
import { useRiskStore } from '@/stores';
|
||||||
|
import { TESTIDS } from '@/utils/testids';
|
||||||
|
|
||||||
|
interface AppShellProps {
|
||||||
|
onLogout?: () => void;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 收集容器内当前可聚焦的元素,供初始聚焦与焦点循环陷阱使用。
|
||||||
|
function getFocusable(container: HTMLElement): HTMLElement[] {
|
||||||
|
return Array.from(
|
||||||
|
container.querySelectorAll<HTMLElement>(
|
||||||
|
'a[href], button:not([disabled]), [tabindex]:not([tabindex="-1"])'
|
||||||
|
)
|
||||||
|
).filter((el) => el.offsetParent !== null || el === document.activeElement);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 仅负责布局骨架(顶栏 / 侧栏 / 内容区),不涉及路由匹配与鉴权。
|
||||||
|
export function AppShell({ onLogout }: AppShellProps) {
|
||||||
|
const alerts = useRiskStore((s) => s.alerts);
|
||||||
|
const [drawerOpen, setDrawerOpen] = useState(false);
|
||||||
|
// 提升手风琴展开态:导轨与抽屉两份 SideNav 共享,保持同步。
|
||||||
|
const [expandedNav, setExpandedNav] = useState<string | null>('monitoring');
|
||||||
|
const drawerRef = useRef<HTMLElement>(null);
|
||||||
|
|
||||||
|
const openDrawer = useCallback(() => setDrawerOpen(true), []);
|
||||||
|
const closeDrawer = useCallback(() => setDrawerOpen(false), []);
|
||||||
|
|
||||||
|
// 抽屉作为模态:ESC 关闭、锁定 body 滚动、焦点移入并在关闭后归还给汉堡。
|
||||||
|
useEffect(() => {
|
||||||
|
if (!drawerOpen) return;
|
||||||
|
|
||||||
|
const opener = document.activeElement as HTMLElement | null;
|
||||||
|
|
||||||
|
// 锁定 body 滚动,关闭时还原原值。
|
||||||
|
const prevOverflow = document.body.style.overflow;
|
||||||
|
document.body.style.overflow = 'hidden';
|
||||||
|
|
||||||
|
// 焦点移入抽屉(优先第一个可聚焦元素,否则聚焦抽屉容器本身)。
|
||||||
|
const drawer = drawerRef.current;
|
||||||
|
const focusables = drawer ? getFocusable(drawer) : [];
|
||||||
|
(focusables[0] ?? drawer)?.focus();
|
||||||
|
|
||||||
|
const onKeyDown = (e: KeyboardEvent) => {
|
||||||
|
if (e.key === 'Escape') {
|
||||||
|
e.preventDefault();
|
||||||
|
closeDrawer();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
// 焦点循环陷阱:Tab 在抽屉内首尾元素之间循环。
|
||||||
|
if (e.key === 'Tab' && drawer) {
|
||||||
|
const items = getFocusable(drawer);
|
||||||
|
if (items.length === 0) {
|
||||||
|
e.preventDefault();
|
||||||
|
drawer.focus();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
const first = items[0];
|
||||||
|
const last = items[items.length - 1];
|
||||||
|
const active = document.activeElement;
|
||||||
|
if (e.shiftKey && (active === first || active === drawer)) {
|
||||||
|
e.preventDefault();
|
||||||
|
last.focus();
|
||||||
|
} else if (!e.shiftKey && active === last) {
|
||||||
|
e.preventDefault();
|
||||||
|
first.focus();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
document.addEventListener('keydown', onKeyDown);
|
||||||
|
|
||||||
|
return () => {
|
||||||
|
document.removeEventListener('keydown', onKeyDown);
|
||||||
|
document.body.style.overflow = prevOverflow;
|
||||||
|
// 关闭后把焦点还给打开抽屉的元素(汉堡按钮),回退到按 testid 查询。
|
||||||
|
const restoreTarget =
|
||||||
|
opener ??
|
||||||
|
document.querySelector<HTMLElement>(`[data-testid="${TESTIDS.hamburger}"]`);
|
||||||
|
restoreTarget?.focus();
|
||||||
|
};
|
||||||
|
}, [drawerOpen, closeDrawer]);
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div data-testid={TESTIDS.appShell} className="h-screen bg-bg-page flex flex-col overflow-hidden">
|
||||||
|
<TopNav onLogout={onLogout} onToggleMenu={openDrawer} isMenuOpen={drawerOpen} />
|
||||||
|
|
||||||
|
<div className="flex flex-1 min-h-0">
|
||||||
|
{/* lg 及以上:持久侧栏导轨 */}
|
||||||
|
<aside
|
||||||
|
data-testid={TESTIDS.sidebarRail}
|
||||||
|
className="hidden lg:block w-[200px] shrink-0 bg-bg-card border-r border-border"
|
||||||
|
>
|
||||||
|
<SideNav
|
||||||
|
alertCount={alerts.length}
|
||||||
|
expanded={expandedNav}
|
||||||
|
onExpandedChange={setExpandedNav}
|
||||||
|
/>
|
||||||
|
</aside>
|
||||||
|
|
||||||
|
{/* lg 以下:离屏抽屉 + 遮罩 */}
|
||||||
|
{drawerOpen && (
|
||||||
|
<div
|
||||||
|
className="fixed inset-0 z-40 bg-black/40 lg:hidden"
|
||||||
|
onClick={closeDrawer}
|
||||||
|
aria-hidden="true"
|
||||||
|
/>
|
||||||
|
)}
|
||||||
|
<aside
|
||||||
|
ref={drawerRef}
|
||||||
|
id="app-drawer"
|
||||||
|
role="dialog"
|
||||||
|
aria-modal="true"
|
||||||
|
aria-label="导航菜单"
|
||||||
|
tabIndex={-1}
|
||||||
|
data-testid={TESTIDS.appDrawer}
|
||||||
|
className={`fixed top-0 left-0 bottom-0 z-50 w-[260px] max-w-[80vw] bg-bg-card border-r border-border shadow-xl transition-transform duration-200 lg:hidden ${
|
||||||
|
drawerOpen ? 'translate-x-0' : '-translate-x-full'
|
||||||
|
}`}
|
||||||
|
>
|
||||||
|
<SideNav
|
||||||
|
alertCount={alerts.length}
|
||||||
|
onNavigate={closeDrawer}
|
||||||
|
expanded={expandedNav}
|
||||||
|
onExpandedChange={setExpandedNav}
|
||||||
|
/>
|
||||||
|
</aside>
|
||||||
|
|
||||||
|
<main className="flex-1 min-w-0 overflow-auto p-5">
|
||||||
|
<RouteErrorBoundary>
|
||||||
|
<Outlet />
|
||||||
|
</RouteErrorBoundary>
|
||||||
|
</main>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
}
|
||||||
38
frontend/src/components/CLAUDE.md
Normal file
38
frontend/src/components/CLAUDE.md
Normal file
@@ -0,0 +1,38 @@
|
|||||||
|
# Components — Reusable UI
|
||||||
|
|
||||||
|
## Conventions
|
||||||
|
|
||||||
|
- One component per file, PascalCase, default export
|
||||||
|
- Props interface: `{ComponentName}Props`, typed strictly (no `any`)
|
||||||
|
- Wrap pure display components in `memo()` for render optimization
|
||||||
|
- All styling via Tailwind utility classes — no CSS modules, no inline styles
|
||||||
|
|
||||||
|
## Component Types
|
||||||
|
|
||||||
|
**Map components** (`*Map.tsx`) — Leaflet-based maps:
|
||||||
|
- Use `react-leaflet` / direct Leaflet manipulation via `useRef`
|
||||||
|
- Risk coloring: centralized `RISK_COLORS` and `RISK_LABELS` constants
|
||||||
|
- Coordinate system: `[lat, lng]` (Leaflet convention, NOT `[lng, lat]`)
|
||||||
|
|
||||||
|
**Chart components** (`*Chart*.tsx`) — Recharts:
|
||||||
|
- Responsive containers with `width="100%" height={...}`
|
||||||
|
|
||||||
|
**Navigation** (`TopNav.tsx`, `SideNav.tsx`):
|
||||||
|
- No data fetching — pure navigation/presentation
|
||||||
|
|
||||||
|
**Overlay/Utility** (`GridStatsOverlay`, `ErrorBanner`, `StatCard`, `TimelinePlayer`):
|
||||||
|
- Small, focused, reusable across pages
|
||||||
|
|
||||||
|
## Data Flow
|
||||||
|
|
||||||
|
- Components receive data via props, never fetch directly
|
||||||
|
- Callbacks passed as props: `onGridSelect`, `onClosePanel`, `onForecastChange`
|
||||||
|
- Complex stateful behavior extracted to custom hooks (e.g., `useTimelineStore`)
|
||||||
|
|
||||||
|
## Anti-Patterns
|
||||||
|
|
||||||
|
- Don't fetch data in components — receive via props or store hooks
|
||||||
|
- Don't create god components (>200 lines) — extract sub-components
|
||||||
|
- Don't use `any` in prop types — use `unknown` and narrow
|
||||||
|
- Don't pass Leaflet map instances between components — each map manages its own instance
|
||||||
|
- Don't use CSS modules or inline styles — Tailwind only
|
||||||
198
frontend/src/components/CalendarHeatmap.tsx
Normal file
198
frontend/src/components/CalendarHeatmap.tsx
Normal file
@@ -0,0 +1,198 @@
|
|||||||
|
import React, { useMemo } from 'react';
|
||||||
|
|
||||||
|
interface CalendarHeatmapProps {
|
||||||
|
data: Array<{ date: string; value: number }>;
|
||||||
|
year: number;
|
||||||
|
onDayClick?: (date: string) => void;
|
||||||
|
}
|
||||||
|
|
||||||
|
function getColor(value: number): string {
|
||||||
|
if (value < 50) return '#10B981';
|
||||||
|
if (value < 100) return '#F59E0B';
|
||||||
|
if (value < 150) return '#F97316';
|
||||||
|
if (value < 200) return '#EF4444';
|
||||||
|
return '#7C3AED';
|
||||||
|
}
|
||||||
|
|
||||||
|
function getLabel(value: number): string {
|
||||||
|
if (value < 50) return '优';
|
||||||
|
if (value < 100) return '良';
|
||||||
|
if (value < 150) return '轻度';
|
||||||
|
if (value < 200) return '中度';
|
||||||
|
return '重度';
|
||||||
|
}
|
||||||
|
|
||||||
|
const MONTH_NAMES = [
|
||||||
|
'1月', '2月', '3月', '4月', '5月', '6月',
|
||||||
|
'7月', '8月', '9月', '10月', '11月', '12月',
|
||||||
|
];
|
||||||
|
|
||||||
|
const DAY_NAMES = ['一', '二', '三', '四', '五', '六', '日'];
|
||||||
|
|
||||||
|
export const CalendarHeatmap = React.memo(function CalendarHeatmap({
|
||||||
|
data,
|
||||||
|
year,
|
||||||
|
onDayClick,
|
||||||
|
}: CalendarHeatmapProps) {
|
||||||
|
const dataMap = useMemo(() => {
|
||||||
|
const map = new Map<string, number>();
|
||||||
|
for (const d of data) {
|
||||||
|
map.set(d.date, d.value);
|
||||||
|
}
|
||||||
|
return map;
|
||||||
|
}, [data]);
|
||||||
|
|
||||||
|
const months = useMemo(() => {
|
||||||
|
const result: Array<{
|
||||||
|
month: number;
|
||||||
|
name: string;
|
||||||
|
weeks: Array<Array<{ date: string; day: number; value: number | null }>>;
|
||||||
|
}> = [];
|
||||||
|
|
||||||
|
for (let m = 0; m < 12; m++) {
|
||||||
|
const daysInMonth = new Date(year, m + 1, 0).getDate();
|
||||||
|
const cells: Array<{ date: string; day: number; value: number | null }> = [];
|
||||||
|
|
||||||
|
for (let d = 1; d <= daysInMonth; d++) {
|
||||||
|
const dateObj = new Date(year, m, d);
|
||||||
|
const dateStr = dateObj.toISOString().slice(0, 10);
|
||||||
|
cells.push({
|
||||||
|
date: dateStr,
|
||||||
|
day: d,
|
||||||
|
value: dataMap.get(dateStr) ?? null,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
// Calculate start day of week (1=Monday, 0=Sunday → JS getDay: 0=Sun)
|
||||||
|
const firstDay = new Date(year, m, 1).getDay();
|
||||||
|
// Convert JS Sunday=0 to Monday=0
|
||||||
|
const startOffset = firstDay === 0 ? 6 : firstDay - 1;
|
||||||
|
|
||||||
|
// Pad beginning with empty cells
|
||||||
|
const padded: Array<{ date: string; day: number; value: number | null } | null> = [];
|
||||||
|
for (let i = 0; i < startOffset; i++) {
|
||||||
|
padded.push(null);
|
||||||
|
}
|
||||||
|
for (const cell of cells) {
|
||||||
|
padded.push(cell);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Split into weeks of 7
|
||||||
|
const weeks: Array<Array<{ date: string; day: number; value: number | null }>> = [];
|
||||||
|
for (let i = 0; i < padded.length; i += 7) {
|
||||||
|
const week = padded.slice(i, i + 7).filter(Boolean) as Array<{
|
||||||
|
date: string;
|
||||||
|
day: number;
|
||||||
|
value: number | null;
|
||||||
|
}>;
|
||||||
|
if (week.length > 0) {
|
||||||
|
weeks.push(week);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
result.push({ month: m, name: MONTH_NAMES[m], weeks });
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}, [year, dataMap]);
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div className="space-y-4">
|
||||||
|
{/* Legend */}
|
||||||
|
<div className="flex items-center gap-1.5 text-[10px] text-gray-500">
|
||||||
|
{[
|
||||||
|
{ color: '#10B981', label: '优 <50' },
|
||||||
|
{ color: '#F59E0B', label: '良 50-100' },
|
||||||
|
{ color: '#F97316', label: '轻度 100-150' },
|
||||||
|
{ color: '#EF4444', label: '中度 150-200' },
|
||||||
|
{ color: '#7C3AED', label: '重度 ≥200' },
|
||||||
|
{ color: '#E5E7EB', label: '无数据' },
|
||||||
|
].map((item) => (
|
||||||
|
<span key={item.label} className="inline-flex items-center gap-1">
|
||||||
|
<span
|
||||||
|
className="inline-block w-2.5 h-2.5 rounded-sm"
|
||||||
|
style={{ backgroundColor: item.color }}
|
||||||
|
/>
|
||||||
|
{item.label}
|
||||||
|
</span>
|
||||||
|
))}
|
||||||
|
</div>
|
||||||
|
|
||||||
|
{/* Calendar grid */}
|
||||||
|
<div className="grid grid-cols-3 sm:grid-cols-4 md:grid-cols-6 lg:grid-cols-12 gap-4">
|
||||||
|
{months.map((month) => (
|
||||||
|
<div key={month.month} className="flex flex-col items-center">
|
||||||
|
<div className="text-[11px] font-medium text-gray-500 mb-1">
|
||||||
|
{month.name}
|
||||||
|
</div>
|
||||||
|
{/* Day header row */}
|
||||||
|
<div className="grid grid-cols-7 gap-px mb-0.5" style={{ width: 98 }}>
|
||||||
|
{DAY_NAMES.map((d) => (
|
||||||
|
<div
|
||||||
|
key={d}
|
||||||
|
className="text-[8px] text-gray-400 text-center leading-3 w-[14px] h-3"
|
||||||
|
>
|
||||||
|
{d}
|
||||||
|
</div>
|
||||||
|
))}
|
||||||
|
</div>
|
||||||
|
{/* Weeks */}
|
||||||
|
{month.weeks.map((week, wi) => (
|
||||||
|
<div key={wi} className="flex gap-px">
|
||||||
|
{Array.from({ length: 7 }).map((_, di) => {
|
||||||
|
// Match by day-of-week index
|
||||||
|
const dayOfWeekMap = [1, 2, 3, 4, 5, 6, 0]; // Mon=1..Sun=0
|
||||||
|
const matchedCell = week.find(
|
||||||
|
(c) => new Date(c.date).getDay() === dayOfWeekMap[di],
|
||||||
|
);
|
||||||
|
|
||||||
|
if (!matchedCell) {
|
||||||
|
return (
|
||||||
|
<div
|
||||||
|
key={di}
|
||||||
|
className="w-[14px] h-[14px]"
|
||||||
|
aria-hidden
|
||||||
|
/>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
const bg =
|
||||||
|
matchedCell.value === null
|
||||||
|
? '#E5E7EB'
|
||||||
|
: getColor(matchedCell.value);
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div
|
||||||
|
key={di}
|
||||||
|
title={
|
||||||
|
matchedCell.value !== null
|
||||||
|
? `${matchedCell.date}: AQI ${matchedCell.value} (${getLabel(matchedCell.value)})`
|
||||||
|
: `${matchedCell.date}: 无数据`
|
||||||
|
}
|
||||||
|
onClick={() => onDayClick?.(matchedCell.date)}
|
||||||
|
className={`w-[14px] h-[14px] rounded-sm transition-transform hover:scale-125 ${
|
||||||
|
onDayClick ? 'cursor-pointer' : ''
|
||||||
|
}`}
|
||||||
|
style={{ backgroundColor: bg }}
|
||||||
|
role={onDayClick ? 'button' : undefined}
|
||||||
|
tabIndex={onDayClick ? 0 : undefined}
|
||||||
|
onKeyDown={
|
||||||
|
onDayClick
|
||||||
|
? (e: React.KeyboardEvent) => {
|
||||||
|
if (e.key === 'Enter' || e.key === ' ') {
|
||||||
|
e.preventDefault();
|
||||||
|
onDayClick(matchedCell.date);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
: undefined
|
||||||
|
}
|
||||||
|
/>
|
||||||
|
);
|
||||||
|
})}
|
||||||
|
</div>
|
||||||
|
))}
|
||||||
|
</div>
|
||||||
|
))}
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
});
|
||||||
@@ -1,23 +1,73 @@
|
|||||||
import { useEffect, useRef, useState, memo } from 'react';
|
import { useEffect, useRef, useState, memo } from 'react';
|
||||||
import L from 'leaflet';
|
import L from 'leaflet';
|
||||||
import { geocodedApi } from '@/services/api';
|
import { geocodedApi } from '@/services/api';
|
||||||
|
import { TESTIDS } from '@/utils/testids';
|
||||||
import type { GeocodedCase } from '@/types';
|
import type { GeocodedCase } from '@/types';
|
||||||
|
|
||||||
const WUHAN_CENTER: [number, number] = [30.59, 114.31];
|
const WUHAN_CENTER: [number, number] = [30.59, 114.31];
|
||||||
|
|
||||||
|
// 视图模式:
|
||||||
|
// 'points' —— 个体病例点(默认,非医生视角)。逐病例渲染 circleMarker,
|
||||||
|
// 并在 DOM 中输出隐藏的 patient-point 镜像供 e2e 计数。
|
||||||
|
// 'density' —— 聚合密度(医生视角,隐私不变量)。仅按行政区/街道聚合的密度圆,
|
||||||
|
// 不渲染任何个体点,patient-point 数量必须为 0。
|
||||||
|
type CaseMapMode = 'points' | 'density';
|
||||||
|
|
||||||
interface CaseLocationMapProps {
|
interface CaseLocationMapProps {
|
||||||
height?: string;
|
height?: string;
|
||||||
district?: string | null;
|
district?: string | null;
|
||||||
street?: string | null;
|
street?: string | null;
|
||||||
|
date?: string | null;
|
||||||
|
mode?: CaseMapMode;
|
||||||
}
|
}
|
||||||
|
|
||||||
function CaseLocationMapComponent({ height = '400px', district = null, street = null }: CaseLocationMapProps) {
|
// 聚合中心:按 street(无则 district)分组,取经纬度均值 + 计数。
|
||||||
|
interface DensityCluster {
|
||||||
|
key: string;
|
||||||
|
label: string;
|
||||||
|
latitude: number;
|
||||||
|
longitude: number;
|
||||||
|
count: number;
|
||||||
|
}
|
||||||
|
|
||||||
|
function aggregateClusters(cases: GeocodedCase[]): DensityCluster[] {
|
||||||
|
const groups: Record<string, { latSum: number; lonSum: number; count: number; label: string }> = {};
|
||||||
|
for (const c of cases) {
|
||||||
|
if (!c.latitude || !c.longitude) continue;
|
||||||
|
const key = `${c.district}/${c.street || ''}`;
|
||||||
|
const label = c.street ? `${c.district} ${c.street}` : c.district;
|
||||||
|
if (!groups[key]) groups[key] = { latSum: 0, lonSum: 0, count: 0, label };
|
||||||
|
groups[key].latSum += c.latitude;
|
||||||
|
groups[key].lonSum += c.longitude;
|
||||||
|
groups[key].count += 1;
|
||||||
|
}
|
||||||
|
return Object.entries(groups).map(([key, g]) => ({
|
||||||
|
key,
|
||||||
|
label: g.label,
|
||||||
|
latitude: g.latSum / g.count,
|
||||||
|
longitude: g.lonSum / g.count,
|
||||||
|
count: g.count,
|
||||||
|
}));
|
||||||
|
}
|
||||||
|
|
||||||
|
function CaseLocationMapComponent({
|
||||||
|
height = '400px',
|
||||||
|
district = null,
|
||||||
|
street = null,
|
||||||
|
date = null,
|
||||||
|
mode = 'points',
|
||||||
|
}: CaseLocationMapProps) {
|
||||||
const mapRef = useRef<HTMLDivElement>(null);
|
const mapRef = useRef<HTMLDivElement>(null);
|
||||||
const mapInstanceRef = useRef<L.Map | null>(null);
|
const mapInstanceRef = useRef<L.Map | null>(null);
|
||||||
const layerRef = useRef<L.LayerGroup | null>(null);
|
const layerRef = useRef<L.LayerGroup | null>(null);
|
||||||
const cancelledRef = useRef(false);
|
const cancelledRef = useRef(false);
|
||||||
|
const resizeObserverRef = useRef<ResizeObserver | null>(null);
|
||||||
const [isLoading, setIsLoading] = useState(true);
|
const [isLoading, setIsLoading] = useState(true);
|
||||||
const [caseCount, setCaseCount] = useState(0);
|
const [caseCount, setCaseCount] = useState(0);
|
||||||
|
// points 模式下的隐藏 DOM 镜像(每病例一项,供 e2e 对 patient-point 计数)。
|
||||||
|
// density 模式下保持为空数组 —— 隐私不变量:医生视角下 patient-point 必须为 0。
|
||||||
|
const [pointKeys, setPointKeys] = useState<string[]>([]);
|
||||||
|
const [clusterCount, setClusterCount] = useState(0);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (!mapRef.current || mapInstanceRef.current) return;
|
if (!mapRef.current || mapInstanceRef.current) return;
|
||||||
@@ -39,8 +89,19 @@ function CaseLocationMapComponent({ height = '400px', district = null, street =
|
|||||||
mapInstanceRef.current = map;
|
mapInstanceRef.current = map;
|
||||||
layerRef.current = L.layerGroup().addTo(map);
|
layerRef.current = L.layerGroup().addTo(map);
|
||||||
|
|
||||||
|
// ResizeObserver: auto-invalidate when container size changes (window resize, layout shifts)
|
||||||
|
const resizeObserver = new ResizeObserver(() => {
|
||||||
|
if (mapInstanceRef.current) {
|
||||||
|
mapInstanceRef.current.invalidateSize({ animate: false });
|
||||||
|
}
|
||||||
|
});
|
||||||
|
if (mapRef.current) {
|
||||||
|
resizeObserver.observe(mapRef.current);
|
||||||
|
}
|
||||||
|
resizeObserverRef.current = resizeObserver;
|
||||||
|
|
||||||
// Fetch case locations
|
// Fetch case locations
|
||||||
geocodedApi.getGeocoded({ limit: 5000, district: district || undefined })
|
geocodedApi.getGeocoded({ limit: 5000, district: district || undefined, date: date || undefined })
|
||||||
.then((data) => {
|
.then((data) => {
|
||||||
if (cancelledRef.current) return;
|
if (cancelledRef.current) return;
|
||||||
const cases: GeocodedCase[] = data.cases || [];
|
const cases: GeocodedCase[] = data.cases || [];
|
||||||
@@ -66,6 +127,42 @@ function CaseLocationMapComponent({ height = '400px', district = null, street =
|
|||||||
|
|
||||||
if (cancelledRef.current) return;
|
if (cancelledRef.current) return;
|
||||||
|
|
||||||
|
if (mode === 'density') {
|
||||||
|
// 医生视角:仅渲染聚合密度圆(按街道/区聚合),不渲染任何个体点。
|
||||||
|
const clusters = aggregateClusters(unique);
|
||||||
|
const maxCount = clusters.reduce((m, c) => Math.max(m, c.count), 1);
|
||||||
|
|
||||||
|
for (const cl of clusters) {
|
||||||
|
// 半径随计数缩放(8–28px),明确表达「密度」而非个体位置。
|
||||||
|
const radius = 8 + Math.round((cl.count / maxCount) * 20);
|
||||||
|
const marker = L.circleMarker([cl.latitude, cl.longitude], {
|
||||||
|
radius,
|
||||||
|
fillColor: '#7c3aed',
|
||||||
|
fillOpacity: 0.35,
|
||||||
|
color: '#7c3aed',
|
||||||
|
weight: 1.5,
|
||||||
|
});
|
||||||
|
marker.bindTooltip(
|
||||||
|
`<div style="font-size:12px"><strong>${cl.label}</strong><br/>病例数: ${cl.count}</div>`,
|
||||||
|
{ direction: 'top', offset: [0, -4] }
|
||||||
|
);
|
||||||
|
marker.addTo(layer);
|
||||||
|
}
|
||||||
|
|
||||||
|
setClusterCount(clusters.length);
|
||||||
|
setCaseCount(unique.length);
|
||||||
|
setPointKeys([]); // 隐私不变量:density 下无个体点镜像
|
||||||
|
setIsLoading(false);
|
||||||
|
|
||||||
|
if (clusters.length > 0) {
|
||||||
|
const bounds = L.latLngBounds(clusters.map((c) => [c.latitude, c.longitude]));
|
||||||
|
map.fitBounds(bounds, { padding: [30, 30] });
|
||||||
|
}
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// points 模式(默认):逐病例渲染个体 circleMarker。
|
||||||
|
const keys: string[] = [];
|
||||||
for (const c of unique) {
|
for (const c of unique) {
|
||||||
if (!c.latitude || !c.longitude) continue;
|
if (!c.latitude || !c.longitude) continue;
|
||||||
|
|
||||||
@@ -87,9 +184,12 @@ function CaseLocationMapComponent({ height = '400px', district = null, street =
|
|||||||
);
|
);
|
||||||
|
|
||||||
marker.addTo(layer);
|
marker.addTo(layer);
|
||||||
|
keys.push(c.case_id);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
setClusterCount(0);
|
||||||
setCaseCount(unique.length);
|
setCaseCount(unique.length);
|
||||||
|
setPointKeys(keys); // 隐藏 DOM 镜像供 e2e 计数
|
||||||
setIsLoading(false);
|
setIsLoading(false);
|
||||||
|
|
||||||
// Fit bounds to case locations
|
// Fit bounds to case locations
|
||||||
@@ -104,26 +204,48 @@ function CaseLocationMapComponent({ height = '400px', district = null, street =
|
|||||||
|
|
||||||
return () => {
|
return () => {
|
||||||
cancelledRef.current = true;
|
cancelledRef.current = true;
|
||||||
|
if (resizeObserverRef.current) {
|
||||||
|
resizeObserverRef.current.disconnect();
|
||||||
|
resizeObserverRef.current = null;
|
||||||
|
}
|
||||||
map.remove();
|
map.remove();
|
||||||
mapInstanceRef.current = null;
|
mapInstanceRef.current = null;
|
||||||
};
|
};
|
||||||
}, [district, street]);
|
}, [district, street, date, mode]);
|
||||||
|
|
||||||
|
const isDensity = mode === 'density';
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className="relative">
|
<div className="relative" data-case-map-mode={mode}>
|
||||||
<div ref={mapRef} style={{ height, width: '100%', borderRadius: '8px' }} />
|
<div ref={mapRef} style={{ height, width: '100%', borderRadius: '8px' }} />
|
||||||
{isLoading && (
|
{isLoading && (
|
||||||
<div className="absolute inset-0 flex items-center justify-center bg-white/80 rounded-lg">
|
<div className="absolute inset-0 flex items-center justify-center bg-white/80 rounded-lg">
|
||||||
<div className="text-sm text-gray-500">加载病例位置...</div>
|
<div className="text-sm text-gray-500">加载病例位置...</div>
|
||||||
</div>
|
</div>
|
||||||
)}
|
)}
|
||||||
{!isLoading && (
|
{!isLoading && !isDensity && (
|
||||||
<div className="absolute top-2 right-2 bg-white/90 px-3 py-1.5 rounded shadow text-xs">
|
<div className="absolute top-2 right-2 bg-white/90 px-3 py-1.5 rounded shadow text-xs">
|
||||||
<span className="text-blue-600 font-semibold">{caseCount.toLocaleString()}</span> 个病例位置
|
<span className="text-blue-600 font-semibold">{caseCount.toLocaleString()}</span> 个病例位置
|
||||||
<span className="ml-2 text-red-500">● 住院</span>
|
<span className="ml-2 text-red-500">● 住院</span>
|
||||||
<span className="ml-1 text-blue-500">● 门诊</span>
|
<span className="ml-1 text-blue-500">● 门诊</span>
|
||||||
</div>
|
</div>
|
||||||
)}
|
)}
|
||||||
|
{!isLoading && isDensity && (
|
||||||
|
<div className="absolute top-2 right-2 bg-white/90 px-3 py-1.5 rounded shadow text-xs">
|
||||||
|
<span className="text-purple-600 font-semibold">{clusterCount.toLocaleString()}</span> 个聚合区域
|
||||||
|
<span className="ml-2 text-gray-500">按区域聚合密度(隐私保护)</span>
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
{/*
|
||||||
|
隐藏 DOM 镜像:points 模式下每病例输出一个 patient-point 节点,使 e2e 能对
|
||||||
|
Leaflet canvas 之外的真实 DOM 做计数断言。density 模式下 pointKeys 恒为空,
|
||||||
|
因此医生视角下 [data-testid=patient-point] 数量必为 0(隐私不变量)。
|
||||||
|
*/}
|
||||||
|
<div className="hidden" aria-hidden="true">
|
||||||
|
{pointKeys.map((id) => (
|
||||||
|
<span key={id} data-testid={TESTIDS.patientPoint} data-case-id={id} />
|
||||||
|
))}
|
||||||
|
</div>
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
import { memo, useEffect, useRef, useState, useCallback } from 'react';
|
import { memo, useEffect, useRef, useState, useCallback } from 'react';
|
||||||
|
import { Skeleton } from '@/components/ui';
|
||||||
import L from 'leaflet';
|
import L from 'leaflet';
|
||||||
import 'leaflet/dist/leaflet.css';
|
import 'leaflet/dist/leaflet.css';
|
||||||
import { geocodedApi } from '@/services/api';
|
import { geocodedApi } from '@/services/api';
|
||||||
@@ -340,7 +341,7 @@ function CaseMapComponent({ height = '480px' }: CaseMapProps) {
|
|||||||
<div className="bg-bg-card/90 backdrop-blur rounded-lg border border-border-light shadow-sm px-3 py-2">
|
<div className="bg-bg-card/90 backdrop-blur rounded-lg border border-border-light shadow-sm px-3 py-2">
|
||||||
<div className="text-[11px] text-text-secondary">
|
<div className="text-[11px] text-text-secondary">
|
||||||
{isLoading ? (
|
{isLoading ? (
|
||||||
<span className="text-text-muted">数据加载中...</span>
|
<Skeleton className="h-3 w-20 inline-block align-middle" />
|
||||||
) : error ? (
|
) : error ? (
|
||||||
<span className="text-danger">加载失败: {error}</span>
|
<span className="text-danger">加载失败: {error}</span>
|
||||||
) : (
|
) : (
|
||||||
|
|||||||
@@ -18,9 +18,9 @@ for (const [, , color] of RISK_COLORS) {
|
|||||||
|
|
||||||
function getRiskColor(value: number): string {
|
function getRiskColor(value: number): string {
|
||||||
for (const [min, max, color] of RISK_COLORS) {
|
for (const [min, max, color] of RISK_COLORS) {
|
||||||
if (value >= min && value <= max) return color;
|
if (value >= min && value < max) return color;
|
||||||
}
|
}
|
||||||
return '#22c55e';
|
return '#ef4444';
|
||||||
}
|
}
|
||||||
|
|
||||||
// 100m grid step in degrees
|
// 100m grid step in degrees
|
||||||
@@ -116,6 +116,7 @@ export function LodGridLayer({
|
|||||||
canvas.style.width = '100%';
|
canvas.style.width = '100%';
|
||||||
canvas.style.height = '100%';
|
canvas.style.height = '100%';
|
||||||
canvas.style.pointerEvents = 'none';
|
canvas.style.pointerEvents = 'none';
|
||||||
|
canvas.style.display = visibleRef.current ? '' : 'none';
|
||||||
pane.appendChild(canvas);
|
pane.appendChild(canvas);
|
||||||
canvasRef.current = canvas;
|
canvasRef.current = canvas;
|
||||||
|
|
||||||
@@ -173,8 +174,8 @@ export function LodGridLayer({
|
|||||||
canvas.style.transform = '';
|
canvas.style.transform = '';
|
||||||
drawnOriginRef.current = null;
|
drawnOriginRef.current = null;
|
||||||
|
|
||||||
if (!visibleRef.current) return;
|
// Visibility is controlled via canvas CSS display (see visible effect),
|
||||||
|
// so we still draw pixels even when hidden to keep them ready on re-show.
|
||||||
const currentGrids = gridsRef.current;
|
const currentGrids = gridsRef.current;
|
||||||
if (!currentGrids || currentGrids.length === 0) return;
|
if (!currentGrids || currentGrids.length === 0) return;
|
||||||
|
|
||||||
@@ -354,13 +355,21 @@ export function LodGridLayer({
|
|||||||
};
|
};
|
||||||
}, [map]);
|
}, [map]);
|
||||||
|
|
||||||
// Trigger redraw when data changes
|
// Trigger redraw when data/geometry-affecting inputs change.
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const canvas = canvasRef.current;
|
const canvas = canvasRef.current;
|
||||||
if (canvas && (canvas as any).__lodRedraw) {
|
if (canvas && (canvas as any).__lodRedraw) {
|
||||||
(canvas as any).__lodRedraw();
|
(canvas as any).__lodRedraw();
|
||||||
}
|
}
|
||||||
}, [grids, forecastDay, riskRange, visible]);
|
}, [grids, forecastDay, riskRange]);
|
||||||
|
|
||||||
|
// Visibility toggle: hide/show via CSS instead of a full geometry redraw.
|
||||||
|
useEffect(() => {
|
||||||
|
const canvas = canvasRef.current;
|
||||||
|
if (canvas) {
|
||||||
|
canvas.style.display = visible ? '' : 'none';
|
||||||
|
}
|
||||||
|
}, [visible]);
|
||||||
|
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|||||||
123
frontend/src/components/MetricHeatmapTable.tsx
Normal file
123
frontend/src/components/MetricHeatmapTable.tsx
Normal file
@@ -0,0 +1,123 @@
|
|||||||
|
import React, { useMemo } from 'react';
|
||||||
|
|
||||||
|
interface MetricHeatmapTableProps {
|
||||||
|
rows: string[];
|
||||||
|
columns: Array<{ key: string; label: string }>;
|
||||||
|
data: Record<string, Record<string, number>>;
|
||||||
|
onSort?: (column: string) => void;
|
||||||
|
onCellClick?: (row: string, column: string) => void;
|
||||||
|
}
|
||||||
|
|
||||||
|
function colorForValue(value: number, min: number, max: number): string {
|
||||||
|
if (max - min === 0) return 'rgb(255, 255, 255)';
|
||||||
|
const ratio = (value - min) / (max - min);
|
||||||
|
// Green (low) → Yellow (mid) → Red (high)
|
||||||
|
if (ratio <= 0.5) {
|
||||||
|
const r = Math.round(ratio * 2 * 245);
|
||||||
|
const g = 220;
|
||||||
|
return `rgb(${r}, ${g}, 230)`;
|
||||||
|
}
|
||||||
|
const r = 245;
|
||||||
|
const g = Math.round(220 - (ratio - 0.5) * 2 * 190);
|
||||||
|
return `rgb(${r}, ${g}, 230)`;
|
||||||
|
}
|
||||||
|
|
||||||
|
function textColorForValue(value: number, min: number, max: number): string {
|
||||||
|
if (max - min === 0) return '#374151';
|
||||||
|
const ratio = (value - min) / (max - min);
|
||||||
|
return ratio > 0.6 ? '#7F1D1D' : '#374151';
|
||||||
|
}
|
||||||
|
|
||||||
|
export const MetricHeatmapTable = React.memo(function MetricHeatmapTable({
|
||||||
|
rows,
|
||||||
|
columns,
|
||||||
|
data,
|
||||||
|
onSort,
|
||||||
|
onCellClick,
|
||||||
|
}: MetricHeatmapTableProps) {
|
||||||
|
const columnStats = useMemo(() => {
|
||||||
|
return columns.map((col) => {
|
||||||
|
const values = rows
|
||||||
|
.map((row) => data[row]?.[col.key])
|
||||||
|
.filter((v): v is number => v !== undefined && v !== null);
|
||||||
|
const min = values.length > 0 ? Math.min(...values) : 0;
|
||||||
|
const max = values.length > 0 ? Math.max(...values) : 0;
|
||||||
|
return { key: col.key, min, max };
|
||||||
|
});
|
||||||
|
}, [columns, rows, data]);
|
||||||
|
|
||||||
|
const colStatMap = useMemo(() => {
|
||||||
|
const map = new Map<string, { min: number; max: number }>();
|
||||||
|
for (const stat of columnStats) {
|
||||||
|
map.set(stat.key, { min: stat.min, max: stat.max });
|
||||||
|
}
|
||||||
|
return map;
|
||||||
|
}, [columnStats]);
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div className="overflow-auto max-h-96">
|
||||||
|
<table className="w-full border-collapse text-xs">
|
||||||
|
<thead className="sticky top-0 z-10">
|
||||||
|
<tr>
|
||||||
|
<th className="bg-gray-100 border border-gray-200 px-2 py-1.5 text-left font-medium text-gray-600 sticky left-0 z-20">
|
||||||
|
|
||||||
|
</th>
|
||||||
|
{columns.map((col) => (
|
||||||
|
<th
|
||||||
|
key={col.key}
|
||||||
|
onClick={() => onSort?.(col.key)}
|
||||||
|
className={`bg-gray-100 border border-gray-200 px-2 py-1.5 text-center font-medium text-gray-600 ${
|
||||||
|
onSort ? 'cursor-pointer hover:bg-gray-200 select-none' : ''
|
||||||
|
}`}
|
||||||
|
>
|
||||||
|
{col.label}
|
||||||
|
</th>
|
||||||
|
))}
|
||||||
|
</tr>
|
||||||
|
</thead>
|
||||||
|
<tbody>
|
||||||
|
{rows.map((row) => (
|
||||||
|
<tr key={row}>
|
||||||
|
<td className="bg-white border border-gray-200 px-2 py-1 text-left font-medium text-gray-700 sticky left-0 z-10">
|
||||||
|
{row}
|
||||||
|
</td>
|
||||||
|
{columns.map((col) => {
|
||||||
|
const value = data[row]?.[col.key];
|
||||||
|
const stats = colStatMap.get(col.key);
|
||||||
|
const hasValue = value !== undefined && value !== null;
|
||||||
|
|
||||||
|
return (
|
||||||
|
<td
|
||||||
|
key={col.key}
|
||||||
|
onClick={() =>
|
||||||
|
onCellClick && hasValue
|
||||||
|
? onCellClick(row, col.key)
|
||||||
|
: undefined
|
||||||
|
}
|
||||||
|
className={`border border-gray-200 px-2 py-1 text-center ${
|
||||||
|
onCellClick && hasValue
|
||||||
|
? 'cursor-pointer hover:ring-1 hover:ring-blue-400'
|
||||||
|
: ''
|
||||||
|
}`}
|
||||||
|
style={
|
||||||
|
hasValue && stats && stats.max > stats.min
|
||||||
|
? {
|
||||||
|
backgroundColor: colorForValue(value, stats.min, stats.max),
|
||||||
|
color: textColorForValue(value, stats.min, stats.max),
|
||||||
|
}
|
||||||
|
: hasValue && stats && stats.max === stats.min
|
||||||
|
? { backgroundColor: 'rgb(255, 255, 255)' }
|
||||||
|
: {}
|
||||||
|
}
|
||||||
|
>
|
||||||
|
{hasValue ? value.toLocaleString() : '-'}
|
||||||
|
</td>
|
||||||
|
);
|
||||||
|
})}
|
||||||
|
</tr>
|
||||||
|
))}
|
||||||
|
</tbody>
|
||||||
|
</table>
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
});
|
||||||
@@ -46,6 +46,22 @@ function debounce<T extends (...args: any[]) => void>(fn: T, ms: number) {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Mercator helpers (avoid per-cell latLngToContainerPoint) — mirrors LodGridLayer.
|
||||||
|
function latToMercY(lat: number): number {
|
||||||
|
return 128 - (256 * Math.log(Math.tan(Math.PI / 4 + (lat * Math.PI) / 360))) / (2 * Math.PI);
|
||||||
|
}
|
||||||
|
|
||||||
|
function lonToMercX(lon: number): number {
|
||||||
|
return ((lon + 180) / 360) * 256;
|
||||||
|
}
|
||||||
|
|
||||||
|
function riskColorForValue(riskValue: number): string {
|
||||||
|
if (riskValue >= 0.7) return RISK_COLORS.high;
|
||||||
|
if (riskValue >= 0.5) return RISK_COLORS.medium_high;
|
||||||
|
if (riskValue >= 0.3) return RISK_COLORS.medium_low;
|
||||||
|
return RISK_COLORS.low;
|
||||||
|
}
|
||||||
|
|
||||||
function RiskMapComponent(props: RiskMapProps) {
|
function RiskMapComponent(props: RiskMapProps) {
|
||||||
const {
|
const {
|
||||||
grids,
|
grids,
|
||||||
@@ -60,9 +76,14 @@ function RiskMapComponent(props: RiskMapProps) {
|
|||||||
|
|
||||||
const mapDivRef = useRef<HTMLDivElement>(null);
|
const mapDivRef = useRef<HTMLDivElement>(null);
|
||||||
const mapRef = useRef<any>(null);
|
const mapRef = useRef<any>(null);
|
||||||
const gridLayerRef = useRef<any>(null);
|
const canvasRef = useRef<HTMLCanvasElement | null>(null);
|
||||||
|
const paneRef = useRef<HTMLElement | null>(null);
|
||||||
|
const animFrameRef = useRef<number>(0);
|
||||||
|
const redrawRef = useRef<() => void>(() => {});
|
||||||
const zoomRef = useRef(9);
|
const zoomRef = useRef(9);
|
||||||
const callbacksRef = useRef({ onGridSelect, onClosePanel, onFullscreen, onForecastChange });
|
const callbacksRef = useRef({ onGridSelect, onClosePanel, onFullscreen, onForecastChange });
|
||||||
|
const resizeObserverRef = useRef<ResizeObserver | null>(null);
|
||||||
|
const gridMapRef = useRef<Map<string, GridRisk>>(new Map());
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
callbacksRef.current = { onGridSelect, onClosePanel, onFullscreen, onForecastChange };
|
callbacksRef.current = { onGridSelect, onClosePanel, onFullscreen, onForecastChange };
|
||||||
@@ -70,6 +91,19 @@ function RiskMapComponent(props: RiskMapProps) {
|
|||||||
|
|
||||||
const containerHeight = isFullscreen ? 'calc(100vh - 52px)' : '420px';
|
const containerHeight = isFullscreen ? 'calc(100vh - 52px)' : '420px';
|
||||||
|
|
||||||
|
// Invalidate map size when container size changes (window resize, fullscreen, layout shifts)
|
||||||
|
useEffect(() => {
|
||||||
|
const map = mapRef.current;
|
||||||
|
if (!map) return;
|
||||||
|
|
||||||
|
// Fullscreen transition: wait for CSS transition to complete
|
||||||
|
const timer = setTimeout(() => {
|
||||||
|
map.invalidateSize({ animate: true });
|
||||||
|
}, 150);
|
||||||
|
|
||||||
|
return () => clearTimeout(timer);
|
||||||
|
}, [isFullscreen, containerHeight]);
|
||||||
|
|
||||||
const gridMap = useMemo(() => {
|
const gridMap = useMemo(() => {
|
||||||
const map = new Map<string, GridRisk>();
|
const map = new Map<string, GridRisk>();
|
||||||
grids.forEach((g) => {
|
grids.forEach((g) => {
|
||||||
@@ -79,6 +113,13 @@ function RiskMapComponent(props: RiskMapProps) {
|
|||||||
return map;
|
return map;
|
||||||
}, [grids]);
|
}, [grids]);
|
||||||
|
|
||||||
|
// Keep gridMap accessible to the canvas render fn (read via ref, no re-init).
|
||||||
|
useEffect(() => {
|
||||||
|
gridMapRef.current = gridMap;
|
||||||
|
redrawRef.current();
|
||||||
|
}, [gridMap]);
|
||||||
|
|
||||||
|
// Create the map + tile layer + canvas overlay + handlers ONCE.
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (!mapDivRef.current || mapRef.current) return;
|
if (!mapDivRef.current || mapRef.current) return;
|
||||||
|
|
||||||
@@ -95,6 +136,139 @@ function RiskMapComponent(props: RiskMapProps) {
|
|||||||
|
|
||||||
mapRef.current = map;
|
mapRef.current = map;
|
||||||
|
|
||||||
|
// Canvas overlay pane for batched grid rendering (replaces per-cell rectangles).
|
||||||
|
const pane = map.createPane('risk-grid-pane');
|
||||||
|
pane.style.zIndex = '450';
|
||||||
|
pane.style.pointerEvents = 'none';
|
||||||
|
paneRef.current = pane;
|
||||||
|
|
||||||
|
const canvas = document.createElement('canvas');
|
||||||
|
canvas.style.position = 'absolute';
|
||||||
|
canvas.style.top = '0';
|
||||||
|
canvas.style.left = '0';
|
||||||
|
canvas.style.width = '100%';
|
||||||
|
canvas.style.height = '100%';
|
||||||
|
canvas.style.pointerEvents = 'none';
|
||||||
|
pane.appendChild(canvas);
|
||||||
|
canvasRef.current = canvas;
|
||||||
|
|
||||||
|
// ResizeObserver: auto-invalidate map size when container changes
|
||||||
|
const resizeObserver = new ResizeObserver(
|
||||||
|
debounce(() => {
|
||||||
|
if (mapRef.current) {
|
||||||
|
mapRef.current.invalidateSize({ animate: false });
|
||||||
|
}
|
||||||
|
}, 100)
|
||||||
|
);
|
||||||
|
if (mapDivRef.current) {
|
||||||
|
resizeObserver.observe(mapDivRef.current);
|
||||||
|
}
|
||||||
|
resizeObserverRef.current = resizeObserver;
|
||||||
|
|
||||||
|
// Batched canvas render: group cells by color and fillRect on one canvas.
|
||||||
|
function renderGridLayer() {
|
||||||
|
if (!mapRef.current || !canvasRef.current) return;
|
||||||
|
const map = mapRef.current;
|
||||||
|
const canvas = canvasRef.current;
|
||||||
|
|
||||||
|
if (animFrameRef.current) cancelAnimationFrame(animFrameRef.current);
|
||||||
|
animFrameRef.current = requestAnimationFrame(() => {
|
||||||
|
const container = map.getContainer();
|
||||||
|
const w = container.clientWidth;
|
||||||
|
const h = container.clientHeight;
|
||||||
|
const dpr = window.devicePixelRatio || 1;
|
||||||
|
|
||||||
|
canvas.width = w * dpr;
|
||||||
|
canvas.height = h * dpr;
|
||||||
|
canvas.style.width = w + 'px';
|
||||||
|
canvas.style.height = h + 'px';
|
||||||
|
|
||||||
|
const ctx = canvas.getContext('2d');
|
||||||
|
if (!ctx) return;
|
||||||
|
ctx.setTransform(dpr, 0, 0, dpr, 0, 0);
|
||||||
|
ctx.clearRect(0, 0, w, h);
|
||||||
|
|
||||||
|
const zoom = map.getZoom();
|
||||||
|
let cellSize: number;
|
||||||
|
let step: number;
|
||||||
|
if (zoom <= 8) { cellSize = 0.1; step = 10; }
|
||||||
|
else if (zoom <= 10) { cellSize = 0.025; step = 4; }
|
||||||
|
else if (zoom <= 12) { cellSize = 0.01; step = 2; }
|
||||||
|
else { cellSize = 0.005; step = 2; }
|
||||||
|
|
||||||
|
const bounds = map.getBounds();
|
||||||
|
const minLat = Math.max(bounds.getSouth(), WUHAN_BOUNDS.minLat);
|
||||||
|
const maxLat = Math.min(bounds.getNorth(), WUHAN_BOUNDS.maxLat);
|
||||||
|
const minLon = Math.max(bounds.getWest(), WUHAN_BOUNDS.minLon);
|
||||||
|
const maxLon = Math.min(bounds.getEast(), WUHAN_BOUNDS.maxLon);
|
||||||
|
|
||||||
|
const latStart = Math.floor((minLat - WUHAN_BOUNDS.minLat) / cellSize) * cellSize + WUHAN_BOUNDS.minLat;
|
||||||
|
const lonStart = Math.floor((minLon - WUHAN_BOUNDS.minLon) / cellSize) * cellSize + WUHAN_BOUNDS.minLon;
|
||||||
|
|
||||||
|
const currentGridMap = gridMapRef.current;
|
||||||
|
if (currentGridMap.size > 5000) {
|
||||||
|
console.warn(`[RiskMap] Data too dense: ${currentGridMap.size} grid cells, rendering may be slow`);
|
||||||
|
}
|
||||||
|
|
||||||
|
const scale = 2 ** zoom;
|
||||||
|
const origin = map.getPixelOrigin();
|
||||||
|
const cellDeg = cellSize * step;
|
||||||
|
|
||||||
|
// Group cells by color to minimize fillStyle changes.
|
||||||
|
const colorGroups: Record<string, { x: number; y: number; w: number; h: number }[]> = {};
|
||||||
|
|
||||||
|
let count = 0;
|
||||||
|
const maxCount = 1500;
|
||||||
|
for (let lat = latStart; lat < maxLat && count < maxCount; lat += cellDeg) {
|
||||||
|
for (let lon = lonStart; lon < maxLon && count < maxCount; lon += cellDeg) {
|
||||||
|
const key = `${lat.toFixed(4)}-${lon.toFixed(4)}`;
|
||||||
|
const grid = currentGridMap.get(key);
|
||||||
|
const riskValue = grid?.risk_value ?? 0.5;
|
||||||
|
const color = riskColorForValue(riskValue);
|
||||||
|
|
||||||
|
const lx = lonToMercX(lon) * scale - origin.x;
|
||||||
|
const rx = lonToMercX(lon + cellDeg) * scale - origin.x;
|
||||||
|
const ty = latToMercY(lat + cellDeg) * scale - origin.y;
|
||||||
|
const by = latToMercY(lat) * scale - origin.y;
|
||||||
|
|
||||||
|
if (!colorGroups[color]) colorGroups[color] = [];
|
||||||
|
colorGroups[color].push({ x: lx, y: ty, w: rx - lx, h: by - ty });
|
||||||
|
count++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
ctx.globalAlpha = 0.6;
|
||||||
|
for (const [color, cells] of Object.entries(colorGroups)) {
|
||||||
|
ctx.fillStyle = color;
|
||||||
|
for (const c of cells) {
|
||||||
|
ctx.fillRect(c.x, c.y, c.w, c.h);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
ctx.globalAlpha = 1;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
redrawRef.current = renderGridLayer;
|
||||||
|
|
||||||
|
// Single map-level click handler: nearest-cell lookup (replaces 1500 handlers).
|
||||||
|
const handleMapClick = (e: L.LeafletMouseEvent) => {
|
||||||
|
const currentGridMap = gridMapRef.current;
|
||||||
|
if (currentGridMap.size === 0) return;
|
||||||
|
const { lat, lng } = e.latlng;
|
||||||
|
let nearestDist = Infinity;
|
||||||
|
let nearestGrid: GridRisk | null = null;
|
||||||
|
for (const grid of currentGridMap.values()) {
|
||||||
|
const d = (grid.latitude - lat) ** 2 + (grid.longitude - lng) ** 2;
|
||||||
|
if (d < nearestDist) {
|
||||||
|
nearestDist = d;
|
||||||
|
nearestGrid = grid;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (nearestGrid && nearestDist < 0.01 * 0.01) {
|
||||||
|
callbacksRef.current.onGridSelect(nearestGrid.grid_id);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
const handleZoom = debounce(() => {
|
const handleZoom = debounce(() => {
|
||||||
zoomRef.current = map.getZoom();
|
zoomRef.current = map.getZoom();
|
||||||
renderGridLayer();
|
renderGridLayer();
|
||||||
@@ -106,112 +280,27 @@ function RiskMapComponent(props: RiskMapProps) {
|
|||||||
|
|
||||||
map.on('zoomend', handleZoom);
|
map.on('zoomend', handleZoom);
|
||||||
map.on('moveend', handleMove);
|
map.on('moveend', handleMove);
|
||||||
|
map.on('resize', renderGridLayer);
|
||||||
function renderGridLayer() {
|
map.on('click', handleMapClick);
|
||||||
if (!mapRef.current) return;
|
|
||||||
const map = mapRef.current;
|
|
||||||
|
|
||||||
if (gridLayerRef.current) {
|
|
||||||
try {
|
|
||||||
map.removeLayer(gridLayerRef.current);
|
|
||||||
} catch {
|
|
||||||
// ignore
|
|
||||||
}
|
|
||||||
gridLayerRef.current = null;
|
|
||||||
}
|
|
||||||
|
|
||||||
const zoom = map.getZoom();
|
|
||||||
let cellSize: number;
|
|
||||||
let step: number;
|
|
||||||
|
|
||||||
if (zoom <= 8) {
|
|
||||||
cellSize = 0.1;
|
|
||||||
step = 10;
|
|
||||||
} else if (zoom <= 10) {
|
|
||||||
cellSize = 0.025;
|
|
||||||
step = 4;
|
|
||||||
} else if (zoom <= 12) {
|
|
||||||
cellSize = 0.01;
|
|
||||||
step = 2;
|
|
||||||
} else {
|
|
||||||
cellSize = 0.005;
|
|
||||||
step = 2;
|
|
||||||
}
|
|
||||||
|
|
||||||
const bounds = map.getBounds();
|
|
||||||
const minLat = Math.max(bounds.getSouth(), WUHAN_BOUNDS.minLat);
|
|
||||||
const maxLat = Math.min(bounds.getNorth(), WUHAN_BOUNDS.maxLat);
|
|
||||||
const minLon = Math.max(bounds.getWest(), WUHAN_BOUNDS.minLon);
|
|
||||||
const maxLon = Math.min(bounds.getEast(), WUHAN_BOUNDS.maxLon);
|
|
||||||
|
|
||||||
const latStart = Math.floor((minLat - WUHAN_BOUNDS.minLat) / cellSize) * cellSize + WUHAN_BOUNDS.minLat;
|
|
||||||
const lonStart = Math.floor((minLon - WUHAN_BOUNDS.minLon) / cellSize) * cellSize + WUHAN_BOUNDS.minLon;
|
|
||||||
|
|
||||||
const gridLayer = L.layerGroup();
|
|
||||||
const currentGridMap = gridMap;
|
|
||||||
|
|
||||||
let count = 0;
|
|
||||||
const maxCount = 1500;
|
|
||||||
|
|
||||||
if (currentGridMap.size > 5000) {
|
|
||||||
console.warn(`[RiskMap] Data too dense: ${currentGridMap.size} grid cells, rendering may be slow`);
|
|
||||||
}
|
|
||||||
|
|
||||||
for (let lat = latStart; lat < maxLat && count < maxCount; lat += cellSize * step) {
|
|
||||||
for (let lon = lonStart; lon < maxLon && count < maxCount; lon += cellSize * step) {
|
|
||||||
const key = `${lat.toFixed(4)}-${lon.toFixed(4)}`;
|
|
||||||
const grid = currentGridMap.get(key);
|
|
||||||
|
|
||||||
const riskValue = grid?.risk_value ?? 0.5;
|
|
||||||
let riskLevel = 'medium';
|
|
||||||
if (riskValue >= 0.7) riskLevel = 'high';
|
|
||||||
else if (riskValue >= 0.5) riskLevel = 'medium_high';
|
|
||||||
else if (riskValue >= 0.3) riskLevel = 'medium_low';
|
|
||||||
else riskLevel = 'low';
|
|
||||||
|
|
||||||
const color = RISK_COLORS[riskLevel];
|
|
||||||
|
|
||||||
const rect = L.rectangle(
|
|
||||||
[[lat, lon], [lat + cellSize * step, lon + cellSize * step]],
|
|
||||||
{
|
|
||||||
fillColor: color,
|
|
||||||
fillOpacity: 0.6,
|
|
||||||
color: 'transparent',
|
|
||||||
weight: 0,
|
|
||||||
}
|
|
||||||
);
|
|
||||||
|
|
||||||
if (grid) {
|
|
||||||
const gridId = grid.grid_id;
|
|
||||||
rect.bindTooltip(
|
|
||||||
`<b>${gridId}</b><br/>风险:${Math.round(riskValue * 100)}%`,
|
|
||||||
{ direction: 'center', permanent: false }
|
|
||||||
);
|
|
||||||
rect.on('click', () => {
|
|
||||||
callbacksRef.current.onGridSelect(gridId);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
rect.addTo(gridLayer);
|
|
||||||
count++;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
gridLayer.addTo(map);
|
|
||||||
gridLayerRef.current = gridLayer;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Initial render
|
// Initial render
|
||||||
renderGridLayer();
|
renderGridLayer();
|
||||||
|
|
||||||
return () => {
|
return () => {
|
||||||
|
if (resizeObserverRef.current) {
|
||||||
|
resizeObserverRef.current.disconnect();
|
||||||
|
resizeObserverRef.current = null;
|
||||||
|
}
|
||||||
|
if (animFrameRef.current) cancelAnimationFrame(animFrameRef.current);
|
||||||
|
redrawRef.current = () => {};
|
||||||
if (mapRef.current) {
|
if (mapRef.current) {
|
||||||
mapRef.current.remove();
|
mapRef.current.remove();
|
||||||
mapRef.current = null;
|
mapRef.current = null;
|
||||||
gridLayerRef.current = null;
|
|
||||||
}
|
}
|
||||||
|
canvasRef.current = null;
|
||||||
|
paneRef.current = null;
|
||||||
};
|
};
|
||||||
}, [gridMap]);
|
}, []);
|
||||||
|
|
||||||
const handleForecastChange = useCallback((d: ForecastDay) => {
|
const handleForecastChange = useCallback((d: ForecastDay) => {
|
||||||
callbacksRef.current.onForecastChange(d);
|
callbacksRef.current.onForecastChange(d);
|
||||||
|
|||||||
13
frontend/src/components/RoleRedirect.tsx
Normal file
13
frontend/src/components/RoleRedirect.tsx
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
import { Navigate } from 'react-router-dom';
|
||||||
|
import { useSessionStore } from '@/stores';
|
||||||
|
import { roleDefaultPath } from '@/utils/roleViews';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 根据当前视角(role)把裸路径 `/` 重定向到该视角的默认落地页。
|
||||||
|
* D2:纯前端视图预设 —— role 只决定默认落地页,不是访问控制。
|
||||||
|
* 角色来源被隔离在 sessionStore 的 getRoleSource() 接缝里。
|
||||||
|
*/
|
||||||
|
export function RoleRedirect(): JSX.Element {
|
||||||
|
const role = useSessionStore((s) => s.role);
|
||||||
|
return <Navigate to={roleDefaultPath(role)} replace />;
|
||||||
|
}
|
||||||
45
frontend/src/components/RouteErrorBoundary.tsx
Normal file
45
frontend/src/components/RouteErrorBoundary.tsx
Normal file
@@ -0,0 +1,45 @@
|
|||||||
|
import { Component, ReactNode } from 'react';
|
||||||
|
|
||||||
|
interface Props {
|
||||||
|
children: ReactNode;
|
||||||
|
}
|
||||||
|
|
||||||
|
interface State {
|
||||||
|
hasError: boolean;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 路由级错误边界:单个页面(含懒加载 chunk)崩溃时只降级内容区,
|
||||||
|
// 保留外层骨架(顶栏 + 侧栏),避免整页白屏。
|
||||||
|
export class RouteErrorBoundary extends Component<Props, State> {
|
||||||
|
constructor(props: Props) {
|
||||||
|
super(props);
|
||||||
|
this.state = { hasError: false };
|
||||||
|
}
|
||||||
|
|
||||||
|
static getDerivedStateFromError() {
|
||||||
|
return { hasError: true };
|
||||||
|
}
|
||||||
|
|
||||||
|
private reset = () => {
|
||||||
|
this.setState({ hasError: false });
|
||||||
|
};
|
||||||
|
|
||||||
|
render() {
|
||||||
|
if (this.state.hasError) {
|
||||||
|
return (
|
||||||
|
<div className="flex h-full items-center justify-center">
|
||||||
|
<div className="text-center">
|
||||||
|
<div className="text-danger text-base mb-2">此页面加载失败</div>
|
||||||
|
<button
|
||||||
|
onClick={this.reset}
|
||||||
|
className="mt-2 px-4 py-2 bg-primary text-white rounded text-sm"
|
||||||
|
>
|
||||||
|
重试
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
return this.props.children;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,73 +1,97 @@
|
|||||||
import { useState } from 'react';
|
import { useState } from 'react';
|
||||||
|
import { NavLink, useLocation } from 'react-router-dom';
|
||||||
|
import { TESTIDS } from '@/utils/testids';
|
||||||
|
|
||||||
interface SideNavProps {
|
interface SideNavProps {
|
||||||
activePage: string;
|
|
||||||
onPageChange: (page: string) => void;
|
|
||||||
alertCount?: number;
|
alertCount?: number;
|
||||||
|
// 抽屉模式下点击导航项后关闭抽屉(持久侧栏可不传)。
|
||||||
|
onNavigate?: () => void;
|
||||||
|
// 受控的展开手风琴分组:由 AppShell 提供时,导轨与抽屉两份实例保持同步。
|
||||||
|
// 不传则回退到内部 state,向后兼容独立使用。
|
||||||
|
expanded?: string | null;
|
||||||
|
onExpandedChange?: (moduleId: string | null) => void;
|
||||||
}
|
}
|
||||||
|
|
||||||
const modules: { id: string; label: string; icon: React.ReactNode; items: { id: string; label: string }[] }[] = [
|
interface NavItem {
|
||||||
|
to: string;
|
||||||
|
label: string;
|
||||||
|
testid: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
const modules: { id: string; label: string; icon: React.ReactNode; items: NavItem[] }[] = [
|
||||||
{
|
{
|
||||||
id: 'monitoring',
|
id: 'monitoring',
|
||||||
label: '监测',
|
label: '监测',
|
||||||
icon: (
|
icon: (
|
||||||
<svg className="w-4 h-4" fill="currentColor" viewBox="0 0 24 24">
|
<svg className="w-4 h-4" fill="currentColor" viewBox="0 0 24 24">
|
||||||
<path d="M3 13h8V3H3v10zm0 8h8v-6H3v6zm10 0h8V11h-8v10zm0-18v6h8V3h-8z"/>
|
<path d="M3 13h8V3H3v10zm0 8h8v-6H3v6zm10 0h8V11h-8v10zm0-18v6h8V3h-8z" />
|
||||||
</svg>
|
</svg>
|
||||||
),
|
),
|
||||||
items: [
|
items: [{ to: '/monitoring', label: '监测面板', testid: TESTIDS.navMonitoring }],
|
||||||
{ id: 'monitoring', label: '监测面板' },
|
|
||||||
],
|
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
id: 'alert',
|
id: 'alert',
|
||||||
label: '预警',
|
label: '预警',
|
||||||
icon: (
|
icon: (
|
||||||
<svg className="w-4 h-4" fill="currentColor" viewBox="0 0 24 24">
|
<svg className="w-4 h-4" fill="currentColor" viewBox="0 0 24 24">
|
||||||
<path d="M12 22c1.1 0 2-.9 2-2h-4c0 1.1.89 2 2 2zm6-6v-5c0-3.07-1.64-5.64-4.5-6.32V4c0-.83-.67-1.5-1.5-1.5s-1.5.67-1.5 1.5v.68C7.63 5.36 6 7.92 6 11v5l-2 2v1h16v-1l-2-2z"/>
|
<path d="M12 22c1.1 0 2-.9 2-2h-4c0 1.1.89 2 2 2zm6-6v-5c0-3.07-1.64-5.64-4.5-6.32V4c0-.83-.67-1.5-1.5-1.5s-1.5.67-1.5 1.5v.68C7.63 5.36 6 7.92 6 11v5l-2 2v1h16v-1l-2-2z" />
|
||||||
</svg>
|
</svg>
|
||||||
),
|
),
|
||||||
items: [
|
items: [{ to: '/alerts', label: '预警地图', testid: TESTIDS.navAlerts }],
|
||||||
{ id: 'alerts', label: '预警地图' },
|
|
||||||
],
|
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
id: 'analysis',
|
id: 'analysis',
|
||||||
label: '分析',
|
label: '分析',
|
||||||
icon: (
|
icon: (
|
||||||
<svg className="w-4 h-4" fill="currentColor" viewBox="0 0 24 24">
|
<svg className="w-4 h-4" fill="currentColor" viewBox="0 0 24 24">
|
||||||
<path d="M19 3H5c-1.1 0-2 .9-2 2v14c0 1.1.9 2 2 2h14c1.1 0 2-.9 2-2V5c0-1.1-.9-2-2-2zM9 17H7v-7h2v7zm4 0h-2V7h2v10zm4 0h-2v-4h2v4z"/>
|
<path d="M19 3H5c-1.1 0-2 .9-2 2v14c0 1.1.9 2 2 2h14c1.1 0 2-.9 2-2V5c0-1.1-.9-2-2-2zM9 17H7v-7h2v7zm4 0h-2V7h2v10zm4 0h-2v-4h2v4z" />
|
||||||
</svg>
|
</svg>
|
||||||
),
|
),
|
||||||
items: [
|
items: [
|
||||||
{ id: 'trend-analysis', label: '趋势分析' },
|
{ to: '/overview', label: '总览', testid: TESTIDS.navOverview },
|
||||||
{ id: 'district-comparison', label: '区域对比' },
|
{ to: '/analysis/trend', label: '趋势分析', testid: TESTIDS.navTrend },
|
||||||
{ id: 'insights', label: '智能洞察' },
|
{ to: '/analysis/district', label: '区域对比', testid: TESTIDS.navDistrict },
|
||||||
{ id: 'reports', label: '报表中心' },
|
{ to: '/analysis/insights', label: '智能洞察', testid: TESTIDS.navInsights },
|
||||||
|
{ to: '/analysis/reports', label: '报表中心', testid: TESTIDS.navReports },
|
||||||
|
{ to: '/analysis/demographics', label: '人群分析', testid: TESTIDS.navDemographics },
|
||||||
|
{ to: '/analysis/disease', label: '疾病分析', testid: TESTIDS.navDisease },
|
||||||
|
{ to: '/analysis/clinical', label: '临床分析', testid: TESTIDS.navClinical },
|
||||||
|
{ to: '/analysis/environment', label: '环境健康', testid: TESTIDS.navEnvironment },
|
||||||
],
|
],
|
||||||
},
|
},
|
||||||
];
|
];
|
||||||
|
|
||||||
export function SideNav({
|
export function SideNav({
|
||||||
activePage,
|
|
||||||
onPageChange,
|
|
||||||
alertCount = 0,
|
alertCount = 0,
|
||||||
|
onNavigate,
|
||||||
|
expanded: expandedProp,
|
||||||
|
onExpandedChange,
|
||||||
}: SideNavProps) {
|
}: SideNavProps) {
|
||||||
const [expanded, setExpanded] = useState<string | null>('monitoring');
|
const location = useLocation();
|
||||||
|
|
||||||
const handleItemClick = (moduleId: string, itemId: string) => {
|
// 当前路径命中的模块默认展开。
|
||||||
setExpanded(moduleId);
|
const moduleForPath = (pathname: string) =>
|
||||||
onPageChange(itemId);
|
modules.find((m) => m.items.some((item) => pathname.startsWith(item.to)))?.id ?? 'monitoring';
|
||||||
|
|
||||||
|
// 受控/非受控双模式:父级传入 expanded 时由父级管理,否则回退内部 state。
|
||||||
|
const [internalExpanded, setInternalExpanded] = useState<string | null>(() =>
|
||||||
|
moduleForPath(location.pathname)
|
||||||
|
);
|
||||||
|
const isControlled = expandedProp !== undefined;
|
||||||
|
const expanded = isControlled ? expandedProp : internalExpanded;
|
||||||
|
const setExpanded = (next: string | null) => {
|
||||||
|
if (isControlled) onExpandedChange?.(next);
|
||||||
|
else setInternalExpanded(next);
|
||||||
};
|
};
|
||||||
|
|
||||||
const isActiveModule = (moduleId: string) => {
|
const isActiveModule = (moduleId: string) => {
|
||||||
const module = modules.find(m => m.id === moduleId);
|
const module = modules.find((m) => m.id === moduleId);
|
||||||
if (!module) return false;
|
if (!module) return false;
|
||||||
return module.items.some(item => item.id === activePage);
|
return module.items.some((item) => location.pathname.startsWith(item.to));
|
||||||
};
|
};
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<aside className="w-[200px] bg-bg-card border-r border-border fixed top-[52px] left-0 bottom-0 overflow-y-auto py-4 px-2">
|
<nav className="h-full overflow-y-auto py-4 px-2">
|
||||||
{modules.map((module) => (
|
{modules.map((module) => (
|
||||||
<div key={module.id} className="mb-4">
|
<div key={module.id} className="mb-4">
|
||||||
<button
|
<button
|
||||||
@@ -78,9 +102,7 @@ export function SideNav({
|
|||||||
: 'text-text-primary hover:bg-bg-hover'
|
: 'text-text-primary hover:bg-bg-hover'
|
||||||
}`}
|
}`}
|
||||||
>
|
>
|
||||||
<span className="w-4 h-4 flex items-center justify-center">
|
<span className="w-4 h-4 flex items-center justify-center">{module.icon}</span>
|
||||||
{module.icon}
|
|
||||||
</span>
|
|
||||||
<span>{module.label}</span>
|
<span>{module.label}</span>
|
||||||
{module.id === 'alert' && alertCount > 0 && (
|
{module.id === 'alert' && alertCount > 0 && (
|
||||||
<span className="ml-auto bg-danger-light text-danger text-[10px] font-semibold px-[5px] py-[2px] rounded">
|
<span className="ml-auto bg-danger-light text-danger text-[10px] font-semibold px-[5px] py-[2px] rounded">
|
||||||
@@ -92,22 +114,26 @@ export function SideNav({
|
|||||||
{expanded === module.id && (
|
{expanded === module.id && (
|
||||||
<div className="mt-1 pl-7">
|
<div className="mt-1 pl-7">
|
||||||
{module.items.map((item) => (
|
{module.items.map((item) => (
|
||||||
<button
|
<NavLink
|
||||||
key={item.id}
|
key={item.to}
|
||||||
onClick={() => handleItemClick(module.id, item.id)}
|
to={item.to}
|
||||||
className={`w-full text-left px-3 py-[7px] rounded text-[13px] font-medium transition-colors ${
|
data-testid={item.testid}
|
||||||
activePage === item.id
|
onClick={onNavigate}
|
||||||
? 'bg-bg-active text-primary'
|
className={({ isActive }) =>
|
||||||
: 'text-text-secondary hover:bg-bg-hover hover:text-text-primary'
|
`block w-full text-left px-3 py-[7px] rounded text-[13px] font-medium transition-colors ${
|
||||||
}`}
|
isActive
|
||||||
|
? 'bg-bg-active text-primary'
|
||||||
|
: 'text-text-secondary hover:bg-bg-hover hover:text-text-primary'
|
||||||
|
}`
|
||||||
|
}
|
||||||
>
|
>
|
||||||
{item.label}
|
{item.label}
|
||||||
</button>
|
</NavLink>
|
||||||
))}
|
))}
|
||||||
</div>
|
</div>
|
||||||
)}
|
)}
|
||||||
</div>
|
</div>
|
||||||
))}
|
))}
|
||||||
</aside>
|
</nav>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,44 +1,107 @@
|
|||||||
|
import React from 'react';
|
||||||
|
|
||||||
interface StatCardProps {
|
interface StatCardProps {
|
||||||
|
icon?: React.ReactNode;
|
||||||
label: string;
|
label: string;
|
||||||
value: string | number;
|
value: string | number;
|
||||||
change?: string;
|
trend?: {
|
||||||
changeType?: 'up' | 'down' | 'neutral';
|
direction: 'up' | 'down' | 'stable';
|
||||||
progress?: number;
|
value: string;
|
||||||
progressColor?: string;
|
};
|
||||||
|
sparkline?: {
|
||||||
|
data: number[];
|
||||||
|
color: string;
|
||||||
|
};
|
||||||
|
color?: string;
|
||||||
|
onClick?: () => void;
|
||||||
}
|
}
|
||||||
|
|
||||||
export function StatCard({
|
export const StatCard = React.memo(function StatCard({
|
||||||
|
icon,
|
||||||
label,
|
label,
|
||||||
value,
|
value,
|
||||||
change,
|
trend,
|
||||||
changeType = 'neutral',
|
sparkline,
|
||||||
progress,
|
color,
|
||||||
progressColor = 'bg-warning',
|
onClick,
|
||||||
}: StatCardProps) {
|
}: StatCardProps) {
|
||||||
|
const trendIndicator = trend ? (
|
||||||
|
<span
|
||||||
|
className={`inline-flex items-center gap-0.5 text-xs font-medium ${
|
||||||
|
trend.direction === 'up'
|
||||||
|
? 'text-green-600'
|
||||||
|
: trend.direction === 'down'
|
||||||
|
? 'text-red-600'
|
||||||
|
: 'text-gray-500'
|
||||||
|
}`}
|
||||||
|
>
|
||||||
|
{trend.direction === 'up' && <span aria-hidden>▲</span>}
|
||||||
|
{trend.direction === 'down' && <span aria-hidden>▼</span>}
|
||||||
|
{trend.direction === 'stable' && <span aria-hidden>▬</span>}
|
||||||
|
{trend.value}
|
||||||
|
</span>
|
||||||
|
) : null;
|
||||||
|
|
||||||
|
const sparklineSvg = sparkline && sparkline.data.length >= 2 ? (
|
||||||
|
<svg
|
||||||
|
width="60"
|
||||||
|
height="24"
|
||||||
|
className="shrink-0"
|
||||||
|
aria-hidden="true"
|
||||||
|
>
|
||||||
|
<polyline
|
||||||
|
fill="none"
|
||||||
|
stroke={sparkline.color}
|
||||||
|
strokeWidth="1.5"
|
||||||
|
strokeLinecap="round"
|
||||||
|
strokeLinejoin="round"
|
||||||
|
points={sparkline.data
|
||||||
|
.map((val, i) => {
|
||||||
|
const x = (i / (sparkline.data.length - 1)) * 58 + 1;
|
||||||
|
const max = Math.max(...sparkline.data);
|
||||||
|
const min = Math.min(...sparkline.data);
|
||||||
|
const range = max - min || 1;
|
||||||
|
const y = 22 - ((val - min) / range) * 20 - 1;
|
||||||
|
return `${x},${y}`;
|
||||||
|
})
|
||||||
|
.join(' ')}
|
||||||
|
/>
|
||||||
|
</svg>
|
||||||
|
) : null;
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className="card p-4">
|
<div
|
||||||
<div className="text-[11px] font-medium text-text-muted uppercase tracking-wide mb-1.5">
|
onClick={onClick}
|
||||||
{label}
|
role={onClick ? 'button' : undefined}
|
||||||
|
tabIndex={onClick ? 0 : undefined}
|
||||||
|
onKeyDown={
|
||||||
|
onClick
|
||||||
|
? (e: React.KeyboardEvent) => {
|
||||||
|
if (e.key === 'Enter' || e.key === ' ') {
|
||||||
|
e.preventDefault();
|
||||||
|
onClick();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
: undefined
|
||||||
|
}
|
||||||
|
className={`bg-white rounded-lg border border-gray-200 p-4 ${
|
||||||
|
onClick ? 'cursor-pointer hover:shadow-md transition-shadow' : ''
|
||||||
|
}`}
|
||||||
|
>
|
||||||
|
<div className="flex items-center gap-2 text-sm text-gray-500 mb-1">
|
||||||
|
{icon}
|
||||||
|
<span>{label}</span>
|
||||||
</div>
|
</div>
|
||||||
<div className="font-display text-[26px] font-bold text-text-primary mb-1">
|
<div className="flex items-end justify-between gap-2">
|
||||||
{value}
|
<div
|
||||||
|
className="text-2xl font-bold text-gray-900"
|
||||||
|
style={color ? { color } : undefined}
|
||||||
|
>
|
||||||
|
{value}
|
||||||
|
</div>
|
||||||
|
{sparklineSvg}
|
||||||
</div>
|
</div>
|
||||||
{change && (
|
{trendIndicator && <div className="mt-1">{trendIndicator}</div>}
|
||||||
<div className={`text-[11px] ${
|
|
||||||
changeType === 'up' ? 'text-danger' :
|
|
||||||
changeType === 'down' ? 'text-success' : 'text-text-muted'
|
|
||||||
}`}>
|
|
||||||
{change}
|
|
||||||
</div>
|
|
||||||
)}
|
|
||||||
{progress !== undefined && (
|
|
||||||
<div className="h-[3px] bg-bg-page rounded mt-2.5 overflow-hidden">
|
|
||||||
<div
|
|
||||||
className={`h-full rounded ${progressColor}`}
|
|
||||||
style={{ width: `${progress}%` }}
|
|
||||||
/>
|
|
||||||
</div>
|
|
||||||
)}
|
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
}
|
});
|
||||||
|
|||||||
@@ -38,8 +38,9 @@ export function StatisticalCharts({
|
|||||||
const secondHalf = values.slice(Math.floor(values.length / 2));
|
const secondHalf = values.slice(Math.floor(values.length / 2));
|
||||||
|
|
||||||
const firstAvg = firstHalf.reduce((a, b) => a + b, 0) / firstHalf.length;
|
const firstAvg = firstHalf.reduce((a, b) => a + b, 0) / firstHalf.length;
|
||||||
const secondAvg = secondHalf.reduce((a, b) => a + b) / secondHalf.length;
|
const secondAvg = secondHalf.reduce((a, b) => a + b, 0) / secondHalf.length;
|
||||||
|
|
||||||
|
if (firstAvg === 0) return secondAvg > 0 ? 'up' : 'stable';
|
||||||
const change = ((secondAvg - firstAvg) / firstAvg) * 100;
|
const change = ((secondAvg - firstAvg) / firstAvg) * 100;
|
||||||
|
|
||||||
if (change > 10) return 'up';
|
if (change > 10) return 'up';
|
||||||
@@ -48,7 +49,11 @@ export function StatisticalCharts({
|
|||||||
};
|
};
|
||||||
|
|
||||||
const stats = useMemo(() => {
|
const stats = useMemo(() => {
|
||||||
if (data.length === 0) return null;
|
const noData = data.length === 0;
|
||||||
|
|
||||||
|
if (noData) {
|
||||||
|
return { totalCases: 0, avgCases: 0, maxCases: 0, trend: 'stable' as const, noData: true };
|
||||||
|
}
|
||||||
|
|
||||||
const totalCases = data.reduce((sum, item) => sum + item.cases, 0);
|
const totalCases = data.reduce((sum, item) => sum + item.cases, 0);
|
||||||
const avgCases = totalCases / data.length;
|
const avgCases = totalCases / data.length;
|
||||||
@@ -60,6 +65,7 @@ export function StatisticalCharts({
|
|||||||
avgCases: Math.round(avgCases),
|
avgCases: Math.round(avgCases),
|
||||||
maxCases,
|
maxCases,
|
||||||
trend,
|
trend,
|
||||||
|
noData: false,
|
||||||
};
|
};
|
||||||
}, [data]);
|
}, [data]);
|
||||||
|
|
||||||
@@ -146,7 +152,7 @@ export function StatisticalCharts({
|
|||||||
</div>
|
</div>
|
||||||
|
|
||||||
{/* Stats cards */}
|
{/* Stats cards */}
|
||||||
{stats && activeChart === 'cases' && (
|
{activeChart === 'cases' && (
|
||||||
<div className="grid grid-cols-3 gap-4 mb-4">
|
<div className="grid grid-cols-3 gap-4 mb-4">
|
||||||
<div className="bg-blue-50 rounded-lg p-3">
|
<div className="bg-blue-50 rounded-lg p-3">
|
||||||
<div className="text-sm text-gray-600">总病例数</div>
|
<div className="text-sm text-gray-600">总病例数</div>
|
||||||
@@ -163,6 +169,11 @@ export function StatisticalCharts({
|
|||||||
</div>
|
</div>
|
||||||
)}
|
)}
|
||||||
|
|
||||||
|
{/* No data banner */}
|
||||||
|
{stats.noData && (
|
||||||
|
<div className="text-center text-sm text-gray-400 py-4">该时段暂无统计数据</div>
|
||||||
|
)}
|
||||||
|
|
||||||
{/* Chart */}
|
{/* Chart */}
|
||||||
<div style={{ height }}>
|
<div style={{ height }}>
|
||||||
<ResponsiveContainer width="100%" height="100%">
|
<ResponsiveContainer width="100%" height="100%">
|
||||||
|
|||||||
@@ -26,6 +26,10 @@ export function TimelinePlayer({
|
|||||||
}: TimelinePlayerProps) {
|
}: TimelinePlayerProps) {
|
||||||
const [playing, setPlaying] = useState(isPlaying);
|
const [playing, setPlaying] = useState(isPlaying);
|
||||||
const timerRef = useRef<ReturnType<typeof setInterval> | null>(null);
|
const timerRef = useRef<ReturnType<typeof setInterval> | null>(null);
|
||||||
|
const advanceRef = useRef<() => void>(() => {});
|
||||||
|
|
||||||
|
// Keep internal play state in sync when the parent/store changes isPlaying.
|
||||||
|
useEffect(() => { setPlaying(isPlaying); }, [isPlaying]);
|
||||||
|
|
||||||
const generateDateRange = useCallback((start: string, end: string) => {
|
const generateDateRange = useCallback((start: string, end: string) => {
|
||||||
const dates: string[] = [];
|
const dates: string[] = [];
|
||||||
@@ -41,7 +45,13 @@ export function TimelinePlayer({
|
|||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
const dateRange = useMemo(() => generateDateRange(startDate, endDate), [startDate, endDate, generateDateRange]);
|
const dateRange = useMemo(() => generateDateRange(startDate, endDate), [startDate, endDate, generateDateRange]);
|
||||||
const currentIndex = useMemo(() => dateRange.indexOf(currentDate), [dateRange, currentDate]);
|
// Compute index arithmetically from the day difference instead of indexOf.
|
||||||
|
const currentIndex = useMemo(() => {
|
||||||
|
if (dateRange.length === 0) return -1;
|
||||||
|
const ms = new Date(currentDate).getTime() - new Date(startDate).getTime();
|
||||||
|
const idx = Math.round(ms / 86400000);
|
||||||
|
return idx >= 0 && idx < dateRange.length ? idx : dateRange.indexOf(currentDate);
|
||||||
|
}, [startDate, currentDate, dateRange]);
|
||||||
const progress = useMemo(() => ((currentIndex + 1) / dateRange.length) * 100, [currentIndex, dateRange.length]);
|
const progress = useMemo(() => ((currentIndex + 1) / dateRange.length) * 100, [currentIndex, dateRange.length]);
|
||||||
|
|
||||||
const play = useCallback(() => {
|
const play = useCallback(() => {
|
||||||
@@ -71,12 +81,19 @@ export function TimelinePlayer({
|
|||||||
onDateChange(dateRange[0]);
|
onDateChange(dateRange[0]);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// Keep the advance logic in a ref so the interval doesn't get recreated each
|
||||||
|
// tick when goToNext's identity changes.
|
||||||
|
useEffect(() => {
|
||||||
|
advanceRef.current = goToNext;
|
||||||
|
}, [goToNext]);
|
||||||
|
|
||||||
|
// Interval is created once per play/speed change (not per tick).
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (playing) {
|
if (playing) {
|
||||||
const interval = 1000 / speed;
|
const interval = 1000 / speed;
|
||||||
|
|
||||||
timerRef.current = setInterval(() => {
|
timerRef.current = setInterval(() => {
|
||||||
goToNext();
|
advanceRef.current();
|
||||||
}, interval);
|
}, interval);
|
||||||
|
|
||||||
return () => {
|
return () => {
|
||||||
@@ -85,7 +102,7 @@ export function TimelinePlayer({
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
}, [playing, speed, goToNext]);
|
}, [playing, speed]);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (currentIndex >= dateRange.length - 1) {
|
if (currentIndex >= dateRange.length - 1) {
|
||||||
|
|||||||
@@ -1,7 +1,15 @@
|
|||||||
import { useState, useEffect } from 'react';
|
import { useState, useEffect } from 'react';
|
||||||
|
import { useNavigate } from 'react-router-dom';
|
||||||
|
import { TESTIDS } from '@/utils/testids';
|
||||||
|
import { useSessionStore, ROLES, type Role } from '@/stores/sessionStore';
|
||||||
|
import { ROLE_LABELS, roleDefaultPath } from '@/utils/roleViews';
|
||||||
|
|
||||||
interface TopNavProps {
|
interface TopNavProps {
|
||||||
onLogout?: () => void;
|
onLogout?: () => void;
|
||||||
|
// 移动端汉堡按钮:切换侧栏抽屉。
|
||||||
|
onToggleMenu?: () => void;
|
||||||
|
// 抽屉是否展开(用于汉堡按钮的 aria-expanded)。
|
||||||
|
isMenuOpen?: boolean;
|
||||||
}
|
}
|
||||||
|
|
||||||
function Clock() {
|
function Clock() {
|
||||||
@@ -13,14 +21,61 @@ function Clock() {
|
|||||||
return <span>{time.toLocaleTimeString('zh-CN', { hour: '2-digit', minute: '2-digit' })}</span>;
|
return <span>{time.toLocaleTimeString('zh-CN', { hour: '2-digit', minute: '2-digit' })}</span>;
|
||||||
}
|
}
|
||||||
|
|
||||||
export function TopNav({ onLogout }: TopNavProps) {
|
// 视角切换器:纯前端视图预设(D2)。刻意标注「视角」而非「权限」——不是访问控制。
|
||||||
|
// 切换时持久化角色并跳转到该视角的默认落地页。
|
||||||
|
function PerspectiveSwitcher() {
|
||||||
|
const role = useSessionStore((s) => s.role);
|
||||||
|
const setRole = useSessionStore((s) => s.setRole);
|
||||||
|
const navigate = useNavigate();
|
||||||
|
|
||||||
|
const handleChange = (next: Role) => {
|
||||||
|
setRole(next);
|
||||||
|
navigate(roleDefaultPath(next));
|
||||||
|
};
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<nav className="h-[52px] bg-bg-card border-b border-border flex items-center px-5 fixed top-0 left-0 right-0 z-50">
|
<label className="flex items-center gap-1.5 text-[13px] text-text-secondary">
|
||||||
|
<span className="text-text-muted hidden sm:inline">视角</span>
|
||||||
|
<select
|
||||||
|
data-testid={TESTIDS.perspectiveSwitcher}
|
||||||
|
value={role}
|
||||||
|
onChange={(e) => handleChange(e.target.value as Role)}
|
||||||
|
aria-label="切换视角"
|
||||||
|
className="bg-bg-card border border-border rounded-md px-2 py-1 text-[13px] text-text-primary hover:bg-bg-hover focus:outline-none focus:ring-1 focus:ring-primary cursor-pointer"
|
||||||
|
>
|
||||||
|
{ROLES.map((r) => (
|
||||||
|
<option key={r} value={r} data-testid={`${TESTIDS.perspectiveOption}-${r}`}>
|
||||||
|
{ROLE_LABELS[r]}
|
||||||
|
</option>
|
||||||
|
))}
|
||||||
|
</select>
|
||||||
|
</label>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
export function TopNav({ onLogout, onToggleMenu, isMenuOpen = false }: TopNavProps) {
|
||||||
|
return (
|
||||||
|
<nav className="h-[52px] bg-bg-card border-b border-border flex items-center px-5 z-50">
|
||||||
|
{onToggleMenu && (
|
||||||
|
<button
|
||||||
|
type="button"
|
||||||
|
onClick={onToggleMenu}
|
||||||
|
aria-label="打开菜单"
|
||||||
|
aria-expanded={isMenuOpen}
|
||||||
|
aria-controls="app-drawer"
|
||||||
|
data-testid={TESTIDS.hamburger}
|
||||||
|
className="lg:hidden mr-3 -ml-1 w-9 h-9 flex items-center justify-center rounded-md text-text-secondary hover:bg-bg-hover transition-colors"
|
||||||
|
>
|
||||||
|
<svg className="w-5 h-5" fill="none" stroke="currentColor" strokeWidth="2" viewBox="0 0 24 24">
|
||||||
|
<path strokeLinecap="round" strokeLinejoin="round" d="M4 6h16M4 12h16M4 18h16" />
|
||||||
|
</svg>
|
||||||
|
</button>
|
||||||
|
)}
|
||||||
|
|
||||||
<div className="flex items-center gap-3">
|
<div className="flex items-center gap-3">
|
||||||
<div className="w-7 h-7 bg-primary rounded-md flex items-center justify-center">
|
<div className="w-7 h-7 bg-primary rounded-md flex items-center justify-center">
|
||||||
<svg className="w-4 h-4 fill-white" viewBox="0 0 24 24">
|
<svg className="w-4 h-4 fill-white" viewBox="0 0 24 24">
|
||||||
<path d="M19 3H5c-1.1 0-2 .9-2 2v14c0 1.1.9 2 2 2h14c1.1 0 2-.9 2-2V5c0-1.1-.9-2-2-2zm-7 3c1.93 0 3.5 1.57 3.5 3.5S13.93 13 12 13s-3.5-1.57-3.5-3.5S10.07 6 12 6zm7 13H5v-.23c0-.62.28-1.2.76-1.58C7.47 15.82 9.64 15 12 15s4.53.82 6.24 2.19c.48.38.76.97.76 1.58V19z"/>
|
<path d="M19 3H5c-1.1 0-2 .9-2 2v14c0 1.1.9 2 2 2h14c1.1 0 2-.9 2-2V5c0-1.1-.9-2-2-2zm-7 3c1.93 0 3.5 1.57 3.5 3.5S13.93 13 12 13s-3.5-1.57-3.5-3.5S10.07 6 12 6zm7 13H5v-.23c0-.62.28-1.2.76-1.58C7.47 15.82 9.64 15 12 15s4.53.82 6.24 2.19c.48.38.76.97.76 1.58V19z" />
|
||||||
</svg>
|
</svg>
|
||||||
</div>
|
</div>
|
||||||
<span className="font-display font-semibold text-[15px] text-text-primary">
|
<span className="font-display font-semibold text-[15px] text-text-primary">
|
||||||
@@ -28,21 +83,21 @@ export function TopNav({ onLogout }: TopNavProps) {
|
|||||||
</span>
|
</span>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div className="w-px h-5 bg-border ml-4 mr-4" />
|
<div className="w-px h-5 bg-border ml-4 mr-4 hidden sm:block" />
|
||||||
|
|
||||||
<span className="text-[13px] text-text-secondary">
|
<span className="text-[13px] text-text-secondary hidden sm:inline">
|
||||||
儿童呼吸道疾病风险监测预警平台
|
儿童呼吸道疾病风险监测预警平台
|
||||||
</span>
|
</span>
|
||||||
|
|
||||||
<div className="ml-auto flex items-center gap-5">
|
<div className="ml-auto flex items-center gap-5">
|
||||||
<span className="text-[12px] text-text-muted">
|
<span className="text-[12px] text-text-muted hidden sm:inline">
|
||||||
<Clock />
|
<Clock />
|
||||||
</span>
|
</span>
|
||||||
<div className="flex items-center gap-2 text-[13px] text-text-secondary">
|
<div className="flex items-center gap-2 text-[13px] text-text-secondary">
|
||||||
<svg className="w-4 h-4" fill="currentColor" viewBox="0 0 24 24">
|
<svg className="w-4 h-4" fill="currentColor" viewBox="0 0 24 24">
|
||||||
<path d="M12 12c2.21 0 4-1.79 4-4s-1.79-4-4-4-4 1.79-4 4 1.79 4 4 4zm0 2c-2.67 0-8 1.34-8 4v2h16v-2c0-2.66-5.33-4-8-4z"/>
|
<path d="M12 12c2.21 0 4-1.79 4-4s-1.79-4-4-4-4 1.79-4 4 1.79 4 4 4zm0 2c-2.67 0-8 1.34-8 4v2h16v-2c0-2.66-5.33-4-8-4z" />
|
||||||
</svg>
|
</svg>
|
||||||
admin
|
<PerspectiveSwitcher />
|
||||||
</div>
|
</div>
|
||||||
{onLogout && (
|
{onLogout && (
|
||||||
<button
|
<button
|
||||||
|
|||||||
108
frontend/src/components/alerts/AlertDetailModal.tsx
Normal file
108
frontend/src/components/alerts/AlertDetailModal.tsx
Normal file
@@ -0,0 +1,108 @@
|
|||||||
|
import React from 'react';
|
||||||
|
import type { CellInfo } from '@/components/AlertMap';
|
||||||
|
import { HORIZON_LABELS } from './types';
|
||||||
|
import type { ExtendedAlert } from './types';
|
||||||
|
|
||||||
|
interface CellInfoPanelProps {
|
||||||
|
cellInfo: CellInfo;
|
||||||
|
onClose: () => void;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Cell info panel - shown when clicking grid cell without alert
|
||||||
|
export const CellInfoPanel = React.memo(function CellInfoPanel({ cellInfo, onClose }: CellInfoPanelProps) {
|
||||||
|
return (
|
||||||
|
<div className="fixed bottom-5 left-1/2 -translate-x-1/2 bg-bg-card rounded-lg border border-border-light shadow-lg z-50 px-5 py-4 min-w-[320px]">
|
||||||
|
<div className="flex items-center justify-between mb-3">
|
||||||
|
<span className="text-[14px] font-semibold text-text-primary">网格详情 (100m)</span>
|
||||||
|
<button onClick={onClose} className="text-text-muted hover:text-text-primary text-[18px] leading-none">×</button>
|
||||||
|
</div>
|
||||||
|
<div className="space-y-2 text-[12px]">
|
||||||
|
<div className="flex justify-between">
|
||||||
|
<span className="text-text-muted">网格</span>
|
||||||
|
<span className="font-mono text-text-primary">{cellInfo.grid_id}</span>
|
||||||
|
</div>
|
||||||
|
<div className="flex justify-between">
|
||||||
|
<span className="text-text-muted">坐标</span>
|
||||||
|
<span className="font-mono text-text-primary">{cellInfo.lat.toFixed(4)}, {cellInfo.lon.toFixed(4)}</span>
|
||||||
|
</div>
|
||||||
|
<div className="flex justify-between">
|
||||||
|
<span className="text-text-muted">当前风险</span>
|
||||||
|
<span className={`font-bold ${cellInfo.risk >= 0.8 ? 'text-danger' : cellInfo.risk >= 0.6 ? 'text-warning' : cellInfo.risk >= 0.4 ? 'text-primary' : 'text-success'}`}>
|
||||||
|
{(cellInfo.risk * 100).toFixed(1)}%
|
||||||
|
</span>
|
||||||
|
</div>
|
||||||
|
<div className="flex gap-3 pt-1">
|
||||||
|
<div className="flex-1 text-center p-1.5 rounded bg-bg-page">
|
||||||
|
<div className="text-[10px] text-text-muted">1天</div>
|
||||||
|
<div className="font-bold text-[13px]">{(cellInfo.risk_1d * 100).toFixed(0)}%</div>
|
||||||
|
</div>
|
||||||
|
<div className="flex-1 text-center p-1.5 rounded bg-bg-page">
|
||||||
|
<div className="text-[10px] text-text-muted">3天</div>
|
||||||
|
<div className="font-bold text-[13px]">{(cellInfo.risk_3d * 100).toFixed(0)}%</div>
|
||||||
|
</div>
|
||||||
|
<div className="flex-1 text-center p-1.5 rounded bg-bg-page">
|
||||||
|
<div className="text-[10px] text-text-muted">7天</div>
|
||||||
|
<div className="font-bold text-[13px]">{(cellInfo.risk_7d * 100).toFixed(0)}%</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
{cellInfo.nearestAlertId && (
|
||||||
|
<div className="flex justify-between">
|
||||||
|
<span className="text-text-muted">最近预警距离</span>
|
||||||
|
<span className="text-text-primary">{(cellInfo.nearestAlertDist * 111).toFixed(1)} km</span>
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
{!cellInfo.nearestAlertId && (
|
||||||
|
<div className="text-[11px] text-text-muted mt-1 pt-2 border-t border-border">
|
||||||
|
该区域无预警
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
interface AlertDetailModalProps {
|
||||||
|
alert: ExtendedAlert;
|
||||||
|
onClose: () => void;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Alert detail modal
|
||||||
|
export const AlertDetailModal = React.memo(function AlertDetailModal({ alert, onClose }: AlertDetailModalProps) {
|
||||||
|
return (
|
||||||
|
<div className="fixed inset-0 bg-black/50 z-50 flex items-center justify-center" onClick={onClose}>
|
||||||
|
<div className="bg-bg-card rounded-lg p-6 max-w-md w-full mx-4" onClick={e => e.stopPropagation()}>
|
||||||
|
<h3 className="font-display text-[16px] font-semibold mb-3">预警详情</h3>
|
||||||
|
<div className="space-y-2 text-[13px]">
|
||||||
|
<div className="flex justify-between">
|
||||||
|
<span className="text-text-muted">优先级</span>
|
||||||
|
<span className={`font-bold ${alert.priority === 'P1' ? 'text-danger' : 'text-warning'}`}>
|
||||||
|
{alert.priority}
|
||||||
|
</span>
|
||||||
|
</div>
|
||||||
|
<div className="flex justify-between">
|
||||||
|
<span className="text-text-muted">风险值</span>
|
||||||
|
<span className="font-bold">{Math.round(alert.risk_value * 100)}%</span>
|
||||||
|
</div>
|
||||||
|
<div className="flex justify-between">
|
||||||
|
<span className="text-text-muted">预测时效</span>
|
||||||
|
<span>{HORIZON_LABELS[alert.forecast_horizon]}</span>
|
||||||
|
</div>
|
||||||
|
<div className="flex justify-between">
|
||||||
|
<span className="text-text-muted">位置</span>
|
||||||
|
<span>{alert.region}</span>
|
||||||
|
</div>
|
||||||
|
<div className="pt-2 border-t border-border">
|
||||||
|
<div className="text-text-muted mb-1">预警原因</div>
|
||||||
|
<div className="text-[12px]">{alert.reason}</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<button
|
||||||
|
onClick={onClose}
|
||||||
|
className="mt-4 w-full px-4 py-2 bg-primary text-white rounded hover:bg-primary/80 transition-colors text-[13px]"
|
||||||
|
>
|
||||||
|
关闭
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
});
|
||||||
194
frontend/src/components/alerts/AlertsFilterBar.tsx
Normal file
194
frontend/src/components/alerts/AlertsFilterBar.tsx
Normal file
@@ -0,0 +1,194 @@
|
|||||||
|
import React from 'react';
|
||||||
|
import { TESTIDS } from '@/utils/testids';
|
||||||
|
import { DiseaseFilter } from '@/components/DiseaseFilter';
|
||||||
|
import { HORIZON_LABELS } from './types';
|
||||||
|
|
||||||
|
interface AlertsFilterBarProps {
|
||||||
|
selectedHorizon: number | 'all';
|
||||||
|
onHorizonChange: (horizon: number | 'all') => void;
|
||||||
|
selectedPriority: 'all' | 'P1' | 'P2';
|
||||||
|
onPriorityChange: (priority: 'all' | 'P1' | 'P2') => void;
|
||||||
|
riskRange: [number, number];
|
||||||
|
onRiskRangeChange: (range: [number, number]) => void;
|
||||||
|
showMap: boolean;
|
||||||
|
onToggleMap: () => void;
|
||||||
|
showAlertMarkers: boolean;
|
||||||
|
onToggleAlertMarkers: () => void;
|
||||||
|
showGrid: boolean;
|
||||||
|
onToggleGrid: () => void;
|
||||||
|
sortBy: 'risk' | 'time';
|
||||||
|
onSortByChange: (sortBy: 'risk' | 'time') => void;
|
||||||
|
// 视角驱动的两条不变量(结果由 orchestrator 计算后下传):
|
||||||
|
isCluster: boolean; // 聚类(医生)视角:隐藏「预警标记」切换 + 挂载病种过滤
|
||||||
|
isOfficial: boolean; // 官员视角:隐藏网格切换
|
||||||
|
}
|
||||||
|
|
||||||
|
// Toolbar Row 2: Filters (时效/优先级/风险值/图层切换/排序).
|
||||||
|
export const AlertsFilterBar = React.memo(function AlertsFilterBar({
|
||||||
|
selectedHorizon,
|
||||||
|
onHorizonChange,
|
||||||
|
selectedPriority,
|
||||||
|
onPriorityChange,
|
||||||
|
riskRange,
|
||||||
|
onRiskRangeChange,
|
||||||
|
showMap,
|
||||||
|
onToggleMap,
|
||||||
|
showAlertMarkers,
|
||||||
|
onToggleAlertMarkers,
|
||||||
|
showGrid,
|
||||||
|
onToggleGrid,
|
||||||
|
sortBy,
|
||||||
|
onSortByChange,
|
||||||
|
isCluster,
|
||||||
|
isOfficial,
|
||||||
|
}: AlertsFilterBarProps) {
|
||||||
|
return (
|
||||||
|
<div className="card p-3 mb-4">
|
||||||
|
<div className="flex items-center gap-4 flex-wrap">
|
||||||
|
<div className="flex items-center gap-2">
|
||||||
|
<span className="text-[12px] text-text-muted">预测时效:</span>
|
||||||
|
<div className="flex gap-1">
|
||||||
|
{(['all', 1, 3, 7] as const).map((horizon) => (
|
||||||
|
<button
|
||||||
|
key={horizon}
|
||||||
|
onClick={() => onHorizonChange(horizon)}
|
||||||
|
className={`px-3 py-1.5 text-[12px] font-medium rounded transition-colors ${
|
||||||
|
selectedHorizon === horizon
|
||||||
|
? 'bg-primary text-white'
|
||||||
|
: 'bg-bg-page text-text-secondary border border-border hover:border-primary'
|
||||||
|
}`}
|
||||||
|
>
|
||||||
|
{horizon === 'all' ? '全部' : HORIZON_LABELS[horizon]}
|
||||||
|
</button>
|
||||||
|
))}
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div className="w-px h-6 bg-border" />
|
||||||
|
|
||||||
|
<div className="flex items-center gap-2">
|
||||||
|
<span className="text-[12px] text-text-muted">优先级:</span>
|
||||||
|
<div className="flex gap-1">
|
||||||
|
{(['all', 'P1', 'P2'] as const).map((priority) => (
|
||||||
|
<button
|
||||||
|
key={priority}
|
||||||
|
onClick={() => onPriorityChange(priority)}
|
||||||
|
className={`px-3 py-1.5 text-[12px] font-medium rounded transition-colors ${
|
||||||
|
selectedPriority === priority
|
||||||
|
? priority === 'P1'
|
||||||
|
? 'bg-danger text-white'
|
||||||
|
: priority === 'P2'
|
||||||
|
? 'bg-warning text-white'
|
||||||
|
: 'bg-primary text-white'
|
||||||
|
: 'bg-bg-page text-text-secondary border border-border hover:border-primary'
|
||||||
|
}`}
|
||||||
|
>
|
||||||
|
{priority === 'all' ? '全部' : priority}
|
||||||
|
</button>
|
||||||
|
))}
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div className="w-px h-6 bg-border" />
|
||||||
|
|
||||||
|
<div className="flex items-center gap-2">
|
||||||
|
<span className="text-[12px] text-text-muted">风险值:</span>
|
||||||
|
<div className="flex items-center gap-2">
|
||||||
|
<input
|
||||||
|
type="number"
|
||||||
|
min={0}
|
||||||
|
max={1}
|
||||||
|
step={0.05}
|
||||||
|
value={riskRange[0]}
|
||||||
|
onChange={(e) => onRiskRangeChange([parseFloat(e.target.value) || 0, riskRange[1]])}
|
||||||
|
className="w-16 px-2 py-1.5 text-[12px] border border-border rounded bg-bg-page text-text-primary focus:outline-none focus:border-primary"
|
||||||
|
/>
|
||||||
|
<span className="text-[12px] text-text-muted">-</span>
|
||||||
|
<input
|
||||||
|
type="number"
|
||||||
|
min={0}
|
||||||
|
max={1}
|
||||||
|
step={0.05}
|
||||||
|
value={riskRange[1]}
|
||||||
|
onChange={(e) => onRiskRangeChange([riskRange[0], parseFloat(e.target.value) || 1])}
|
||||||
|
className="w-16 px-2 py-1.5 text-[12px] border border-border rounded bg-bg-page text-text-primary focus:outline-none focus:border-primary"
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div className="w-px h-6 bg-border" />
|
||||||
|
|
||||||
|
<div className="flex items-center gap-1">
|
||||||
|
<button
|
||||||
|
onClick={onToggleMap}
|
||||||
|
className={`px-2.5 py-1.5 text-[12px] font-medium rounded transition-colors ${
|
||||||
|
showMap
|
||||||
|
? 'bg-primary/10 text-primary border border-primary/30'
|
||||||
|
: 'bg-bg-page text-text-muted border border-border'
|
||||||
|
}`}
|
||||||
|
>
|
||||||
|
地图
|
||||||
|
</button>
|
||||||
|
{/* 预警标记切换:聚类(医生)视角隐藏整块——个体病例点不可开启(隐私不变量)。 */}
|
||||||
|
{!isCluster && (
|
||||||
|
<button
|
||||||
|
onClick={onToggleAlertMarkers}
|
||||||
|
className={`px-2.5 py-1.5 text-[12px] font-medium rounded transition-colors ${
|
||||||
|
showAlertMarkers
|
||||||
|
? 'bg-primary/10 text-primary border border-primary/30'
|
||||||
|
: 'bg-bg-page text-text-muted border border-border'
|
||||||
|
}`}
|
||||||
|
>
|
||||||
|
预警标记
|
||||||
|
</button>
|
||||||
|
)}
|
||||||
|
{/* 网格切换:官员视角隐藏整块(100m 网格对其无意义/太超前)。 */}
|
||||||
|
{!isOfficial && (
|
||||||
|
<div data-testid={TESTIDS.gridLayerWrapper}>
|
||||||
|
<button
|
||||||
|
onClick={onToggleGrid}
|
||||||
|
className={`px-2.5 py-1.5 text-[12px] font-medium rounded transition-colors ${
|
||||||
|
showGrid
|
||||||
|
? 'bg-primary/10 text-primary border border-primary/30'
|
||||||
|
: 'bg-bg-page text-text-muted border border-border'
|
||||||
|
}`}
|
||||||
|
>
|
||||||
|
网格
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
{/* 聚类(医生)视角:病种过滤是其核心工具,挂载于此。 */}
|
||||||
|
{isCluster && <DiseaseFilter />}
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div className="w-px h-6 bg-border" />
|
||||||
|
|
||||||
|
<div className="flex items-center gap-2">
|
||||||
|
<span className="text-[12px] text-text-muted">排序:</span>
|
||||||
|
<div className="flex gap-1">
|
||||||
|
<button
|
||||||
|
onClick={() => onSortByChange('risk')}
|
||||||
|
className={`px-3 py-1.5 text-[12px] font-medium rounded transition-colors ${
|
||||||
|
sortBy === 'risk'
|
||||||
|
? 'bg-bg-card text-primary border border-primary'
|
||||||
|
: 'bg-bg-page text-text-secondary border border-border hover:border-primary'
|
||||||
|
}`}
|
||||||
|
>
|
||||||
|
风险值
|
||||||
|
</button>
|
||||||
|
<button
|
||||||
|
onClick={() => onSortByChange('time')}
|
||||||
|
className={`px-3 py-1.5 text-[12px] font-medium rounded transition-colors ${
|
||||||
|
sortBy === 'time'
|
||||||
|
? 'bg-bg-card text-primary border border-primary'
|
||||||
|
: 'bg-bg-page text-text-secondary border border-border hover:border-primary'
|
||||||
|
}`}
|
||||||
|
>
|
||||||
|
时间
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
});
|
||||||
57
frontend/src/components/alerts/AlertsHeader.tsx
Normal file
57
frontend/src/components/alerts/AlertsHeader.tsx
Normal file
@@ -0,0 +1,57 @@
|
|||||||
|
import React from 'react';
|
||||||
|
|
||||||
|
interface AlertsHeaderProps {
|
||||||
|
total: number;
|
||||||
|
p1: number;
|
||||||
|
p2: number;
|
||||||
|
activeTab: 'list' | 'stats';
|
||||||
|
onTabChange: (tab: 'list' | 'stats') => void;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 页头(标题 + 计数)+ 页内 tab 切换条(不走 router)。
|
||||||
|
export const AlertsHeader = React.memo(function AlertsHeader({
|
||||||
|
total,
|
||||||
|
p1,
|
||||||
|
p2,
|
||||||
|
activeTab,
|
||||||
|
onTabChange,
|
||||||
|
}: AlertsHeaderProps) {
|
||||||
|
return (
|
||||||
|
<>
|
||||||
|
{/* Header */}
|
||||||
|
<div className="flex items-center justify-between mb-4 flex-wrap gap-x-4 gap-y-2">
|
||||||
|
<div className="min-w-0">
|
||||||
|
<h1 className="font-display text-[18px] font-semibold mb-1">风险预警</h1>
|
||||||
|
<p className="text-[12px] text-text-muted truncate">
|
||||||
|
100m网格风险预测 · 多时间尺度预警 · 病例-气象关联分析
|
||||||
|
</p>
|
||||||
|
</div>
|
||||||
|
<div className="flex items-center gap-3 text-[11px] shrink-0 flex-wrap">
|
||||||
|
<span className="text-text-muted">共 <span className="font-semibold text-text-primary">{total}</span> 条预警</span>
|
||||||
|
<span className="px-2 py-1 bg-danger/10 border border-danger/20 rounded text-danger font-semibold">P1: {p1}</span>
|
||||||
|
<span className="px-2 py-1 bg-warning/10 border border-warning/20 rounded text-warning font-semibold">P2: {p2}</span>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
{/* Tab strip — in-page, no router */}
|
||||||
|
<div className="flex gap-1 mb-4 border-b border-border">
|
||||||
|
{([
|
||||||
|
{ key: 'list', label: '预警列表' },
|
||||||
|
{ key: 'stats', label: '风险统计' },
|
||||||
|
] as const).map((tab) => (
|
||||||
|
<button
|
||||||
|
key={tab.key}
|
||||||
|
onClick={() => onTabChange(tab.key)}
|
||||||
|
className={`px-4 py-2 text-[13px] font-medium -mb-px border-b-2 transition-colors ${
|
||||||
|
activeTab === tab.key
|
||||||
|
? 'border-primary text-primary'
|
||||||
|
: 'border-transparent text-text-secondary hover:text-text-primary'
|
||||||
|
}`}
|
||||||
|
>
|
||||||
|
{tab.label}
|
||||||
|
</button>
|
||||||
|
))}
|
||||||
|
</div>
|
||||||
|
</>
|
||||||
|
);
|
||||||
|
});
|
||||||
138
frontend/src/components/alerts/AlertsList.tsx
Normal file
138
frontend/src/components/alerts/AlertsList.tsx
Normal file
@@ -0,0 +1,138 @@
|
|||||||
|
import React, { useCallback } from 'react';
|
||||||
|
import { HORIZON_LABELS } from './types';
|
||||||
|
import type { ExtendedAlert, RiskStats } from './types';
|
||||||
|
|
||||||
|
interface RiskDistributionSummaryProps {
|
||||||
|
riskStats: RiskStats;
|
||||||
|
total: number;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 预警列表 tab 顶部的风险分布概要(4 卡)。
|
||||||
|
export const RiskDistributionSummary = React.memo(function RiskDistributionSummary({
|
||||||
|
riskStats,
|
||||||
|
total,
|
||||||
|
}: RiskDistributionSummaryProps) {
|
||||||
|
return (
|
||||||
|
<div className="grid grid-cols-4 gap-3 mb-4">
|
||||||
|
<div className="card p-3">
|
||||||
|
<div className="text-[11px] text-text-muted mb-1">高风险 (≥0.8)</div>
|
||||||
|
<div className="text-xl font-bold text-danger">{riskStats.high}</div>
|
||||||
|
<div className="mt-1.5 h-1.5 bg-gray-100 rounded-full overflow-hidden">
|
||||||
|
<div className="h-full bg-danger rounded-full" style={{ width: `${total > 0 ? (riskStats.high / total) * 100 : 0}%` }} />
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<div className="card p-3">
|
||||||
|
<div className="text-[11px] text-text-muted mb-1">中高风险 (0.6-0.8)</div>
|
||||||
|
<div className="text-xl font-bold text-warning">{riskStats.mediumHigh}</div>
|
||||||
|
<div className="mt-1.5 h-1.5 bg-gray-100 rounded-full overflow-hidden">
|
||||||
|
<div className="h-full bg-warning rounded-full" style={{ width: `${total > 0 ? (riskStats.mediumHigh / total) * 100 : 0}%` }} />
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<div className="card p-3">
|
||||||
|
<div className="text-[11px] text-text-muted mb-1">中风险 (0.4-0.6)</div>
|
||||||
|
<div className="text-xl font-bold text-primary">{riskStats.medium}</div>
|
||||||
|
<div className="mt-1.5 h-1.5 bg-gray-100 rounded-full overflow-hidden">
|
||||||
|
<div className="h-full bg-primary rounded-full" style={{ width: `${total > 0 ? (riskStats.medium / total) * 100 : 0}%` }} />
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<div className="card p-3">
|
||||||
|
<div className="text-[11px] text-text-muted mb-1">平均风险</div>
|
||||||
|
<div className="text-xl font-bold text-text-primary">{(riskStats.avgRisk * 100).toFixed(1)}%</div>
|
||||||
|
<div className="mt-1.5 text-[10px] text-text-muted">
|
||||||
|
高风险区域: {riskStats.topDistricts.slice(0, 2).map(([d, n]) => `${d}(${n})`).join(', ')}
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
interface AlertCardProps {
|
||||||
|
alert: ExtendedAlert;
|
||||||
|
isSelected?: boolean;
|
||||||
|
alertId: string;
|
||||||
|
onCardClick: (id: string) => void;
|
||||||
|
}
|
||||||
|
|
||||||
|
const AlertCard = React.memo(function AlertCard({ alert, isSelected, alertId, onCardClick }: AlertCardProps) {
|
||||||
|
const isP1 = alert.priority === 'P1';
|
||||||
|
const riskPercent = Math.round(alert.risk_value * 100);
|
||||||
|
|
||||||
|
const handleClick = useCallback(() => {
|
||||||
|
onCardClick(alertId);
|
||||||
|
}, [alertId, onCardClick]);
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div
|
||||||
|
className={`card overflow-hidden transition-colors cursor-pointer ${
|
||||||
|
isSelected ? 'border-primary ring-1 ring-primary' : 'hover:border-primary'
|
||||||
|
}`}
|
||||||
|
onClick={handleClick}
|
||||||
|
>
|
||||||
|
<div className={`px-4 py-3 border-b ${isP1 ? 'bg-danger/5 border-danger/20' : 'bg-warning/5 border-warning/20'}`}>
|
||||||
|
<div className="flex items-center justify-between">
|
||||||
|
<div className="flex items-center gap-2">
|
||||||
|
<div className={`w-2 h-2 rounded-full ${isP1 ? 'bg-danger' : 'bg-warning'}`} />
|
||||||
|
<span className={`text-[11px] font-bold ${isP1 ? 'text-danger' : 'text-warning'}`}>
|
||||||
|
{alert.priority}
|
||||||
|
</span>
|
||||||
|
<span className="text-[10px] text-text-muted">
|
||||||
|
{HORIZON_LABELS[alert.forecast_horizon] || '未知'}
|
||||||
|
</span>
|
||||||
|
</div>
|
||||||
|
<span className={`text-[18px] font-bold ${isP1 ? 'text-danger' : 'text-warning'}`}>
|
||||||
|
{riskPercent}%
|
||||||
|
</span>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div className="p-4">
|
||||||
|
<div className="mb-3">
|
||||||
|
<div className="text-[13px] font-semibold mb-1">
|
||||||
|
{alert.region} - {alert.street}
|
||||||
|
</div>
|
||||||
|
<div className="text-[11px] text-text-muted">
|
||||||
|
网格:{alert.grid_id}
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div className={`text-[12px] px-3 py-2 rounded mb-3 ${
|
||||||
|
isP1 ? 'bg-danger/10 text-danger' : 'bg-warning/10 text-warning'
|
||||||
|
}`}>
|
||||||
|
{alert.reason}
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div className="flex items-center justify-between text-[11px] text-text-muted">
|
||||||
|
<span>预测时间:{alert.forecast_time}</span>
|
||||||
|
<span>生成:{alert.timestamp}</span>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
interface AlertsListProps {
|
||||||
|
filteredAlerts: ExtendedAlert[];
|
||||||
|
selectedAlert: string | null;
|
||||||
|
onCardClick: (id: string) => void;
|
||||||
|
}
|
||||||
|
|
||||||
|
export const AlertsList = React.memo(function AlertsList({ filteredAlerts, selectedAlert, onCardClick }: AlertsListProps) {
|
||||||
|
return (
|
||||||
|
<div className="space-y-3 max-h-[calc(100vh-280px)] overflow-y-auto">
|
||||||
|
{filteredAlerts.slice(0, 50).map((alert) => (
|
||||||
|
<AlertCard
|
||||||
|
key={alert.alert_id}
|
||||||
|
alert={alert}
|
||||||
|
isSelected={selectedAlert === alert.alert_id}
|
||||||
|
alertId={alert.alert_id}
|
||||||
|
onCardClick={onCardClick}
|
||||||
|
/>
|
||||||
|
))}
|
||||||
|
{filteredAlerts.length > 50 && (
|
||||||
|
<div className="text-center text-text-muted text-[12px] py-2">
|
||||||
|
还有 {filteredAlerts.length - 50} 条预警未显示
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
});
|
||||||
129
frontend/src/components/alerts/AlertsListTab.tsx
Normal file
129
frontend/src/components/alerts/AlertsListTab.tsx
Normal file
@@ -0,0 +1,129 @@
|
|||||||
|
import React from 'react';
|
||||||
|
import { LoadingState } from '@/components/ui';
|
||||||
|
import type { CellInfo } from '@/components/AlertMap';
|
||||||
|
import { AlertsToolbar } from './AlertsToolbar';
|
||||||
|
import { AlertsFilterBar } from './AlertsFilterBar';
|
||||||
|
import { AlertsMapPanel } from './AlertsMapPanel';
|
||||||
|
import { AlertsList, RiskDistributionSummary } from './AlertsList';
|
||||||
|
import type { ExtendedAlert, RiskStats } from './types';
|
||||||
|
|
||||||
|
interface AlertsListTabProps {
|
||||||
|
// toolbar
|
||||||
|
forecastDay: 1 | 3 | 7;
|
||||||
|
onForecastDayChange: (day: 1 | 3 | 7) => void;
|
||||||
|
isFullscreen: boolean;
|
||||||
|
onToggleFullscreen: () => void;
|
||||||
|
onExportCsv: () => void;
|
||||||
|
onExportJson: () => void;
|
||||||
|
// filter bar
|
||||||
|
selectedHorizon: number | 'all';
|
||||||
|
onHorizonChange: (horizon: number | 'all') => void;
|
||||||
|
selectedPriority: 'all' | 'P1' | 'P2';
|
||||||
|
onPriorityChange: (priority: 'all' | 'P1' | 'P2') => void;
|
||||||
|
riskRange: [number, number];
|
||||||
|
onRiskRangeChange: (range: [number, number]) => void;
|
||||||
|
showMap: boolean;
|
||||||
|
onToggleMap: () => void;
|
||||||
|
showAlertMarkers: boolean;
|
||||||
|
onToggleAlertMarkers: () => void;
|
||||||
|
showGrid: boolean;
|
||||||
|
onToggleGrid: () => void;
|
||||||
|
sortBy: 'risk' | 'time';
|
||||||
|
onSortByChange: (sortBy: 'risk' | 'time') => void;
|
||||||
|
// data
|
||||||
|
riskStats: RiskStats;
|
||||||
|
filteredAlerts: ExtendedAlert[];
|
||||||
|
isLoading: boolean;
|
||||||
|
selectedGridId: string | null;
|
||||||
|
selectedAlert: string | null;
|
||||||
|
onGridClick: (gridId: string) => void;
|
||||||
|
onCellInfo: (info: CellInfo) => void;
|
||||||
|
onCardClick: (id: string) => void;
|
||||||
|
// privacy/role results (computed by orchestrator)
|
||||||
|
effectiveShowAlertMarkers: boolean;
|
||||||
|
isCluster: boolean;
|
||||||
|
isOfficial: boolean;
|
||||||
|
}
|
||||||
|
|
||||||
|
export const AlertsListTab = React.memo(function AlertsListTab(props: AlertsListTabProps) {
|
||||||
|
const {
|
||||||
|
filteredAlerts,
|
||||||
|
isLoading,
|
||||||
|
isCluster,
|
||||||
|
isFullscreen,
|
||||||
|
showMap,
|
||||||
|
riskStats,
|
||||||
|
} = props;
|
||||||
|
|
||||||
|
return (
|
||||||
|
<>
|
||||||
|
<AlertsToolbar
|
||||||
|
forecastDay={props.forecastDay}
|
||||||
|
onForecastDayChange={props.onForecastDayChange}
|
||||||
|
isFullscreen={isFullscreen}
|
||||||
|
onToggleFullscreen={props.onToggleFullscreen}
|
||||||
|
onExportCsv={props.onExportCsv}
|
||||||
|
onExportJson={props.onExportJson}
|
||||||
|
/>
|
||||||
|
|
||||||
|
<AlertsFilterBar
|
||||||
|
selectedHorizon={props.selectedHorizon}
|
||||||
|
onHorizonChange={props.onHorizonChange}
|
||||||
|
selectedPriority={props.selectedPriority}
|
||||||
|
onPriorityChange={props.onPriorityChange}
|
||||||
|
riskRange={props.riskRange}
|
||||||
|
onRiskRangeChange={props.onRiskRangeChange}
|
||||||
|
showMap={showMap}
|
||||||
|
onToggleMap={props.onToggleMap}
|
||||||
|
showAlertMarkers={props.showAlertMarkers}
|
||||||
|
onToggleAlertMarkers={props.onToggleAlertMarkers}
|
||||||
|
showGrid={props.showGrid}
|
||||||
|
onToggleGrid={props.onToggleGrid}
|
||||||
|
sortBy={props.sortBy}
|
||||||
|
onSortByChange={props.onSortByChange}
|
||||||
|
isCluster={isCluster}
|
||||||
|
isOfficial={props.isOfficial}
|
||||||
|
/>
|
||||||
|
|
||||||
|
<RiskDistributionSummary riskStats={riskStats} total={filteredAlerts.length} />
|
||||||
|
|
||||||
|
{isLoading ? (
|
||||||
|
<div className="card p-8">
|
||||||
|
<LoadingState />
|
||||||
|
</div>
|
||||||
|
) : filteredAlerts.length === 0 && !isCluster ? (
|
||||||
|
// 聚类(医生)视角即使没有个体预警,也要展示聚合密度栅格——故不走空状态分支。
|
||||||
|
<div className="card p-8 text-center">
|
||||||
|
<svg className="w-12 h-12 mx-auto mb-3 text-text-muted opacity-50" fill="currentColor" viewBox="0 0 24 24">
|
||||||
|
<path d="M12 22c1.1 0 2-.9 2-2h-4c0 1.1.89 2 2 2zm6-6v-5c0-3.07-1.64-5.64-4.5-6.32V4c0-.83-.67-1.5-1.5-1.5s-1.5.67-1.5 1.5v.68C7.63 5.36 6 7.92 6 11v5l-2 2v1h16v-1l-2-2z"/>
|
||||||
|
</svg>
|
||||||
|
<div className="text-text-muted text-[13px]">暂无符合条件的预警</div>
|
||||||
|
</div>
|
||||||
|
) : (
|
||||||
|
<div className={`grid gap-4 ${isFullscreen ? 'grid-cols-1' : 'grid-cols-[1fr_400px]'}`}>
|
||||||
|
{showMap && (
|
||||||
|
<AlertsMapPanel
|
||||||
|
selectedGridId={props.selectedGridId}
|
||||||
|
onGridClick={props.onGridClick}
|
||||||
|
onCellInfo={props.onCellInfo}
|
||||||
|
forecastDay={props.forecastDay}
|
||||||
|
effectiveShowAlertMarkers={props.effectiveShowAlertMarkers}
|
||||||
|
showGrid={props.showGrid}
|
||||||
|
filteredAlerts={filteredAlerts}
|
||||||
|
isFullscreen={isFullscreen}
|
||||||
|
isCluster={isCluster}
|
||||||
|
isOfficial={props.isOfficial}
|
||||||
|
/>
|
||||||
|
)}
|
||||||
|
{!isFullscreen && (
|
||||||
|
<AlertsList
|
||||||
|
filteredAlerts={filteredAlerts}
|
||||||
|
selectedAlert={props.selectedAlert}
|
||||||
|
onCardClick={props.onCardClick}
|
||||||
|
/>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
</>
|
||||||
|
);
|
||||||
|
});
|
||||||
64
frontend/src/components/alerts/AlertsMapPanel.tsx
Normal file
64
frontend/src/components/alerts/AlertsMapPanel.tsx
Normal file
@@ -0,0 +1,64 @@
|
|||||||
|
import React from 'react';
|
||||||
|
import { TESTIDS } from '@/utils/testids';
|
||||||
|
import { AlertMap } from '@/components/AlertMap';
|
||||||
|
import type { CellInfo } from '@/components/AlertMap';
|
||||||
|
import type { ExtendedAlert } from './types';
|
||||||
|
|
||||||
|
interface AlertsMapPanelProps {
|
||||||
|
selectedGridId: string | null;
|
||||||
|
onGridClick: (gridId: string) => void;
|
||||||
|
onCellInfo: (info: CellInfo) => void;
|
||||||
|
forecastDay: 1 | 3 | 7;
|
||||||
|
// effectiveShowAlertMarkers:唯一真值,cluster 模式恒为 false(隐私不变量),由 orchestrator 计算。
|
||||||
|
effectiveShowAlertMarkers: boolean;
|
||||||
|
showGrid: boolean;
|
||||||
|
filteredAlerts: ExtendedAlert[];
|
||||||
|
isFullscreen: boolean;
|
||||||
|
isCluster: boolean;
|
||||||
|
isOfficial: boolean;
|
||||||
|
}
|
||||||
|
|
||||||
|
export const AlertsMapPanel = React.memo(function AlertsMapPanel({
|
||||||
|
selectedGridId,
|
||||||
|
onGridClick,
|
||||||
|
onCellInfo,
|
||||||
|
forecastDay,
|
||||||
|
effectiveShowAlertMarkers,
|
||||||
|
showGrid,
|
||||||
|
filteredAlerts,
|
||||||
|
isFullscreen,
|
||||||
|
isCluster,
|
||||||
|
isOfficial,
|
||||||
|
}: AlertsMapPanelProps) {
|
||||||
|
return (
|
||||||
|
<div data-testid={isCluster ? TESTIDS.clusterView : undefined}>
|
||||||
|
<AlertMap
|
||||||
|
selectedGridId={selectedGridId}
|
||||||
|
onGridClick={onGridClick}
|
||||||
|
onCellInfo={onCellInfo}
|
||||||
|
forecastDay={forecastDay}
|
||||||
|
showAlertMarkers={effectiveShowAlertMarkers}
|
||||||
|
showGrid={isOfficial ? false : showGrid}
|
||||||
|
filteredAlerts={filteredAlerts}
|
||||||
|
isFullscreen={isFullscreen}
|
||||||
|
/>
|
||||||
|
{/*
|
||||||
|
隐私不变量的「数据级」可断言点:每渲染一个个体病例点标记,就在此输出一个
|
||||||
|
data-testid="patient-point" 的隐藏标记。Leaflet 的 CircleMarker 是 canvas/SVG
|
||||||
|
内部对象、不带 testid,无法被 e2e 直接计数;这里把「实际会显示的个体点集合」
|
||||||
|
镜像成 DOM,使测试可断言医生/聚类视角下 patient-point 计数恒为 0,
|
||||||
|
而无需窥探 Leaflet 内部。effectiveShowAlertMarkers 在 cluster 模式恒为 false,
|
||||||
|
故该集合为空。
|
||||||
|
*/}
|
||||||
|
{effectiveShowAlertMarkers &&
|
||||||
|
filteredAlerts.map((a) => (
|
||||||
|
<span
|
||||||
|
key={a.alert_id}
|
||||||
|
data-testid={TESTIDS.patientPoint}
|
||||||
|
className="hidden"
|
||||||
|
aria-hidden
|
||||||
|
/>
|
||||||
|
))}
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
});
|
||||||
130
frontend/src/components/alerts/AlertsRiskPanel.tsx
Normal file
130
frontend/src/components/alerts/AlertsRiskPanel.tsx
Normal file
@@ -0,0 +1,130 @@
|
|||||||
|
import React, { useMemo } from 'react';
|
||||||
|
import { LoadingState } from '@/components/ui';
|
||||||
|
import { StatCard } from '@/components/StatCard';
|
||||||
|
import { StatisticalCharts } from '@/components/StatisticalCharts';
|
||||||
|
import { PieChart, Pie, Cell, Tooltip as RechartsTooltip, Legend, ResponsiveContainer } from 'recharts';
|
||||||
|
import type { RiskStats } from './types';
|
||||||
|
|
||||||
|
interface AlertsRiskPanelProps {
|
||||||
|
riskStats: RiskStats;
|
||||||
|
trendData: Array<{ date: string; cases: number; risk: number }>;
|
||||||
|
trendLoading: boolean;
|
||||||
|
trendError: string | null;
|
||||||
|
}
|
||||||
|
|
||||||
|
export const AlertsRiskPanel = React.memo(function AlertsRiskPanel({
|
||||||
|
riskStats,
|
||||||
|
trendData,
|
||||||
|
trendLoading,
|
||||||
|
trendError,
|
||||||
|
}: AlertsRiskPanelProps) {
|
||||||
|
// Severity donut data (P1/P2)
|
||||||
|
const alertPie = useMemo(() => ([
|
||||||
|
{ name: 'P1 (紧急)', value: riskStats.p1, color: '#ef4444' },
|
||||||
|
{ name: 'P2 (关注)', value: riskStats.p2, color: '#f59e0b' },
|
||||||
|
]), [riskStats.p1, riskStats.p2]);
|
||||||
|
|
||||||
|
const topDistrictMax = useMemo(
|
||||||
|
() => riskStats.topDistricts.reduce((m, [, n]) => Math.max(m, n), 0),
|
||||||
|
[riskStats.topDistricts],
|
||||||
|
);
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div className="space-y-4">
|
||||||
|
{/* Risk distribution as StatCards */}
|
||||||
|
<div className="grid grid-cols-2 md:grid-cols-4 gap-3">
|
||||||
|
<StatCard label="高风险 (≥0.8)" value={riskStats.high} color="#ef4444" />
|
||||||
|
<StatCard label="中高风险 (0.6-0.8)" value={riskStats.mediumHigh} color="#f59e0b" />
|
||||||
|
<StatCard label="中风险 (0.4-0.6)" value={riskStats.medium} color="#3b82f6" />
|
||||||
|
<StatCard label="平均风险" value={`${(riskStats.avgRisk * 100).toFixed(1)}%`} />
|
||||||
|
</div>
|
||||||
|
|
||||||
|
{/* Risk trend chart (real data from /api/analysis/trend) */}
|
||||||
|
{trendLoading ? (
|
||||||
|
<div className="card p-8"><LoadingState /></div>
|
||||||
|
) : trendError ? (
|
||||||
|
<div className="card p-8 text-center text-danger text-[13px]">{trendError}</div>
|
||||||
|
) : trendData.length === 0 ? (
|
||||||
|
<div className="card p-8 text-center text-text-muted text-[13px]">暂无风险趋势数据</div>
|
||||||
|
) : (
|
||||||
|
<StatisticalCharts
|
||||||
|
data={trendData}
|
||||||
|
showCases={false}
|
||||||
|
showRisk
|
||||||
|
height={280}
|
||||||
|
/>
|
||||||
|
)}
|
||||||
|
|
||||||
|
<div className="grid grid-cols-1 lg:grid-cols-2 gap-4">
|
||||||
|
{/* Top high-risk districts bar */}
|
||||||
|
<div className="card p-4">
|
||||||
|
<div className="text-[11px] font-medium text-text-muted uppercase tracking-wide mb-4">
|
||||||
|
高风险区域 Top 5
|
||||||
|
</div>
|
||||||
|
{riskStats.topDistricts.length > 0 ? (
|
||||||
|
<div className="space-y-3">
|
||||||
|
{riskStats.topDistricts.map(([district, count]) => (
|
||||||
|
<div key={district}>
|
||||||
|
<div className="flex items-center justify-between text-[12px] mb-1">
|
||||||
|
<span className="text-text-primary font-medium">{district}</span>
|
||||||
|
<span className="text-text-muted">{count} 条</span>
|
||||||
|
</div>
|
||||||
|
<div className="h-2 bg-gray-100 rounded-full overflow-hidden">
|
||||||
|
<div
|
||||||
|
className="h-full bg-danger rounded-full"
|
||||||
|
style={{ width: `${topDistrictMax > 0 ? (count / topDistrictMax) * 100 : 0}%` }}
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
))}
|
||||||
|
</div>
|
||||||
|
) : (
|
||||||
|
<div className="text-center py-8 text-text-muted text-[13px]">暂无区域数据</div>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
|
||||||
|
{/* Alert severity donut (P1/P2) */}
|
||||||
|
<div className="card p-4">
|
||||||
|
<div className="text-[11px] font-medium text-text-muted uppercase tracking-wide mb-4">
|
||||||
|
预警严重度分布
|
||||||
|
</div>
|
||||||
|
{riskStats.p1 > 0 || riskStats.p2 > 0 ? (
|
||||||
|
<ResponsiveContainer width="100%" height={240}>
|
||||||
|
<PieChart>
|
||||||
|
<Pie
|
||||||
|
data={alertPie}
|
||||||
|
cx="50%"
|
||||||
|
cy="50%"
|
||||||
|
innerRadius={50}
|
||||||
|
outerRadius={80}
|
||||||
|
paddingAngle={4}
|
||||||
|
dataKey="value"
|
||||||
|
nameKey="name"
|
||||||
|
>
|
||||||
|
{alertPie.map((entry) => (
|
||||||
|
<Cell key={entry.name} fill={entry.color} />
|
||||||
|
))}
|
||||||
|
</Pie>
|
||||||
|
<RechartsTooltip
|
||||||
|
contentStyle={{
|
||||||
|
backgroundColor: '#FFFFFF',
|
||||||
|
border: '1px solid #E2E8F0',
|
||||||
|
borderRadius: '8px',
|
||||||
|
fontSize: '12px',
|
||||||
|
}}
|
||||||
|
formatter={(value: number, name: string) => [value, name]}
|
||||||
|
/>
|
||||||
|
<Legend
|
||||||
|
wrapperStyle={{ fontSize: '12px' }}
|
||||||
|
formatter={(value: string) => <span className="text-text-secondary">{value}</span>}
|
||||||
|
/>
|
||||||
|
</PieChart>
|
||||||
|
</ResponsiveContainer>
|
||||||
|
) : (
|
||||||
|
<div className="text-center py-8 text-text-muted text-[13px]">暂无预警数据</div>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
});
|
||||||
73
frontend/src/components/alerts/AlertsToolbar.tsx
Normal file
73
frontend/src/components/alerts/AlertsToolbar.tsx
Normal file
@@ -0,0 +1,73 @@
|
|||||||
|
import React from 'react';
|
||||||
|
|
||||||
|
interface AlertsToolbarProps {
|
||||||
|
forecastDay: 1 | 3 | 7;
|
||||||
|
onForecastDayChange: (day: 1 | 3 | 7) => void;
|
||||||
|
isFullscreen: boolean;
|
||||||
|
onToggleFullscreen: () => void;
|
||||||
|
onExportCsv: () => void;
|
||||||
|
onExportJson: () => void;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Toolbar Row 1: 网格预测时效 + 全屏 + 导出.
|
||||||
|
export const AlertsToolbar = React.memo(function AlertsToolbar({
|
||||||
|
forecastDay,
|
||||||
|
onForecastDayChange,
|
||||||
|
isFullscreen,
|
||||||
|
onToggleFullscreen,
|
||||||
|
onExportCsv,
|
||||||
|
onExportJson,
|
||||||
|
}: AlertsToolbarProps) {
|
||||||
|
return (
|
||||||
|
<div className="card p-3 mb-3">
|
||||||
|
<div className="flex items-center gap-3 flex-wrap">
|
||||||
|
<div className="flex items-center gap-2">
|
||||||
|
<span className="text-[12px] text-text-muted">网格预测:</span>
|
||||||
|
<div className="flex gap-0.5 bg-bg-page p-0.5 rounded">
|
||||||
|
{([1, 3, 7] as const).map((day) => (
|
||||||
|
<button
|
||||||
|
key={day}
|
||||||
|
onClick={() => onForecastDayChange(day)}
|
||||||
|
className={`px-3 py-1 text-[12px] font-medium rounded transition-colors ${
|
||||||
|
forecastDay === day
|
||||||
|
? 'bg-bg-card text-primary shadow-sm'
|
||||||
|
: 'text-text-secondary hover:text-text-primary'
|
||||||
|
}`}
|
||||||
|
>
|
||||||
|
{day}天
|
||||||
|
</button>
|
||||||
|
))}
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div className="w-px h-6 bg-border" />
|
||||||
|
|
||||||
|
<button
|
||||||
|
onClick={onToggleFullscreen}
|
||||||
|
className={`px-3 py-1.5 text-[12px] font-medium rounded transition-colors ${
|
||||||
|
isFullscreen
|
||||||
|
? 'bg-bg-card text-primary border border-primary'
|
||||||
|
: 'bg-bg-page text-text-secondary border border-border'
|
||||||
|
}`}
|
||||||
|
>
|
||||||
|
{isFullscreen ? '退出全屏' : '全屏'}
|
||||||
|
</button>
|
||||||
|
|
||||||
|
<div className="w-px h-6 bg-border" />
|
||||||
|
|
||||||
|
<button
|
||||||
|
onClick={onExportCsv}
|
||||||
|
className="px-3 py-1.5 text-[12px] font-medium rounded bg-bg-page text-text-secondary border border-border hover:border-primary transition-colors"
|
||||||
|
>
|
||||||
|
导出CSV
|
||||||
|
</button>
|
||||||
|
<button
|
||||||
|
onClick={onExportJson}
|
||||||
|
className="px-3 py-1.5 text-[12px] font-medium rounded bg-bg-page text-text-secondary border border-border hover:border-primary transition-colors"
|
||||||
|
>
|
||||||
|
导出JSON
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
});
|
||||||
32
frontend/src/components/alerts/types.ts
Normal file
32
frontend/src/components/alerts/types.ts
Normal file
@@ -0,0 +1,32 @@
|
|||||||
|
// Shared types for the alerts dashboard subcomponents.
|
||||||
|
export interface ExtendedAlert {
|
||||||
|
alert_id: string;
|
||||||
|
grid_id: string;
|
||||||
|
region: string;
|
||||||
|
street: string;
|
||||||
|
latitude: number;
|
||||||
|
longitude: number;
|
||||||
|
risk_value: number;
|
||||||
|
risk_level: 'high' | 'medium_high' | 'medium' | 'medium_low' | 'low';
|
||||||
|
priority: 'P1' | 'P2';
|
||||||
|
forecast_horizon: number;
|
||||||
|
forecast_time: string;
|
||||||
|
reason: string;
|
||||||
|
timestamp: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
export const HORIZON_LABELS: Record<number, string> = {
|
||||||
|
1: '1 天后',
|
||||||
|
3: '3 天后',
|
||||||
|
7: '7 天后',
|
||||||
|
};
|
||||||
|
|
||||||
|
export interface RiskStats {
|
||||||
|
p1: number;
|
||||||
|
p2: number;
|
||||||
|
high: number;
|
||||||
|
mediumHigh: number;
|
||||||
|
medium: number;
|
||||||
|
avgRisk: number;
|
||||||
|
topDistricts: [string, number][];
|
||||||
|
}
|
||||||
163
frontend/src/components/alerts/useAlertsData.ts
Normal file
163
frontend/src/components/alerts/useAlertsData.ts
Normal file
@@ -0,0 +1,163 @@
|
|||||||
|
import { useState, useMemo, useEffect, useCallback } from 'react';
|
||||||
|
import { useRiskStore } from '@/stores';
|
||||||
|
import { analysisApi } from '@/services/api';
|
||||||
|
import type { ExtendedAlert, RiskStats } from './types';
|
||||||
|
|
||||||
|
interface UseAlertsDataParams {
|
||||||
|
selectedHorizon: number | 'all';
|
||||||
|
selectedPriority: 'all' | 'P1' | 'P2';
|
||||||
|
sortBy: 'risk' | 'time';
|
||||||
|
debouncedRiskRange: [number, number];
|
||||||
|
activeTab: 'list' | 'stats';
|
||||||
|
}
|
||||||
|
|
||||||
|
interface TrendPoint { date: string; cases: number; risk: number }
|
||||||
|
|
||||||
|
// 预警仪表盘的数据层:派生 extendedAlerts/filteredAlerts/riskStats、按需拉取风险趋势、
|
||||||
|
// 以及 CSV/JSON 导出辅助。角色/隐私计算保留在 orchestrator,不在此处。
|
||||||
|
export function useAlertsData({
|
||||||
|
selectedHorizon,
|
||||||
|
selectedPriority,
|
||||||
|
sortBy,
|
||||||
|
debouncedRiskRange,
|
||||||
|
activeTab,
|
||||||
|
}: UseAlertsDataParams) {
|
||||||
|
const alerts = useRiskStore((s) => s.alerts);
|
||||||
|
|
||||||
|
// Risk-trend data for the 风险统计 tab, fetched on demand
|
||||||
|
const [trendData, setTrendData] = useState<TrendPoint[]>([]);
|
||||||
|
const [trendLoading, setTrendLoading] = useState(false);
|
||||||
|
const [trendError, setTrendError] = useState<string | null>(null);
|
||||||
|
const [trendLoaded, setTrendLoaded] = useState(false);
|
||||||
|
|
||||||
|
// Fetch real risk-trend data when the 风险统计 tab is first opened
|
||||||
|
useEffect(() => {
|
||||||
|
if (activeTab !== 'stats' || trendLoaded) return;
|
||||||
|
let cancelled = false;
|
||||||
|
setTrendLoading(true);
|
||||||
|
setTrendError(null);
|
||||||
|
analysisApi
|
||||||
|
.getTrend(14)
|
||||||
|
.then((res: { dates?: string[]; values?: number[] }) => {
|
||||||
|
if (cancelled) return;
|
||||||
|
const dates = res?.dates ?? [];
|
||||||
|
const values = res?.values ?? [];
|
||||||
|
setTrendData(dates.map((date, i) => ({ date, cases: 0, risk: values[i] ?? 0 })));
|
||||||
|
setTrendLoaded(true);
|
||||||
|
})
|
||||||
|
.catch((err: unknown) => {
|
||||||
|
if (cancelled) return;
|
||||||
|
setTrendError(err instanceof Error ? err.message : '加载风险趋势失败');
|
||||||
|
})
|
||||||
|
.finally(() => {
|
||||||
|
if (!cancelled) setTrendLoading(false);
|
||||||
|
});
|
||||||
|
return () => { cancelled = true; };
|
||||||
|
}, [activeTab, trendLoaded]);
|
||||||
|
|
||||||
|
const extendedAlerts: ExtendedAlert[] = useMemo(() => {
|
||||||
|
const now = Date.now();
|
||||||
|
return (alerts || []).map((alert) => {
|
||||||
|
const forecastDate = new Date(alert.forecast_time);
|
||||||
|
const diffDays = Math.ceil((forecastDate.getTime() - now) / (1000 * 60 * 60 * 24));
|
||||||
|
const horizon = diffDays <= 1 ? 1 : diffDays <= 3 ? 3 : 7;
|
||||||
|
|
||||||
|
return {
|
||||||
|
...alert,
|
||||||
|
latitude: alert.latitude || 0,
|
||||||
|
longitude: alert.longitude || 0,
|
||||||
|
forecast_horizon: horizon,
|
||||||
|
};
|
||||||
|
});
|
||||||
|
}, [alerts]);
|
||||||
|
|
||||||
|
const filteredAlerts = useMemo(() => {
|
||||||
|
return extendedAlerts
|
||||||
|
.filter((alert) => {
|
||||||
|
const horizonMatch = selectedHorizon === 'all' || alert.forecast_horizon === selectedHorizon;
|
||||||
|
const priorityMatch = selectedPriority === 'all' || alert.priority === selectedPriority;
|
||||||
|
const riskMatch = alert.risk_value >= debouncedRiskRange[0] && alert.risk_value <= debouncedRiskRange[1];
|
||||||
|
return horizonMatch && priorityMatch && riskMatch;
|
||||||
|
})
|
||||||
|
.sort((a, b) => {
|
||||||
|
if (sortBy === 'risk') {
|
||||||
|
return b.risk_value - a.risk_value;
|
||||||
|
}
|
||||||
|
return new Date(b.forecast_time).getTime() - new Date(a.forecast_time).getTime();
|
||||||
|
});
|
||||||
|
}, [extendedAlerts, selectedHorizon, selectedPriority, sortBy, debouncedRiskRange]);
|
||||||
|
|
||||||
|
// Risk distribution stats (includes p1/p2 counts) — single pass over each array
|
||||||
|
const riskStats: RiskStats = useMemo(() => {
|
||||||
|
// p1/p2 reflect the full (unfiltered) alert set
|
||||||
|
let p1 = 0;
|
||||||
|
let p2 = 0;
|
||||||
|
for (const a of extendedAlerts) {
|
||||||
|
if (a.priority === 'P1') p1++;
|
||||||
|
else if (a.priority === 'P2') p2++;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Single pass over filteredAlerts: counters + sum + district map
|
||||||
|
let high = 0;
|
||||||
|
let mediumHigh = 0;
|
||||||
|
let medium = 0;
|
||||||
|
let sum = 0;
|
||||||
|
const byDistrict: Record<string, number> = {};
|
||||||
|
for (const a of filteredAlerts) {
|
||||||
|
const v = a.risk_value;
|
||||||
|
if (v >= 0.8) high++;
|
||||||
|
else if (v >= 0.6) mediumHigh++;
|
||||||
|
else if (v >= 0.4) medium++;
|
||||||
|
sum += v;
|
||||||
|
const d = a.region || '未知';
|
||||||
|
byDistrict[d] = (byDistrict[d] || 0) + 1;
|
||||||
|
}
|
||||||
|
const avgRisk = filteredAlerts.length > 0 ? sum / filteredAlerts.length : 0;
|
||||||
|
|
||||||
|
const topDistricts = Object.entries(byDistrict)
|
||||||
|
.sort((a, b) => b[1] - a[1])
|
||||||
|
.slice(0, 5);
|
||||||
|
|
||||||
|
return { p1, p2, high, mediumHigh, medium, avgRisk, topDistricts };
|
||||||
|
}, [extendedAlerts, filteredAlerts]);
|
||||||
|
|
||||||
|
// Export utilities
|
||||||
|
const exportToCsv = useCallback(() => {
|
||||||
|
const headers = ['alert_id', 'grid_id', 'region', 'street', 'latitude', 'longitude', 'risk_value', 'priority', 'forecast_horizon', 'reason', 'timestamp'];
|
||||||
|
const rows = filteredAlerts.map(a => [
|
||||||
|
a.alert_id, a.grid_id, a.region, a.street,
|
||||||
|
a.latitude, a.longitude, a.risk_value, a.priority,
|
||||||
|
a.forecast_horizon, `"${a.reason}"`, a.timestamp,
|
||||||
|
]);
|
||||||
|
const csv = [headers.join(','), ...rows.map(r => r.join(','))].join('\n');
|
||||||
|
const blob = new Blob(['' + csv], { type: 'text/csv;charset=utf-8;' });
|
||||||
|
const url = URL.createObjectURL(blob);
|
||||||
|
const a = document.createElement('a');
|
||||||
|
a.href = url;
|
||||||
|
a.download = `alerts_${new Date().toISOString().split('T')[0]}.csv`;
|
||||||
|
a.click();
|
||||||
|
URL.revokeObjectURL(url);
|
||||||
|
}, [filteredAlerts]);
|
||||||
|
|
||||||
|
const exportToJson = useCallback(() => {
|
||||||
|
const json = JSON.stringify(filteredAlerts, null, 2);
|
||||||
|
const blob = new Blob([json], { type: 'application/json' });
|
||||||
|
const url = URL.createObjectURL(blob);
|
||||||
|
const a = document.createElement('a');
|
||||||
|
a.href = url;
|
||||||
|
a.download = `alerts_${new Date().toISOString().split('T')[0]}.json`;
|
||||||
|
a.click();
|
||||||
|
URL.revokeObjectURL(url);
|
||||||
|
}, [filteredAlerts]);
|
||||||
|
|
||||||
|
return {
|
||||||
|
extendedAlerts,
|
||||||
|
filteredAlerts,
|
||||||
|
riskStats,
|
||||||
|
trendData,
|
||||||
|
trendLoading,
|
||||||
|
trendError,
|
||||||
|
exportToCsv,
|
||||||
|
exportToJson,
|
||||||
|
};
|
||||||
|
}
|
||||||
87
frontend/src/components/clinical/BoxPlotRows.tsx
Normal file
87
frontend/src/components/clinical/BoxPlotRows.tsx
Normal file
@@ -0,0 +1,87 @@
|
|||||||
|
import { memo } from 'react';
|
||||||
|
import { CLINICAL_COLORS } from './chartColors';
|
||||||
|
|
||||||
|
export interface BoxRow {
|
||||||
|
label: string;
|
||||||
|
p25: number;
|
||||||
|
median: number;
|
||||||
|
p75: number;
|
||||||
|
n: number;
|
||||||
|
}
|
||||||
|
|
||||||
|
interface BoxPlotRowsProps {
|
||||||
|
rows: BoxRow[];
|
||||||
|
/** 数值单位后缀,如 "天" / ""。 */
|
||||||
|
unit?: string;
|
||||||
|
/** 标签列宽(px)。 */
|
||||||
|
labelWidth?: number;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 横向箱线图(p25–中位–p75)。Recharts 无原生 box plot,
|
||||||
|
* 故用纯 div 渲染:每行一条从 p25 到 p75 的横条,中位处一根竖向刻度。
|
||||||
|
* 复用于「各病种住院天数」与「年龄别BMI」。
|
||||||
|
*/
|
||||||
|
export const BoxPlotRows = memo(function BoxPlotRows({
|
||||||
|
rows,
|
||||||
|
unit = '',
|
||||||
|
labelWidth = 96,
|
||||||
|
}: BoxPlotRowsProps) {
|
||||||
|
if (!rows || rows.length === 0) {
|
||||||
|
return <div className="text-center py-8 text-text-muted text-sm">暂无数据</div>;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 统一横轴域:覆盖所有行的 p25..p75,留一点边距。
|
||||||
|
const domainMin = Math.min(...rows.map((r) => r.p25));
|
||||||
|
const domainMax = Math.max(...rows.map((r) => r.p75));
|
||||||
|
const span = domainMax - domainMin || 1;
|
||||||
|
const pct = (v: number) => ((v - domainMin) / span) * 100;
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div className="space-y-2.5">
|
||||||
|
{rows.map((r) => {
|
||||||
|
const left = pct(r.p25);
|
||||||
|
const right = pct(r.p75);
|
||||||
|
const width = Math.max(right - left, 0.5);
|
||||||
|
const medianLeft = pct(r.median);
|
||||||
|
return (
|
||||||
|
<div key={r.label} className="flex items-center gap-2 text-[11px]">
|
||||||
|
<div
|
||||||
|
className="shrink-0 truncate text-text-secondary text-right"
|
||||||
|
style={{ width: labelWidth }}
|
||||||
|
title={r.label}
|
||||||
|
>
|
||||||
|
{r.label}
|
||||||
|
</div>
|
||||||
|
<div className="relative flex-1 h-5 rounded bg-bg-hover">
|
||||||
|
{/* p25–p75 箱体 */}
|
||||||
|
<div
|
||||||
|
className="absolute top-1 bottom-1 rounded-sm"
|
||||||
|
style={{
|
||||||
|
left: `${left}%`,
|
||||||
|
width: `${width}%`,
|
||||||
|
backgroundColor: CLINICAL_COLORS.box,
|
||||||
|
opacity: 0.35,
|
||||||
|
}}
|
||||||
|
/>
|
||||||
|
{/* 中位刻度 */}
|
||||||
|
<div
|
||||||
|
className="absolute top-0.5 bottom-0.5 w-[2px] rounded"
|
||||||
|
style={{
|
||||||
|
left: `${medianLeft}%`,
|
||||||
|
backgroundColor: CLINICAL_COLORS.boxMedian,
|
||||||
|
}}
|
||||||
|
title={`中位 ${r.median}${unit}`}
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
<div className="shrink-0 w-28 text-text-muted tabular-nums">
|
||||||
|
{r.p25}–<span className="font-semibold text-text-secondary">{r.median}</span>–{r.p75}
|
||||||
|
{unit}
|
||||||
|
<span className="ml-1 text-[10px] text-text-muted">n={r.n}</span>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
})}
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
});
|
||||||
42
frontend/src/components/clinical/ClinicalKpiRow.tsx
Normal file
42
frontend/src/components/clinical/ClinicalKpiRow.tsx
Normal file
@@ -0,0 +1,42 @@
|
|||||||
|
import { memo } from 'react';
|
||||||
|
import { Users, CalendarDays, HeartPulse, Siren } from 'lucide-react';
|
||||||
|
import { StatCard } from '@/components/StatCard';
|
||||||
|
import { TESTIDS } from '@/utils/testids';
|
||||||
|
import type { InpatientClinicalResponse } from '@/services/api';
|
||||||
|
|
||||||
|
interface ClinicalKpiRowProps {
|
||||||
|
kpis: InpatientClinicalResponse['kpis'];
|
||||||
|
}
|
||||||
|
|
||||||
|
/** 住院临床 4 项核心指标。375px 下 2 列,sm 起 4 列。 */
|
||||||
|
export const ClinicalKpiRow = memo(function ClinicalKpiRow({ kpis }: ClinicalKpiRowProps) {
|
||||||
|
return (
|
||||||
|
<div
|
||||||
|
data-testid={TESTIDS.clinicalKpis}
|
||||||
|
className="grid grid-cols-2 sm:grid-cols-4 gap-3"
|
||||||
|
>
|
||||||
|
<StatCard
|
||||||
|
icon={<Users className="w-4 h-4 text-primary" />}
|
||||||
|
label="住院总人次"
|
||||||
|
value={kpis.total_admissions.toLocaleString()}
|
||||||
|
/>
|
||||||
|
<StatCard
|
||||||
|
icon={<CalendarDays className="w-4 h-4 text-primary" />}
|
||||||
|
label="中位住院日"
|
||||||
|
value={`${kpis.median_los_days} 天`}
|
||||||
|
/>
|
||||||
|
<StatCard
|
||||||
|
icon={<HeartPulse className="w-4 h-4 text-success" />}
|
||||||
|
label="治愈好转率"
|
||||||
|
value={`${(kpis.cure_rate * 100).toFixed(1)}%`}
|
||||||
|
color="#16A34A"
|
||||||
|
/>
|
||||||
|
<StatCard
|
||||||
|
icon={<Siren className="w-4 h-4 text-warning" />}
|
||||||
|
label="急诊入院占比"
|
||||||
|
value={`${(kpis.emergency_admit_ratio * 100).toFixed(1)}%`}
|
||||||
|
color="#D97706"
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
});
|
||||||
56
frontend/src/components/clinical/DonutChart.tsx
Normal file
56
frontend/src/components/clinical/DonutChart.tsx
Normal file
@@ -0,0 +1,56 @@
|
|||||||
|
import { memo } from 'react';
|
||||||
|
import { PieChart, Pie, Cell, Tooltip, Legend, ResponsiveContainer } from 'recharts';
|
||||||
|
import { CLINICAL_COLORS, TOOLTIP_STYLE } from './chartColors';
|
||||||
|
|
||||||
|
export interface DonutSlice {
|
||||||
|
name: string;
|
||||||
|
value: number;
|
||||||
|
}
|
||||||
|
|
||||||
|
interface DonutChartProps {
|
||||||
|
data: DonutSlice[];
|
||||||
|
/** name -> color。未命中时按 palette 顺序回退。 */
|
||||||
|
colorMap?: Record<string, string>;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** 通用环形图。复用于「出院结局构成」与「入院途径构成」。 */
|
||||||
|
export const DonutChart = memo(function DonutChart({ data, colorMap }: DonutChartProps) {
|
||||||
|
if (!data || data.length === 0) {
|
||||||
|
return <div className="text-center py-8 text-text-muted text-sm">暂无数据</div>;
|
||||||
|
}
|
||||||
|
|
||||||
|
const total = data.reduce((s, d) => s + d.value, 0);
|
||||||
|
const colorFor = (name: string, idx: number) =>
|
||||||
|
colorMap?.[name] ??
|
||||||
|
CLINICAL_COLORS.routePalette[idx % CLINICAL_COLORS.routePalette.length] ??
|
||||||
|
CLINICAL_COLORS.outcomeFallback;
|
||||||
|
|
||||||
|
return (
|
||||||
|
<ResponsiveContainer width="100%" height={280}>
|
||||||
|
<PieChart>
|
||||||
|
<Pie
|
||||||
|
data={data}
|
||||||
|
dataKey="value"
|
||||||
|
nameKey="name"
|
||||||
|
cx="50%"
|
||||||
|
cy="50%"
|
||||||
|
innerRadius={56}
|
||||||
|
outerRadius={88}
|
||||||
|
paddingAngle={2}
|
||||||
|
>
|
||||||
|
{data.map((d, idx) => (
|
||||||
|
<Cell key={d.name} fill={colorFor(d.name, idx)} />
|
||||||
|
))}
|
||||||
|
</Pie>
|
||||||
|
<Tooltip
|
||||||
|
contentStyle={TOOLTIP_STYLE}
|
||||||
|
formatter={(v: number, name: string) => [
|
||||||
|
`${v.toLocaleString()}(${total > 0 ? ((v / total) * 100).toFixed(1) : '0'}%)`,
|
||||||
|
name,
|
||||||
|
]}
|
||||||
|
/>
|
||||||
|
<Legend wrapperStyle={{ fontSize: '11px' }} />
|
||||||
|
</PieChart>
|
||||||
|
</ResponsiveContainer>
|
||||||
|
);
|
||||||
|
});
|
||||||
51
frontend/src/components/clinical/HistogramChart.tsx
Normal file
51
frontend/src/components/clinical/HistogramChart.tsx
Normal file
@@ -0,0 +1,51 @@
|
|||||||
|
import { memo } from 'react';
|
||||||
|
import {
|
||||||
|
BarChart,
|
||||||
|
Bar,
|
||||||
|
XAxis,
|
||||||
|
YAxis,
|
||||||
|
CartesianGrid,
|
||||||
|
Tooltip,
|
||||||
|
ResponsiveContainer,
|
||||||
|
} from 'recharts';
|
||||||
|
import { CLINICAL_COLORS, TOOLTIP_STYLE } from './chartColors';
|
||||||
|
|
||||||
|
interface HistogramChartProps {
|
||||||
|
data: { bin_label: string; count: number }[];
|
||||||
|
color?: string;
|
||||||
|
/** tooltip 中数量的标签,如 "住院天数"。 */
|
||||||
|
countLabel?: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** 通用直方图。用于「住院天数分布」。 */
|
||||||
|
export const HistogramChart = memo(function HistogramChart({
|
||||||
|
data,
|
||||||
|
color = CLINICAL_COLORS.los,
|
||||||
|
countLabel = '人次',
|
||||||
|
}: HistogramChartProps) {
|
||||||
|
if (!data || data.length === 0) {
|
||||||
|
return <div className="text-center py-8 text-text-muted text-sm">暂无数据</div>;
|
||||||
|
}
|
||||||
|
|
||||||
|
return (
|
||||||
|
<ResponsiveContainer width="100%" height={300}>
|
||||||
|
<BarChart data={data} margin={{ top: 5, right: 12, left: 0, bottom: 5 }}>
|
||||||
|
<CartesianGrid strokeDasharray="3 3" stroke={CLINICAL_COLORS.grid} vertical={false} />
|
||||||
|
<XAxis
|
||||||
|
dataKey="bin_label"
|
||||||
|
tick={{ fontSize: 10, fill: CLINICAL_COLORS.axis }}
|
||||||
|
interval={0}
|
||||||
|
angle={-30}
|
||||||
|
textAnchor="end"
|
||||||
|
height={50}
|
||||||
|
/>
|
||||||
|
<YAxis tick={{ fontSize: 10, fill: CLINICAL_COLORS.axis }} width={40} />
|
||||||
|
<Tooltip
|
||||||
|
contentStyle={TOOLTIP_STYLE}
|
||||||
|
formatter={(v: number) => [`${v.toLocaleString()}`, countLabel]}
|
||||||
|
/>
|
||||||
|
<Bar dataKey="count" fill={color} radius={[3, 3, 0, 0]} />
|
||||||
|
</BarChart>
|
||||||
|
</ResponsiveContainer>
|
||||||
|
);
|
||||||
|
});
|
||||||
34
frontend/src/components/clinical/chartColors.ts
Normal file
34
frontend/src/components/clinical/chartColors.ts
Normal file
@@ -0,0 +1,34 @@
|
|||||||
|
/**
|
||||||
|
* 住院临床分析页图表字面色值集中处。
|
||||||
|
* Recharts 需要原始 hex,无法用 Tailwind class,故在此集中定义,避免散落 magic hex。
|
||||||
|
*/
|
||||||
|
export const CLINICAL_COLORS = {
|
||||||
|
primary: '#2563EB', // primary
|
||||||
|
los: '#2563EB',
|
||||||
|
box: '#3B82F6', // 箱体填充
|
||||||
|
boxMedian: '#1D4ED8', // 中位刻度
|
||||||
|
grid: '#E2E8F0',
|
||||||
|
axis: '#64748B',
|
||||||
|
axisLabel: '#374151',
|
||||||
|
tooltipBorder: '#E2E8F0',
|
||||||
|
tooltipText: '#1E293B',
|
||||||
|
// 出院结局按严重程度配色:治愈/好转偏绿,未愈/死亡偏红,其他中性
|
||||||
|
outcome: {
|
||||||
|
治愈: '#16A34A',
|
||||||
|
好转: '#4ADE80',
|
||||||
|
其他: '#94A3B8',
|
||||||
|
未愈: '#F97316',
|
||||||
|
死亡: '#DC2626',
|
||||||
|
} as Record<string, string>,
|
||||||
|
outcomeFallback: '#94A3B8',
|
||||||
|
// 入院途径 donut 顺序色板
|
||||||
|
routePalette: ['#2563EB', '#0891B2', '#7C3AED', '#D97706', '#16A34A', '#DC2626'],
|
||||||
|
} as const;
|
||||||
|
|
||||||
|
/** Recharts tooltip 通用样式。 */
|
||||||
|
export const TOOLTIP_STYLE = {
|
||||||
|
backgroundColor: '#FFFFFF',
|
||||||
|
border: `1px solid ${CLINICAL_COLORS.tooltipBorder}`,
|
||||||
|
borderRadius: '8px',
|
||||||
|
fontSize: '12px',
|
||||||
|
} as const;
|
||||||
145
frontend/src/components/monitoring/CaseStatsTab.tsx
Normal file
145
frontend/src/components/monitoring/CaseStatsTab.tsx
Normal file
@@ -0,0 +1,145 @@
|
|||||||
|
import { memo } from 'react';
|
||||||
|
import {
|
||||||
|
LineChart,
|
||||||
|
Line,
|
||||||
|
BarChart,
|
||||||
|
Bar,
|
||||||
|
XAxis,
|
||||||
|
YAxis,
|
||||||
|
CartesianGrid,
|
||||||
|
Tooltip,
|
||||||
|
Legend,
|
||||||
|
ResponsiveContainer,
|
||||||
|
} from 'recharts';
|
||||||
|
import { ErrorBanner } from '@/components/ErrorBanner';
|
||||||
|
import { CalendarHeatmap } from '@/components/CalendarHeatmap';
|
||||||
|
import type { TopDiagnosis } from './types';
|
||||||
|
|
||||||
|
function formatDateLabel(dateStr: string): string {
|
||||||
|
const d = new Date(dateStr);
|
||||||
|
return `${d.getMonth() + 1}/${d.getDate()}`;
|
||||||
|
}
|
||||||
|
|
||||||
|
interface CaseStatsTabProps {
|
||||||
|
loading: boolean;
|
||||||
|
loaded: boolean;
|
||||||
|
error: string | null;
|
||||||
|
currentDate: string;
|
||||||
|
topDiagnoses: TopDiagnosis[];
|
||||||
|
caseTrend: Array<{ date: string; cases: number; aqi: number }>;
|
||||||
|
heatmapData: Array<{ date: string; value: number }>;
|
||||||
|
heatmapYear: number | null;
|
||||||
|
onRetry: () => void;
|
||||||
|
onDismissError: () => void;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 病例统计 tab —— 诊断分布 / 病例与AQI趋势 / 日历热力图。纯展示,数据由父级按需加载。
|
||||||
|
export const CaseStatsTab = memo(function CaseStatsTab({
|
||||||
|
loading,
|
||||||
|
loaded,
|
||||||
|
error,
|
||||||
|
currentDate,
|
||||||
|
topDiagnoses,
|
||||||
|
caseTrend,
|
||||||
|
heatmapData,
|
||||||
|
heatmapYear,
|
||||||
|
onRetry,
|
||||||
|
onDismissError,
|
||||||
|
}: CaseStatsTabProps) {
|
||||||
|
if (loading && !loaded) {
|
||||||
|
return (
|
||||||
|
<div className="flex items-center justify-center h-64">
|
||||||
|
<div className="animate-spin rounded-full h-8 w-8 border-b-2 border-blue-600"></div>
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div className="space-y-6">
|
||||||
|
{error && (
|
||||||
|
<ErrorBanner
|
||||||
|
error={error}
|
||||||
|
onRetry={onRetry}
|
||||||
|
onDismiss={onDismissError}
|
||||||
|
/>
|
||||||
|
)}
|
||||||
|
|
||||||
|
{/* Top 5 诊断分布 */}
|
||||||
|
<div className="bg-white rounded-lg shadow-sm border border-gray-200 p-4">
|
||||||
|
<h3 className="text-lg font-semibold text-gray-900 mb-4">Top 5 诊断分布</h3>
|
||||||
|
{topDiagnoses.length > 0 ? (
|
||||||
|
<ResponsiveContainer width="100%" height={240}>
|
||||||
|
<BarChart
|
||||||
|
data={[...topDiagnoses].reverse()}
|
||||||
|
layout="vertical"
|
||||||
|
margin={{ top: 0, right: 10, left: 60, bottom: 0 }}
|
||||||
|
>
|
||||||
|
<CartesianGrid strokeDasharray="3 3" stroke="#E2E8F0" horizontal={false} />
|
||||||
|
<XAxis type="number" tick={{ fontSize: 10, fill: '#64748B' }} />
|
||||||
|
<YAxis
|
||||||
|
type="category"
|
||||||
|
dataKey="diagnosis"
|
||||||
|
tick={{ fontSize: 11, fill: '#374151' }}
|
||||||
|
width={100}
|
||||||
|
axisLine={false}
|
||||||
|
tickLine={false}
|
||||||
|
/>
|
||||||
|
<Tooltip
|
||||||
|
contentStyle={{ backgroundColor: '#FFFFFF', border: '1px solid #E2E8F0', borderRadius: '8px', fontSize: '12px' }}
|
||||||
|
formatter={(value: number) => [value.toLocaleString(), '病例数']}
|
||||||
|
/>
|
||||||
|
<Legend wrapperStyle={{ fontSize: '11px' }} />
|
||||||
|
<Bar dataKey="outpatient" stackId="a" fill="#3B82F6" name="门诊" barSize={16} />
|
||||||
|
<Bar dataKey="inpatient" stackId="a" fill="#EF4444" name="住院" barSize={16} />
|
||||||
|
</BarChart>
|
||||||
|
</ResponsiveContainer>
|
||||||
|
) : (
|
||||||
|
<div className="text-center py-8 text-gray-400 text-sm">暂无数据</div>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
|
||||||
|
{/* 近30日病例与AQI趋势 (driven off Monitoring timeline currentDate) */}
|
||||||
|
<div className="bg-white rounded-lg shadow-sm border border-gray-200 p-4">
|
||||||
|
<h3 className="text-lg font-semibold text-gray-900 mb-1">病例与AQI趋势</h3>
|
||||||
|
<p className="text-xs text-gray-500 mb-4">截至 {currentDate} 的近30日窗口</p>
|
||||||
|
{caseTrend.length > 0 ? (
|
||||||
|
<ResponsiveContainer width="100%" height={260}>
|
||||||
|
<LineChart data={caseTrend} margin={{ top: 5, right: 10, left: 0, bottom: 5 }}>
|
||||||
|
<CartesianGrid strokeDasharray="3 3" stroke="#E2E8F0" />
|
||||||
|
<XAxis
|
||||||
|
dataKey="date"
|
||||||
|
tickFormatter={formatDateLabel}
|
||||||
|
tick={{ fontSize: 10, fill: '#64748B' }}
|
||||||
|
interval="preserveStartEnd"
|
||||||
|
axisLine={{ stroke: '#E2E8F0' }}
|
||||||
|
/>
|
||||||
|
<YAxis yAxisId="left" tick={{ fontSize: 10, fill: '#64748B' }} axisLine={{ stroke: '#E2E8F0' }} />
|
||||||
|
<YAxis yAxisId="right" orientation="right" tick={{ fontSize: 10, fill: '#F59E0B' }} axisLine={{ stroke: '#E2E8F0' }} />
|
||||||
|
<Tooltip
|
||||||
|
contentStyle={{ backgroundColor: '#FFFFFF', border: '1px solid #E2E8F0', borderRadius: '8px', fontSize: '12px' }}
|
||||||
|
labelStyle={{ color: '#1E293B', fontWeight: 600 }}
|
||||||
|
/>
|
||||||
|
<Legend wrapperStyle={{ fontSize: '11px' }} />
|
||||||
|
<Line yAxisId="left" type="monotone" dataKey="cases" name="病例数" stroke="#3B82F6" strokeWidth={2} dot={false} activeDot={{ r: 3 }} />
|
||||||
|
<Line yAxisId="right" type="monotone" dataKey="aqi" name="AQI" stroke="#F59E0B" strokeWidth={2} strokeDasharray="5 5" dot={false} activeDot={{ r: 3 }} />
|
||||||
|
</LineChart>
|
||||||
|
</ResponsiveContainer>
|
||||||
|
) : (
|
||||||
|
<div className="text-center py-8 text-gray-400 text-sm">暂无数据</div>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
|
||||||
|
{/* 日历热力图 (year derived from data) */}
|
||||||
|
<div className="bg-white rounded-lg shadow-sm border border-gray-200 p-4">
|
||||||
|
<h3 className="text-lg font-semibold text-gray-900 mb-4">
|
||||||
|
{heatmapYear ? `${heatmapYear}年 ` : ''}每日病例日历
|
||||||
|
</h3>
|
||||||
|
{heatmapYear && heatmapData.length > 0 ? (
|
||||||
|
<CalendarHeatmap data={heatmapData} year={heatmapYear} />
|
||||||
|
) : (
|
||||||
|
<div className="text-center py-8 text-gray-400 text-sm">暂无数据</div>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
});
|
||||||
66
frontend/src/components/monitoring/DistrictStatsTab.tsx
Normal file
66
frontend/src/components/monitoring/DistrictStatsTab.tsx
Normal file
@@ -0,0 +1,66 @@
|
|||||||
|
import { memo } from 'react';
|
||||||
|
import { ErrorBanner } from '@/components/ErrorBanner';
|
||||||
|
import { MetricHeatmapTable } from '@/components/MetricHeatmapTable';
|
||||||
|
|
||||||
|
interface DistrictStatsTabProps {
|
||||||
|
loading: boolean;
|
||||||
|
loaded: boolean;
|
||||||
|
error: string | null;
|
||||||
|
rows: string[];
|
||||||
|
data: Record<string, Record<string, number>>;
|
||||||
|
onRetry: () => void;
|
||||||
|
onDismissError: () => void;
|
||||||
|
onSort: (col: string) => void;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 区域统计 tab —— 区域指标热力表。纯展示,排序键由父级持有。
|
||||||
|
export const DistrictStatsTab = memo(function DistrictStatsTab({
|
||||||
|
loading,
|
||||||
|
loaded,
|
||||||
|
error,
|
||||||
|
rows,
|
||||||
|
data,
|
||||||
|
onRetry,
|
||||||
|
onDismissError,
|
||||||
|
onSort,
|
||||||
|
}: DistrictStatsTabProps) {
|
||||||
|
if (loading && !loaded) {
|
||||||
|
return (
|
||||||
|
<div className="flex items-center justify-center h-64">
|
||||||
|
<div className="animate-spin rounded-full h-8 w-8 border-b-2 border-blue-600"></div>
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div className="space-y-6">
|
||||||
|
{error && (
|
||||||
|
<ErrorBanner
|
||||||
|
error={error}
|
||||||
|
onRetry={onRetry}
|
||||||
|
onDismiss={onDismissError}
|
||||||
|
/>
|
||||||
|
)}
|
||||||
|
|
||||||
|
<div className="bg-white rounded-lg shadow-sm border border-gray-200 p-4">
|
||||||
|
<h3 className="text-lg font-semibold text-gray-900 mb-1">区域指标热力表</h3>
|
||||||
|
<p className="text-xs text-gray-500 mb-4">点击列标题排序</p>
|
||||||
|
{rows.length > 0 ? (
|
||||||
|
<MetricHeatmapTable
|
||||||
|
rows={rows}
|
||||||
|
columns={[
|
||||||
|
{ key: 'total', label: '病例' },
|
||||||
|
{ key: 'outpatient', label: '门诊' },
|
||||||
|
{ key: 'inpatient', label: '住院' },
|
||||||
|
{ key: 'inpatient_ratio', label: '住院占比%' },
|
||||||
|
]}
|
||||||
|
data={data}
|
||||||
|
onSort={onSort}
|
||||||
|
/>
|
||||||
|
) : (
|
||||||
|
<div className="text-center py-8 text-gray-400 text-sm">暂无数据</div>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
});
|
||||||
56
frontend/src/components/monitoring/MonitoringStatsBar.tsx
Normal file
56
frontend/src/components/monitoring/MonitoringStatsBar.tsx
Normal file
@@ -0,0 +1,56 @@
|
|||||||
|
import { memo } from 'react';
|
||||||
|
import { Calendar, Activity, TrendingUp, TrendingDown, Stethoscope, Zap, BarChart3 } from 'lucide-react';
|
||||||
|
import { StatCard } from '@/components/StatCard';
|
||||||
|
import type { MonitoringStats } from './types';
|
||||||
|
|
||||||
|
interface MonitoringStatsBarProps {
|
||||||
|
stats: MonitoringStats;
|
||||||
|
sparkline7d: number[];
|
||||||
|
}
|
||||||
|
|
||||||
|
// 监测页顶部统计条 —— 纯展示,已自适应(grid-cols-2 sm:grid-cols-3 lg:grid-cols-6)。
|
||||||
|
export const MonitoringStatsBar = memo(function MonitoringStatsBar({ stats, sparkline7d }: MonitoringStatsBarProps) {
|
||||||
|
return (
|
||||||
|
<div className="flex-1 min-w-0 grid grid-cols-2 sm:grid-cols-3 lg:grid-cols-6 gap-3">
|
||||||
|
<StatCard
|
||||||
|
icon={<Calendar className="w-4 h-4 text-blue-600" />}
|
||||||
|
label="当日病例"
|
||||||
|
value={stats.todayCases !== null ? stats.todayCases.toLocaleString() : '--'}
|
||||||
|
/>
|
||||||
|
<StatCard
|
||||||
|
icon={<Activity className="w-4 h-4 text-indigo-600" />}
|
||||||
|
label="7日均值"
|
||||||
|
value={stats.avg7d.toLocaleString()}
|
||||||
|
sparkline={sparkline7d.length >= 2 ? { data: sparkline7d, color: '#6366F1' } : undefined}
|
||||||
|
/>
|
||||||
|
<StatCard
|
||||||
|
icon={
|
||||||
|
stats.trend === 'up' ? <TrendingUp className="w-4 h-4 text-red-500" /> :
|
||||||
|
stats.trend === 'down' ? <TrendingDown className="w-4 h-4 text-green-500" /> :
|
||||||
|
<Activity className="w-4 h-4 text-gray-400" />
|
||||||
|
}
|
||||||
|
label="趋势"
|
||||||
|
value={stats.trend === 'up' ? '上升' : stats.trend === 'down' ? '下降' : '平稳'}
|
||||||
|
trend={{
|
||||||
|
direction: stats.trend === 'up' ? 'up' : stats.trend === 'down' ? 'down' : 'stable',
|
||||||
|
value: stats.trend === 'up' ? '上升' : stats.trend === 'down' ? '下降' : '平稳',
|
||||||
|
}}
|
||||||
|
/>
|
||||||
|
<StatCard
|
||||||
|
icon={<Zap className="w-4 h-4 text-amber-500" />}
|
||||||
|
label="峰值日"
|
||||||
|
value={`${stats.maxDay.cases.toLocaleString()} (${stats.maxDay.date.slice(5)})`}
|
||||||
|
/>
|
||||||
|
<StatCard
|
||||||
|
icon={<BarChart3 className="w-4 h-4 text-purple-500" />}
|
||||||
|
label="标准差"
|
||||||
|
value={stats.stdDev.toLocaleString()}
|
||||||
|
/>
|
||||||
|
<StatCard
|
||||||
|
icon={<Stethoscope className="w-4 h-4 text-orange-500" />}
|
||||||
|
label="门诊 / 住院"
|
||||||
|
value={`${stats.totalOutpatient.toLocaleString()} / ${stats.totalInpatient.toLocaleString()}`}
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
});
|
||||||
142
frontend/src/components/monitoring/OverviewTab.tsx
Normal file
142
frontend/src/components/monitoring/OverviewTab.tsx
Normal file
@@ -0,0 +1,142 @@
|
|||||||
|
import { memo, useMemo, useCallback } from 'react';
|
||||||
|
import { StatisticalCharts } from '@/components/StatisticalCharts';
|
||||||
|
import { CaseLocationMap } from '@/components/CaseLocationMap';
|
||||||
|
import { Segmented } from '@/components/ui';
|
||||||
|
import { TESTIDS } from '@/utils/testids';
|
||||||
|
import type { Granularity, DistrictCaseRow } from './types';
|
||||||
|
|
||||||
|
interface OverviewTabProps {
|
||||||
|
isLoading: boolean;
|
||||||
|
chartData: Array<{ date: string; cases: number; aqi?: number }>;
|
||||||
|
districtCases: DistrictCaseRow[];
|
||||||
|
selectedDistrict: string | null;
|
||||||
|
selectedStreet: string | null;
|
||||||
|
currentDate: string;
|
||||||
|
granularity: Granularity;
|
||||||
|
onGranularityChange: (g: Granularity) => void;
|
||||||
|
onDistrictSelect: (district: string) => void;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 概览 tab —— 病例分布地图 + 统计图表 + 区县 roll-up(粒度真相来源在父级 URL)。
|
||||||
|
export const OverviewTab = memo(function OverviewTab({
|
||||||
|
isLoading,
|
||||||
|
chartData,
|
||||||
|
districtCases,
|
||||||
|
selectedDistrict,
|
||||||
|
selectedStreet,
|
||||||
|
currentDate,
|
||||||
|
granularity,
|
||||||
|
onGranularityChange,
|
||||||
|
onDistrictSelect,
|
||||||
|
}: OverviewTabProps) {
|
||||||
|
if (isLoading) {
|
||||||
|
return (
|
||||||
|
<div className="flex items-center justify-center h-64">
|
||||||
|
<div className="animate-spin rounded-full h-8 w-8 border-b-2 border-blue-600"></div>
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div className="space-y-6">
|
||||||
|
{/* Case Location Map */}
|
||||||
|
<div className="bg-white rounded-lg shadow-sm border border-gray-200 p-4">
|
||||||
|
<h3 className="text-lg font-semibold text-gray-900 mb-4">病例分布地图</h3>
|
||||||
|
<CaseLocationMap height="400px" district={selectedDistrict} street={selectedStreet} date={currentDate} />
|
||||||
|
</div>
|
||||||
|
|
||||||
|
{/* Statistical Charts */}
|
||||||
|
<StatisticalCharts
|
||||||
|
data={chartData}
|
||||||
|
height={350}
|
||||||
|
showCases={true}
|
||||||
|
showAQI={true}
|
||||||
|
/>
|
||||||
|
|
||||||
|
{/* District breakdown — 区域 roll-up(URL 粒度真相来源) */}
|
||||||
|
<div data-testid={TESTIDS.districtRollup} className="bg-white rounded-lg shadow-sm border border-gray-200 p-4">
|
||||||
|
<div className="flex items-center justify-between mb-4">
|
||||||
|
<h3 className="text-lg font-semibold text-gray-900">区县病例分布</h3>
|
||||||
|
<Segmented<Granularity>
|
||||||
|
testid={TESTIDS.granularityControl}
|
||||||
|
size="sm"
|
||||||
|
options={[
|
||||||
|
{ value: 'city', label: '全市' },
|
||||||
|
{ value: 'district', label: '区域' },
|
||||||
|
{ value: 'street', label: '街道' },
|
||||||
|
]}
|
||||||
|
value={granularity}
|
||||||
|
onChange={onGranularityChange}
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
<div className="space-y-2">
|
||||||
|
<DistrictBreakdown
|
||||||
|
districtCases={districtCases}
|
||||||
|
selectedDistrict={selectedDistrict}
|
||||||
|
onDistrictSelect={onDistrictSelect}
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
<div className="flex items-center gap-4 mt-3 pt-2 border-t border-gray-100">
|
||||||
|
<div className="flex items-center gap-1.5 text-xs text-gray-500">
|
||||||
|
<span className="w-3 h-3 bg-orange-400 rounded-sm" />门诊
|
||||||
|
</div>
|
||||||
|
<div className="flex items-center gap-1.5 text-xs text-gray-500">
|
||||||
|
<span className="w-3 h-3 bg-red-400 rounded-sm" />住院
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
interface DistrictBreakdownProps {
|
||||||
|
districtCases: DistrictCaseRow[];
|
||||||
|
selectedDistrict: string | null;
|
||||||
|
// 点击区域条目时上抛——由父组件驱动 URL(粒度真相来源),不在此处 mutate store。
|
||||||
|
onDistrictSelect: (district: string) => void;
|
||||||
|
}
|
||||||
|
|
||||||
|
const DistrictBreakdown = memo(function DistrictBreakdown({ districtCases, selectedDistrict, onDistrictSelect }: DistrictBreakdownProps) {
|
||||||
|
const sortedCases = useMemo(() => [...districtCases].sort((a, b) => b.total - a.total), [districtCases]);
|
||||||
|
const maxTotal = useMemo(() => sortedCases.length > 0 ? sortedCases[0].total : 1, [sortedCases]);
|
||||||
|
|
||||||
|
const handleDistrictClick = useCallback((district: string) => {
|
||||||
|
onDistrictSelect(district);
|
||||||
|
}, [onDistrictSelect]);
|
||||||
|
|
||||||
|
return (
|
||||||
|
<>
|
||||||
|
{sortedCases.map((d) => {
|
||||||
|
const outPct = d.total > 0 ? (d.outpatient / d.total) * 100 : 0;
|
||||||
|
const inPct = d.total > 0 ? (d.inpatient / d.total) * 100 : 0;
|
||||||
|
const barWidth = (d.total / maxTotal) * 100;
|
||||||
|
return (
|
||||||
|
<div
|
||||||
|
key={d.district}
|
||||||
|
className={`flex items-center gap-3 p-2 rounded cursor-pointer transition-colors ${
|
||||||
|
selectedDistrict === d.district ? 'bg-blue-50' : 'hover:bg-gray-50'
|
||||||
|
}`}
|
||||||
|
onClick={() => handleDistrictClick(d.district)}
|
||||||
|
>
|
||||||
|
<div className="w-16 text-sm text-gray-700 text-right shrink-0">{d.district}</div>
|
||||||
|
<div className="flex-1 h-6 bg-gray-100 rounded overflow-hidden flex">
|
||||||
|
<div
|
||||||
|
className="bg-orange-400 h-full transition-all"
|
||||||
|
style={{ width: `${barWidth * outPct / 100}%` }}
|
||||||
|
title={`门诊: ${d.outpatient.toLocaleString()}`}
|
||||||
|
/>
|
||||||
|
<div
|
||||||
|
className="bg-red-400 h-full transition-all"
|
||||||
|
style={{ width: `${barWidth * inPct / 100}%` }}
|
||||||
|
title={`住院: ${d.inpatient.toLocaleString()}`}
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
<div className="w-20 text-right text-sm font-medium text-gray-900 shrink-0">
|
||||||
|
{d.total.toLocaleString()}
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
})}
|
||||||
|
</>
|
||||||
|
);
|
||||||
|
});
|
||||||
38
frontend/src/components/monitoring/types.ts
Normal file
38
frontend/src/components/monitoring/types.ts
Normal file
@@ -0,0 +1,38 @@
|
|||||||
|
// 监测页内部共享类型。Granularity 的真相来源仍是 URL,由 MonitoringDashboard 拥有;
|
||||||
|
// 此处只暴露类型与子组件复用的 props 形状。
|
||||||
|
export type Granularity = 'city' | 'district' | 'street';
|
||||||
|
|
||||||
|
export const GRANULARITY_VALUES: readonly Granularity[] = ['city', 'district', 'street'] as const;
|
||||||
|
|
||||||
|
export function parseGranularity(raw: string | null): Granularity {
|
||||||
|
return GRANULARITY_VALUES.includes(raw as Granularity) ? (raw as Granularity) : 'city';
|
||||||
|
}
|
||||||
|
|
||||||
|
// 概览 tab 区县条目所需的最小字段(来自 monitoringStore 的 districtCases)。
|
||||||
|
export interface DistrictCaseRow {
|
||||||
|
district: string;
|
||||||
|
total: number;
|
||||||
|
outpatient: number;
|
||||||
|
inpatient: number;
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface MonitoringStats {
|
||||||
|
totalCases: number;
|
||||||
|
avgCases: number;
|
||||||
|
maxDay: { date: string; cases: number };
|
||||||
|
minDay: { date: string; cases: number };
|
||||||
|
stdDev: number;
|
||||||
|
trend: 'up' | 'down' | 'stable';
|
||||||
|
totalOutpatient: number;
|
||||||
|
totalInpatient: number;
|
||||||
|
avg7d: number;
|
||||||
|
todayCases: number | null;
|
||||||
|
noData: boolean;
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface TopDiagnosis {
|
||||||
|
diagnosis: string;
|
||||||
|
outpatient: number;
|
||||||
|
inpatient: number;
|
||||||
|
total: number;
|
||||||
|
}
|
||||||
317
frontend/src/components/monitoring/useMonitoringData.ts
Normal file
317
frontend/src/components/monitoring/useMonitoringData.ts
Normal file
@@ -0,0 +1,317 @@
|
|||||||
|
import { useEffect, useState, useMemo, useRef, useCallback } from 'react';
|
||||||
|
import { useMonitoringStore } from '@/stores';
|
||||||
|
import { useDiseaseStore } from '@/stores/diseaseStore';
|
||||||
|
import { gridApi, caseApi, envApi } from '@/services/api';
|
||||||
|
import type { DistrictCaseData } from '@/types';
|
||||||
|
import type { MonitoringStats, TopDiagnosis } from './types';
|
||||||
|
|
||||||
|
type MonitoringTab = 'overview' | 'cases' | 'districts';
|
||||||
|
|
||||||
|
interface UseMonitoringDataArgs {
|
||||||
|
activeTab: MonitoringTab;
|
||||||
|
currentDate: string;
|
||||||
|
selectedDistrict: string | null;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 监测页数据层:图表 90 天窗口、病例统计/区域统计两个按需 tab 的加载与派生。
|
||||||
|
// 不触碰 URL/drilldown(粒度真相来源仍由 MonitoringDashboard 持有),只消费 currentDate 与
|
||||||
|
// selectedDistrict 作为入参,避免把 store-mutation 逻辑下沉到子组件。
|
||||||
|
export function useMonitoringData({ activeTab, currentDate, selectedDistrict }: UseMonitoringDataArgs) {
|
||||||
|
const [chartData, setChartData] = useState<Array<{ date: string; cases: number; aqi?: number }>>([]);
|
||||||
|
|
||||||
|
// --- 病例统计 tab state (fetched on demand) ---
|
||||||
|
const [topDiagnoses, setTopDiagnoses] = useState<TopDiagnosis[]>([]);
|
||||||
|
const [caseTrend, setCaseTrend] = useState<Array<{ date: string; cases: number; aqi: number }>>([]);
|
||||||
|
const [heatmapData, setHeatmapData] = useState<Array<{ date: string; value: number }>>([]);
|
||||||
|
const [heatmapYear, setHeatmapYear] = useState<number | null>(null);
|
||||||
|
const [casesTabLoaded, setCasesTabLoaded] = useState(false);
|
||||||
|
const [casesTabLoading, setCasesTabLoading] = useState(false);
|
||||||
|
const [casesTabError, setCasesTabError] = useState<string | null>(null);
|
||||||
|
|
||||||
|
// --- 区域统计 tab state (fetched on demand) ---
|
||||||
|
const [districtMetrics, setDistrictMetrics] = useState<DistrictCaseData[]>([]);
|
||||||
|
const [districtSortKey, setDistrictSortKey] = useState<string>('total');
|
||||||
|
const [districtTabLoaded, setDistrictTabLoaded] = useState(false);
|
||||||
|
const [districtTabLoading, setDistrictTabLoading] = useState(false);
|
||||||
|
const [districtTabError, setDistrictTabError] = useState<string | null>(null);
|
||||||
|
|
||||||
|
const districtCases = useMonitoringStore((s) => s.districtCases);
|
||||||
|
const fetchDistrictCases = useMonitoringStore((s) => s.fetchDistrictCases);
|
||||||
|
const { selectedDiagnoses } = useDiseaseStore();
|
||||||
|
|
||||||
|
const debounceRef = useRef<ReturnType<typeof setTimeout> | null>(null);
|
||||||
|
|
||||||
|
// Load chart data for 90-day window ending at the given reference date
|
||||||
|
const loadChartData = useCallback((refDate: string, district?: string) => {
|
||||||
|
const end = new Date(refDate);
|
||||||
|
const start = new Date(refDate);
|
||||||
|
start.setDate(start.getDate() - 90);
|
||||||
|
const startStr = start.toISOString().split('T')[0];
|
||||||
|
const endStr = end.toISOString().split('T')[0];
|
||||||
|
|
||||||
|
if (selectedDiagnoses.length > 0 && selectedDiagnoses.length <= 3) {
|
||||||
|
caseApi.getTrend({
|
||||||
|
start_date: startStr,
|
||||||
|
end_date: endStr,
|
||||||
|
group_by: 'day',
|
||||||
|
diagnosis: selectedDiagnoses.join(','),
|
||||||
|
}).then((data) => {
|
||||||
|
const trend = data.trend || [];
|
||||||
|
setChartData(
|
||||||
|
trend.map((t: { date: string; total: number }) => ({ date: t.date, cases: t.total }))
|
||||||
|
);
|
||||||
|
}).catch((e) => { console.error('Failed to load chart data:', e); });
|
||||||
|
} else {
|
||||||
|
gridApi.getHistoricalAggregated(startStr, endStr, 'daily', district)
|
||||||
|
.then((data) => {
|
||||||
|
const rows = data.aggregations || [];
|
||||||
|
const dailyCases: Record<string, number> = {};
|
||||||
|
rows.forEach((item: { date: string; total_cases: number }) => {
|
||||||
|
dailyCases[item.date] = (dailyCases[item.date] || 0) + item.total_cases;
|
||||||
|
});
|
||||||
|
setChartData(
|
||||||
|
Object.entries(dailyCases)
|
||||||
|
.map(([date, cases]) => ({ date, cases }))
|
||||||
|
.sort((a, b) => a.date.localeCompare(b.date))
|
||||||
|
);
|
||||||
|
}).catch((e) => { console.error('Failed to load chart data:', e); });
|
||||||
|
}
|
||||||
|
|
||||||
|
// Fetch districtCases with date filter (single day = currentDate)
|
||||||
|
const diagnosisParam = selectedDiagnoses.length > 0 ? selectedDiagnoses.join(',') : undefined;
|
||||||
|
fetchDistrictCases(diagnosisParam, undefined, refDate);
|
||||||
|
}, [fetchDistrictCases, selectedDiagnoses]);
|
||||||
|
|
||||||
|
// 提供给外部(手动刷新 / 病种过滤)触发的去抖加载。
|
||||||
|
const debouncedLoadChart = useCallback(() => {
|
||||||
|
if (debounceRef.current) clearTimeout(debounceRef.current);
|
||||||
|
debounceRef.current = setTimeout(() => {
|
||||||
|
loadChartData(currentDate, selectedDistrict || undefined);
|
||||||
|
}, 300);
|
||||||
|
}, [loadChartData, currentDate, selectedDistrict]);
|
||||||
|
|
||||||
|
// Re-fetch when currentDate, district, or diagnoses change
|
||||||
|
useEffect(() => {
|
||||||
|
if (debounceRef.current) clearTimeout(debounceRef.current);
|
||||||
|
debounceRef.current = setTimeout(() => {
|
||||||
|
loadChartData(currentDate, selectedDistrict || undefined);
|
||||||
|
}, 300);
|
||||||
|
return () => {
|
||||||
|
if (debounceRef.current) clearTimeout(debounceRef.current);
|
||||||
|
};
|
||||||
|
}, [currentDate, selectedDistrict, loadChartData]);
|
||||||
|
|
||||||
|
// Enhanced stats: window stats + current-date snapshot
|
||||||
|
const stats = useMemo<MonitoringStats>(() => {
|
||||||
|
const noData = chartData.length === 0;
|
||||||
|
|
||||||
|
const totalCases = noData ? 0 : chartData.reduce((sum, d) => sum + d.cases, 0);
|
||||||
|
const avgCases = noData ? 0 : Math.round(totalCases / chartData.length);
|
||||||
|
|
||||||
|
let maxDay = { date: '--', cases: 0 };
|
||||||
|
let minDay = { date: '--', cases: 0 };
|
||||||
|
let stdDev = 0;
|
||||||
|
let trend: 'up' | 'down' | 'stable' = 'stable';
|
||||||
|
|
||||||
|
if (!noData) {
|
||||||
|
maxDay = chartData.reduce((max, d) => d.cases > max.cases ? d : max, chartData[0]);
|
||||||
|
minDay = chartData.reduce((min, d) => d.cases < min.cases ? d : min, chartData[0]);
|
||||||
|
const variance = chartData.reduce((sum, d) => sum + (d.cases - avgCases) ** 2, 0) / chartData.length;
|
||||||
|
stdDev = Math.round(Math.sqrt(variance));
|
||||||
|
|
||||||
|
const halfIdx = Math.floor(chartData.length / 2);
|
||||||
|
const firstHalf = chartData.slice(0, halfIdx);
|
||||||
|
const secondHalf = chartData.slice(halfIdx);
|
||||||
|
const firstAvg = firstHalf.reduce((s, d) => s + d.cases, 0) / firstHalf.length;
|
||||||
|
const secondAvg = secondHalf.reduce((s, d) => s + d.cases, 0) / secondHalf.length;
|
||||||
|
trend = secondAvg > firstAvg * 1.1 ? 'up' : secondAvg < firstAvg * 0.9 ? 'down' : 'stable';
|
||||||
|
}
|
||||||
|
|
||||||
|
// 7-day moving average (last 7 days of the window)
|
||||||
|
const last7 = chartData.slice(-7);
|
||||||
|
const avg7d = last7.length > 0 ? Math.round(last7.reduce((s, d) => s + d.cases, 0) / last7.length) : 0;
|
||||||
|
|
||||||
|
// Current date snapshot: find the data point matching currentDate
|
||||||
|
const todaySnapshot = chartData.find((d) => d.date === currentDate);
|
||||||
|
const todayCases = todaySnapshot?.cases ?? null;
|
||||||
|
|
||||||
|
// Case type breakdown from districtCases
|
||||||
|
const totalOutpatient = districtCases.reduce((s, d) => s + d.outpatient, 0);
|
||||||
|
const totalInpatient = districtCases.reduce((s, d) => s + d.inpatient, 0);
|
||||||
|
|
||||||
|
return {
|
||||||
|
totalCases, avgCases, maxDay, minDay,
|
||||||
|
stdDev, trend, totalOutpatient, totalInpatient,
|
||||||
|
avg7d, todayCases, noData,
|
||||||
|
};
|
||||||
|
}, [chartData, districtCases, currentDate]);
|
||||||
|
|
||||||
|
// 7-day sparkline for the StatCard bar (last 7 days of the loaded window)
|
||||||
|
const sparkline7d = useMemo(() => chartData.slice(-7).map((d) => d.cases), [chartData]);
|
||||||
|
|
||||||
|
// --- On-demand loader: 病例统计 tab ---
|
||||||
|
// Drives the trend off the Monitoring timeline (30-day window ending at currentDate),
|
||||||
|
// NOT a fixed now-30d window. Year for the heatmap is derived from the data.
|
||||||
|
const loadCasesTab = useCallback(async (refDate: string) => {
|
||||||
|
setCasesTabLoading(true);
|
||||||
|
setCasesTabError(null);
|
||||||
|
|
||||||
|
const end = new Date(refDate);
|
||||||
|
const start = new Date(refDate);
|
||||||
|
start.setDate(start.getDate() - 30);
|
||||||
|
const startStr = start.toISOString().split('T')[0];
|
||||||
|
const endStr = end.toISOString().split('T')[0];
|
||||||
|
|
||||||
|
const yearStart = `${end.getFullYear()}-01-01`;
|
||||||
|
const yearEnd = `${end.getFullYear()}-12-31`;
|
||||||
|
|
||||||
|
const [statsR, trendR, pollutantsR, yearTrendR] = await Promise.allSettled([
|
||||||
|
caseApi.getStats(),
|
||||||
|
caseApi.getTrend({ start_date: startStr, end_date: endStr, group_by: 'day' }),
|
||||||
|
envApi.getPollutants(30),
|
||||||
|
caseApi.getTrend({ start_date: yearStart, end_date: yearEnd, group_by: 'day' }),
|
||||||
|
]);
|
||||||
|
|
||||||
|
const errs: string[] = [];
|
||||||
|
|
||||||
|
if (statsR.status === 'fulfilled') {
|
||||||
|
const topDiag = statsR.value.top_diagnoses || [];
|
||||||
|
setTopDiagnoses(
|
||||||
|
topDiag.slice(0, 5).map((d) => ({
|
||||||
|
diagnosis: d.diagnosis,
|
||||||
|
outpatient: d.outpatient,
|
||||||
|
inpatient: d.inpatient,
|
||||||
|
total: d.outpatient + d.inpatient,
|
||||||
|
}))
|
||||||
|
);
|
||||||
|
} else {
|
||||||
|
errs.push('诊断分布加载失败');
|
||||||
|
}
|
||||||
|
|
||||||
|
const aqiMap: Record<string, number> = {};
|
||||||
|
if (pollutantsR.status === 'fulfilled') {
|
||||||
|
for (const p of pollutantsR.value.data || []) {
|
||||||
|
aqiMap[p.date] = p.AQI || 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (trendR.status === 'fulfilled') {
|
||||||
|
const trend = trendR.value.trend || [];
|
||||||
|
setCaseTrend(
|
||||||
|
trend.map((t) => ({ date: t.date, cases: t.total, aqi: aqiMap[t.date] || 0 }))
|
||||||
|
);
|
||||||
|
} else {
|
||||||
|
errs.push('趋势数据加载失败');
|
||||||
|
}
|
||||||
|
|
||||||
|
// Calendar heatmap: daily cases for the data's actual year (derived from trend data)
|
||||||
|
if (yearTrendR.status === 'fulfilled') {
|
||||||
|
const yearTrend = yearTrendR.value.trend || [];
|
||||||
|
if (yearTrend.length > 0) {
|
||||||
|
const derivedYear = new Date(yearTrend[0].date).getFullYear();
|
||||||
|
setHeatmapYear(derivedYear);
|
||||||
|
setHeatmapData(yearTrend.map((t) => ({ date: t.date, value: t.total })));
|
||||||
|
} else {
|
||||||
|
setHeatmapYear(end.getFullYear());
|
||||||
|
setHeatmapData([]);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
errs.push('日历热力图加载失败');
|
||||||
|
}
|
||||||
|
|
||||||
|
setCasesTabError(errs.length > 0 ? errs.join(';') : null);
|
||||||
|
setCasesTabLoading(false);
|
||||||
|
setCasesTabLoaded(true);
|
||||||
|
}, []);
|
||||||
|
|
||||||
|
// --- On-demand loader: 区域统计 tab ---
|
||||||
|
const loadDistrictTab = useCallback(async () => {
|
||||||
|
setDistrictTabLoading(true);
|
||||||
|
setDistrictTabError(null);
|
||||||
|
try {
|
||||||
|
const res = await caseApi.getDistricts();
|
||||||
|
setDistrictMetrics(res.districts || []);
|
||||||
|
setDistrictTabError(null);
|
||||||
|
} catch {
|
||||||
|
setDistrictTabError('区域统计加载失败');
|
||||||
|
} finally {
|
||||||
|
setDistrictTabLoading(false);
|
||||||
|
setDistrictTabLoaded(true);
|
||||||
|
}
|
||||||
|
}, []);
|
||||||
|
|
||||||
|
// Fetch tab data the first time a tab is opened (avoids loading everything upfront)
|
||||||
|
useEffect(() => {
|
||||||
|
if (activeTab === 'cases' && !casesTabLoaded && !casesTabLoading) {
|
||||||
|
loadCasesTab(currentDate);
|
||||||
|
}
|
||||||
|
if (activeTab === 'districts' && !districtTabLoaded && !districtTabLoading) {
|
||||||
|
loadDistrictTab();
|
||||||
|
}
|
||||||
|
}, [activeTab, casesTabLoaded, casesTabLoading, districtTabLoaded, districtTabLoading, currentDate, loadCasesTab, loadDistrictTab]);
|
||||||
|
|
||||||
|
// When the timeline date moves, refresh an already-opened 病例统计 tab so its
|
||||||
|
// trend window tracks the Monitoring timeline rather than going stale.
|
||||||
|
useEffect(() => {
|
||||||
|
if (activeTab === 'cases' && casesTabLoaded) {
|
||||||
|
loadCasesTab(currentDate);
|
||||||
|
}
|
||||||
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
||||||
|
}, [currentDate]);
|
||||||
|
|
||||||
|
// 区域统计 table: sortable district rows + heatmap columns
|
||||||
|
const districtTableRows = useMemo(() => {
|
||||||
|
const sorted = [...districtMetrics].sort((a, b) => {
|
||||||
|
switch (districtSortKey) {
|
||||||
|
case 'outpatient': return b.outpatient - a.outpatient;
|
||||||
|
case 'inpatient': return b.inpatient - a.inpatient;
|
||||||
|
case 'inpatient_ratio': return (b.inpatient_ratio ?? 0) - (a.inpatient_ratio ?? 0);
|
||||||
|
default: return b.total - a.total;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
return sorted.map((d) => d.district);
|
||||||
|
}, [districtMetrics, districtSortKey]);
|
||||||
|
|
||||||
|
const districtTableData = useMemo(() => {
|
||||||
|
const map: Record<string, Record<string, number>> = {};
|
||||||
|
for (const d of districtMetrics) {
|
||||||
|
map[d.district] = {
|
||||||
|
total: d.total,
|
||||||
|
outpatient: d.outpatient,
|
||||||
|
inpatient: d.inpatient,
|
||||||
|
inpatient_ratio: Math.round((d.inpatient_ratio ?? 0) * 1000) / 10,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
return map;
|
||||||
|
}, [districtMetrics]);
|
||||||
|
|
||||||
|
return {
|
||||||
|
// 概览
|
||||||
|
chartData,
|
||||||
|
stats,
|
||||||
|
sparkline7d,
|
||||||
|
districtCases,
|
||||||
|
// 病例统计
|
||||||
|
topDiagnoses,
|
||||||
|
caseTrend,
|
||||||
|
heatmapData,
|
||||||
|
heatmapYear,
|
||||||
|
casesTabLoaded,
|
||||||
|
casesTabLoading,
|
||||||
|
casesTabError,
|
||||||
|
setCasesTabError,
|
||||||
|
loadCasesTab,
|
||||||
|
// 区域统计
|
||||||
|
districtTableRows,
|
||||||
|
districtTableData,
|
||||||
|
districtTabLoaded,
|
||||||
|
districtTabLoading,
|
||||||
|
districtTabError,
|
||||||
|
setDistrictTabError,
|
||||||
|
setDistrictSortKey,
|
||||||
|
loadDistrictTab,
|
||||||
|
// 图表手动加载(错误重试 / 病种过滤)
|
||||||
|
loadChartData,
|
||||||
|
debouncedLoadChart,
|
||||||
|
};
|
||||||
|
}
|
||||||
67
frontend/src/components/overview/AlertSeverityDonut.tsx
Normal file
67
frontend/src/components/overview/AlertSeverityDonut.tsx
Normal file
@@ -0,0 +1,67 @@
|
|||||||
|
import { memo } from 'react';
|
||||||
|
import { PieChart, Pie, Cell, Tooltip, Legend, ResponsiveContainer } from 'recharts';
|
||||||
|
import { EmptyState } from '@/components/ui';
|
||||||
|
import { CHART_COLORS } from './chartColors';
|
||||||
|
|
||||||
|
export interface AlertSlice {
|
||||||
|
name: string;
|
||||||
|
value: number;
|
||||||
|
color: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
interface AlertSeverityDonutProps {
|
||||||
|
data: AlertSlice[];
|
||||||
|
}
|
||||||
|
|
||||||
|
const tooltipStyle = {
|
||||||
|
backgroundColor: '#FFFFFF',
|
||||||
|
border: `1px solid ${CHART_COLORS.tooltipBorder}`,
|
||||||
|
borderRadius: '8px',
|
||||||
|
fontSize: '12px',
|
||||||
|
};
|
||||||
|
|
||||||
|
function AlertSeverityDonutComponent({ data }: AlertSeverityDonutProps) {
|
||||||
|
const hasData = data.some((d) => d.value > 0);
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div className="card p-4">
|
||||||
|
<div className="text-[11px] font-medium text-text-secondary uppercase tracking-wide mb-4">
|
||||||
|
预警严重度分布
|
||||||
|
</div>
|
||||||
|
{hasData ? (
|
||||||
|
<div className="flex items-center justify-center">
|
||||||
|
<ResponsiveContainer width="100%" height={240}>
|
||||||
|
<PieChart>
|
||||||
|
<Pie
|
||||||
|
data={data}
|
||||||
|
cx="50%"
|
||||||
|
cy="50%"
|
||||||
|
innerRadius={50}
|
||||||
|
outerRadius={80}
|
||||||
|
paddingAngle={4}
|
||||||
|
dataKey="value"
|
||||||
|
nameKey="name"
|
||||||
|
>
|
||||||
|
{data.map((entry) => (
|
||||||
|
<Cell key={entry.name} fill={entry.color} />
|
||||||
|
))}
|
||||||
|
</Pie>
|
||||||
|
<Tooltip
|
||||||
|
contentStyle={tooltipStyle}
|
||||||
|
formatter={(value: number, name: string) => [value, name]}
|
||||||
|
/>
|
||||||
|
<Legend
|
||||||
|
wrapperStyle={{ fontSize: '12px' }}
|
||||||
|
formatter={(value: string) => <span className="text-text-primary">{value}</span>}
|
||||||
|
/>
|
||||||
|
</PieChart>
|
||||||
|
</ResponsiveContainer>
|
||||||
|
</div>
|
||||||
|
) : (
|
||||||
|
<EmptyState title="暂无预警数据" />
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
export const AlertSeverityDonut = memo(AlertSeverityDonutComponent);
|
||||||
100
frontend/src/components/overview/CaseAqiTrend.tsx
Normal file
100
frontend/src/components/overview/CaseAqiTrend.tsx
Normal file
@@ -0,0 +1,100 @@
|
|||||||
|
import { memo } from 'react';
|
||||||
|
import {
|
||||||
|
LineChart,
|
||||||
|
Line,
|
||||||
|
XAxis,
|
||||||
|
YAxis,
|
||||||
|
CartesianGrid,
|
||||||
|
Tooltip,
|
||||||
|
Legend,
|
||||||
|
ResponsiveContainer,
|
||||||
|
} from 'recharts';
|
||||||
|
import { EmptyState } from '@/components/ui';
|
||||||
|
import { CHART_COLORS } from './chartColors';
|
||||||
|
|
||||||
|
export interface MergedTrendItem {
|
||||||
|
date: string;
|
||||||
|
cases: number;
|
||||||
|
aqi: number;
|
||||||
|
}
|
||||||
|
|
||||||
|
interface CaseAqiTrendProps {
|
||||||
|
data: MergedTrendItem[];
|
||||||
|
}
|
||||||
|
|
||||||
|
function formatDateLabel(dateStr: string): string {
|
||||||
|
const d = new Date(dateStr);
|
||||||
|
return `${d.getMonth() + 1}/${d.getDate()}`;
|
||||||
|
}
|
||||||
|
|
||||||
|
const tooltipStyle = {
|
||||||
|
backgroundColor: '#FFFFFF',
|
||||||
|
border: `1px solid ${CHART_COLORS.tooltipBorder}`,
|
||||||
|
borderRadius: '8px',
|
||||||
|
fontSize: '12px',
|
||||||
|
};
|
||||||
|
|
||||||
|
function CaseAqiTrendComponent({ data }: CaseAqiTrendProps) {
|
||||||
|
return (
|
||||||
|
<div className="card p-4">
|
||||||
|
<div className="text-[11px] font-medium text-text-secondary uppercase tracking-wide mb-4">
|
||||||
|
近30日病例与AQI趋势
|
||||||
|
</div>
|
||||||
|
{data.length > 0 ? (
|
||||||
|
<ResponsiveContainer width="100%" height={200}>
|
||||||
|
<LineChart data={data} margin={{ top: 5, right: 10, left: 0, bottom: 5 }}>
|
||||||
|
<CartesianGrid strokeDasharray="3 3" stroke={CHART_COLORS.grid} />
|
||||||
|
<XAxis
|
||||||
|
dataKey="date"
|
||||||
|
tickFormatter={formatDateLabel}
|
||||||
|
tick={{ fontSize: 10, fill: CHART_COLORS.axis }}
|
||||||
|
interval="preserveStartEnd"
|
||||||
|
axisLine={{ stroke: CHART_COLORS.grid }}
|
||||||
|
/>
|
||||||
|
<YAxis
|
||||||
|
yAxisId="left"
|
||||||
|
tick={{ fontSize: 10, fill: CHART_COLORS.axis }}
|
||||||
|
axisLine={{ stroke: CHART_COLORS.grid }}
|
||||||
|
/>
|
||||||
|
<YAxis
|
||||||
|
yAxisId="right"
|
||||||
|
orientation="right"
|
||||||
|
tick={{ fontSize: 10, fill: CHART_COLORS.aqi }}
|
||||||
|
axisLine={{ stroke: CHART_COLORS.grid }}
|
||||||
|
/>
|
||||||
|
<Tooltip
|
||||||
|
contentStyle={tooltipStyle}
|
||||||
|
labelStyle={{ color: CHART_COLORS.tooltipText, fontWeight: 600 }}
|
||||||
|
/>
|
||||||
|
<Legend wrapperStyle={{ fontSize: '11px' }} />
|
||||||
|
<Line
|
||||||
|
yAxisId="left"
|
||||||
|
type="monotone"
|
||||||
|
dataKey="cases"
|
||||||
|
name="病例数"
|
||||||
|
stroke={CHART_COLORS.cases}
|
||||||
|
strokeWidth={2}
|
||||||
|
dot={false}
|
||||||
|
activeDot={{ r: 3 }}
|
||||||
|
/>
|
||||||
|
<Line
|
||||||
|
yAxisId="right"
|
||||||
|
type="monotone"
|
||||||
|
dataKey="aqi"
|
||||||
|
name="AQI"
|
||||||
|
stroke={CHART_COLORS.aqi}
|
||||||
|
strokeWidth={2}
|
||||||
|
strokeDasharray="5 5"
|
||||||
|
dot={false}
|
||||||
|
activeDot={{ r: 3 }}
|
||||||
|
/>
|
||||||
|
</LineChart>
|
||||||
|
</ResponsiveContainer>
|
||||||
|
) : (
|
||||||
|
<EmptyState title="暂无数据" />
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
export const CaseAqiTrend = memo(CaseAqiTrendComponent);
|
||||||
168
frontend/src/components/overview/DistrictChoropleth.tsx
Normal file
168
frontend/src/components/overview/DistrictChoropleth.tsx
Normal file
@@ -0,0 +1,168 @@
|
|||||||
|
import { memo, useEffect, useMemo, useRef } from 'react';
|
||||||
|
import L from 'leaflet';
|
||||||
|
import 'leaflet/dist/leaflet.css';
|
||||||
|
import { CHART_COLORS } from './chartColors';
|
||||||
|
|
||||||
|
interface DistrictChoroplethProps {
|
||||||
|
/** 区名(规范,带「区」) → 当前 metric 标量值 的查表。 */
|
||||||
|
metricLookup: Record<string, number>;
|
||||||
|
/** 当前指标的中文标签,用于 tooltip(如「门诊病例」)。 */
|
||||||
|
metricLabel: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
const WUHAN_CENTER: [number, number] = [30.59, 114.3];
|
||||||
|
|
||||||
|
/** 把值映射到 7 档顺序色阶;高值 → 深色。 */
|
||||||
|
function colorForValue(value: number, max: number): string {
|
||||||
|
const scale = CHART_COLORS.choropleth;
|
||||||
|
if (max <= 0 || value <= 0) return CHART_COLORS.choroplethEmpty;
|
||||||
|
const ratio = value / max;
|
||||||
|
const idx = Math.min(scale.length - 1, Math.floor(ratio * scale.length));
|
||||||
|
return scale[idx];
|
||||||
|
}
|
||||||
|
|
||||||
|
interface WuhanFeatureProps {
|
||||||
|
name: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
// @types/geojson 随 @types/leaflet 一并提供 GeoJSON 全局命名空间。
|
||||||
|
type WuhanFeatureCollection = GeoJSON.FeatureCollection;
|
||||||
|
|
||||||
|
function DistrictChoroplethComponent({ metricLookup, metricLabel }: DistrictChoroplethProps) {
|
||||||
|
const mapDivRef = useRef<HTMLDivElement>(null);
|
||||||
|
const mapRef = useRef<L.Map | null>(null);
|
||||||
|
const geoLayerRef = useRef<L.GeoJSON | null>(null);
|
||||||
|
const geoDataRef = useRef<WuhanFeatureCollection | null>(null);
|
||||||
|
|
||||||
|
const maxValue = useMemo(() => {
|
||||||
|
const vals = Object.values(metricLookup);
|
||||||
|
return vals.length ? Math.max(...vals) : 0;
|
||||||
|
}, [metricLookup]);
|
||||||
|
|
||||||
|
// 创建地图 + 加载 geojson 一次。
|
||||||
|
useEffect(() => {
|
||||||
|
if (!mapDivRef.current || mapRef.current) return;
|
||||||
|
|
||||||
|
const map = L.map(mapDivRef.current, {
|
||||||
|
center: WUHAN_CENTER,
|
||||||
|
zoom: 9,
|
||||||
|
zoomControl: true,
|
||||||
|
attributionControl: false,
|
||||||
|
scrollWheelZoom: false,
|
||||||
|
});
|
||||||
|
mapRef.current = map;
|
||||||
|
|
||||||
|
L.tileLayer('https://{s}.basemaps.cartocdn.com/light_all/{z}/{x}/{y}{r}.png', {
|
||||||
|
maxZoom: 18,
|
||||||
|
}).addTo(map);
|
||||||
|
|
||||||
|
let cancelled = false;
|
||||||
|
fetch('/wuhan_districts.geojson')
|
||||||
|
.then((r) => r.json())
|
||||||
|
.then((data: WuhanFeatureCollection) => {
|
||||||
|
if (cancelled || !mapRef.current) return;
|
||||||
|
geoDataRef.current = data;
|
||||||
|
renderLayer();
|
||||||
|
try {
|
||||||
|
const tmp = L.geoJSON(data);
|
||||||
|
map.fitBounds(tmp.getBounds(), { padding: [12, 12] });
|
||||||
|
} catch {
|
||||||
|
/* keep default center if bounds fail */
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.catch(() => {
|
||||||
|
/* network/mock failure — wrapper still renders for tests */
|
||||||
|
});
|
||||||
|
|
||||||
|
return () => {
|
||||||
|
cancelled = true;
|
||||||
|
if (mapRef.current) {
|
||||||
|
mapRef.current.remove();
|
||||||
|
mapRef.current = null;
|
||||||
|
}
|
||||||
|
geoLayerRef.current = null;
|
||||||
|
};
|
||||||
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
||||||
|
}, []);
|
||||||
|
|
||||||
|
// 当 metric 变化时重绘填色。
|
||||||
|
useEffect(() => {
|
||||||
|
renderLayer();
|
||||||
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
||||||
|
}, [metricLookup, maxValue, metricLabel]);
|
||||||
|
|
||||||
|
function renderLayer() {
|
||||||
|
const map = mapRef.current;
|
||||||
|
const data = geoDataRef.current;
|
||||||
|
if (!map || !data) return;
|
||||||
|
|
||||||
|
if (geoLayerRef.current) {
|
||||||
|
geoLayerRef.current.remove();
|
||||||
|
geoLayerRef.current = null;
|
||||||
|
}
|
||||||
|
|
||||||
|
geoLayerRef.current = L.geoJSON(data, {
|
||||||
|
style: (feature) => {
|
||||||
|
const name = (feature?.properties as WuhanFeatureProps | undefined)?.name ?? '';
|
||||||
|
const value = metricLookup[name] ?? 0;
|
||||||
|
return {
|
||||||
|
fillColor: colorForValue(value, maxValue),
|
||||||
|
fillOpacity: 0.78,
|
||||||
|
color: CHART_COLORS.choroplethStroke,
|
||||||
|
weight: 1.2,
|
||||||
|
};
|
||||||
|
},
|
||||||
|
onEachFeature: (feature, layer) => {
|
||||||
|
const name = (feature.properties as WuhanFeatureProps).name ?? '未知';
|
||||||
|
const value = metricLookup[name] ?? 0;
|
||||||
|
layer.bindTooltip(
|
||||||
|
`<div style="font-size:12px"><b>${name}</b><br/>${metricLabel}:${value.toLocaleString()}</div>`,
|
||||||
|
{ sticky: true }
|
||||||
|
);
|
||||||
|
layer.on({
|
||||||
|
mouseover: (e) => {
|
||||||
|
(e.target as L.Path).setStyle({ weight: 2.4, color: CHART_COLORS.cases });
|
||||||
|
},
|
||||||
|
mouseout: (e) => {
|
||||||
|
(e.target as L.Path).setStyle({ weight: 1.2, color: CHART_COLORS.choroplethStroke });
|
||||||
|
},
|
||||||
|
click: (e) => {
|
||||||
|
map.fitBounds((e.target as L.GeoJSON).getBounds(), { padding: [40, 40] });
|
||||||
|
},
|
||||||
|
});
|
||||||
|
},
|
||||||
|
}).addTo(map);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 图例的 5 档分界值。
|
||||||
|
const legendStops = useMemo(() => {
|
||||||
|
const scale = CHART_COLORS.choropleth;
|
||||||
|
return scale.map((color, i) => ({
|
||||||
|
color,
|
||||||
|
label: maxValue > 0 ? Math.round((maxValue * (i + 1)) / scale.length).toLocaleString() : '0',
|
||||||
|
}));
|
||||||
|
}, [maxValue]);
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div data-testid="choropleth-wrapper" className="relative">
|
||||||
|
<div ref={mapDivRef} className="w-full rounded-lg overflow-hidden" style={{ height: 420 }} />
|
||||||
|
|
||||||
|
<div className="absolute bottom-3 right-3 z-[1000] bg-bg-card/95 px-3 py-2 rounded-lg border border-border shadow-sm">
|
||||||
|
<div className="text-[11px] font-semibold text-text-secondary mb-1.5">{metricLabel}</div>
|
||||||
|
<div className="flex items-center gap-0">
|
||||||
|
{legendStops.map((s) => (
|
||||||
|
<div key={s.color} className="flex flex-col items-center">
|
||||||
|
<div className="w-7 h-3" style={{ backgroundColor: s.color }} />
|
||||||
|
</div>
|
||||||
|
))}
|
||||||
|
</div>
|
||||||
|
<div className="flex justify-between mt-1 text-[10px] text-text-muted">
|
||||||
|
<span>低</span>
|
||||||
|
<span>高</span>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
export const DistrictChoropleth = memo(DistrictChoroplethComponent);
|
||||||
84
frontend/src/components/overview/KpiRow.tsx
Normal file
84
frontend/src/components/overview/KpiRow.tsx
Normal file
@@ -0,0 +1,84 @@
|
|||||||
|
import { memo } from 'react';
|
||||||
|
import {
|
||||||
|
Activity,
|
||||||
|
AlertTriangle,
|
||||||
|
Droplets,
|
||||||
|
Building2,
|
||||||
|
TrendingUp,
|
||||||
|
TrendingDown,
|
||||||
|
Users,
|
||||||
|
} from 'lucide-react';
|
||||||
|
import { StatCard } from '@/components/StatCard';
|
||||||
|
import { TESTIDS } from '@/utils/testids';
|
||||||
|
import { CHART_COLORS } from './chartColors';
|
||||||
|
|
||||||
|
export interface KpiData {
|
||||||
|
totalCases: number;
|
||||||
|
todayCases: number;
|
||||||
|
changeRatio: number | null;
|
||||||
|
activeAlerts: number;
|
||||||
|
highRiskGrids: number;
|
||||||
|
avgAQI: number;
|
||||||
|
}
|
||||||
|
|
||||||
|
interface KpiRowProps {
|
||||||
|
kpi: KpiData | null;
|
||||||
|
}
|
||||||
|
|
||||||
|
function changeTrendOf(ratio: number | null | undefined) {
|
||||||
|
if (ratio == null) return undefined;
|
||||||
|
if (ratio > 0) return { direction: 'up' as const, value: `${ratio.toFixed(1)}%` };
|
||||||
|
if (ratio < 0) return { direction: 'down' as const, value: `${Math.abs(ratio).toFixed(1)}%` };
|
||||||
|
return { direction: 'stable' as const, value: '0%' };
|
||||||
|
}
|
||||||
|
|
||||||
|
function KpiRowComponent({ kpi }: KpiRowProps) {
|
||||||
|
const changeTrend = changeTrendOf(kpi?.changeRatio);
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div
|
||||||
|
data-testid={TESTIDS.kpiRow}
|
||||||
|
className="grid grid-cols-2 md:grid-cols-3 lg:grid-cols-6 gap-3"
|
||||||
|
>
|
||||||
|
<StatCard
|
||||||
|
icon={<Users className="w-4 h-4 text-primary" />}
|
||||||
|
label="累计病例总数"
|
||||||
|
value={kpi?.totalCases?.toLocaleString() ?? '--'}
|
||||||
|
/>
|
||||||
|
<StatCard
|
||||||
|
icon={<Activity className="w-4 h-4 text-success" />}
|
||||||
|
label="今日病例"
|
||||||
|
value={kpi?.todayCases?.toLocaleString() ?? '--'}
|
||||||
|
/>
|
||||||
|
<StatCard
|
||||||
|
icon={
|
||||||
|
(changeTrend?.direction === 'up' && <TrendingUp className="w-4 h-4 text-danger" />) ||
|
||||||
|
(changeTrend?.direction === 'down' && (
|
||||||
|
<TrendingDown className="w-4 h-4 text-success" />
|
||||||
|
)) || <Activity className="w-4 h-4 text-text-muted" />
|
||||||
|
}
|
||||||
|
label="7日变化率"
|
||||||
|
value={changeTrend ? changeTrend.value : '--'}
|
||||||
|
trend={changeTrend}
|
||||||
|
/>
|
||||||
|
<StatCard
|
||||||
|
icon={<AlertTriangle className="w-4 h-4 text-warning" />}
|
||||||
|
label="活跃预警数"
|
||||||
|
value={kpi?.activeAlerts?.toLocaleString() ?? '--'}
|
||||||
|
color={kpi && kpi.activeAlerts > 0 ? CHART_COLORS.alertP1 : undefined}
|
||||||
|
/>
|
||||||
|
<StatCard
|
||||||
|
icon={<Building2 className="w-4 h-4 text-danger" />}
|
||||||
|
label="高风险网格"
|
||||||
|
value={kpi?.highRiskGrids?.toLocaleString() ?? '--'}
|
||||||
|
/>
|
||||||
|
<StatCard
|
||||||
|
icon={<Droplets className="w-4 h-4 text-primary-light" />}
|
||||||
|
label="平均AQI"
|
||||||
|
value={kpi?.avgAQI?.toLocaleString() ?? '--'}
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
export const KpiRow = memo(KpiRowComponent);
|
||||||
64
frontend/src/components/overview/TopDiagnosesBar.tsx
Normal file
64
frontend/src/components/overview/TopDiagnosesBar.tsx
Normal file
@@ -0,0 +1,64 @@
|
|||||||
|
import { memo } from 'react';
|
||||||
|
import {
|
||||||
|
BarChart,
|
||||||
|
Bar,
|
||||||
|
XAxis,
|
||||||
|
YAxis,
|
||||||
|
CartesianGrid,
|
||||||
|
Tooltip,
|
||||||
|
ResponsiveContainer,
|
||||||
|
} from 'recharts';
|
||||||
|
import { EmptyState } from '@/components/ui';
|
||||||
|
import type { DiagnosisBreakdown } from '@/types';
|
||||||
|
import { CHART_COLORS } from './chartColors';
|
||||||
|
|
||||||
|
interface TopDiagnosesBarProps {
|
||||||
|
diagnoses: DiagnosisBreakdown[];
|
||||||
|
}
|
||||||
|
|
||||||
|
const tooltipStyle = {
|
||||||
|
backgroundColor: '#FFFFFF',
|
||||||
|
border: `1px solid ${CHART_COLORS.tooltipBorder}`,
|
||||||
|
borderRadius: '8px',
|
||||||
|
fontSize: '12px',
|
||||||
|
};
|
||||||
|
|
||||||
|
function TopDiagnosesBarComponent({ diagnoses }: TopDiagnosesBarProps) {
|
||||||
|
return (
|
||||||
|
<div className="card p-4">
|
||||||
|
<div className="text-[11px] font-medium text-text-secondary uppercase tracking-wide mb-4">
|
||||||
|
Top 5 诊断分布
|
||||||
|
</div>
|
||||||
|
{diagnoses.length > 0 ? (
|
||||||
|
<ResponsiveContainer width="100%" height={220}>
|
||||||
|
<BarChart
|
||||||
|
data={[...diagnoses].reverse()}
|
||||||
|
layout="vertical"
|
||||||
|
margin={{ top: 0, right: 10, left: 60, bottom: 0 }}
|
||||||
|
>
|
||||||
|
<CartesianGrid strokeDasharray="3 3" stroke={CHART_COLORS.grid} horizontal={false} />
|
||||||
|
<XAxis type="number" tick={{ fontSize: 10, fill: CHART_COLORS.axis }} />
|
||||||
|
<YAxis
|
||||||
|
type="category"
|
||||||
|
dataKey="diagnosis"
|
||||||
|
tick={{ fontSize: 11, fill: CHART_COLORS.axisLabel }}
|
||||||
|
width={100}
|
||||||
|
axisLine={false}
|
||||||
|
tickLine={false}
|
||||||
|
/>
|
||||||
|
<Tooltip
|
||||||
|
contentStyle={tooltipStyle}
|
||||||
|
formatter={(value: number, name: string) => [value.toLocaleString(), name]}
|
||||||
|
/>
|
||||||
|
<Bar dataKey="outpatient" stackId="a" fill={CHART_COLORS.outpatient} name="门诊" barSize={16} />
|
||||||
|
<Bar dataKey="inpatient" stackId="a" fill={CHART_COLORS.inpatient} name="住院" barSize={16} />
|
||||||
|
</BarChart>
|
||||||
|
</ResponsiveContainer>
|
||||||
|
) : (
|
||||||
|
<EmptyState title="暂无数据" />
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
export const TopDiagnosesBar = memo(TopDiagnosesBarComponent);
|
||||||
91
frontend/src/components/overview/TopDistrictsBar.tsx
Normal file
91
frontend/src/components/overview/TopDistrictsBar.tsx
Normal file
@@ -0,0 +1,91 @@
|
|||||||
|
import { memo, useMemo } from 'react';
|
||||||
|
import {
|
||||||
|
BarChart,
|
||||||
|
Bar,
|
||||||
|
XAxis,
|
||||||
|
YAxis,
|
||||||
|
CartesianGrid,
|
||||||
|
Tooltip,
|
||||||
|
ResponsiveContainer,
|
||||||
|
} from 'recharts';
|
||||||
|
import { EmptyState } from '@/components/ui';
|
||||||
|
import { CHART_COLORS } from './chartColors';
|
||||||
|
import { metricValue, type DistrictMetric, type MetricKey } from './districtNormalize';
|
||||||
|
|
||||||
|
interface TopDistrictsBarProps {
|
||||||
|
/** 已归一并聚合到 13 区的指标数据。 */
|
||||||
|
districts: DistrictMetric[];
|
||||||
|
metric: MetricKey;
|
||||||
|
metricLabel: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
const tooltipStyle = {
|
||||||
|
backgroundColor: '#FFFFFF',
|
||||||
|
border: `1px solid ${CHART_COLORS.tooltipBorder}`,
|
||||||
|
borderRadius: '8px',
|
||||||
|
fontSize: '12px',
|
||||||
|
};
|
||||||
|
|
||||||
|
function TopDistrictsBarComponent({ districts, metric, metricLabel }: TopDistrictsBarProps) {
|
||||||
|
// 按当前 metric 排序取 Top5;横向条形图需 reverse 使最大值在顶部。
|
||||||
|
const top5 = useMemo(() => {
|
||||||
|
return [...districts]
|
||||||
|
.sort((a, b) => metricValue(b, metric) - metricValue(a, metric))
|
||||||
|
.slice(0, 5)
|
||||||
|
.map((d) => ({
|
||||||
|
district: d.district,
|
||||||
|
outpatient: d.outpatient,
|
||||||
|
inpatient: d.inpatient,
|
||||||
|
value: metricValue(d, metric),
|
||||||
|
}))
|
||||||
|
.reverse();
|
||||||
|
}, [districts, metric]);
|
||||||
|
|
||||||
|
const hasData = top5.some((d) => d.value > 0);
|
||||||
|
const showStack = metric === 'all';
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div className="card p-4">
|
||||||
|
<div className="text-[11px] font-medium text-text-secondary uppercase tracking-wide mb-4">
|
||||||
|
Top 5 区县{metricLabel}分布
|
||||||
|
</div>
|
||||||
|
{hasData ? (
|
||||||
|
<ResponsiveContainer width="100%" height={220}>
|
||||||
|
<BarChart data={top5} layout="vertical" margin={{ top: 0, right: 10, left: 30, bottom: 0 }}>
|
||||||
|
<CartesianGrid strokeDasharray="3 3" stroke={CHART_COLORS.grid} horizontal={false} />
|
||||||
|
<XAxis type="number" tick={{ fontSize: 10, fill: CHART_COLORS.axis }} />
|
||||||
|
<YAxis
|
||||||
|
type="category"
|
||||||
|
dataKey="district"
|
||||||
|
tick={{ fontSize: 11, fill: CHART_COLORS.axisLabel }}
|
||||||
|
width={64}
|
||||||
|
axisLine={false}
|
||||||
|
tickLine={false}
|
||||||
|
/>
|
||||||
|
<Tooltip
|
||||||
|
contentStyle={tooltipStyle}
|
||||||
|
formatter={(value: number, name: string) => [value.toLocaleString(), name]}
|
||||||
|
/>
|
||||||
|
{showStack ? (
|
||||||
|
<>
|
||||||
|
<Bar dataKey="outpatient" stackId="a" fill={CHART_COLORS.outpatient} name="门诊" barSize={20} />
|
||||||
|
<Bar dataKey="inpatient" stackId="a" fill={CHART_COLORS.inpatient} name="住院" barSize={20} />
|
||||||
|
</>
|
||||||
|
) : (
|
||||||
|
<Bar
|
||||||
|
dataKey="value"
|
||||||
|
fill={metric === 'inpatient' ? CHART_COLORS.inpatient : CHART_COLORS.outpatient}
|
||||||
|
name={metricLabel}
|
||||||
|
barSize={20}
|
||||||
|
/>
|
||||||
|
)}
|
||||||
|
</BarChart>
|
||||||
|
</ResponsiveContainer>
|
||||||
|
) : (
|
||||||
|
<EmptyState title="暂无数据" />
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
export const TopDistrictsBar = memo(TopDistrictsBarComponent);
|
||||||
22
frontend/src/components/overview/chartColors.ts
Normal file
22
frontend/src/components/overview/chartColors.ts
Normal file
@@ -0,0 +1,22 @@
|
|||||||
|
/**
|
||||||
|
* 概览大屏图表与地图使用的字面色值集中处。
|
||||||
|
* Recharts / Leaflet 需要原始 hex,无法用 Tailwind class,故在此集中定义,
|
||||||
|
* 避免页面里散落 magic hex。
|
||||||
|
*/
|
||||||
|
export const CHART_COLORS = {
|
||||||
|
outpatient: '#2563EB', // 门诊(primary)
|
||||||
|
inpatient: '#DC2626', // 住院(danger)
|
||||||
|
cases: '#2563EB',
|
||||||
|
aqi: '#D97706', // warning
|
||||||
|
grid: '#E2E8F0', // border
|
||||||
|
axis: '#64748B', // text-secondary
|
||||||
|
axisLabel: '#374151',
|
||||||
|
tooltipBorder: '#E2E8F0',
|
||||||
|
tooltipText: '#1E293B',
|
||||||
|
alertP1: '#DC2626',
|
||||||
|
alertP2: '#D97706',
|
||||||
|
// choropleth 顺序色阶(浅 → 深),高值高亮
|
||||||
|
choropleth: ['#DBEAFE', '#BFDBFE', '#93C5FD', '#60A5FA', '#3B82F6', '#2563EB', '#1D4ED8'],
|
||||||
|
choroplethEmpty: '#F1F5F9', // 无数据区填充
|
||||||
|
choroplethStroke: '#FFFFFF',
|
||||||
|
} as const;
|
||||||
111
frontend/src/components/overview/districtNormalize.test.ts
Normal file
111
frontend/src/components/overview/districtNormalize.test.ts
Normal file
@@ -0,0 +1,111 @@
|
|||||||
|
import { describe, it, expect } from 'vitest';
|
||||||
|
import {
|
||||||
|
WUHAN_DISTRICTS,
|
||||||
|
normalizeDistrictName,
|
||||||
|
joinDistrictCases,
|
||||||
|
buildMetricLookup,
|
||||||
|
} from './districtNormalize';
|
||||||
|
import type { DistrictCaseData } from '@/types';
|
||||||
|
|
||||||
|
function mk(district: string, outpatient: number, inpatient: number): DistrictCaseData {
|
||||||
|
return {
|
||||||
|
district,
|
||||||
|
outpatient,
|
||||||
|
inpatient,
|
||||||
|
total: outpatient + inpatient,
|
||||||
|
outpatient_ratio: 0,
|
||||||
|
inpatient_ratio: 0,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
describe('normalizeDistrictName', () => {
|
||||||
|
it('maps every bare form to its canonical 区-name', () => {
|
||||||
|
for (const canonical of WUHAN_DISTRICTS) {
|
||||||
|
const bare = canonical.replace(/区$/, '');
|
||||||
|
expect(normalizeDistrictName(bare)).toBe(canonical);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
it('passes canonical names through unchanged', () => {
|
||||||
|
for (const canonical of WUHAN_DISTRICTS) {
|
||||||
|
expect(normalizeDistrictName(canonical)).toBe(canonical);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
it('trims whitespace and returns null for unknown/empty', () => {
|
||||||
|
expect(normalizeDistrictName(' 武昌 ')).toBe('武昌区');
|
||||||
|
expect(normalizeDistrictName('')).toBeNull();
|
||||||
|
expect(normalizeDistrictName(null)).toBeNull();
|
||||||
|
expect(normalizeDistrictName('火星区')).toBeNull();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('joinDistrictCases', () => {
|
||||||
|
it('always yields exactly the 13 canonical districts in canonical order', () => {
|
||||||
|
const joined = joinDistrictCases([mk('武昌', 5, 1)]);
|
||||||
|
expect(joined).toHaveLength(13);
|
||||||
|
expect(joined.map((d) => d.district)).toEqual([...WUHAN_DISTRICTS]);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('collapses 武昌 + 武昌区 into ONE summed district (no double-count)', () => {
|
||||||
|
const cases = [mk('武昌', 10, 2), mk('武昌区', 4, 3)];
|
||||||
|
const joined = joinDistrictCases(cases);
|
||||||
|
const wuchang = joined.find((d) => d.district === '武昌区')!;
|
||||||
|
expect(wuchang.outpatient).toBe(14);
|
||||||
|
expect(wuchang.inpatient).toBe(5);
|
||||||
|
expect(wuchang.total).toBe(19);
|
||||||
|
// exactly 13 entries — the duplicate did not create a 14th row
|
||||||
|
expect(joined).toHaveLength(13);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('preserves sum integrity: sum(joined) == sum(input) for the 13 known districts', () => {
|
||||||
|
const cases: DistrictCaseData[] = [
|
||||||
|
mk('武昌', 10, 2),
|
||||||
|
mk('武昌区', 4, 3),
|
||||||
|
mk('江岸', 7, 1),
|
||||||
|
mk('江岸区', 2, 0),
|
||||||
|
mk('洪山区', 9, 4),
|
||||||
|
mk('黄陂', 3, 1),
|
||||||
|
];
|
||||||
|
const inputOut = cases.reduce((s, c) => s + c.outpatient, 0);
|
||||||
|
const inputIn = cases.reduce((s, c) => s + c.inpatient, 0);
|
||||||
|
const inputTotal = cases.reduce((s, c) => s + c.total, 0);
|
||||||
|
|
||||||
|
const joined = joinDistrictCases(cases);
|
||||||
|
const joinedOut = joined.reduce((s, d) => s + d.outpatient, 0);
|
||||||
|
const joinedIn = joined.reduce((s, d) => s + d.inpatient, 0);
|
||||||
|
const joinedTotal = joined.reduce((s, d) => s + d.total, 0);
|
||||||
|
|
||||||
|
expect(joinedOut).toBe(inputOut);
|
||||||
|
expect(joinedIn).toBe(inputIn);
|
||||||
|
expect(joinedTotal).toBe(inputTotal);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('ignores records outside the 13 districts (no leakage into the sum)', () => {
|
||||||
|
const cases = [mk('武昌区', 5, 0), mk('火星区', 99, 99)];
|
||||||
|
const joined = joinDistrictCases(cases);
|
||||||
|
expect(joined.reduce((s, d) => s + d.total, 0)).toBe(5);
|
||||||
|
expect(joined).toHaveLength(13);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('fills unseen districts with zeros', () => {
|
||||||
|
const joined = joinDistrictCases([mk('武昌区', 5, 1)]);
|
||||||
|
const jiangan = joined.find((d) => d.district === '江岸区')!;
|
||||||
|
expect(jiangan.total).toBe(0);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('buildMetricLookup', () => {
|
||||||
|
const joined = joinDistrictCases([mk('武昌', 10, 2), mk('江岸区', 3, 4)]);
|
||||||
|
|
||||||
|
it('keys by canonical name for the selected metric', () => {
|
||||||
|
expect(buildMetricLookup(joined, 'all')['武昌区']).toBe(12);
|
||||||
|
expect(buildMetricLookup(joined, 'outpatient')['武昌区']).toBe(10);
|
||||||
|
expect(buildMetricLookup(joined, 'inpatient')['武昌区']).toBe(2);
|
||||||
|
expect(buildMetricLookup(joined, 'all')['江岸区']).toBe(7);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('produces a lookup covering all 13 districts', () => {
|
||||||
|
expect(Object.keys(buildMetricLookup(joined, 'all'))).toHaveLength(13);
|
||||||
|
});
|
||||||
|
});
|
||||||
112
frontend/src/components/overview/districtNormalize.ts
Normal file
112
frontend/src/components/overview/districtNormalize.ts
Normal file
@@ -0,0 +1,112 @@
|
|||||||
|
/**
|
||||||
|
* 区县名称归一化与 case 数据聚合。
|
||||||
|
*
|
||||||
|
* 武汉市 geojson 的 `name` 属性是带「区」后缀的规范名(武昌区、江岸区…)。
|
||||||
|
* 后端 case 数据可能返回裸名(武昌)或带后缀名(武昌区),甚至两者并存。
|
||||||
|
* 这里把所有形式归一到 13 个规范名,并把同一区的门诊/住院/总数求和,
|
||||||
|
* 保证 join 后恰好 13 个区、无重复计数、求和守恒。
|
||||||
|
*/
|
||||||
|
|
||||||
|
import type { DistrictCaseData } from '@/types';
|
||||||
|
|
||||||
|
/** geojson 中武汉市的 13 个区(带「区」后缀),即规范名集合。 */
|
||||||
|
export const WUHAN_DISTRICTS = [
|
||||||
|
'江岸区',
|
||||||
|
'江汉区',
|
||||||
|
'硚口区',
|
||||||
|
'汉阳区',
|
||||||
|
'武昌区',
|
||||||
|
'青山区',
|
||||||
|
'洪山区',
|
||||||
|
'东西湖区',
|
||||||
|
'汉南区',
|
||||||
|
'蔡甸区',
|
||||||
|
'江夏区',
|
||||||
|
'黄陂区',
|
||||||
|
'新洲区',
|
||||||
|
] as const;
|
||||||
|
|
||||||
|
export type CanonicalDistrict = (typeof WUHAN_DISTRICTS)[number];
|
||||||
|
|
||||||
|
/** 规范名去掉「区」后缀的裸名 → 规范名 的映射,用于把裸名补全。 */
|
||||||
|
const BARE_TO_CANONICAL: Record<string, CanonicalDistrict> = WUHAN_DISTRICTS.reduce(
|
||||||
|
(acc, name) => {
|
||||||
|
acc[name.replace(/区$/, '')] = name;
|
||||||
|
return acc;
|
||||||
|
},
|
||||||
|
{} as Record<string, CanonicalDistrict>
|
||||||
|
);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 把任意形式的区名归一到规范名(带「区」后缀)。
|
||||||
|
* - 已是规范名 → 原样返回
|
||||||
|
* - 裸名(武昌)→ 补「区」(武昌区)
|
||||||
|
* - 不在 13 区内 → 返回 null(调用方应忽略,避免污染 join)
|
||||||
|
*/
|
||||||
|
export function normalizeDistrictName(raw: string | null | undefined): CanonicalDistrict | null {
|
||||||
|
if (!raw) return null;
|
||||||
|
const trimmed = raw.trim();
|
||||||
|
if (!trimmed) return null;
|
||||||
|
// 已带后缀且在规范集合内
|
||||||
|
if ((WUHAN_DISTRICTS as readonly string[]).includes(trimmed)) {
|
||||||
|
return trimmed as CanonicalDistrict;
|
||||||
|
}
|
||||||
|
// 裸名补全
|
||||||
|
const bare = trimmed.replace(/区$/, '');
|
||||||
|
return BARE_TO_CANONICAL[bare] ?? null;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** join 后每个区的指标值(按当前 metric 取出的标量)。 */
|
||||||
|
export interface DistrictMetric {
|
||||||
|
district: CanonicalDistrict;
|
||||||
|
outpatient: number;
|
||||||
|
inpatient: number;
|
||||||
|
total: number;
|
||||||
|
}
|
||||||
|
|
||||||
|
export type MetricKey = 'all' | 'outpatient' | 'inpatient';
|
||||||
|
|
||||||
|
/** 取出某条聚合记录在当前 metric 下用于着色/排序的标量值。 */
|
||||||
|
export function metricValue(d: DistrictMetric, metric: MetricKey): number {
|
||||||
|
if (metric === 'outpatient') return d.outpatient;
|
||||||
|
if (metric === 'inpatient') return d.inpatient;
|
||||||
|
return d.total;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 把 case 数组按区名归一后聚合到 13 个规范区。
|
||||||
|
* 同名区(武昌 + 武昌区)会被折叠并对各字段求和,绝不重复计数。
|
||||||
|
* 返回固定 13 项(未出现的区补 0),顺序与 WUHAN_DISTRICTS 一致,
|
||||||
|
* 便于与 geojson 稳定 join。
|
||||||
|
*/
|
||||||
|
export function joinDistrictCases(cases: readonly DistrictCaseData[]): DistrictMetric[] {
|
||||||
|
const acc = new Map<CanonicalDistrict, DistrictMetric>();
|
||||||
|
for (const name of WUHAN_DISTRICTS) {
|
||||||
|
acc.set(name, { district: name, outpatient: 0, inpatient: 0, total: 0 });
|
||||||
|
}
|
||||||
|
|
||||||
|
for (const c of cases) {
|
||||||
|
const canonical = normalizeDistrictName(c.district);
|
||||||
|
if (!canonical) continue; // 非 13 区的记录忽略
|
||||||
|
const entry = acc.get(canonical)!;
|
||||||
|
entry.outpatient += c.outpatient || 0;
|
||||||
|
entry.inpatient += c.inpatient || 0;
|
||||||
|
entry.total += c.total || 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
return WUHAN_DISTRICTS.map((name) => acc.get(name)!);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 构建 区名(规范) → 指标标量 的查表,供 geojson 着色按 name 直接索引。
|
||||||
|
*/
|
||||||
|
export function buildMetricLookup(
|
||||||
|
joined: readonly DistrictMetric[],
|
||||||
|
metric: MetricKey
|
||||||
|
): Record<string, number> {
|
||||||
|
const lookup: Record<string, number> = {};
|
||||||
|
for (const d of joined) {
|
||||||
|
lookup[d.district] = metricValue(d, metric);
|
||||||
|
}
|
||||||
|
return lookup;
|
||||||
|
}
|
||||||
41
frontend/src/components/ui/Card.tsx
Normal file
41
frontend/src/components/ui/Card.tsx
Normal file
@@ -0,0 +1,41 @@
|
|||||||
|
import React, { memo } from 'react';
|
||||||
|
|
||||||
|
export const Card = memo(function Card({
|
||||||
|
children,
|
||||||
|
className,
|
||||||
|
title,
|
||||||
|
actions,
|
||||||
|
testid,
|
||||||
|
}: {
|
||||||
|
children: React.ReactNode;
|
||||||
|
className?: string;
|
||||||
|
title?: React.ReactNode;
|
||||||
|
actions?: React.ReactNode;
|
||||||
|
testid?: string;
|
||||||
|
}): JSX.Element {
|
||||||
|
const hasHeader = title != null || actions != null;
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div
|
||||||
|
data-testid={testid}
|
||||||
|
className={[
|
||||||
|
'bg-bg-card rounded-lg border border-border',
|
||||||
|
className,
|
||||||
|
]
|
||||||
|
.filter(Boolean)
|
||||||
|
.join(' ')}
|
||||||
|
>
|
||||||
|
{hasHeader && (
|
||||||
|
<div className="flex items-center justify-between gap-2 px-4 py-3 border-b border-border-light">
|
||||||
|
{title != null && (
|
||||||
|
<div className="text-sm font-medium text-text-primary">{title}</div>
|
||||||
|
)}
|
||||||
|
{actions != null && (
|
||||||
|
<div className="flex items-center gap-2 shrink-0">{actions}</div>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
<div className="p-4">{children}</div>
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
});
|
||||||
29
frontend/src/components/ui/EmptyState.tsx
Normal file
29
frontend/src/components/ui/EmptyState.tsx
Normal file
@@ -0,0 +1,29 @@
|
|||||||
|
import React, { memo } from 'react';
|
||||||
|
|
||||||
|
export const EmptyState = memo(function EmptyState({
|
||||||
|
title,
|
||||||
|
description,
|
||||||
|
icon,
|
||||||
|
action,
|
||||||
|
}: {
|
||||||
|
title: string;
|
||||||
|
description?: string;
|
||||||
|
icon?: React.ReactNode;
|
||||||
|
action?: React.ReactNode;
|
||||||
|
}): JSX.Element {
|
||||||
|
return (
|
||||||
|
<div
|
||||||
|
data-testid="empty-state"
|
||||||
|
className="flex flex-col items-center justify-center gap-3 py-12 px-6 text-center"
|
||||||
|
>
|
||||||
|
{icon && (
|
||||||
|
<div className="text-text-muted text-4xl">{icon}</div>
|
||||||
|
)}
|
||||||
|
<p className="text-sm font-medium text-text-secondary">{title}</p>
|
||||||
|
{description && (
|
||||||
|
<p className="text-xs text-text-muted max-w-xs">{description}</p>
|
||||||
|
)}
|
||||||
|
{action && <div className="mt-2">{action}</div>}
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
});
|
||||||
30
frontend/src/components/ui/LoadingState.tsx
Normal file
30
frontend/src/components/ui/LoadingState.tsx
Normal file
@@ -0,0 +1,30 @@
|
|||||||
|
import { Skeleton } from './Skeleton';
|
||||||
|
|
||||||
|
export function LoadingState({
|
||||||
|
label,
|
||||||
|
testid,
|
||||||
|
lines = 3,
|
||||||
|
}: {
|
||||||
|
label?: string;
|
||||||
|
testid?: string;
|
||||||
|
lines?: number;
|
||||||
|
}): JSX.Element {
|
||||||
|
return (
|
||||||
|
<div
|
||||||
|
data-testid={testid ?? 'loading-state'}
|
||||||
|
className="flex flex-col items-center justify-center gap-3 p-6 w-full"
|
||||||
|
>
|
||||||
|
<div className="flex flex-col gap-2 w-full max-w-sm">
|
||||||
|
{Array.from({ length: lines }).map((_, i) => (
|
||||||
|
<Skeleton
|
||||||
|
key={i}
|
||||||
|
className={`h-4 ${i === lines - 1 ? 'w-2/3' : 'w-full'}`}
|
||||||
|
/>
|
||||||
|
))}
|
||||||
|
</div>
|
||||||
|
{label && (
|
||||||
|
<p className="text-xs text-text-muted">{label}</p>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
}
|
||||||
22
frontend/src/components/ui/Panel.tsx
Normal file
22
frontend/src/components/ui/Panel.tsx
Normal file
@@ -0,0 +1,22 @@
|
|||||||
|
import React, { memo } from 'react';
|
||||||
|
|
||||||
|
export const Panel = memo(function Panel({
|
||||||
|
children,
|
||||||
|
className,
|
||||||
|
testid,
|
||||||
|
}: {
|
||||||
|
children: React.ReactNode;
|
||||||
|
className?: string;
|
||||||
|
testid?: string;
|
||||||
|
}): JSX.Element {
|
||||||
|
return (
|
||||||
|
<div
|
||||||
|
data-testid={testid}
|
||||||
|
className={['bg-bg-hover/50 rounded-md p-3', className]
|
||||||
|
.filter(Boolean)
|
||||||
|
.join(' ')}
|
||||||
|
>
|
||||||
|
{children}
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
});
|
||||||
53
frontend/src/components/ui/Segmented.tsx
Normal file
53
frontend/src/components/ui/Segmented.tsx
Normal file
@@ -0,0 +1,53 @@
|
|||||||
|
import { memo } from 'react';
|
||||||
|
|
||||||
|
export const Segmented = memo(function Segmented<T extends string>({
|
||||||
|
options,
|
||||||
|
value,
|
||||||
|
onChange,
|
||||||
|
size = 'md',
|
||||||
|
testid,
|
||||||
|
}: {
|
||||||
|
options: { value: T; label: string }[];
|
||||||
|
value: T;
|
||||||
|
onChange: (v: T) => void;
|
||||||
|
size?: 'sm' | 'md';
|
||||||
|
testid?: string;
|
||||||
|
}): JSX.Element {
|
||||||
|
const sizeClasses = size === 'sm'
|
||||||
|
? 'px-2.5 py-0.5 text-xs'
|
||||||
|
: 'px-3.5 py-1 text-[13px]';
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div
|
||||||
|
data-testid={testid}
|
||||||
|
className="inline-flex items-center gap-0.5 rounded-full bg-bg-hover p-0.5"
|
||||||
|
>
|
||||||
|
{options.map((opt) => {
|
||||||
|
const isActive = opt.value === value;
|
||||||
|
return (
|
||||||
|
<button
|
||||||
|
key={opt.value}
|
||||||
|
type="button"
|
||||||
|
data-testid={testid ? `${testid}-${opt.value}` : undefined}
|
||||||
|
onClick={() => onChange(opt.value)}
|
||||||
|
className={[
|
||||||
|
'rounded-full font-medium transition-colors',
|
||||||
|
sizeClasses,
|
||||||
|
isActive
|
||||||
|
? 'bg-primary text-white shadow-sm'
|
||||||
|
: 'text-text-secondary hover:bg-bg-active',
|
||||||
|
].join(' ')}
|
||||||
|
>
|
||||||
|
{opt.label}
|
||||||
|
</button>
|
||||||
|
);
|
||||||
|
})}
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
}) as <T extends string>(props: {
|
||||||
|
options: { value: T; label: string }[];
|
||||||
|
value: T;
|
||||||
|
onChange: (v: T) => void;
|
||||||
|
size?: 'sm' | 'md';
|
||||||
|
testid?: string;
|
||||||
|
}) => JSX.Element;
|
||||||
22
frontend/src/components/ui/Skeleton.tsx
Normal file
22
frontend/src/components/ui/Skeleton.tsx
Normal file
@@ -0,0 +1,22 @@
|
|||||||
|
import { memo } from 'react';
|
||||||
|
|
||||||
|
export const Skeleton = memo(function Skeleton({
|
||||||
|
className,
|
||||||
|
rounded,
|
||||||
|
}: {
|
||||||
|
className?: string;
|
||||||
|
rounded?: boolean;
|
||||||
|
}): JSX.Element {
|
||||||
|
return (
|
||||||
|
<div
|
||||||
|
data-testid="skeleton"
|
||||||
|
className={[
|
||||||
|
'animate-pulse bg-bg-hover',
|
||||||
|
rounded ? 'rounded-full' : 'rounded',
|
||||||
|
className,
|
||||||
|
]
|
||||||
|
.filter(Boolean)
|
||||||
|
.join(' ')}
|
||||||
|
/>
|
||||||
|
);
|
||||||
|
});
|
||||||
6
frontend/src/components/ui/index.ts
Normal file
6
frontend/src/components/ui/index.ts
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
export { Skeleton } from './Skeleton';
|
||||||
|
export { LoadingState } from './LoadingState';
|
||||||
|
export { EmptyState } from './EmptyState';
|
||||||
|
export { Card } from './Card';
|
||||||
|
export { Panel } from './Panel';
|
||||||
|
export { Segmented } from './Segmented';
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
import { useState, useEffect, useRef, useCallback } from 'react';
|
import { useState, useEffect, useRef, useCallback, useMemo } from 'react';
|
||||||
import { cachedGet } from '../services/api';
|
import { cachedGet } from '../services/api';
|
||||||
|
|
||||||
export interface LodGridResult {
|
export interface LodGridResult {
|
||||||
@@ -24,13 +24,44 @@ export interface MapBounds {
|
|||||||
max_lon: number;
|
max_lon: number;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Snap bounds to a coarse grid so small pans don't produce a new identity.
|
||||||
|
const BOUNDS_SNAP = 0.05;
|
||||||
|
function snapBounds(b: MapBounds): MapBounds {
|
||||||
|
return {
|
||||||
|
min_lat: Math.floor(b.min_lat / BOUNDS_SNAP) * BOUNDS_SNAP,
|
||||||
|
max_lat: Math.ceil(b.max_lat / BOUNDS_SNAP) * BOUNDS_SNAP,
|
||||||
|
min_lon: Math.floor(b.min_lon / BOUNDS_SNAP) * BOUNDS_SNAP,
|
||||||
|
max_lon: Math.ceil(b.max_lon / BOUNDS_SNAP) * BOUNDS_SNAP,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
// True if `inner` is fully contained within `outer`.
|
||||||
|
function boundsContains(outer: MapBounds, inner: MapBounds): boolean {
|
||||||
|
return (
|
||||||
|
outer.min_lat <= inner.min_lat &&
|
||||||
|
outer.max_lat >= inner.max_lat &&
|
||||||
|
outer.min_lon <= inner.min_lon &&
|
||||||
|
outer.max_lon >= inner.max_lon
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
export function useLodGrid(zoom: number, forecastDay: 1 | 3 | 7, bounds?: MapBounds): LodGridResult {
|
export function useLodGrid(zoom: number, forecastDay: 1 | 3 | 7, bounds?: MapBounds): LodGridResult {
|
||||||
const [result, setResult] = useState<LodGridResult>(EMPTY_RESULT);
|
const [result, setResult] = useState<LodGridResult>(EMPTY_RESULT);
|
||||||
const prevResultRef = useRef<LodGridResult>(EMPTY_RESULT);
|
const prevResultRef = useRef<LodGridResult>(EMPTY_RESULT);
|
||||||
const debounceRef = useRef<ReturnType<typeof setTimeout>>();
|
const debounceRef = useRef<ReturnType<typeof setTimeout>>();
|
||||||
const abortRef = useRef<AbortController>();
|
const abortRef = useRef<AbortController>();
|
||||||
|
const seqRef = useRef(0);
|
||||||
|
// Track the snapped bounds + zoom of the last successful fetch so we can skip
|
||||||
|
// refetches when the new viewport is already contained in fetched data.
|
||||||
|
const lastFetchRef = useRef<{ zoom: number; day: 1 | 3 | 7; bounds?: MapBounds } | null>(null);
|
||||||
|
|
||||||
const fetchData = useCallback(async (z: number, day: 1 | 3 | 7, b?: MapBounds) => {
|
const fetchData = useCallback(async (z: number, day: 1 | 3 | 7, b?: MapBounds) => {
|
||||||
|
// Abort the previous in-flight request and bump the sequence guard.
|
||||||
|
abortRef.current?.abort();
|
||||||
|
const controller = new AbortController();
|
||||||
|
abortRef.current = controller;
|
||||||
|
const seq = ++seqRef.current;
|
||||||
|
|
||||||
setResult((prev) => ({ ...prev, loading: true }));
|
setResult((prev) => ({ ...prev, loading: true }));
|
||||||
|
|
||||||
try {
|
try {
|
||||||
@@ -41,7 +72,9 @@ export function useLodGrid(zoom: number, forecastDay: 1 | 3 | 7, bounds?: MapBou
|
|||||||
params.min_lon = b.min_lon;
|
params.min_lon = b.min_lon;
|
||||||
params.max_lon = b.max_lon;
|
params.max_lon = b.max_lon;
|
||||||
}
|
}
|
||||||
const data = await cachedGet<any>('/risk/lod-grid', params);
|
const data = await cachedGet<any>('/risk/lod-grid', params, controller.signal);
|
||||||
|
// Out-of-order guard: only the latest request commits its result.
|
||||||
|
if (seq !== seqRef.current) return;
|
||||||
const grids: number[][] = data.grids || [];
|
const grids: number[][] = data.grids || [];
|
||||||
const count = data.total_count || grids.length;
|
const count = data.total_count || grids.length;
|
||||||
|
|
||||||
@@ -63,26 +96,50 @@ export function useLodGrid(zoom: number, forecastDay: 1 | 3 | 7, bounds?: MapBou
|
|||||||
};
|
};
|
||||||
|
|
||||||
prevResultRef.current = newResult;
|
prevResultRef.current = newResult;
|
||||||
|
lastFetchRef.current = { zoom: z, day, bounds: b };
|
||||||
setResult(newResult);
|
setResult(newResult);
|
||||||
} catch (err: unknown) {
|
} catch (err: unknown) {
|
||||||
if ((err as Error)?.name === 'AbortError') return;
|
if ((err as Error)?.name === 'AbortError' || (err as Error)?.name === 'CanceledError') return;
|
||||||
|
if (seq !== seqRef.current) return;
|
||||||
// Keep previous data on error, just stop loading
|
// Keep previous data on error, just stop loading
|
||||||
setResult((prev) => ({ ...prev, loading: false }));
|
setResult((prev) => ({ ...prev, loading: false }));
|
||||||
}
|
}
|
||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
|
// Snap bounds and stabilize identity: the memo only changes when the snapped
|
||||||
|
// rounded values change, so small pans within the same coarse cell are no-ops.
|
||||||
|
const snapped = bounds ? snapBounds(bounds) : undefined;
|
||||||
|
const snappedKey = snapped
|
||||||
|
? `${snapped.min_lat},${snapped.max_lat},${snapped.min_lon},${snapped.max_lon}`
|
||||||
|
: '';
|
||||||
|
const stableBounds = useMemo(() => snapped, [snappedKey]); // eslint-disable-line react-hooks/exhaustive-deps
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
|
const roundedZoom = Math.round(zoom);
|
||||||
|
|
||||||
|
// Skip refetch when the new viewport is already contained within the last
|
||||||
|
// fetched bounds at the same zoom/day (no new data needed).
|
||||||
|
const last = lastFetchRef.current;
|
||||||
|
if (
|
||||||
|
last &&
|
||||||
|
last.zoom === roundedZoom &&
|
||||||
|
last.day === forecastDay &&
|
||||||
|
((!stableBounds && !last.bounds) ||
|
||||||
|
(stableBounds && last.bounds && boundsContains(last.bounds, stableBounds)))
|
||||||
|
) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
if (debounceRef.current) clearTimeout(debounceRef.current);
|
if (debounceRef.current) clearTimeout(debounceRef.current);
|
||||||
|
|
||||||
debounceRef.current = setTimeout(() => {
|
debounceRef.current = setTimeout(() => {
|
||||||
const roundedZoom = Math.round(zoom);
|
fetchData(roundedZoom, forecastDay, stableBounds);
|
||||||
fetchData(roundedZoom, forecastDay, bounds);
|
|
||||||
}, 150);
|
}, 150);
|
||||||
|
|
||||||
return () => {
|
return () => {
|
||||||
if (debounceRef.current) clearTimeout(debounceRef.current);
|
if (debounceRef.current) clearTimeout(debounceRef.current);
|
||||||
};
|
};
|
||||||
}, [zoom, forecastDay, bounds, fetchData]);
|
}, [zoom, forecastDay, stableBounds, fetchData]);
|
||||||
|
|
||||||
// Cleanup on unmount
|
// Cleanup on unmount
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
|
|||||||
@@ -24,11 +24,20 @@
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Leaflet overrides */
|
/* Leaflet overrides — keep z-index below TopNav (z-50) and SideNav */
|
||||||
.leaflet-container {
|
.leaflet-container {
|
||||||
font-family: inherit;
|
font-family: inherit;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
.leaflet-pane {
|
||||||
|
z-index: 1 !important;
|
||||||
|
}
|
||||||
|
|
||||||
|
.leaflet-top,
|
||||||
|
.leaflet-bottom {
|
||||||
|
z-index: 5 !important;
|
||||||
|
}
|
||||||
|
|
||||||
.leaflet-popup-content-wrapper {
|
.leaflet-popup-content-wrapper {
|
||||||
@apply rounded-lg shadow-lg;
|
@apply rounded-lg shadow-lg;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,34 +1,26 @@
|
|||||||
import { useState, useMemo, useCallback, useEffect, useRef } from 'react';
|
import { useState, useMemo, useCallback, useEffect, useRef } from 'react';
|
||||||
import React from 'react';
|
import { useSearchParams } from 'react-router-dom';
|
||||||
import { useRiskStore } from '@/stores';
|
import { useRiskStore, useSessionStore } from '@/stores';
|
||||||
import { useLodGrid } from '@/hooks/useLodGrid';
|
import { TESTIDS } from '@/utils/testids';
|
||||||
import { AlertMap } from '@/components/AlertMap';
|
|
||||||
import type { CellInfo } from '@/components/AlertMap';
|
import type { CellInfo } from '@/components/AlertMap';
|
||||||
import { ErrorBanner } from '@/components/ErrorBanner';
|
import { ErrorBanner } from '@/components/ErrorBanner';
|
||||||
|
import { analysisApi } from '@/services/api';
|
||||||
interface ExtendedAlert {
|
import { AlertsHeader } from '@/components/alerts/AlertsHeader';
|
||||||
alert_id: string;
|
import { AlertsListTab } from '@/components/alerts/AlertsListTab';
|
||||||
grid_id: string;
|
import { AlertsRiskPanel } from '@/components/alerts/AlertsRiskPanel';
|
||||||
region: string;
|
import { AlertDetailModal, CellInfoPanel } from '@/components/alerts/AlertDetailModal';
|
||||||
street: string;
|
import type { ExtendedAlert, RiskStats } from '@/components/alerts/types';
|
||||||
latitude: number;
|
|
||||||
longitude: number;
|
|
||||||
risk_value: number;
|
|
||||||
risk_level: 'high' | 'medium_high' | 'medium' | 'medium_low' | 'low';
|
|
||||||
priority: 'P1' | 'P2';
|
|
||||||
forecast_horizon: number;
|
|
||||||
forecast_time: string;
|
|
||||||
reason: string;
|
|
||||||
timestamp: string;
|
|
||||||
}
|
|
||||||
|
|
||||||
const HORIZON_LABELS: Record<number, string> = {
|
|
||||||
1: '1 天后',
|
|
||||||
3: '3 天后',
|
|
||||||
7: '7 天后',
|
|
||||||
};
|
|
||||||
|
|
||||||
export function AlertsDashboard() {
|
export function AlertsDashboard() {
|
||||||
|
// 视角驱动的两条不变量(D2:纯前端视图预设,非访问控制):
|
||||||
|
// 1. 官员(厅领导)不展示 100m 网格(「对他没意义/太超前」)——强制 showGrid=false 且隐藏网格切换。
|
||||||
|
// 2. 医生(或 ?view=cluster)= 聚类/密度视角:只看聚合栅格密度 + 病种过滤,
|
||||||
|
// 绝不渲染任何个体病例点(隐私不变量)——强制 showAlertMarkers=false 且隐藏「预警标记」切换。
|
||||||
|
const role = useSessionStore((s) => s.role);
|
||||||
|
const [searchParams] = useSearchParams();
|
||||||
|
const view = searchParams.get('view');
|
||||||
|
const isOfficial = role === 'official';
|
||||||
|
const isCluster = role === 'doctor' || view === 'cluster';
|
||||||
const alerts = useRiskStore((s) => s.alerts);
|
const alerts = useRiskStore((s) => s.alerts);
|
||||||
const isLoading = useRiskStore((s) => s.isLoading);
|
const isLoading = useRiskStore((s) => s.isLoading);
|
||||||
const error = useRiskStore((s) => s.error);
|
const error = useRiskStore((s) => s.error);
|
||||||
@@ -45,11 +37,23 @@ export function AlertsDashboard() {
|
|||||||
const [debouncedRiskRange, setDebouncedRiskRange] = useState<[number, number]>([0.6, 1.0]);
|
const [debouncedRiskRange, setDebouncedRiskRange] = useState<[number, number]>([0.6, 1.0]);
|
||||||
const [forecastDay, setForecastDay] = useState<1 | 3 | 7>(1);
|
const [forecastDay, setForecastDay] = useState<1 | 3 | 7>(1);
|
||||||
const [isFullscreen, setIsFullscreen] = useState(false);
|
const [isFullscreen, setIsFullscreen] = useState(false);
|
||||||
const [showGrid, setShowGrid] = useState(true);
|
// 官员视角默认隐藏网格(见上);其余角色默认显示。
|
||||||
|
const [showGrid, setShowGrid] = useState(!isOfficial);
|
||||||
const [cellInfo, setCellInfo] = useState<CellInfo | null>(null);
|
const [cellInfo, setCellInfo] = useState<CellInfo | null>(null);
|
||||||
|
|
||||||
// LOD grid data for cell info lookup (1d/3d/7d risk values)
|
// 隐私不变量:聚类(医生)视角下,个体病例点标记永远关闭,且无法被打开。
|
||||||
const { grids: lodGrids } = useLodGrid(10, forecastDay);
|
// 这里把「用户意图的开关状态」与「实际生效的状态」分开:effectiveShowAlertMarkers
|
||||||
|
// 是唯一传给地图/渲染的真值,cluster 模式恒为 false,与用户点击无关。
|
||||||
|
const effectiveShowAlertMarkers = isCluster ? false : showAlertMarkers;
|
||||||
|
|
||||||
|
// In-page tab strip (no router) — matches existing activePage pattern
|
||||||
|
const [activeTab, setActiveTab] = useState<'list' | 'stats'>('list');
|
||||||
|
|
||||||
|
// Risk-trend data for the 风险统计 tab, fetched on demand
|
||||||
|
const [trendData, setTrendData] = useState<Array<{ date: string; cases: number; risk: number }>>([]);
|
||||||
|
const [trendLoading, setTrendLoading] = useState(false);
|
||||||
|
const [trendError, setTrendError] = useState<string | null>(null);
|
||||||
|
const [trendLoaded, setTrendLoaded] = useState(false);
|
||||||
|
|
||||||
// Debounce riskRange for filteredAlerts computation
|
// Debounce riskRange for filteredAlerts computation
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
@@ -63,11 +67,36 @@ export function AlertsDashboard() {
|
|||||||
fetchAlerts();
|
fetchAlerts();
|
||||||
}, [fetchRiskMap, fetchAlerts]);
|
}, [fetchRiskMap, fetchAlerts]);
|
||||||
|
|
||||||
|
// Fetch real risk-trend data when the 风险统计 tab is first opened
|
||||||
|
useEffect(() => {
|
||||||
|
if (activeTab !== 'stats' || trendLoaded) return;
|
||||||
|
let cancelled = false;
|
||||||
|
setTrendLoading(true);
|
||||||
|
setTrendError(null);
|
||||||
|
analysisApi
|
||||||
|
.getTrend(14)
|
||||||
|
.then((res: { dates?: string[]; values?: number[] }) => {
|
||||||
|
if (cancelled) return;
|
||||||
|
const dates = res?.dates ?? [];
|
||||||
|
const values = res?.values ?? [];
|
||||||
|
setTrendData(dates.map((date, i) => ({ date, cases: 0, risk: values[i] ?? 0 })));
|
||||||
|
setTrendLoaded(true);
|
||||||
|
})
|
||||||
|
.catch((err: unknown) => {
|
||||||
|
if (cancelled) return;
|
||||||
|
setTrendError(err instanceof Error ? err.message : '加载风险趋势失败');
|
||||||
|
})
|
||||||
|
.finally(() => {
|
||||||
|
if (!cancelled) setTrendLoading(false);
|
||||||
|
});
|
||||||
|
return () => { cancelled = true; };
|
||||||
|
}, [activeTab, trendLoaded]);
|
||||||
|
|
||||||
const extendedAlerts: ExtendedAlert[] = useMemo(() => {
|
const extendedAlerts: ExtendedAlert[] = useMemo(() => {
|
||||||
|
const now = Date.now();
|
||||||
return (alerts || []).map((alert) => {
|
return (alerts || []).map((alert) => {
|
||||||
const forecastDate = new Date(alert.forecast_time);
|
const forecastDate = new Date(alert.forecast_time);
|
||||||
const now = new Date();
|
const diffDays = Math.ceil((forecastDate.getTime() - now) / (1000 * 60 * 60 * 24));
|
||||||
const diffDays = Math.ceil((forecastDate.getTime() - now.getTime()) / (1000 * 60 * 60 * 24));
|
|
||||||
const horizon = diffDays <= 1 ? 1 : diffDays <= 3 ? 3 : 7;
|
const horizon = diffDays <= 1 ? 1 : diffDays <= 3 ? 3 : 7;
|
||||||
|
|
||||||
return {
|
return {
|
||||||
@@ -95,22 +124,33 @@ export function AlertsDashboard() {
|
|||||||
});
|
});
|
||||||
}, [extendedAlerts, selectedHorizon, selectedPriority, sortBy, debouncedRiskRange]);
|
}, [extendedAlerts, selectedHorizon, selectedPriority, sortBy, debouncedRiskRange]);
|
||||||
|
|
||||||
// Risk distribution stats (includes p1/p2 counts)
|
// Risk distribution stats (includes p1/p2 counts) — single pass over each array
|
||||||
const riskStats = useMemo(() => {
|
const riskStats: RiskStats = useMemo(() => {
|
||||||
const p1 = extendedAlerts.filter(a => a.priority === 'P1').length;
|
// p1/p2 reflect the full (unfiltered) alert set
|
||||||
const p2 = extendedAlerts.filter(a => a.priority === 'P2').length;
|
let p1 = 0;
|
||||||
const high = filteredAlerts.filter(a => a.risk_value >= 0.8).length;
|
let p2 = 0;
|
||||||
const mediumHigh = filteredAlerts.filter(a => a.risk_value >= 0.6 && a.risk_value < 0.8).length;
|
for (const a of extendedAlerts) {
|
||||||
const medium = filteredAlerts.filter(a => a.risk_value >= 0.4 && a.risk_value < 0.6).length;
|
if (a.priority === 'P1') p1++;
|
||||||
const avgRisk = filteredAlerts.length > 0
|
else if (a.priority === 'P2') p2++;
|
||||||
? filteredAlerts.reduce((s, a) => s + a.risk_value, 0) / filteredAlerts.length
|
}
|
||||||
: 0;
|
|
||||||
|
|
||||||
|
// Single pass over filteredAlerts: counters + sum + district map
|
||||||
|
let high = 0;
|
||||||
|
let mediumHigh = 0;
|
||||||
|
let medium = 0;
|
||||||
|
let sum = 0;
|
||||||
const byDistrict: Record<string, number> = {};
|
const byDistrict: Record<string, number> = {};
|
||||||
for (const a of filteredAlerts) {
|
for (const a of filteredAlerts) {
|
||||||
|
const v = a.risk_value;
|
||||||
|
if (v >= 0.8) high++;
|
||||||
|
else if (v >= 0.6) mediumHigh++;
|
||||||
|
else if (v >= 0.4) medium++;
|
||||||
|
sum += v;
|
||||||
const d = a.region || '未知';
|
const d = a.region || '未知';
|
||||||
byDistrict[d] = (byDistrict[d] || 0) + 1;
|
byDistrict[d] = (byDistrict[d] || 0) + 1;
|
||||||
}
|
}
|
||||||
|
const avgRisk = filteredAlerts.length > 0 ? sum / filteredAlerts.length : 0;
|
||||||
|
|
||||||
const topDistricts = Object.entries(byDistrict)
|
const topDistricts = Object.entries(byDistrict)
|
||||||
.sort((a, b) => b[1] - a[1])
|
.sort((a, b) => b[1] - a[1])
|
||||||
.slice(0, 5);
|
.slice(0, 5);
|
||||||
@@ -164,7 +204,7 @@ export function AlertsDashboard() {
|
|||||||
a.forecast_horizon, `"${a.reason}"`, a.timestamp,
|
a.forecast_horizon, `"${a.reason}"`, a.timestamp,
|
||||||
]);
|
]);
|
||||||
const csv = [headers.join(','), ...rows.map(r => r.join(','))].join('\n');
|
const csv = [headers.join(','), ...rows.map(r => r.join(','))].join('\n');
|
||||||
const blob = new Blob(['\uFEFF' + csv], { type: 'text/csv;charset=utf-8;' });
|
const blob = new Blob(['' + csv], { type: 'text/csv;charset=utf-8;' });
|
||||||
const url = URL.createObjectURL(blob);
|
const url = URL.createObjectURL(blob);
|
||||||
const a = document.createElement('a');
|
const a = document.createElement('a');
|
||||||
a.href = url;
|
a.href = url;
|
||||||
@@ -184,22 +224,8 @@ export function AlertsDashboard() {
|
|||||||
URL.revokeObjectURL(url);
|
URL.revokeObjectURL(url);
|
||||||
}, [filteredAlerts]);
|
}, [filteredAlerts]);
|
||||||
|
|
||||||
const nearestGrid = useMemo(() => {
|
|
||||||
if (!cellInfo || !lodGrids.length) return null;
|
|
||||||
let best: { lat: number; lon: number; risk_1d: number; risk_3d: number; risk_7d: number } | null = null;
|
|
||||||
let bestDist = Infinity;
|
|
||||||
for (const g of lodGrids) {
|
|
||||||
const d = Math.sqrt((g[0] - cellInfo.lat) ** 2 + (g[1] - cellInfo.lon) ** 2);
|
|
||||||
if (d < bestDist) {
|
|
||||||
bestDist = d;
|
|
||||||
best = { lat: g[0], lon: g[1], risk_1d: g[2] ?? 0, risk_3d: g[3] ?? 0, risk_7d: g[4] ?? 0 };
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return best;
|
|
||||||
}, [cellInfo, lodGrids]);
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className={isFullscreen ? 'fixed inset-0 z-40 bg-bg-page pt-[52px] p-5' : 'p-5'}>
|
<div data-testid={TESTIDS.pageAlerts} className={isFullscreen ? 'fixed inset-0 z-40 bg-bg-page pt-[52px] p-5' : 'p-5'}>
|
||||||
{error && (
|
{error && (
|
||||||
<ErrorBanner
|
<ErrorBanner
|
||||||
error={error}
|
error={error}
|
||||||
@@ -208,440 +234,68 @@ export function AlertsDashboard() {
|
|||||||
/>
|
/>
|
||||||
)}
|
)}
|
||||||
|
|
||||||
{/* Header */}
|
<AlertsHeader
|
||||||
<div className="flex items-center justify-between mb-4">
|
total={filteredAlerts.length}
|
||||||
<div>
|
p1={riskStats.p1}
|
||||||
<h1 className="font-display text-[18px] font-semibold mb-1">风险预警</h1>
|
p2={riskStats.p2}
|
||||||
<p className="text-[12px] text-text-muted">
|
activeTab={activeTab}
|
||||||
100m网格风险预测 · 多时间尺度预警 · 病例-气象关联分析
|
onTabChange={setActiveTab}
|
||||||
</p>
|
/>
|
||||||
</div>
|
|
||||||
<div className="flex items-center gap-3 text-[11px]">
|
|
||||||
<span className="text-text-muted">共 <span className="font-semibold text-text-primary">{filteredAlerts.length}</span> 条预警</span>
|
|
||||||
<span className="px-2 py-1 bg-danger/10 border border-danger/20 rounded text-danger font-semibold">P1: {riskStats.p1}</span>
|
|
||||||
<span className="px-2 py-1 bg-warning/10 border border-warning/20 rounded text-warning font-semibold">P2: {riskStats.p2}</span>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
{/* Toolbar Row 1: Forecast + Fullscreen + Export */}
|
{activeTab === 'list' && (
|
||||||
<div className="card p-3 mb-3">
|
<AlertsListTab
|
||||||
<div className="flex items-center gap-3 flex-wrap">
|
forecastDay={forecastDay}
|
||||||
<div className="flex items-center gap-2">
|
onForecastDayChange={setForecastDay}
|
||||||
<span className="text-[12px] text-text-muted">网格预测:</span>
|
isFullscreen={isFullscreen}
|
||||||
<div className="flex gap-0.5 bg-bg-page p-0.5 rounded">
|
onToggleFullscreen={() => setIsFullscreen(!isFullscreen)}
|
||||||
{([1, 3, 7] as const).map((day) => (
|
onExportCsv={exportToCsv}
|
||||||
<button
|
onExportJson={exportToJson}
|
||||||
key={day}
|
selectedHorizon={selectedHorizon}
|
||||||
onClick={() => setForecastDay(day)}
|
onHorizonChange={setSelectedHorizon}
|
||||||
className={`px-3 py-1 text-[12px] font-medium rounded transition-colors ${
|
selectedPriority={selectedPriority}
|
||||||
forecastDay === day
|
onPriorityChange={setSelectedPriority}
|
||||||
? 'bg-bg-card text-primary shadow-sm'
|
riskRange={riskRange}
|
||||||
: 'text-text-secondary hover:text-text-primary'
|
onRiskRangeChange={setRiskRange}
|
||||||
}`}
|
showMap={showMap}
|
||||||
>
|
onToggleMap={() => setShowMap(!showMap)}
|
||||||
{day}天
|
showAlertMarkers={showAlertMarkers}
|
||||||
</button>
|
onToggleAlertMarkers={() => setShowAlertMarkers(!showAlertMarkers)}
|
||||||
))}
|
showGrid={showGrid}
|
||||||
</div>
|
onToggleGrid={() => setShowGrid(!showGrid)}
|
||||||
</div>
|
sortBy={sortBy}
|
||||||
|
onSortByChange={setSortBy}
|
||||||
|
riskStats={riskStats}
|
||||||
|
filteredAlerts={filteredAlerts}
|
||||||
|
isLoading={isLoading}
|
||||||
|
selectedGridId={selectedGridId}
|
||||||
|
selectedAlert={selectedAlert}
|
||||||
|
onGridClick={handleGridClick}
|
||||||
|
onCellInfo={handleCellInfo}
|
||||||
|
onCardClick={handleAlertCardClick}
|
||||||
|
effectiveShowAlertMarkers={effectiveShowAlertMarkers}
|
||||||
|
isCluster={isCluster}
|
||||||
|
isOfficial={isOfficial}
|
||||||
|
/>
|
||||||
|
)}
|
||||||
|
|
||||||
<div className="w-px h-6 bg-border" />
|
{activeTab === 'stats' && (
|
||||||
|
<AlertsRiskPanel
|
||||||
<button
|
riskStats={riskStats}
|
||||||
onClick={() => setIsFullscreen(!isFullscreen)}
|
trendData={trendData}
|
||||||
className={`px-3 py-1.5 text-[12px] font-medium rounded transition-colors ${
|
trendLoading={trendLoading}
|
||||||
isFullscreen
|
trendError={trendError}
|
||||||
? 'bg-bg-card text-primary border border-primary'
|
/>
|
||||||
: 'bg-bg-page text-text-secondary border border-border'
|
|
||||||
}`}
|
|
||||||
>
|
|
||||||
{isFullscreen ? '退出全屏' : '全屏'}
|
|
||||||
</button>
|
|
||||||
|
|
||||||
<div className="w-px h-6 bg-border" />
|
|
||||||
|
|
||||||
<button
|
|
||||||
onClick={exportToCsv}
|
|
||||||
className="px-3 py-1.5 text-[12px] font-medium rounded bg-bg-page text-text-secondary border border-border hover:border-primary transition-colors"
|
|
||||||
>
|
|
||||||
导出CSV
|
|
||||||
</button>
|
|
||||||
<button
|
|
||||||
onClick={exportToJson}
|
|
||||||
className="px-3 py-1.5 text-[12px] font-medium rounded bg-bg-page text-text-secondary border border-border hover:border-primary transition-colors"
|
|
||||||
>
|
|
||||||
导出JSON
|
|
||||||
</button>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
{/* Toolbar Row 2: Filters */}
|
|
||||||
<div className="card p-3 mb-4">
|
|
||||||
<div className="flex items-center gap-4 flex-wrap">
|
|
||||||
<div className="flex items-center gap-2">
|
|
||||||
<span className="text-[12px] text-text-muted">预测时效:</span>
|
|
||||||
<div className="flex gap-1">
|
|
||||||
{(['all', 1, 3, 7] as const).map((horizon) => (
|
|
||||||
<button
|
|
||||||
key={horizon}
|
|
||||||
onClick={() => setSelectedHorizon(horizon)}
|
|
||||||
className={`px-3 py-1.5 text-[12px] font-medium rounded transition-colors ${
|
|
||||||
selectedHorizon === horizon
|
|
||||||
? 'bg-primary text-white'
|
|
||||||
: 'bg-bg-page text-text-secondary border border-border hover:border-primary'
|
|
||||||
}`}
|
|
||||||
>
|
|
||||||
{horizon === 'all' ? '全部' : HORIZON_LABELS[horizon]}
|
|
||||||
</button>
|
|
||||||
))}
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<div className="w-px h-6 bg-border" />
|
|
||||||
|
|
||||||
<div className="flex items-center gap-2">
|
|
||||||
<span className="text-[12px] text-text-muted">优先级:</span>
|
|
||||||
<div className="flex gap-1">
|
|
||||||
{(['all', 'P1', 'P2'] as const).map((priority) => (
|
|
||||||
<button
|
|
||||||
key={priority}
|
|
||||||
onClick={() => setSelectedPriority(priority)}
|
|
||||||
className={`px-3 py-1.5 text-[12px] font-medium rounded transition-colors ${
|
|
||||||
selectedPriority === priority
|
|
||||||
? priority === 'P1'
|
|
||||||
? 'bg-danger text-white'
|
|
||||||
: priority === 'P2'
|
|
||||||
? 'bg-warning text-white'
|
|
||||||
: 'bg-primary text-white'
|
|
||||||
: 'bg-bg-page text-text-secondary border border-border hover:border-primary'
|
|
||||||
}`}
|
|
||||||
>
|
|
||||||
{priority === 'all' ? '全部' : priority}
|
|
||||||
</button>
|
|
||||||
))}
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<div className="w-px h-6 bg-border" />
|
|
||||||
|
|
||||||
<div className="flex items-center gap-2">
|
|
||||||
<span className="text-[12px] text-text-muted">风险值:</span>
|
|
||||||
<div className="flex items-center gap-2">
|
|
||||||
<input
|
|
||||||
type="number"
|
|
||||||
min={0}
|
|
||||||
max={1}
|
|
||||||
step={0.05}
|
|
||||||
value={riskRange[0]}
|
|
||||||
onChange={(e) => setRiskRange([parseFloat(e.target.value) || 0, riskRange[1]])}
|
|
||||||
className="w-16 px-2 py-1.5 text-[12px] border border-border rounded bg-bg-page text-text-primary focus:outline-none focus:border-primary"
|
|
||||||
/>
|
|
||||||
<span className="text-[12px] text-text-muted">-</span>
|
|
||||||
<input
|
|
||||||
type="number"
|
|
||||||
min={0}
|
|
||||||
max={1}
|
|
||||||
step={0.05}
|
|
||||||
value={riskRange[1]}
|
|
||||||
onChange={(e) => setRiskRange([riskRange[0], parseFloat(e.target.value) || 1])}
|
|
||||||
className="w-16 px-2 py-1.5 text-[12px] border border-border rounded bg-bg-page text-text-primary focus:outline-none focus:border-primary"
|
|
||||||
/>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<div className="w-px h-6 bg-border" />
|
|
||||||
|
|
||||||
<div className="flex items-center gap-1">
|
|
||||||
<button
|
|
||||||
onClick={() => setShowMap(!showMap)}
|
|
||||||
className={`px-2.5 py-1.5 text-[12px] font-medium rounded transition-colors ${
|
|
||||||
showMap
|
|
||||||
? 'bg-primary/10 text-primary border border-primary/30'
|
|
||||||
: 'bg-bg-page text-text-muted border border-border'
|
|
||||||
}`}
|
|
||||||
>
|
|
||||||
地图
|
|
||||||
</button>
|
|
||||||
<button
|
|
||||||
onClick={() => setShowAlertMarkers(!showAlertMarkers)}
|
|
||||||
className={`px-2.5 py-1.5 text-[12px] font-medium rounded transition-colors ${
|
|
||||||
showAlertMarkers
|
|
||||||
? 'bg-primary/10 text-primary border border-primary/30'
|
|
||||||
: 'bg-bg-page text-text-muted border border-border'
|
|
||||||
}`}
|
|
||||||
>
|
|
||||||
预警标记
|
|
||||||
</button>
|
|
||||||
<button
|
|
||||||
onClick={() => setShowGrid(!showGrid)}
|
|
||||||
className={`px-2.5 py-1.5 text-[12px] font-medium rounded transition-colors ${
|
|
||||||
showGrid
|
|
||||||
? 'bg-primary/10 text-primary border border-primary/30'
|
|
||||||
: 'bg-bg-page text-text-muted border border-border'
|
|
||||||
}`}
|
|
||||||
>
|
|
||||||
网格
|
|
||||||
</button>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<div className="w-px h-6 bg-border" />
|
|
||||||
|
|
||||||
<div className="flex items-center gap-2">
|
|
||||||
<span className="text-[12px] text-text-muted">排序:</span>
|
|
||||||
<div className="flex gap-1">
|
|
||||||
<button
|
|
||||||
onClick={() => setSortBy('risk')}
|
|
||||||
className={`px-3 py-1.5 text-[12px] font-medium rounded transition-colors ${
|
|
||||||
sortBy === 'risk'
|
|
||||||
? 'bg-bg-card text-primary border border-primary'
|
|
||||||
: 'bg-bg-page text-text-secondary border border-border hover:border-primary'
|
|
||||||
}`}
|
|
||||||
>
|
|
||||||
风险值
|
|
||||||
</button>
|
|
||||||
<button
|
|
||||||
onClick={() => setSortBy('time')}
|
|
||||||
className={`px-3 py-1.5 text-[12px] font-medium rounded transition-colors ${
|
|
||||||
sortBy === 'time'
|
|
||||||
? 'bg-bg-card text-primary border border-primary'
|
|
||||||
: 'bg-bg-page text-text-secondary border border-border hover:border-primary'
|
|
||||||
}`}
|
|
||||||
>
|
|
||||||
时间
|
|
||||||
</button>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
{/* Risk distribution summary */}
|
|
||||||
<div className="grid grid-cols-4 gap-3 mb-4">
|
|
||||||
<div className="card p-3">
|
|
||||||
<div className="text-[11px] text-text-muted mb-1">高风险 (≥0.8)</div>
|
|
||||||
<div className="text-xl font-bold text-danger">{riskStats.high}</div>
|
|
||||||
<div className="mt-1.5 h-1.5 bg-gray-100 rounded-full overflow-hidden">
|
|
||||||
<div className="h-full bg-danger rounded-full" style={{ width: `${filteredAlerts.length > 0 ? (riskStats.high / filteredAlerts.length) * 100 : 0}%` }} />
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
<div className="card p-3">
|
|
||||||
<div className="text-[11px] text-text-muted mb-1">中高风险 (0.6-0.8)</div>
|
|
||||||
<div className="text-xl font-bold text-warning">{riskStats.mediumHigh}</div>
|
|
||||||
<div className="mt-1.5 h-1.5 bg-gray-100 rounded-full overflow-hidden">
|
|
||||||
<div className="h-full bg-warning rounded-full" style={{ width: `${filteredAlerts.length > 0 ? (riskStats.mediumHigh / filteredAlerts.length) * 100 : 0}%` }} />
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
<div className="card p-3">
|
|
||||||
<div className="text-[11px] text-text-muted mb-1">中风险 (0.4-0.6)</div>
|
|
||||||
<div className="text-xl font-bold text-primary">{riskStats.medium}</div>
|
|
||||||
<div className="mt-1.5 h-1.5 bg-gray-100 rounded-full overflow-hidden">
|
|
||||||
<div className="h-full bg-primary rounded-full" style={{ width: `${filteredAlerts.length > 0 ? (riskStats.medium / filteredAlerts.length) * 100 : 0}%` }} />
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
<div className="card p-3">
|
|
||||||
<div className="text-[11px] text-text-muted mb-1">平均风险</div>
|
|
||||||
<div className="text-xl font-bold text-text-primary">{(riskStats.avgRisk * 100).toFixed(1)}%</div>
|
|
||||||
<div className="mt-1.5 text-[10px] text-text-muted">
|
|
||||||
高风险区域: {riskStats.topDistricts.slice(0, 2).map(([d, n]) => `${d}(${n})`).join(', ')}
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
{isLoading ? (
|
|
||||||
<div className="card p-8 text-center">
|
|
||||||
<div className="text-text-secondary text-[13px]">加载中...</div>
|
|
||||||
</div>
|
|
||||||
) : filteredAlerts.length === 0 ? (
|
|
||||||
<div className="card p-8 text-center">
|
|
||||||
<svg className="w-12 h-12 mx-auto mb-3 text-text-muted opacity-50" fill="currentColor" viewBox="0 0 24 24">
|
|
||||||
<path d="M12 22c1.1 0 2-.9 2-2h-4c0 1.1.89 2 2 2zm6-6v-5c0-3.07-1.64-5.64-4.5-6.32V4c0-.83-.67-1.5-1.5-1.5s-1.5.67-1.5 1.5v.68C7.63 5.36 6 7.92 6 11v5l-2 2v1h16v-1l-2-2z"/>
|
|
||||||
</svg>
|
|
||||||
<div className="text-text-muted text-[13px]">暂无符合条件的预警</div>
|
|
||||||
</div>
|
|
||||||
) : (
|
|
||||||
<div className={`grid gap-4 ${isFullscreen ? 'grid-cols-1' : 'grid-cols-[1fr_400px]'}`}>
|
|
||||||
{showMap && (
|
|
||||||
<AlertMap
|
|
||||||
selectedGridId={selectedGridId}
|
|
||||||
onGridClick={handleGridClick}
|
|
||||||
onCellInfo={handleCellInfo}
|
|
||||||
forecastDay={forecastDay}
|
|
||||||
showAlertMarkers={showAlertMarkers}
|
|
||||||
showGrid={showGrid}
|
|
||||||
filteredAlerts={filteredAlerts}
|
|
||||||
riskRange={riskRange}
|
|
||||||
isFullscreen={isFullscreen}
|
|
||||||
/>
|
|
||||||
)}
|
|
||||||
{!isFullscreen && (
|
|
||||||
<div className="space-y-3 max-h-[calc(100vh-280px)] overflow-y-auto">
|
|
||||||
{filteredAlerts.slice(0, 50).map((alert) => (
|
|
||||||
<AlertCard
|
|
||||||
key={alert.alert_id}
|
|
||||||
alert={alert}
|
|
||||||
isSelected={selectedAlert === alert.alert_id}
|
|
||||||
alertId={alert.alert_id}
|
|
||||||
onCardClick={handleAlertCardClick}
|
|
||||||
/>
|
|
||||||
))}
|
|
||||||
{filteredAlerts.length > 50 && (
|
|
||||||
<div className="text-center text-text-muted text-[12px] py-2">
|
|
||||||
还有 {filteredAlerts.length - 50} 条预警未显示
|
|
||||||
</div>
|
|
||||||
)}
|
|
||||||
</div>
|
|
||||||
)}
|
|
||||||
</div>
|
|
||||||
)}
|
)}
|
||||||
|
|
||||||
{/* Cell info panel - shown when clicking grid cell without alert */}
|
{/* Cell info panel - shown when clicking grid cell without alert */}
|
||||||
{cellInfo && !selectedAlertData && nearestGrid && (
|
{cellInfo && !selectedAlertData && (
|
||||||
<div className="fixed bottom-5 left-1/2 -translate-x-1/2 bg-bg-card rounded-lg border border-border-light shadow-lg z-50 px-5 py-4 min-w-[320px]">
|
<CellInfoPanel cellInfo={cellInfo} onClose={clearCellInfo} />
|
||||||
<div className="flex items-center justify-between mb-3">
|
|
||||||
<span className="text-[14px] font-semibold text-text-primary">网格详情</span>
|
|
||||||
<button onClick={clearCellInfo} className="text-text-muted hover:text-text-primary text-[18px] leading-none">×</button>
|
|
||||||
</div>
|
|
||||||
<div className="space-y-2 text-[12px]">
|
|
||||||
<div className="flex justify-between">
|
|
||||||
<span className="text-text-muted">坐标</span>
|
|
||||||
<span className="font-mono text-text-primary">{cellInfo.lat.toFixed(4)}, {cellInfo.lon.toFixed(4)}</span>
|
|
||||||
</div>
|
|
||||||
<div className="flex justify-between">
|
|
||||||
<span className="text-text-muted">当前风险</span>
|
|
||||||
<span className={`font-bold ${cellInfo.risk >= 0.8 ? 'text-danger' : cellInfo.risk >= 0.6 ? 'text-warning' : cellInfo.risk >= 0.4 ? 'text-primary' : 'text-success'}`}>
|
|
||||||
{(cellInfo.risk * 100).toFixed(1)}%
|
|
||||||
</span>
|
|
||||||
</div>
|
|
||||||
<div className="flex gap-3 pt-1">
|
|
||||||
<div className="flex-1 text-center p-1.5 rounded bg-bg-page">
|
|
||||||
<div className="text-[10px] text-text-muted">1天</div>
|
|
||||||
<div className="font-bold text-[13px]">{(nearestGrid.risk_1d * 100).toFixed(0)}%</div>
|
|
||||||
</div>
|
|
||||||
<div className="flex-1 text-center p-1.5 rounded bg-bg-page">
|
|
||||||
<div className="text-[10px] text-text-muted">3天</div>
|
|
||||||
<div className="font-bold text-[13px]">{(nearestGrid.risk_3d * 100).toFixed(0)}%</div>
|
|
||||||
</div>
|
|
||||||
<div className="flex-1 text-center p-1.5 rounded bg-bg-page">
|
|
||||||
<div className="text-[10px] text-text-muted">7天</div>
|
|
||||||
<div className="font-bold text-[13px]">{(nearestGrid.risk_7d * 100).toFixed(0)}%</div>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
{cellInfo.nearestAlertId && (
|
|
||||||
<div className="flex justify-between">
|
|
||||||
<span className="text-text-muted">最近预警距离</span>
|
|
||||||
<span className="text-text-primary">{(cellInfo.nearestAlertDist * 111).toFixed(1)} km</span>
|
|
||||||
</div>
|
|
||||||
)}
|
|
||||||
{!cellInfo.nearestAlertId && (
|
|
||||||
<div className="text-[11px] text-text-muted mt-1 pt-2 border-t border-border">
|
|
||||||
该区域无预警
|
|
||||||
</div>
|
|
||||||
)}
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
)}
|
)}
|
||||||
|
|
||||||
{/* Alert detail modal */}
|
{/* Alert detail modal */}
|
||||||
{selectedAlertData && (
|
{selectedAlertData && (
|
||||||
<div className="fixed inset-0 bg-black/50 z-50 flex items-center justify-center" onClick={clearSelectedAlert}>
|
<AlertDetailModal alert={selectedAlertData} onClose={clearSelectedAlert} />
|
||||||
<div className="bg-bg-card rounded-lg p-6 max-w-md w-full mx-4" onClick={e => e.stopPropagation()}>
|
|
||||||
<h3 className="font-display text-[16px] font-semibold mb-3">预警详情</h3>
|
|
||||||
<div className="space-y-2 text-[13px]">
|
|
||||||
<div className="flex justify-between">
|
|
||||||
<span className="text-text-muted">优先级</span>
|
|
||||||
<span className={`font-bold ${selectedAlertData.priority === 'P1' ? 'text-danger' : 'text-warning'}`}>
|
|
||||||
{selectedAlertData.priority}
|
|
||||||
</span>
|
|
||||||
</div>
|
|
||||||
<div className="flex justify-between">
|
|
||||||
<span className="text-text-muted">风险值</span>
|
|
||||||
<span className="font-bold">{Math.round(selectedAlertData.risk_value * 100)}%</span>
|
|
||||||
</div>
|
|
||||||
<div className="flex justify-between">
|
|
||||||
<span className="text-text-muted">预测时效</span>
|
|
||||||
<span>{HORIZON_LABELS[selectedAlertData.forecast_horizon]}</span>
|
|
||||||
</div>
|
|
||||||
<div className="flex justify-between">
|
|
||||||
<span className="text-text-muted">位置</span>
|
|
||||||
<span>{selectedAlertData.region}</span>
|
|
||||||
</div>
|
|
||||||
<div className="pt-2 border-t border-border">
|
|
||||||
<div className="text-text-muted mb-1">预警原因</div>
|
|
||||||
<div className="text-[12px]">{selectedAlertData.reason}</div>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
<button
|
|
||||||
onClick={clearSelectedAlert}
|
|
||||||
className="mt-4 w-full px-4 py-2 bg-primary text-white rounded hover:bg-primary/80 transition-colors text-[13px]"
|
|
||||||
>
|
|
||||||
关闭
|
|
||||||
</button>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
)}
|
)}
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
interface AlertCardProps {
|
|
||||||
alert: ExtendedAlert;
|
|
||||||
isSelected?: boolean;
|
|
||||||
alertId: string;
|
|
||||||
onCardClick: (id: string) => void;
|
|
||||||
}
|
|
||||||
|
|
||||||
const AlertCard = React.memo(function AlertCard({ alert, isSelected, alertId, onCardClick }: AlertCardProps) {
|
|
||||||
const isP1 = alert.priority === 'P1';
|
|
||||||
const riskPercent = Math.round(alert.risk_value * 100);
|
|
||||||
|
|
||||||
const handleClick = useCallback(() => {
|
|
||||||
onCardClick(alertId);
|
|
||||||
}, [alertId, onCardClick]);
|
|
||||||
|
|
||||||
return (
|
|
||||||
<div
|
|
||||||
className={`card overflow-hidden transition-colors cursor-pointer ${
|
|
||||||
isSelected ? 'border-primary ring-1 ring-primary' : 'hover:border-primary'
|
|
||||||
}`}
|
|
||||||
onClick={handleClick}
|
|
||||||
>
|
|
||||||
<div className={`px-4 py-3 border-b ${isP1 ? 'bg-danger/5 border-danger/20' : 'bg-warning/5 border-warning/20'}`}>
|
|
||||||
<div className="flex items-center justify-between">
|
|
||||||
<div className="flex items-center gap-2">
|
|
||||||
<div className={`w-2 h-2 rounded-full ${isP1 ? 'bg-danger' : 'bg-warning'}`} />
|
|
||||||
<span className={`text-[11px] font-bold ${isP1 ? 'text-danger' : 'text-warning'}`}>
|
|
||||||
{alert.priority}
|
|
||||||
</span>
|
|
||||||
<span className="text-[10px] text-text-muted">
|
|
||||||
{HORIZON_LABELS[alert.forecast_horizon] || '未知'}
|
|
||||||
</span>
|
|
||||||
</div>
|
|
||||||
<span className={`text-[18px] font-bold ${isP1 ? 'text-danger' : 'text-warning'}`}>
|
|
||||||
{riskPercent}%
|
|
||||||
</span>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<div className="p-4">
|
|
||||||
<div className="mb-3">
|
|
||||||
<div className="text-[13px] font-semibold mb-1">
|
|
||||||
{alert.region} - {alert.street}
|
|
||||||
</div>
|
|
||||||
<div className="text-[11px] text-text-muted">
|
|
||||||
网格:{alert.grid_id}
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<div className={`text-[12px] px-3 py-2 rounded mb-3 ${
|
|
||||||
isP1 ? 'bg-danger/10 text-danger' : 'bg-warning/10 text-warning'
|
|
||||||
}`}>
|
|
||||||
{alert.reason}
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<div className="flex items-center justify-between text-[11px] text-text-muted">
|
|
||||||
<span>预测时间:{alert.forecast_time}</span>
|
|
||||||
<span>生成:{alert.timestamp}</span>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
);
|
|
||||||
});
|
|
||||||
|
|||||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user