#!/usr/bin/env Rscript # Diagnostic script to validate KPI RDS file structure # Usage: Rscript test_kpi_validation.R [project] [date] # Set up arguments args <- commandArgs(trailingOnly = TRUE) if (length(args) < 2) { cat("Usage: Rscript test_kpi_validation.R [project] [date]\n") cat("Example: Rscript test_kpi_validation.R aura 2022-11-14\n") quit(status = 1) } project_dir <- args[1] report_date <- as.Date(args[2]) cat("\n=== KPI RDS Validation ===\n") cat("Project:", project_dir, "\n") cat("Date:", report_date, "\n") # Load utilities source("r_app/parameters_project.R") source("r_app/00_common_utils.R") # Set up paths paths <- setup_project_directories(project_dir) kpi_data_dir <- paths$kpi_reports_dir # Calculate week current_week <- as.numeric(format(as.Date(report_date), "%V")) current_year <- as.numeric(format(as.Date(report_date), "%G")) kpi_rds_filename <- paste0(project_dir, "_kpi_summary_tables_week", sprintf("%02d_%d", current_week, current_year), ".rds") kpi_rds_path <- file.path(kpi_data_dir, kpi_rds_filename) cat("\nKPI directory:", kpi_data_dir, "\n") cat("KPI filename:", kpi_rds_filename, "\n") cat("Full path:", kpi_rds_path, "\n\n") # Check if directory exists if (!dir.exists(kpi_data_dir)) { cat("ERROR: KPI directory does not exist!\n") quit(status = 1) } # List available files cat("Files in KPI directory:\n") files <- list.files(kpi_data_dir, pattern = "\\.rds$") if (length(files) == 0) { cat(" (none)\n") } else { for (f in files) { cat(" -", f, "\n") } } # Check if our specific file exists if (!file.exists(kpi_rds_path)) { cat("\nWARNING: Expected KPI file not found!\n") cat("Expected:", kpi_rds_filename, "\n") quit(status = 1) } cat("\n✓ KPI file found. Loading...\n\n") # Load the RDS loaded_data <- readRDS(kpi_rds_path) # Inspect structure cat("=== RDS Structure ===\n") cat("Class:", class(loaded_data), "\n") cat("Length:", length(loaded_data), "\n") cat("Names:", paste(names(loaded_data), collapse = ", "), "\n\n") # Check if new or legacy structure if (is.list(loaded_data) && "summary_tables" %in% names(loaded_data)) { cat("✓ New structure detected (has $summary_tables)\n\n") summary_tables <- loaded_data$summary_tables if ("field_details" %in% names(loaded_data)) { cat("✓ Also has $field_details\n\n") } } else { cat("✓ Legacy structure (direct list of KPI tables)\n\n") summary_tables <- loaded_data } # Now inspect the summary_tables cat("=== Available KPI Tables ===\n") cat("Keys:", paste(names(summary_tables), collapse = ", "), "\n\n") # Expected KPIs expected_kpis <- c( "uniformity", "area_change", "tch_forecasted", "growth_decline", "weed_pressure", "gap_filling" ) cat("=== Expected vs Actual ===\n") for (kpi in expected_kpis) { # Try both formats found <- FALSE actual_key <- NA if (kpi %in% names(summary_tables)) { found <- TRUE actual_key <- kpi } else if (paste0(kpi, "_summary") %in% names(summary_tables)) { found <- TRUE actual_key <- paste0(kpi, "_summary") } status <- if (found) "✓ FOUND" else "✗ MISSING" cat(sprintf("%-20s %s", kpi, status)) if (found) { cat(" (key: ", actual_key, ")") } cat("\n") } cat("\n=== Detailed KPI Contents ===\n") for (kpi_key in names(summary_tables)) { kpi_df <- summary_tables[[kpi_key]] cat("\n", kpi_key, ":\n", sep="") cat(" Class:", class(kpi_df), "\n") cat(" Dimensions:", nrow(kpi_df), "rows ×", ncol(kpi_df), "cols\n") cat(" Columns:", paste(names(kpi_df), collapse = ", "), "\n") if (nrow(kpi_df) > 0) { cat(" First few rows:\n") print(head(kpi_df, 3)) } else { cat(" (empty dataframe)\n") } } cat("\n=== Validation Summary ===\n") missing_count <- sum(!expected_kpis %in% c(names(summary_tables), paste0(expected_kpis, "_summary"))) if (missing_count == 0) { cat("✓ All expected KPIs are present!\n") } else { cat("✗ Missing", missing_count, "KPI(s):\n") for (kpi in expected_kpis) { if (!kpi %in% names(summary_tables) && !paste0(kpi, "_summary") %in% names(summary_tables)) { cat(" -", kpi, "\n") } } } cat("\n")