Skip to content

fix(eew): show every intensity the map paints in the legend - #659

Open
whes1015 wants to merge 2 commits into
mainfrom
fix/eew-legend-painted-ceiling
Open

whes1015 wants to merge 2 commits into
mainfrom
fix/eew-legend-painted-ceiling

Conversation

@whes1015

@whes1015 whes1015 commented Nov 9, 2026 •

Copy link
Copy Markdown
Member

這個 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 在地圖算得比回報值高時要取地圖那個值。

刻意沒做的:

  • 沒重做圖例 UI,也沒動左側「各地震度排行」和下方卡片(討論串的結論是那邊才有真正的空間問題)。
  • 沒做「點開看完整色階」的互動,維持原本就沒有這個入口的狀態。

相關 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 通過。
  • 沒有實機或模擬器驗證畫面:圖例高度跟著著色上限變動這件事,只有在一場「模型算得比警報回報高」的速報裡才看得到。要請有裝 app 的人在一場速報裡對一下圖例最高那一格和地圖上最深的顏色。

檢查清單

  • tool/check/commits.sh origin/main..HEAD 通過
  • 一個 commit 一件事
  • mise exec -- flutter analyze 與 mise exec -- flutter test 通過 —— analyze 通過、測試只有上述與改動無關的 10 個環境失敗
  • 新的使用者可見字串都走 AppLocalizations,沒有寫死(這次沒有新增字串)
  • CI 全部通過(Android、iOS、build、codecov 都是 pass,Instrumented 是 skipped)
  • 沒有 UI 尺寸/顏色改動;圖例既有的深淺色與 spacing 都沒動

@codecov

codecov Bot commented Nov 9, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 79.66102% with 12 lines in your changes missing coverage. Please review.
✅ Project coverage is 94.66%. Comparing base (d88c21b) to head (503918c).

Files with missing lines Patch % Lines
...ib/features/earthquake/data/eew_ml_max_source.dart 84.37% 5 Missing ⚠️
...ib/features/map/presentation/layers/rts_layer.dart 33.33% 4 Missing ⚠️
...thquake/presentation/pages/report_replay_page.dart 72.72% 3 Missing ⚠️

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@lowrt

lowrt commented Nov 10, 2026

Copy link
Copy Markdown
Contributor

這次改為在 EEW 資料進入 realtime channel 時處理:

  • 模型可用時,計算全部鄉鎮的 ML 預測震度,並以其中最大值替換 EewInfo.max
  • 首頁、地震頁、強震監視器、重播、圖例、無定位時的排序與前景播報會共用同一個 ML 最大震度
  • 模型尚未載入時不延遲警報,暫時保留 payload 值並啟動模型載入
  • 模型無法估算或警報已取消時保留 payload 值
  • 移除前一版只在圖例末端替換最大震度的邏輯
  • 新增 ML 最大震度資料來源測試,包含 ML 結果低於 payload 的情況

Commit: 3e6b7d1

@lowrt lowrt added this to the v26.5 milestone Nov 12, 2026
@YinCheng0106 YinCheng0106 added android Issues or pull requests related to the Android platform. ios Issues or pull requests related to the iOS platform. labels Nov 13, 2026
@lowrt
lowrt force-pushed the fix/eew-legend-painted-ceiling branch 3 times, most recently from 52c0bcd to 37ca275 Compare November 17, 2026 10:46

@whes1015 whes1015 left a comment

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

先不核准。

全國最大震度會被模型結果直接覆蓋,比氣象署回報低、甚至全部鄉鎮都是 0 時也會變成 0。這個數字會進卡片、前景播報,以及沒有定位估計時的排序。圖例上限的比較寫了兩份,「地圖塗得比回報高」的單元測試也被拿掉了。Android、iOS、build 還在跑。

  1. 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 再換成比較低的值,速報進行中畫面上的震度會往下跳。

  2. 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 靈靈 ⇛ 臺南新營 要求)

@lowrt
lowrt force-pushed the fix/eew-legend-painted-ceiling branch from 37ca275 to 8b66295 Compare November 18, 2026 13:17

@whes1015 whes1015 left a comment

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

先前那三點這版都對過了,程式沒有還要改的問題。

  • 全國最大震度只會往上抬。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 ・ω・ (竹子) ⇛ 新竹竹東 要求)

whes1015 and others added 2 commits November 21, 2026 08:38
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
@lowrt
lowrt force-pushed the fix/eew-legend-painted-ceiling branch from 8b66295 to 503918c Compare November 21, 2026 00:38
@github-actions

github-actions Bot commented Nov 21, 2026 •

Copy link
Copy Markdown

🔍 OpenCodeReview found 15 issue(s) in this PR.

  • ✅ Successfully posted inline: 14 comment(s)
  • 📝 In summary (no line info): 1 comment(s)

maintainability · low

📄 lib/features/earthquake/presentation/pages/report_replay_page.dart

⚠️ GitHub could not post this as an inline comment: No line information provided

