Files
tilbudgivern/backend/functions/analyzer.sh
Alex 10473e8a32 feat: Add complete data import and incremental update scripts for Ordrestyring
- Implemented setup_ordrestyring_import.sh for initial database setup and data import from API.
- Created sync_ordrestyring_data.sh for incremental updates to the local database.
- Developed test_ordrestyring_import.js to validate API data import functionality.
- Added test_enhanced_quote_demo.js to demonstrate the enhanced quote system using hybrid data sources.
- Introduced test_ordrestyring_system.sh for comprehensive testing of the database system and API functionality.
- Enhanced logging and error handling across scripts for better traceability.
- Included analysis script creation for post-import data analysis.
2025-09-19 15:06:03 +02:00

160 lines
6.0 KiB
Bash

#!/usr/bin/env bash
# Analyzer functions for tag tasks
# Search for specific task types
search_task_type() {
local task_type="$1"
echo "Søger efter $task_type opgaver..."
if [[ -f "$DATA_DIR/all_cases.jsonl" ]]; then
case "$task_type" in
"tag")
grep -i "tag\|eternit\|tegl\|rende\|velux\|rygning" "$DATA_DIR/all_cases.jsonl" > "$DATA_DIR/${task_type}_cases.jsonl" 2>/dev/null || echo -n ""
;;
"b7")
grep -i "b7\|fiber\|cement\|plader" "$DATA_DIR/all_cases.jsonl" > "$DATA_DIR/${task_type}_cases.jsonl" 2>/dev/null || echo -n ""
;;
*)
grep -i "$task_type" "$DATA_DIR/all_cases.jsonl" > "$DATA_DIR/${task_type}_cases.jsonl" 2>/dev/null || echo -n ""
;;
esac
local count=$(wc -l < "$DATA_DIR/${task_type}_cases.jsonl" 2>/dev/null || echo "0")
echo "Fundet $count $task_type opgaver"
fi
}
# Analyze task type
analyze_task_type() {
local task_type="$1"
local cases_file="$DATA_DIR/${task_type}_cases.jsonl"
if [[ ! -f "$cases_file" ]]; then
echo "Ingen data fundet for $task_type"
return 1
fi
local report_file="$REPORTS_DIR/${task_type}_analysis.txt"
echo "=== $task_type OPGAVER ANALYSE ===" > "$report_file"
echo "Genereret: $(date)" >> "$report_file"
echo "" >> "$report_file"
local total_cases=$(wc -l < "$cases_file")
echo "Total opgaver: $total_cases" >> "$report_file"
# Basic statistics
echo "" >> "$report_file"
echo "=== STATISTIK ===" >> "$report_file"
if command -v jq >/dev/null 2>&1; then
# Status distribution
echo "Status fordeling:" >> "$report_file"
jq -r '.status' "$cases_file" 2>/dev/null | sort | uniq -c | sort -nr >> "$report_file"
echo "" >> "$report_file"
# Cases with offers
local with_offers=$(jq -r 'select(.offer_number != null and .offer_number != "") | .case_number' "$cases_file" 2>/dev/null | wc -l)
echo "Cases med tilbud: $with_offers" >> "$report_file"
fi
echo "Rapport gemt: $report_file"
}
# Generate tag experience JSON for project flow
generate_tag_experience_json() {
local api_key="$1"
echo "Genererer tag erfaring JSON..."
local json_file="$REPORTS_DIR/tag_experience_suggestions.json"
# Find tag cases
search_task_type "tag"
local tag_cases_file="$DATA_DIR/tag_cases.jsonl"
if [[ ! -f "$tag_cases_file" ]]; then
echo "Ingen tag cases fundet"
return 1
fi
# Start building JSON structure
echo '{' > "$json_file"
echo ' "generated": "'$(date -Iseconds)'",' >> "$json_file"
echo ' "source": "ordrestyring_api",' >> "$json_file"
echo ' "task_suggestions": [' >> "$json_file"
local first_entry=true
# Process each tag case to extract task suggestions
while IFS= read -r line; do
if command -v jq >/dev/null 2>&1; then
local case_number=$(echo "$line" | jq -r '.case_number // empty')
local description=$(echo "$line" | jq -r '.description // empty')
local status=$(echo "$line" | jq -r '.status // empty')
# Skip empty descriptions
if [[ -z "$description" || "$description" == "null" ]]; then
continue
fi
# Try to get hours data for this case
local hours_data=""
if [[ -f "$DATA_DIR/hours_data.jsonl" ]]; then
hours_data=$(grep -F "\"case_number\":\"$case_number\"" "$DATA_DIR/hours_data.jsonl" 2>/dev/null || echo "")
fi
local total_hours=0
local cost_price=0
if [[ -n "$hours_data" ]]; then
total_hours=$(echo "$hours_data" | jq -r '.hours_registered // 0' 2>/dev/null || echo "0")
cost_price=$(echo "$hours_data" | jq -r '.costprice_total_oere // 0' 2>/dev/null || echo "0")
# Convert øre to kroner
cost_price=$(echo "scale=2; $cost_price / 100" | bc 2>/dev/null || echo "0")
fi
# Only include cases with actual hours data
if [[ "$total_hours" != "0" && "$total_hours" != "null" ]]; then
if [[ "$first_entry" == false ]]; then
echo ' },' >> "$json_file"
fi
echo ' {' >> "$json_file"
echo " \"case_number\": \"$case_number\"," >> "$json_file"
echo " \"task_description\": \"$(echo "$description" | sed 's/"/\\"/g')\"," >> "$json_file"
echo " \"estimated_hours\": $total_hours," >> "$json_file"
echo " \"estimated_cost\": $cost_price," >> "$json_file"
echo " \"hourly_rate\": $(echo "scale=0; $cost_price / $total_hours" | bc 2>/dev/null || echo "500")," >> "$json_file"
echo " \"status\": \"$status\"," >> "$json_file"
echo " \"category\": \"tag_arbejde\"," >> "$json_file"
echo " \"confidence\": 0.8" >> "$json_file"
first_entry=false
fi
fi
done < "$tag_cases_file"
# Close the last entry if any
if [[ "$first_entry" == false ]]; then
echo ' }' >> "$json_file"
fi
echo ' ]' >> "$json_file"
echo '}' >> "$json_file"
echo "Tag erfaring JSON genereret: $json_file"
# Show summary
if command -v jq >/dev/null 2>&1; then
local suggestion_count=$(jq '.task_suggestions | length' "$json_file" 2>/dev/null || echo "0")
echo "Antal opgave forslag: $suggestion_count"
if [[ "$suggestion_count" -gt 0 ]]; then
echo ""
echo "=== TOP 5 TAG OPGAVER ==="
jq -r '.task_suggestions[:5] | .[] | "• \(.task_description) - \(.estimated_hours)t (\(.estimated_cost) kr)"' "$json_file" 2>/dev/null || true
fi
fi
}