phase 5 qa and review pass

This commit is contained in:
2026-08-18 22:21:45 +01:00
parent bd7459e455
commit 42a697aa57
4 changed files with 295 additions and 168 deletions
+67 -8
View File
@@ -1,3 +1,4 @@
// src/main.rs
mod api;
mod cli;
mod db;
@@ -19,7 +20,6 @@ struct LoggerGuard {
fn init_logging(log_file_path: Option<&str>) -> LoggerGuard {
let filter = EnvFilter::try_from_default_env().unwrap_or_else(|_| EnvFilter::new("info"));
let is_json = std::env::var("HAGFISH_LOG_FORMAT").as_deref() == Ok("json");
match log_file_path {
@@ -28,7 +28,7 @@ fn init_logging(log_file_path: Option<&str>) -> LoggerGuard {
let (non_blocking, guard) = tracing_appender::non_blocking(file);
let subscriber = tracing_subscriber::fmt()
.with_env_filter(filter.clone())
.with_env_filter(filter)
.with_writer(non_blocking);
if is_json {
@@ -42,7 +42,7 @@ fn init_logging(log_file_path: Option<&str>) -> LoggerGuard {
}
}
None => {
let subscriber = tracing_subscriber::fmt().with_env_filter(filter.clone());
let subscriber = tracing_subscriber::fmt().with_env_filter(filter);
if is_json {
subscriber.json().init();
@@ -89,15 +89,20 @@ async fn main() -> anyhow::Result<()> {
if default_path.exists() {
types::Config::load(default_path)?
} else {
tracing::warn!("No config file found, using defaults");
eprintln!("WARN: No config file found, using defaults");
types::Config::default()
}
}
};
// Initialize logging AFTER config is loaded so we have log_file_path
let _logger_guard = init_logging(config.log_file_path.as_deref());
tracing::info!(
duckdb_path = %config.duckdb_path,
bind_address = %config.bind_address,
"configuration loaded"
);
match cli.command {
cli::Command::Serve => run_serve(&config).await,
cli::Command::Ingest { full } => run_ingest(&config, full).await,
@@ -134,7 +139,9 @@ async fn run_ingest(config: &types::Config, full: bool) -> anyhow::Result<()> {
);
let conn = db::init(&config.duckdb_path)?;
let client = reqwest::Client::new();
let client = reqwest::Client::builder()
.timeout(std::time::Duration::from_secs(60))
.build()?;
let (lookup_maps, metadata) = ingest::fetch_metadata(&client, &config.data_source_url)
.await
@@ -219,7 +226,7 @@ async fn run_ingest(config: &types::Config, full: bool) -> anyhow::Result<()> {
for j in 0..data.dataset.value.len() {
match ingest::parse_row(j, &data.dataset, &lookup_maps) {
Ok(row) => {
landings.push(ingest::data_row_to_landing(&row));
landings.push(ingest::data_row_to_landing(row));
}
Err(e) => {
tracing::warn!(
@@ -260,7 +267,6 @@ async fn run_ingest(config: &types::Config, full: bool) -> anyhow::Result<()> {
"batch complete"
);
// Backoff between batches to avoid rate limiting
if i < batches.len() - 1 {
tokio::time::sleep(std::time::Duration::from_secs(1)).await;
}
@@ -300,3 +306,56 @@ fn run_export(config: &types::Config, out: &Path) -> anyhow::Result<()> {
Ok(())
}
#[cfg(test)]
mod tests {
use super::*;
use crate::types::Config;
#[test]
fn test_config_default_values() {
let config = Config::default();
assert_eq!(config.duckdb_path, "hagfish.db");
assert_eq!(config.bind_address, "127.0.0.1:8090");
assert!(config.data_source_url.contains("statbank.hagstova.fo"));
}
#[test]
fn test_run_export_writes_valid_parquet() {
let tmp_dir = tempfile::tempdir().unwrap();
let db_path = tmp_dir.path().join("test.db");
let parquet_path = tmp_dir.path().join("export.parquet");
let conn = db::init(db_path.to_str().unwrap()).unwrap();
let rows = vec![crate::types::Landing {
month: "2024M01".to_string(),
species_code: "COD".to_string(),
species_label: "Toskur".to_string(),
gear_code: "TOTAL".to_string(),
zone_code: "TOTAL".to_string(),
processing_code: "TOTAL".to_string(),
preservation_code: "TOTAL".to_string(),
shipsize_code: "TOTAL".to_string(),
measure_code: "MASS".to_string(),
value: Some(1000.0),
}];
db::upsert_landings(&conn, &rows).unwrap();
drop(conn);
let config = Config {
duckdb_path: db_path.to_str().unwrap().to_string(),
..Default::default()
};
let result = run_export(&config, &parquet_path);
assert!(result.is_ok());
let metadata = std::fs::metadata(&parquet_path).unwrap();
assert!(metadata.len() > 0);
let file_bytes = std::fs::read(&parquet_path).unwrap();
assert_eq!(&file_bytes[..4], b"PAR1");
}
}