Files
egg f14591c7dc feat(mid-section-defect): full-line bidirectional defect trace center with dual query mode
Transform /mid-section-defect from TMTT-only backward analysis into a full-line
bidirectional defect traceability center supporting all detection stations.

Key changes:
- Parameterized station detection: any workcenter group as detection station
- Bidirectional tracing: backward (upstream attribution) + forward (downstream reject rates)
- Dual query mode: date range OR LOT/工單/WAFER container-based seed resolution
- Multi-select filters for upstream station, equipment model (RESOURCEFAMILYNAME), and loss reasons
- Progressive 3-stage trace pipeline (seed-resolve → lineage → events) with streaming UI
- Equipment model lookup via resource cache instead of SPECNAME
- Session caching, auto-refresh, searchable MultiSelect with fuzzy matching
- Remove legacy tmtt-defect module (fully superseded)
- Archive openspec change artifacts

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-24 16:16:33 +08:00

5.7 KiB

1. SQL Layer

  • 1.1 Create station_detection.sql — copy tmtt_detection.sql, replace hardcoded TMTT filter with {{ STATION_FILTER }} / {{ STATION_FILTER_REJECTS }} placeholders, rename TMTT_EQUIPMENTID/NAMEDETECTION_EQUIPMENTID/NAME
  • 1.2 Create downstream_rejects.sql — query DW_MES_LOTREJECTHISTORY for batched CONTAINERIDs with WORKCENTER_GROUP CASE WHEN, returning CONTAINERID, WORKCENTERNAME, WORKCENTER_GROUP, LOSSREASONNAME, EQUIPMENTNAME, REJECT_TOTAL_QTY, TXNDATE
  • 1.3 Modify upstream_history.sql — add h.TRACKINQTY (with COALESCE to 0) to ranked_history CTE and final SELECT

2. Backend Service — Station Parameterization

  • 2.1 Add _build_station_filter(station_name, column_prefix) to mid_section_defect_service.py — reads WORKCENTER_GROUPS patterns/exclude, builds OR-LIKE SQL with bind params
  • 2.2 Replace _fetch_tmtt_data() with _fetch_station_detection_data(start_date, end_date, station) — uses station_detection.sql + _build_station_filter()
  • 2.3 Update all public API signatures (query_analysis, query_analysis_detail, export_csv, resolve_trace_seed_lots, build_trace_aggregation_from_events) to accept station and direction params (default '測試'/'backward')
  • 2.4 Add station+direction to cache keys
  • 2.5 Rename all internal TMTT_DETECTION_ references (variables, dict keys, DIMENSION_MAP entries)

3. Backend Service — Forward Pipeline

  • 3.1 Extract existing backward logic into _run_backward_pipeline(start_date, end_date, station, loss_reasons)
  • 3.2 Add _fetch_downstream_rejects(tracked_cids) — batch query using downstream_rejects.sql
  • 3.3 Implement _attribute_forward_defects(detection_df, detection_cids, downstream_wip, downstream_rejects, station_order) — per-station reject rate using TRACKINQTY denominator
  • 3.4 Implement _run_forward_pipeline(start_date, end_date, station, loss_reasons) — full 8-stage pipeline (detection → forward lineage → downstream WIP+rejects → attribution → KPI/charts/detail)
  • 3.5 Implement _build_forward_kpi(), _build_forward_charts(), _build_forward_detail_table() builders
  • 3.6 Add direction dispatch in query_analysis(): backward → _run_backward_pipeline(), forward → _run_forward_pipeline()
  • 3.7 Add query_station_options() — returns ordered workcenter groups list

4. Backend Routes & EventFetcher

  • 4.1 Update mid_section_defect_routes.py — add station + direction query params to all endpoints, add station validation, add GET /station-options endpoint
  • 4.2 Update trace_routes.py_seed_resolve_mid_section_defect() passes station; lineage stage uses direction to choose resolve_full_genealogy() vs resolve_forward_tree(); events stage passes direction for domain selection
  • 4.3 Add downstream_rejects domain to event_fetcher.py — in SUPPORTED_EVENT_DOMAINS and _build_domain_sql(), loading mid_section_defect/downstream_rejects.sql

5. Frontend — FilterBar & App

  • 5.1 Update FilterBar.vue — add station <select> dropdown (fetches from /station-options on mount), add direction toggle button group (反向追溯/正向追溯), emit station+direction via update-filters
  • 5.2 Update App.vue — add station: '測試' and direction: 'backward' to filters reactive, include in buildFilterParams(), add computed isForward, switch chart layout by direction, update page header to '製程不良追溯分析' with dynamic subtitle
  • 5.3 Update useTraceProgress.js — add downstream_rejects to PROFILE_DOMAINS.mid_section_defect for forward, update collectAllContainerIds() to support children_map for forward direction

6. Frontend — Direction-Aware Components

  • 6.1 Update KpiCards.vue — accept direction + stationLabel props, switch card labels between backward/forward modes
  • 6.2 Update DetailTable.vue — accept direction prop, switch column definitions between backward (existing) and forward (偵測設備, 偵測投入, 偵測不良, 下游到達站數, 下游不良總數, 下游不良率, 最差下游站)
  • 6.3 Add .direction-toggle styles to style.css

7. Remove TMTT Defect Page

  • 7.1 Delete frontend/src/tmtt-defect/ directory
  • 7.2 Delete src/mes_dashboard/routes/tmtt_defect_routes.py
  • 7.3 Delete src/mes_dashboard/services/tmtt_defect_service.py
  • 7.4 Delete src/mes_dashboard/sql/tmtt_defect/ directory
  • 7.5 Remove tmtt-defect registration from nativeModuleRegistry.js, routeContracts.js, vite.config.js, page_status.json, routes/__init__.py, app.py, page_registry.py, and all migration baseline/config files
  • 7.6 Delete related test files and update remaining tests referencing tmtt-defect

8. Config & Metadata

  • 8.1 Update page_status.json — rename mid-section-defect page name from '中段製程不良追溯' to '製程不良追溯分析', remove tmtt-defect entry

9. Verification

  • 9.1 Run python -m pytest tests/test_mid_section_defect_*.py -v — all 22 tests pass
  • 9.2 Run cd frontend && node --test — 69/69 frontend tests pass
  • 9.3 Run all change-relevant backend tests (app_factory, navigation_contract, full_modernization_gates, page_registry, portal_shell_wave_b_native_smoke) — 64/64 pass
  • 9.4 Verify backward compat: station=測試, direction=backward produces identical data (renamed columns) — 25,415 detail rows, DETECTION_EQUIPMENTNAME columns (no TMTT_), KPI/charts/genealogy all correct
  • 9.5 Verify forward basic: station=成型 (order=4), direction=forward → 8 downstream stations, 1,673 detail rows, downstream reject distribution: 測試 1.67%, 水吹砂 0.03%, 切彎腳 0.03%, 去膠 0.02%, 電鍍 0.01%, 移印 0.01%