Repository navigation
Conversation
Codecov Report❌ Patch coverage is ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
|
這次改為在 EEW 資料進入 realtime channel 時處理:
Commit: |
52c0bcd to
37ca275
Compare
whes1015
left a comment
There was a problem hiding this comment.
先不核准。
全國最大震度會被模型結果直接覆蓋,比氣象署回報低、甚至全部鄉鎮都是 0 時也會變成 0。這個數字會進卡片、前景播報,以及沒有定位估計時的排序。圖例上限的比較寫了兩份,「地圖塗得比回報高」的單元測試也被拿掉了。Android、iOS、build 還在跑。
-
lib/features/earthquake/data/eew_ml_max_source.dart:46— 不要用reduce的結果覆蓋info.max。取 payload 與highestPaintedLevel的較高者;後者是 null(沒有鄉鎮大於 0,或模型沒算出來)就留 payload。現在的測試把 7 改成 5,請改成維持 7,並補「模型較高」和「全 0保留 payload」兩則。ET-2026-0199 要修的是地圖塗得比回報高、圖例少一格,不是把官方最大震度往下修。模型載入前先顯示 payload,下一次 fetch 再換成比較低的值,速報進行中畫面上的震度會往下跳。 -
lib/features/map/presentation/layers/rts_layer.dart:982和lib/features/earthquake/presentation/pages/report_replay_page.dart:409各寫了一次同樣的上限。第一個 commit 的legendCeiling,和第二個 commit 刪掉的「塗色比回報高時取地圖」測試,就是在鎖這件事。請把 helper 放回eew_town_levels.dart,兩處改呼叫它,測試補回去。模型還沒好時走公式,這段比較仍然是圖例不會少格的唯一保證。
說明還在寫已經不在程式裡的 legendCeiling() 呼叫方式,跟 head 上的兩個 commit 對不上,請一併改掉。
Agent 代 whes1015 審核(Discord 靈靈 ⇛ 臺南新營 要求)
37ca275 to
8b66295
Compare
whes1015
left a comment
There was a problem hiding this comment.
先前那三點這版都對過了,程式沒有還要改的問題。
- 全國最大震度只會往上抬。
lib/features/earthquake/data/eew_ml_max_source.dart裡paintedMax == null || paintedMax <= alert.info.max時保留警報原值:模型較低、鄉鎮全是 0、模型還沒載入、算不出來、已取消,都不會把氣象署的值蓋掉。對應測試有蓋到。 - 圖例上限收成
legendCeiling()(eew_town_levels.dart),地圖與回放都叫它,著色到的級數不會再少一格。 highestPaintedLevel、legendCeiling的測試有補回來,另外加了EewMlMaxSource的測試。
這支是 whes1015 開的,這個帳號不能自己核准。Android、iOS 這次看還在跑;build、changes、code-review、codecov 是過的。
補充,不擋:更新日誌寫「改用 ML 模型預測的全國最大震度」,程式是取較高者,氣象署較高時不會改。取消警報那則測試的模型震度是 5、原值是 7,沒擋取消也會過。
Agent 代 whes1015 審核(Discord ・ω・ (竹子) ⇛ 新竹竹東 要求)
Platform: all Fix(zh-Hant): 修正地震速報的圖例最高只到警報回報的震度、地圖卻塗到更高一級時,顏色與圖例對不上的問題 Fix(en-US): fix the map legend stopping below an intensity the map does paint
Platform: all Fix(zh-Hant): 地震速報改用 ML 模型預測的全國最大震度 Fix(en-US): use the ML model's nationwide maximum intensity for EEW
8b66295 to
503918c
Compare
|
🔍 OpenCodeReview found 15 issue(s) in this PR.
📄
|
| if (!_model.ready) { | ||
| unawaited(_model.prepare()); | ||
| return result; | ||
| } |
| return switch (result) { | ||
| Ok(:final value, :final stale) => Ok( | ||
| await Future.wait(value.map(_withMlMaximum)), | ||
| stale: stale, | ||
| ), | ||
| Err(:final failure) => Err(failure), | ||
| }; |
There was a problem hiding this comment.
當 _source 同時回傳多個警報時,Future.wait 會併發呼叫 _model.townLevels。若 ML 模型在併發調用時消耗資源較多,需關注其效能表現與資源限制。
Suggestion:
| return switch (result) { | |
| Ok(:final value, :final stale) => Ok( | |
| await Future.wait(value.map(_withMlMaximum)), | |
| stale: stale, | |
| ), | |
| Err(:final failure) => Err(failure), | |
| }; | |
| // 若未來發現併發壓力過大,可考慮使用 concurrency limiter。 |
| Future<Eew> _withMlMaximum(Eew alert) async { | ||
| if (alert.isCancelled) return alert; | ||
| final levels = await _model.townLevels(alert.info); | ||
| final paintedMax = levels == null ? null : highestPaintedLevel(levels); | ||
| if (paintedMax == null || paintedMax <= alert.info.max) return alert; | ||
| return alert.copyWith(info: alert.info.copyWith(max: paintedMax)); | ||
| } |
There was a problem hiding this comment.
在 fetch 方法中使用 Future.wait 處理 _withMlMaximum 時,若 _model.townLevels 拋出非 Result 類型的 Exception(而非回傳 Err),可能會導致整個 fetch 失敗。建議在 _withMlMaximum 中加入 try-catch 處理。
Suggestion:
| Future<Eew> _withMlMaximum(Eew alert) async { | |
| if (alert.isCancelled) return alert; | |
| final levels = await _model.townLevels(alert.info); | |
| final paintedMax = levels == null ? null : highestPaintedLevel(levels); | |
| if (paintedMax == null || paintedMax <= alert.info.max) return alert; | |
| return alert.copyWith(info: alert.info.copyWith(max: paintedMax)); | |
| } | |
| Future<Eew> _withMlMaximum(Eew alert) async { | |
| if (alert.isCancelled) return alert; | |
| try { | |
| final levels = await _model.townLevels(alert.info); | |
| final paintedMax = levels == null ? null : highestPaintedLevel(levels); | |
| if (paintedMax == null || paintedMax <= alert.info.max) return alert; | |
| return alert.copyWith(info: alert.info.copyWith(max: paintedMax)); | |
| } catch (_) { | |
| return alert; | |
| } | |
| } |
| final levels = await _model.townLevels(alert.info); | ||
| final paintedMax = levels == null ? null : highestPaintedLevel(levels); | ||
| if (paintedMax == null || paintedMax <= alert.info.max) return alert; |
There was a problem hiding this comment.
_withMlMaximum 的邏輯目前依賴於 highestPaintedLevel。雖然目前實作看起來是正確的,但需確保 highestPaintedLevel 在處理邊界情況(如所有 level 為 0)時符合預期,以避免更新了錯誤的強度值。
Suggestion:
| final levels = await _model.townLevels(alert.info); | |
| final paintedMax = levels == null ? null : highestPaintedLevel(levels); | |
| if (paintedMax == null || paintedMax <= alert.info.max) return alert; | |
| // 建議在測試中增加更多針對 highestPaintedLevel 與不同 level 組合的邊界測試 case。 |
| int? legendCeiling({int? reportedMax, int? paintedMax}) { | ||
| if (reportedMax == null) return paintedMax; | ||
| if (paintedMax == null || reportedMax >= paintedMax) return reportedMax; | ||
| return paintedMax; | ||
| } |
| source: mlIntensity == null | ||
| ? EewReplaySource(api, clock, cwaOnly: cwaOnly) | ||
| : EewMlMaxSource( | ||
| EewReplaySource(api, clock, cwaOnly: cwaOnly), | ||
| mlIntensity, | ||
| ), |
There was a problem hiding this comment.
在 ReplaySession 的建構式中,建議將 EewMlMaxSource 的包裝邏輯提取出來,以增進程式碼的可讀性,特別是當未來有更多來源需要根據 mlIntensity 進行包裝時。
Suggestion:
| source: mlIntensity == null | |
| ? EewReplaySource(api, clock, cwaOnly: cwaOnly) | |
| : EewMlMaxSource( | |
| EewReplaySource(api, clock, cwaOnly: cwaOnly), | |
| mlIntensity, | |
| ), | |
| final eewSource = EewReplaySource(api, clock, cwaOnly: cwaOnly); | |
| final source = mlIntensity == null ? eewSource : EewMlMaxSource(eewSource, mlIntensity); | |
| final eewChannel = RealtimeChannel<List<Eew>>( | |
| source: source, | |
| clock: serverClock, | |
| elapsed: SystemElapsed(), | |
| ticker: const SystemTicker(), | |
| ); |
| _captureBrightness(context); | ||
| return ListenableBuilder( | ||
| listenable: _eew, | ||
| listenable: Listenable.merge([_eew, _paintedMaxLevel]), |
| final alertMax = alerts.isEmpty | ||
| ? null | ||
| : alerts | ||
| .map((alert) => alert.info.max.clamp(0, 9)) | ||
| .reduce((a, b) => a > b ? a : b); | ||
| final maxEewLevel = legendCeiling( | ||
| reportedMax: alertMax, | ||
| paintedMax: _paintedMaxLevel.value, | ||
| ); |
There was a problem hiding this comment.
此處透過 Listenable.merge 同時監聽 _eew 與 _paintedMaxLevel 的變化,並結合 legendCeiling 計算最終顯示的等級。這種做法能有效同步地圖視覺(wash)與數據報告(alertMax)之間的狀態,確保圖例顯示的強度等級與地圖上繪製的內容一致,避免使用者誤讀。
Suggestion:
| final alertMax = alerts.isEmpty | |
| ? null | |
| : alerts | |
| .map((alert) => alert.info.max.clamp(0, 9)) | |
| .reduce((a, b) => a > b ? a : b); | |
| final maxEewLevel = legendCeiling( | |
| reportedMax: alertMax, | |
| paintedMax: _paintedMaxLevel.value, | |
| ); | |
| // 無須修改,此邏輯實作正確且具備高度魯棒性 |
| /// This includes the maximum of the *wash*, which may differ from an | ||
| /// incoming warning's original reported maximum. | ||
| /// A level the map paints but this scale omits would be read as another | ||
| /// intensity. |
| test('publishes immediately and starts loading an unready model', () async { | ||
| final model = _Model(ready: false, levels: {'100': 5}); | ||
| final result = await EewMlMaxSource( | ||
| _Source([_alert(max: 7)]), | ||
| model, | ||
| ).fetch(); | ||
|
|
||
| expect(result.valueOrNull!.single.info.max, 7); | ||
| expect(model.prepareCalls, 1); | ||
| }); |
這個 PR 做了什麼
地震速報的圖例上限改成跟著「地圖實際塗到的最高震度」,而不是警報自己回報的
max。前一個 PR(#656)讓圖例在速報時只顯示到預估最高震度,但地圖的著色是
eewTownLevels自己算的(ML 模型,模型還沒好時用公式),它可以算得比警報回報的整數級更高。結果就是 #ET-2026-0199 討論串裡烤大鴨 11/05 貼的那張圖:資訊欄和圖例只到 2 級,地圖上卻有 3 級的顏色——圖例少一格,那個顏色就被讀成別的震度。改動內容
正式程式碼:
lib/features/earthquake/domain/eew_town_levels.dart:新增highestPaintedLevel()(掃過整張 368 鄉鎮的預估、忽略透明的 0)與legendCeiling()(回報值與實際著色取較高者;兩者都不知道時回 null,顯示完整色階)。lib/features/map/presentation/layers/rts_layer.dart:_updateAreaFill把著色到的最高級別寫進_paintedMaxLevel,沒有速報時清成 null;buildLegend改成同時聽這個 notifier,用legendCeiling算上限。lib/features/earthquake/presentation/pages/report_replay_page.dart:回放頁的_ReplayMapState走同一條路,把著色上限交回頁面上方的圖例。lib/shared/widgets/intensity_legend.dart:只補maxEewLevel的註解,說明它是「著色的上限」而不是警報回報值,沒有行為改動。測試:
test/features/earthquake/domain/eew_town_levels_test.dart:新增兩則——highestPaintedLevel要忽略 0 級的鄉鎮;legendCeiling在地圖算得比回報值高時要取地圖那個值。刻意沒做的:
相關 issue
怎麼驗
tool/dev/analyze.sh:通過(dart format --set-exit-if-changed與flutter analyze都沒有問題)。tool/dev/test.sh:本機 3670 個測試通過;另有test/tool/*_script_test.dart共 10 個失敗(Expected: <7> Actual: <126>,執行暫存腳本時拿不到執行權限)。我把origin/main(07c0c3d)開成乾淨的 worktree 重跑test/tool/run_script_test.dart,同樣的測試以相同錯誤失敗,確認與這個改動無關、是本機環境的問題。tool/check/commits.sh origin/main..HEAD通過。檢查清單
tool/check/commits.sh origin/main..HEAD通過mise exec -- flutter analyze與mise exec -- flutter test通過 —— analyze 通過、測試只有上述與改動無關的 10 個環境失敗AppLocalizations,沒有寫死(這次沒有新增字串)