SmartCane/09_run_calculate_kpis.sh
Timon d5fd4bb463 Add KPI reporting system and deployment documentation
Major Changes:
- NEW: Scripts 09 & 10 for KPI calculation and enhanced reporting
- NEW: Shell script wrappers (01-10) for easier execution
- NEW: R packages flextable and officer for enhanced Word reports
- NEW: DEPLOYMENT_README.md with complete deployment guide
- RENAMED: Numbered R scripts (02, 03, 04) for clarity
- REMOVED: Old package management scripts (using renv only)
- UPDATED: Workflow now uses scripts 09->10 instead of 05

Files Changed: 90+ files
New Packages: flextable, officer
New Scripts: 09_run_calculate_kpis.sh, 10_run_kpi_report.sh
Documentation: DEPLOYMENT_README.md, EMAIL_TO_ADMIN.txt

See DEPLOYMENT_README.md for full deployment instructions.
2025-10-14 11:49:30 +02:00

188 lines
5.6 KiB
Bash

#!/bin/bash
# 09_RUN_CALCULATE_KPIS.SH
# ======================
# Shell script wrapper for KPI calculation in the SmartCane pipeline
# This script integrates KPI calculation into the existing pipeline sequence (01-05)
# and ensures proper R execution with renv environment and error handling.
# Script configuration
SCRIPT_NAME="09_run_calculate_kpis.sh"
R_SCRIPT_NAME="09_calculate_kpis.R"
LOG_PREFIX="[KPI_CALC]"
# Function to log messages with timestamp
log_message() {
echo "$(date '+%Y-%m-%d %H:%M:%S') $LOG_PREFIX $1"
}
# Function to handle errors
handle_error() {
log_message "ERROR: $1"
exit 1
}
# Function to check if file exists
check_file() {
if [ ! -f "$1" ]; then
handle_error "Required file not found: $1"
fi
}
# Function to check if directory exists
check_directory() {
if [ ! -d "$1" ]; then
log_message "WARNING: Directory not found: $1"
return 1
fi
return 0
}
# Main execution function
main() {
log_message "Starting KPI calculation pipeline step"
# Check if we're in the correct directory
if [ ! -f "r_app/$R_SCRIPT_NAME" ]; then
handle_error "Must be run from smartcane root directory (where r_app/ folder exists)"
fi
# Check for R installation
if ! command -v R &> /dev/null; then
# Try Windows R installation path
R_CMD="C:/Program Files/R/R-4.4.3/bin/x64/R.exe"
if [ ! -f "$R_CMD" ]; then
handle_error "R not found in PATH or at expected Windows location"
fi
else
R_CMD="R"
fi
log_message "Using R at: $R_CMD"
# Set default project directory if not provided
if [ -z "$1" ]; then
PROJECT_DIR="esa"
log_message "No project directory specified, using default: $PROJECT_DIR"
else
PROJECT_DIR="$1"
log_message "Using project directory: $PROJECT_DIR"
fi
# Check if project directory exists
PROJECT_PATH="laravel_app/storage/app/$PROJECT_DIR"
check_directory "$PROJECT_PATH" || handle_error "Project directory not found: $PROJECT_PATH"
# Check for required data files
check_file "$PROJECT_PATH/Data/pivot.geojson"
# Check for weekly mosaic directory
MOSAIC_DIR="$PROJECT_PATH/weekly_mosaic"
check_directory "$MOSAIC_DIR" || handle_error "Weekly mosaic directory not found: $MOSAIC_DIR"
# Count available mosaics
MOSAIC_COUNT=$(find "$MOSAIC_DIR" -name "week_*.tif" 2>/dev/null | wc -l)
if [ "$MOSAIC_COUNT" -lt 1 ]; then
handle_error "No weekly mosaics found in $MOSAIC_DIR"
fi
log_message "Found $MOSAIC_COUNT weekly mosaics in $MOSAIC_DIR"
# Create temporary R script with project configuration
TEMP_R_SCRIPT="temp_kpi_calc_$$.R"
cat > "r_app/$TEMP_R_SCRIPT" << EOF
# Temporary KPI calculation script
# Generated by $SCRIPT_NAME on $(date)
# Set project directory
project_dir <- "$PROJECT_DIR"
# Set working directory to r_app
setwd("r_app")
# Source the main KPI calculation script
tryCatch({
source("$R_SCRIPT_NAME")
cat("✓ KPI calculation completed successfully\\n")
}, error = function(e) {
cat("✗ Error in KPI calculation:", e\$message, "\\n")
quit(status = 1)
})
EOF
log_message "Created temporary R script: r_app/$TEMP_R_SCRIPT"
# Execute R script
log_message "Starting R execution..."
# Change to smartcane root directory for proper relative paths
cd "$(dirname "$0")" || handle_error "Failed to change to script directory"
# Run R script with proper error handling
if [[ "$OSTYPE" == "msys" || "$OSTYPE" == "win32" ]]; then
# Windows execution
"$R_CMD" --vanilla < "r_app/$TEMP_R_SCRIPT"
R_EXIT_CODE=$?
else
# Unix/Linux execution
"$R_CMD" --vanilla < "r_app/$TEMP_R_SCRIPT"
R_EXIT_CODE=$?
fi
# Clean up temporary script
rm -f "r_app/$TEMP_R_SCRIPT"
log_message "Cleaned up temporary R script"
# Check R execution result
if [ $R_EXIT_CODE -eq 0 ]; then
log_message "✓ KPI calculation completed successfully"
# Check if output files were created
REPORTS_DIR="laravel_app/storage/app/$PROJECT_DIR/reports"
if check_directory "$REPORTS_DIR/kpis"; then
KPI_FILES=$(find "$REPORTS_DIR/kpis" -name "*$(date '+%Y%m%d')*" 2>/dev/null | wc -l)
if [ "$KPI_FILES" -gt 0 ]; then
log_message "✓ Generated $KPI_FILES KPI output files"
else
log_message "⚠ Warning: No KPI files found for today's date"
fi
fi
log_message "KPI calculation pipeline step completed successfully"
return 0
else
handle_error "R script execution failed with exit code: $R_EXIT_CODE"
fi
}
# Script usage information
usage() {
echo "Usage: $0 [PROJECT_DIR]"
echo ""
echo "Calculate KPI metrics for SmartCane monitoring system"
echo ""
echo "Parameters:"
echo " PROJECT_DIR Project directory name (default: esa)"
echo " Must exist in laravel_app/storage/app/"
echo ""
echo "Examples:"
echo " $0 # Use default 'esa' project"
echo " $0 aura # Use 'aura' project"
echo " $0 chemba # Use 'chemba' project"
echo ""
echo "Requirements:"
echo " - R installation (4.4.3 or compatible)"
echo " - renv environment set up"
echo " - Weekly mosaic files in PROJECT_DIR/weekly_mosaic/"
echo " - Field boundaries in PROJECT_DIR/Data/pivot.geojson"
}
# Handle command line arguments
case "${1:-}" in
-h|--help)
usage
exit 0
;;
*)
main "$@"
;;
esac