目前的 .map() 用法看起來缺少了回呼函數 (callback) 的定義 (例如 (alert) => ...)。雖然在某些擴展函數下可能成立,但為了提高程式碼的可讀性與標準性,建議使用明確的匿名函數寫法,以避免維護者誤以為這是錯誤的語法。

💡 Suggested Change

Before:

                                            .map(
                                                                                 alert.info.max.clamp(0, 9),
                                                                           )

After:

                                            .map((alert) => alert.info.max.clamp(0, 9))

Comment on lines +29 to +32
if (!_model.ready) {
unawaited(_model.prepare());
return result;
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

performance · low
在 fetch 方法中,若 _model.ready 為 false,會透過 unawaited 呼叫 _model.prepare()。若在模型準備期間多次呼叫 fetch,可能會導致 _model.prepare() 被重複執行。建議確保 MlIntensityEstimator.prepare 的實作是冪等的(idempotent),或者在 fetch 中加入防止重複呼叫的機制,以避免不必要的資源消耗。

Comment on lines +33 to +39
return switch (result) {
Ok(:final value, :final stale) => Ok(
await Future.wait(value.map(_withMlMaximum)),
stale: stale,
),
Err(:final failure) => Err(failure),
};

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

performance · low
當 _source 同時回傳多個警報時,Future.wait 會併發呼叫 _model.townLevels。若 ML 模型在併發調用時消耗資源較多,需關注其效能表現與資源限制。

Suggestion:

Suggested change
return switch (result) {
Ok(:final value, :final stale) => Ok(
await Future.wait(value.map(_withMlMaximum)),
stale: stale,
),
Err(:final failure) => Err(failure),
};
// 若未來發現併發壓力過大,可考慮使用 concurrency limiter。

Comment on lines +42 to +48
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));
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

bug · medium
在 fetch 方法中使用 Future.wait 處理 _withMlMaximum 時,若 _model.townLevels 拋出非 Result 類型的 Exception(而非回傳 Err),可能會導致整個 fetch 失敗。建議在 _withMlMaximum 中加入 try-catch 處理。

Suggestion:

Suggested change
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;
}
}

Comment on lines +44 to +46
final levels = await _model.townLevels(alert.info);
final paintedMax = levels == null ? null : highestPaintedLevel(levels);
if (paintedMax == null || paintedMax <= alert.info.max) return alert;

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maintainability · low
_withMlMaximum 的邏輯目前依賴於 highestPaintedLevel。雖然目前實作看起來是正確的,但需確保 highestPaintedLevel 在處理邊界情況(如所有 level 為 0)時符合預期,以避免更新了錯誤的強度值。

Suggestion:

Suggested change
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。

Comment on lines +75 to +79
int? legendCeiling({int? reportedMax, int? paintedMax}) {
if (reportedMax == null) return paintedMax;
if (paintedMax == null || reportedMax >= paintedMax) return reportedMax;
return paintedMax;
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maintainability · low
新增的 highestPaintedLevel 與 legendCeiling 邏輯正確地處理了地圖繪製強度與警報報告最大值之間的關係。透過 legendCeiling 確保圖例的頂端(ceiling)會涵蓋 paintedMax,避免了地圖上已繪製顏色卻在圖例中找不到對應列的問題。

Comment on lines +64 to +69
source: mlIntensity == null
? EewReplaySource(api, clock, cwaOnly: cwaOnly)
: EewMlMaxSource(
EewReplaySource(api, clock, cwaOnly: cwaOnly),
mlIntensity,
),

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maintainability · low
在 ReplaySession 的建構式中,建議將 EewMlMaxSource 的包裝邏輯提取出來,以增進程式碼的可讀性,特別是當未來有更多來源需要根據 mlIntensity 進行包裝時。

Suggestion:

Suggested change
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]),

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

other · low
透過 _paintedMaxLevel 與 Listenable.merge 同步圖例與繪製狀態,能有效處理數據與視覺表現不一致的情況,確保 UI 的一致性。

Comment on lines +977 to +985
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,
);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maintainability · low
此處透過 Listenable.merge 同時監聽 _eew 與 _paintedMaxLevel 的變化,並結合 legendCeiling 計算最終顯示的等級。這種做法能有效同步地圖視覺(wash)與數據報告(alertMax)之間的狀態,確保圖例顯示的強度等級與地圖上繪製的內容一致,避免使用者誤讀。

Suggestion:

Suggested change
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,
);
// 無須修改,此邏輯實作正確且具備高度魯棒性

Comment on lines +33 to +36
/// 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.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

documentation · low
更新了 maxEewLevel 的文件註釋,明確說明其會包含 paintedMax 的值,這對於開發者理解圖例顯示邏輯非常有幫助。

Comment on lines +86 to +95
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);
});

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

test · low
測試案例 publishes immediately and starts loading an unready model 僅驗證了 prepareCalls 是否增加,但沒有驗證當 _model.ready 在 prepare() 之後變為 true 時,下一次 fetch() 是否能正確拿到更新後的強度資訊。建議增加測試確保模型載入完成後的行為符合預期。

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

android Issues or pull requests related to the Android platform. ios Issues or pull requests related to the iOS platform. size/L

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants