-
-
Notifications
You must be signed in to change notification settings - Fork 3
data_validatorの不整合検出時にPRコメントへ内訳を投稿する #1406
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
36a763a
data_validatorの不整合検出時にPRコメントへ内訳を投稿する
claude 6c31f02
data_validator: Vec→HashSet化とループ統合で検証を効率化
claude 587f58b
data_validator: escape backticks and pipes in Markdown table cells
claude 1f35759
data_validator: propagate CSV parse errors instead of silently droppi…
claude 5a99b32
data_validator: replace unwrap() with safe parsing in validation loop
claude File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,55 +1,114 @@ | ||
| use core::panic; | ||
| use std::collections::HashSet; | ||
| use std::path::Path; | ||
|
|
||
| use csv::{ReaderBuilder, StringRecord}; | ||
|
|
||
| fn main() -> Result<(), Box<dyn std::error::Error>> { | ||
| let mut has_err = false; | ||
| let mut invalid_station_ids: Vec<String> = Vec::new(); | ||
| let mut invalid_type_ids: Vec<String> = Vec::new(); | ||
|
|
||
| let data_path: &Path = Path::new("data"); | ||
| let mut rdr = ReaderBuilder::new().from_path(data_path.join("3!stations.csv"))?; | ||
| let records: Vec<StringRecord> = rdr.records().filter_map(|row| row.ok()).collect(); | ||
| let station_ids: Vec<u32> = records | ||
| let records: Vec<StringRecord> = rdr.records().collect::<Result<Vec<_>, _>>()?; | ||
| let station_ids: HashSet<u32> = records | ||
| .iter() | ||
| .map(|row| row.get(0).unwrap().parse::<u32>().unwrap()) | ||
| .collect(); | ||
|
|
||
| let mut rdr = ReaderBuilder::new().from_path(data_path.join("4!types.csv"))?; | ||
| let records: Vec<StringRecord> = rdr.records().filter_map(|row| row.ok()).collect(); | ||
| let type_ids: Vec<u32> = records | ||
| let records: Vec<StringRecord> = rdr.records().collect::<Result<Vec<_>, _>>()?; | ||
| let type_ids: HashSet<u32> = records | ||
| .iter() | ||
| .map(|row| row.get(1).unwrap().parse::<u32>().unwrap()) | ||
| .collect(); | ||
|
|
||
| let mut rdr = ReaderBuilder::new().from_path(data_path.join("5!station_station_types.csv"))?; | ||
| let records: Vec<StringRecord> = rdr.records().filter_map(|row| row.ok()).collect(); | ||
| let records: Vec<StringRecord> = rdr.records().collect::<Result<Vec<_>, _>>()?; | ||
|
|
||
| if let Some(invalid_record) = records | ||
| .iter() | ||
| .find(|row| !station_ids.contains(&row.get(1).unwrap().parse::<u32>().unwrap())) | ||
| { | ||
| println!( | ||
| "[INVALID] Unrecognized Station ID {:?} Found!", | ||
| invalid_record.get(1).unwrap() | ||
| ); | ||
| has_err = true; | ||
| } | ||
| for record in &records { | ||
| let line = || record.iter().collect::<Vec<&str>>().join(","); | ||
|
|
||
| if let Some(invalid_record) = records | ||
| .iter() | ||
| .find(|row| !type_ids.contains(&row.get(2).unwrap().parse::<u32>().unwrap())) | ||
| { | ||
| println!( | ||
| "[INVALID] Unrecognized Type ID {:?} Found!", | ||
| invalid_record.get(2).unwrap() | ||
| ); | ||
| has_err = true; | ||
| let station_cd: u32 = match record.get(1).and_then(|v| v.parse().ok()) { | ||
| Some(id) => id, | ||
| None => { | ||
| println!("[INVALID] Failed to parse station_cd from row: {}", line()); | ||
| invalid_station_ids.push(line()); | ||
| continue; | ||
| } | ||
| }; | ||
| let type_cd: u32 = match record.get(2).and_then(|v| v.parse().ok()) { | ||
| Some(id) => id, | ||
| None => { | ||
| println!("[INVALID] Failed to parse type_cd from row: {}", line()); | ||
| invalid_type_ids.push(line()); | ||
| continue; | ||
| } | ||
| }; | ||
|
|
||
| if !station_ids.contains(&station_cd) { | ||
| println!("[INVALID] Unrecognized Station ID {:?} Found!", station_cd); | ||
| invalid_station_ids.push(line()); | ||
| } | ||
| if !type_ids.contains(&type_cd) { | ||
| println!("[INVALID] Unrecognized Type ID {:?} Found!", type_cd); | ||
| invalid_type_ids.push(line()); | ||
| } | ||
| } | ||
|
|
||
| let has_err = !invalid_station_ids.is_empty() || !invalid_type_ids.is_empty(); | ||
coderabbitai[bot] marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| if has_err { | ||
| let report = build_markdown_report(&invalid_station_ids, &invalid_type_ids); | ||
| let report_path = | ||
| std::env::var("VALIDATION_REPORT_PATH").unwrap_or("/tmp/validation_report.md".into()); | ||
| std::fs::write(&report_path, &report)?; | ||
| eprintln!("Validation report written to {}", report_path); | ||
| panic!("[FATAL] Verification hasn't been passed!"); | ||
| } | ||
|
|
||
| println!("[VALID] No errors reported."); | ||
| Ok(()) | ||
| } | ||
|
|
||
| fn build_markdown_report(invalid_station_ids: &[String], invalid_type_ids: &[String]) -> String { | ||
| let mut md = String::new(); | ||
|
|
||
| md.push_str("<!-- data-validator -->\n"); | ||
| md.push_str("## :x: データ整合性チェックに失敗しました\n\n"); | ||
| md.push_str("`5!station_station_types.csv` に存在しない外部キーへの参照が含まれています。\n\n"); | ||
|
|
||
| if !invalid_station_ids.is_empty() { | ||
| md.push_str(&format!( | ||
| "### 不正な Station ID ({} 件)\n\n", | ||
| invalid_station_ids.len() | ||
| )); | ||
| md.push_str("`3!stations.csv` に存在しない `station_cd` が参照されています。\n\n"); | ||
| md.push_str("<details>\n<summary>該当レコード一覧</summary>\n\n"); | ||
| md.push_str("| 行データ |\n|---|\n"); | ||
| for line in invalid_station_ids { | ||
| md.push_str(&format!("| `{}` |\n", escape_markdown_cell(line))); | ||
| } | ||
coderabbitai[bot] marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| md.push_str("\n</details>\n\n"); | ||
| } | ||
|
|
||
| if !invalid_type_ids.is_empty() { | ||
| md.push_str(&format!( | ||
| "### 不正な Type ID ({} 件)\n\n", | ||
| invalid_type_ids.len() | ||
| )); | ||
| md.push_str("`4!types.csv` に存在しない `type_cd` が参照されています。\n\n"); | ||
| md.push_str("<details>\n<summary>該当レコード一覧</summary>\n\n"); | ||
| md.push_str("| 行データ |\n|---|\n"); | ||
| for line in invalid_type_ids { | ||
| md.push_str(&format!("| `{}` |\n", escape_markdown_cell(line))); | ||
| } | ||
| md.push_str("\n</details>\n\n"); | ||
| } | ||
|
|
||
| md | ||
| } | ||
|
|
||
| fn escape_markdown_cell(s: &str) -> String { | ||
| s.replace('`', "`").replace('|', "|") | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.