feat(realtime): 增加目标接收计数与频率计算支持

- 在 Target 结构体中新增 received_count 字段,用于跟踪每个目标在当前内存轨迹中的接收更新次数。
- 实现 ReceptionRate 结构体,计算目标更新频率,并在 State 中维护每个目标的接收率。
- 更新相关测试,确保接收计数和频率计算的准确性。
- 修改 Web UI 以显示目标的接收频率,提升用户体验。
- 升级协议版本至 4,确保后端、Worker 和前端的一致性。
This commit is contained in:
2026-11-02 17:32:44 +08:00
parent 7af8fb32cf
commit 1d6d7a277e
9 changed files with 194 additions and 34 deletions
+27
View File
@@ -93,6 +93,7 @@ impl Store {
observation: observation.clone(),
received_at_ms: now_ms,
position_generation: 0,
received_count: 0,
sources: vec![],
},
sources: HashMap::new(),
@@ -125,6 +126,7 @@ impl Store {
.sources
.retain(|_, s| now_ms.saturating_sub(s.received_at_ms) < self.ttl_ms);
refresh(track);
track.target.received_count = track.target.received_count.saturating_add(1);
track.suppressed = false;
if let Some(observer) = &self.observer {
observer(&track.target.observation, now_ms);
@@ -333,6 +335,31 @@ mod tests {
}
}
#[test]
fn received_count_tracks_only_accepted_updates_per_target() {
let mut store = Store::new(4, 20, 30_000, 1);
assert!(store.apply(sample("A", 1), 100));
assert!(!store.apply(sample("A", 1), 200));
let mut invalid = sample("A", 2);
invalid.longitude = Some(f64::NAN);
assert!(!store.apply(invalid, 300));
assert!(store.apply(sample("B", 1), 400));
assert!(store.apply(sample("A", 2), 500));
let mut second_source = sample("A", 1);
second_source.receiver_id = "station-2".into();
assert!(store.apply(second_source, 600));
let frame = store.frame(Some(1), 600);
let counts: HashMap<_, _> = frame
.targets
.iter()
.map(|target| (target.observation.target_id.as_str(), target.received_count))
.collect();
assert_eq!(counts.get("A"), Some(&3));
assert_eq!(counts.get("B"), Some(&1));
store.expire(30_600);
assert!(store.apply(sample("A", 1), 30_601));
assert_eq!(store.frame(None, 30_601).targets[0].received_count, 1);
}
#[test]
fn coalesced_position_loss_still_breaks_the_trail() {
let mut store = Store::new(10, 100, 30000, 1);
let a = sample("TEST", 1);