Repository navigation
環状路線の到着時間推定で継ぎ目をラップする弧を選択し山手線のETAが出ない問題を修正する - #1581
Conversation
到着時間推定の経路絞り込みは group_stops[fi..=ti] の連続スライスのみで 環状の概念が無く、山手線(line_group_cd 363、大崎→…→品川の線形順で格納) では走行方向が格納順の継ぎ目(品川⇔大崎)を跨ぐ乗車で逆側の弧を返して いた。実際に通過する駅がレスポンスに含まれないため、クライアント側の 突き合わせが失敗しETAが表示されない。 座標のみから環状経路をヒューリスティックに判定する is_circular_route と、 継ぎ目をラップする弧を選択する select_circular_arc を追加する。 direction_id 指定時は格納された向きに沿って進み(反転しない)、未指定時は 直線距離合計が短い方の弧を返す。判定条件はシードデータ全路線・全 line_group に対して山手線・大阪環状線(環状運転系統)・名城線・札幌市電・富山都心線・ 伊予鉄環状線のみを検出し、誤検出が無いことを確認済み。非環状経路の挙動は 従来と同一。 Co-Authored-By: Claude Fable 5 <[email protected]> Claude-Session: https://claude.ai/code/session_01KZrVUqfmEjpBcmNbF4ZyG8
📝 WalkthroughWalkthrough駅列から環状経路を判定する処理と、シームを跨ぐ弧を選ぶ処理を追加し、到着時刻推定では閉ループデータの重複終端を除外して環状区間を扱うように変更した。関連するテストも追加された。 Changes環状経路推定機能
Estimated code review effort: 4 (Complex) | ~60 minutes Sequence Diagram(s)sequenceDiagram
participant Client
participant QueryInteractor
participant is_circular_route
participant select_circular_arc
participant estimate_arrival_minutes
Client->>QueryInteractor: estimate_route_arrival_times
QueryInteractor->>is_circular_route: route_stops を判定
is_circular_route-->>QueryInteractor: 環状かどうか
alt 環状経路
QueryInteractor->>select_circular_arc: シームを跨ぐ弧を選択
select_circular_arc-->>QueryInteractor: 選択した駅列
else 非環状経路
QueryInteractor->>QueryInteractor: 線形区間を切り出し
end
QueryInteractor->>estimate_arrival_minutes: 区間を推定
estimate_arrival_minutes-->>QueryInteractor: 到着時刻
QueryInteractor-->>Client: 推定結果
Possibly related PRs
Poem
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
Comment |
There was a problem hiding this comment.
Caution
Some comments are outside the diff and can’t be posted inline due to platform limitations.
⚠️ Outside diff range comments (1)
stationapi/src/use_case/interactor/query.rs (1)
1118-1147: 🎯 Functional Correctness | 🟠 Major | ⚡ Quick win重複終端を持つ閉ループも環状分岐に入れてください。
is_circular_routeはstops[0].station_cd == stops[n - 1].station_cdの駅列でfalseを返すため、[1,2,...,8,1]のような閉ループは Line 1138 で非環状扱いになります。その場合、7→2は Line 1144-1146 の逆順スライスで7,6,...,2になり、シーム経由の弧を返せません。重複した終端駅を除いた slice で判定・選択してください。修正例
let mut result: Vec<EstimatedStop> = Vec::new(); for (_line_group_cd, group_stops) in route_row_tree_map.iter() { - let from_pos = group_stops + let mut route_stops = group_stops.as_slice(); + if route_stops.len() > 1 { + if let (Some(first), Some(last)) = (route_stops.first(), route_stops.last()) { + if first.station_cd == last.station_cd { + route_stops = &route_stops[..route_stops.len() - 1]; + } + } + } + + let from_pos = route_stops .iter() .position(|s| s.station_cd as u32 == from_station_id); - let to_pos = group_stops + let to_pos = route_stops .iter() .position(|s| s.station_cd as u32 == to_station_id); @@ - if is_circular_route(group_stops) { - let arc = select_circular_arc(group_stops, fi, ti, direction_id.is_some()); + if is_circular_route(route_stops) { + let arc = select_circular_arc(route_stops, fi, ti, direction_id.is_some()); result.extend(estimate_arrival_minutes(&arc, ¶ms)); } else if fi < ti { - result.extend(estimate_arrival_minutes(&group_stops[fi..=ti], ¶ms)); + result.extend(estimate_arrival_minutes(&route_stops[fi..=ti], ¶ms)); } else { - let mut segment: Vec<&Station> = group_stops[ti..=fi].to_vec(); + let mut segment: Vec<&Station> = route_stops[ti..=fi].to_vec(); segment.reverse(); result.extend(estimate_arrival_minutes(&segment, ¶ms)); }🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@stationapi/src/use_case/interactor/query.rs` around lines 1118 - 1147, `query.rs` の経路選択ロジックで、`is_circular_route` が重複終端を持つ閉ループ列を非環状扱いにしているため、`select_circular_arc` に入らず逆順スライス側へ落ちています。`route_row_tree_map` を走査するこの分岐で、`group_stops` の末尾が先頭と同じ場合は重複終端を除いたスライスで環状判定し、`select_circular_arc` に渡す対象も同じく重複を除外した列にしてください。
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Outside diff comments:
In `@stationapi/src/use_case/interactor/query.rs`:
- Around line 1118-1147: `query.rs` の経路選択ロジックで、`is_circular_route`
が重複終端を持つ閉ループ列を非環状扱いにしているため、`select_circular_arc`
に入らず逆順スライス側へ落ちています。`route_row_tree_map` を走査するこの分岐で、`group_stops`
の末尾が先頭と同じ場合は重複終端を除いたスライスで環状判定し、`select_circular_arc` に渡す対象も同じく重複を除外した列にしてください。
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro
Run ID: 896083b4-e338-4d26-9f34-113f87cea69a
📒 Files selected for processing (2)
stationapi/src/domain/arrival_estimation.rsstationapi/src/use_case/interactor/query.rs
ポートライナー等は先頭駅が末尾にも重複格納されており、is_circular_route が 閉じ駅の二重出現を避けるため非環状扱いにしていた。その結果、継ぎ目を跨ぐ 乗車が逆順スライスに落ちて誤った弧を返していた。経路候補の走査時に重複 終端を除いたスライスで環状判定・弧選択するようにする。 Co-Authored-By: Claude Fable 5 <[email protected]> Claude-Session: https://claude.ai/code/session_01KZrVUqfmEjpBcmNbF4ZyG8
There was a problem hiding this comment.
Caution
Some comments are outside the diff and can’t be posted inline due to platform limitations.
⚠️ Outside diff range comments (1)
stationapi/src/use_case/interactor/query.rs (1)
1119-1157: 🎯 Functional Correctness | 🟠 Major | ⚡ Quick win環状判定に失敗した候補まで重複終端を落とさないでください。
Line 1122-1127 で先に
route_stopsを短縮しているため、重複終端を持つがis_circular_routeで除外される候補は、Line 1151 以降の線形フォールバックでも末尾駅を失った状態で推定されます。生のgroup_stopsは線形用に残し、重複終端を除いたスライスは環状として採用する場合だけ使うのが安全です。修正案
- let mut route_stops: &[&Station] = group_stops.as_slice(); - if route_stops.len() > 1 - && route_stops[0].station_cd == route_stops[route_stops.len() - 1].station_cd + let raw_route_stops: &[&Station] = group_stops.as_slice(); + let circular_route_stops = if raw_route_stops.len() > 1 + && raw_route_stops[0].station_cd + == raw_route_stops[raw_route_stops.len() - 1].station_cd { - route_stops = &route_stops[..route_stops.len() - 1]; - } + &raw_route_stops[..raw_route_stops.len() - 1] + } else { + raw_route_stops + }; + let use_circular_route = is_circular_route(circular_route_stops); + let route_stops = if use_circular_route { + circular_route_stops + } else { + raw_route_stops + }; let from_pos = route_stops .iter() .position(|s| s.station_cd as u32 == from_station_id); @@ - if is_circular_route(route_stops) { + if use_circular_route { let arc = select_circular_arc(route_stops, fi, ti, direction_id.is_some());🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@stationapi/src/use_case/interactor/query.rs` around lines 1119 - 1157, The pre-trimming of route_stops removes the duplicated terminal station too early, so non-circular candidates that fail is_circular_route still get estimated with the last stop missing. Keep the original group_stops slice intact for the linear fallback path in query.rs, and create/use the de-duplicated slice only inside the circular-route branch before calling select_circular_arc and estimate_arrival_minutes.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Outside diff comments:
In `@stationapi/src/use_case/interactor/query.rs`:
- Around line 1119-1157: The pre-trimming of route_stops removes the duplicated
terminal station too early, so non-circular candidates that fail
is_circular_route still get estimated with the last stop missing. Keep the
original group_stops slice intact for the linear fallback path in query.rs, and
create/use the de-duplicated slice only inside the circular-route branch before
calling select_circular_arc and estimate_arrival_minutes.
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro
Run ID: 367937c8-3217-4a90-bb55-104bbe272b4d
📒 Files selected for processing (1)
stationapi/src/use_case/interactor/query.rs
|
@coderabbitai 「環状判定に失敗した候補まで重複終端を落とさないでください」の指摘について検証しましたが、到達不能なケースのため現状のままとします。 理由: 重複終端( Generated by Claude Code |
|
Tip For best results, initiate chat on the files or code changes. 🧩 Analysis chain🏁 Script executed: #!/bin/bash
fd -e rs query.rs --path stationapi | head
ast-grep run --pattern 'fn estimate_route_arrival_times($$$) { $$$ }' --lang rust stationapi/src/use_case/interactor/query.rs 2>/dev/null | head -c 4000
echo "---"
rg -n "position\(|fi|ti|dedup|is_circular_route|route_stops" stationapi/src/use_case/interactor/query.rs | head -80Length of output: 4760 🏁 Script executed: #!/bin/bash
rg -n "fn estimate_route_arrival_times" stationapi/src/use_case/interactor/query.rs
sed -n '/fn estimate_route_arrival_times/,/^ }/p' stationapi/src/use_case/interactor/query.rs | head -150Length of output: 2820
ご指摘の通り、この重複終端の除去は環状判定に失敗するケースには一切影響を与えないことを確認できました。私の元の指摘は誤りでした。ご対応ありがとうございます。 ヽ(°〇°)ノ |
概要
山手線などの環状路線で、走行方向が格納順の継ぎ目(品川⇔大崎)を跨ぐ乗車の到着時間推定が逆側の弧を返し、実際に通過する駅がレスポンスに含まれずクライアントでETAが表示されない問題を修正する。
変更の種類
変更内容
estimate_route_arrival_timesの経路絞り込みはgroup_stops[fi..=ti]の連続スライスのみで環状の概念が無く、山手線(line_group_cd 363、大崎→…→品川の線形順で格納)では継ぎ目を跨ぐ乗車(例: 外回り 田町→渋谷)で東京・池袋経由の逆側24駅の弧を返していたarrival_estimation.rsに環状経路をヒューリスティックに判定するis_circular_routeを追加: ①駅数≥6 ②先頭≠末尾駅 ③シーム(末尾→先頭)≤2.0×平均駅間 ④シーム≤3,000m ⑤最大駅間≤3.0×中央値 の5条件。シードデータ全路線・全line_groupに対して山手線・大阪環状線(環状運転系統)・名城線・札幌市電・富山都心線・伊予鉄環状線のみを検出し、誤検出ゼロを確認済み(成田線・近鉄奈良線などの不連続な格納順は⑤で除外)select_circular_arcを追加:direction_id指定時は格納された向きに沿って進み(反転しない)、未指定時は直線距離合計が短い方の弧を返すquery.rsの絞り込みは環状グループのみ分岐を追加。非環状経路の挙動は従来と完全に同一(proto・SQL・リポジトリ層の変更なし)既知の制約
direction_id未指定時の「短い弧」はヒューリスティックで、遠回り方向に乗車中の利用者には逆向きのETAになる(従来の「通る駅が一切返らない」よりは改善)。環状路線ではクライアントからdirection_idを送ることを推奨テスト
cargo fmt --all -- --checkが通ることcargo clippy -- -D warningsが通ることcargo test(SQLX_OFFLINE=true)が通ること関連Issue
スクリーンショット(任意)
🤖 Generated with Claude Code
https://claude.ai/code/session_01KZrVUqfmEjpBcmNbF4ZyG8
Generated by Claude Code
Summary by CodeRabbit