308 lines
8.9 KiB
Bash
Executable File
308 lines
8.9 KiB
Bash
Executable File
#!/bin/bash
|
|
#
|
|
# Ordrestyring Complete Data Import Setup
|
|
# Sets up database and imports all API data locally
|
|
#
|
|
|
|
set -e # Exit on any error
|
|
|
|
# Colors for output
|
|
RED='\033[0;31m'
|
|
GREEN='\033[0;32m'
|
|
YELLOW='\033[1;33m'
|
|
BLUE='\033[0;34m'
|
|
NC='\033[0m' # No Color
|
|
|
|
# Configuration
|
|
DB_NAME="ordrestyring_local"
|
|
DB_USER="ordrestyring_user"
|
|
DB_PASS="secure_password123"
|
|
MYSQL_ROOT_PASS="your_root_password"
|
|
|
|
echo -e "${BLUE}🚀 Ordrestyring Complete Data Import Setup${NC}"
|
|
echo -e "${BLUE}================================================${NC}"
|
|
|
|
# Check if Bearer token is provided
|
|
if [ -z "$ORDRESTYRING_TOKEN" ]; then
|
|
echo -e "${RED}❌ Error: ORDRESTYRING_TOKEN environment variable is required${NC}"
|
|
echo "Usage: ORDRESTYRING_TOKEN=your_token ./setup_ordrestyring_import.sh"
|
|
exit 1
|
|
fi
|
|
|
|
echo -e "${GREEN}✅ Bearer token found${NC}"
|
|
|
|
# Function to check if MySQL is running
|
|
check_mysql() {
|
|
if ! systemctl is-active --quiet mysql; then
|
|
echo -e "${RED}❌ MySQL is not running. Please start MySQL first:${NC}"
|
|
echo "sudo systemctl start mysql"
|
|
exit 1
|
|
fi
|
|
echo -e "${GREEN}✅ MySQL is running${NC}"
|
|
}
|
|
|
|
# Function to create database and user
|
|
setup_database() {
|
|
echo -e "${YELLOW}📊 Setting up database...${NC}"
|
|
|
|
# Create database and user
|
|
mysql -u root -p"$MYSQL_ROOT_PASS" << EOF
|
|
-- Create database
|
|
CREATE DATABASE IF NOT EXISTS $DB_NAME CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
|
|
|
|
-- Create user
|
|
CREATE USER IF NOT EXISTS '$DB_USER'@'localhost' IDENTIFIED BY '$DB_PASS';
|
|
GRANT ALL PRIVILEGES ON $DB_NAME.* TO '$DB_USER'@'localhost';
|
|
FLUSH PRIVILEGES;
|
|
|
|
-- Show databases
|
|
SHOW DATABASES LIKE '$DB_NAME';
|
|
EOF
|
|
|
|
if [ $? -eq 0 ]; then
|
|
echo -e "${GREEN}✅ Database and user created successfully${NC}"
|
|
else
|
|
echo -e "${RED}❌ Failed to create database and user${NC}"
|
|
exit 1
|
|
fi
|
|
}
|
|
|
|
# Function to create tables
|
|
create_tables() {
|
|
echo -e "${YELLOW}🏗️ Creating database tables...${NC}"
|
|
|
|
mysql -u "$DB_USER" -p"$DB_PASS" "$DB_NAME" < create_ordrestyring_tables.sql
|
|
|
|
if [ $? -eq 0 ]; then
|
|
echo -e "${GREEN}✅ Database tables created successfully${NC}"
|
|
else
|
|
echo -e "${RED}❌ Failed to create database tables${NC}"
|
|
exit 1
|
|
fi
|
|
}
|
|
|
|
# Function to check Node.js dependencies
|
|
check_dependencies() {
|
|
echo -e "${YELLOW}📦 Checking Node.js dependencies...${NC}"
|
|
|
|
# Check if package.json exists
|
|
if [ ! -f "package.json" ]; then
|
|
echo -e "${RED}❌ package.json not found${NC}"
|
|
exit 1
|
|
fi
|
|
|
|
# Install dependencies if node_modules doesn't exist
|
|
if [ ! -d "node_modules" ]; then
|
|
echo -e "${YELLOW}📦 Installing Node.js dependencies...${NC}"
|
|
npm install
|
|
fi
|
|
|
|
# Check for required packages
|
|
required_packages=("mysql2" "axios")
|
|
for package in "${required_packages[@]}"; do
|
|
if ! npm list "$package" &>/dev/null; then
|
|
echo -e "${YELLOW}📦 Installing missing package: $package${NC}"
|
|
npm install "$package"
|
|
fi
|
|
done
|
|
|
|
echo -e "${GREEN}✅ Dependencies checked${NC}"
|
|
}
|
|
|
|
# Function to run the import
|
|
run_import() {
|
|
echo -e "${YELLOW}📥 Starting full data import...${NC}"
|
|
echo -e "${BLUE}This may take several minutes depending on the amount of data${NC}"
|
|
|
|
# Set environment variables for the Node.js script
|
|
export ORDRESTYRING_TOKEN="$ORDRESTYRING_TOKEN"
|
|
|
|
# Run the import script
|
|
node ordrestyring_full_import.js
|
|
|
|
if [ $? -eq 0 ]; then
|
|
echo -e "${GREEN}🎉 Data import completed successfully!${NC}"
|
|
|
|
# Show import summary if available
|
|
if [ -f "last_import_summary.json" ]; then
|
|
echo -e "${BLUE}📊 Import Summary:${NC}"
|
|
cat last_import_summary.json | jq -r '
|
|
"Start time: " + .startTime + "\n" +
|
|
"End time: " + .endTime + "\n" +
|
|
"Duration: " + (.duration | tostring) + " seconds\n" +
|
|
"\nStatistics:" +
|
|
(.statistics | to_entries[] | "\n " + .key + ": " +
|
|
(.value.imported | tostring) + " imported, " +
|
|
(.value.updated | tostring) + " updated, " +
|
|
(.value.failed | tostring) + " failed")
|
|
'
|
|
fi
|
|
|
|
else
|
|
echo -e "${RED}❌ Data import failed${NC}"
|
|
exit 1
|
|
fi
|
|
}
|
|
|
|
# Function to test database connection and show sample data
|
|
test_database() {
|
|
echo -e "${YELLOW}🔍 Testing database and showing sample data...${NC}"
|
|
|
|
mysql -u "$DB_USER" -p"$DB_PASS" "$DB_NAME" << EOF
|
|
-- Show table counts
|
|
SELECT 'cases' as table_name, COUNT(*) as record_count FROM ordrestyring_cases
|
|
UNION ALL
|
|
SELECT 'materials', COUNT(*) FROM ordrestyring_case_materials
|
|
UNION ALL
|
|
SELECT 'hours', COUNT(*) FROM ordrestyring_hours
|
|
UNION ALL
|
|
SELECT 'debtors', COUNT(*) FROM ordrestyring_debtors
|
|
UNION ALL
|
|
SELECT 'invoices', COUNT(*) FROM ordrestyring_debtor_invoices
|
|
UNION ALL
|
|
SELECT 'users', COUNT(*) FROM ordrestyring_users;
|
|
|
|
-- Show sample case with details
|
|
SELECT
|
|
case_number,
|
|
customer_name,
|
|
case_type_name,
|
|
case_status_name,
|
|
creation_datetime,
|
|
description
|
|
FROM ordrestyring_cases_with_details
|
|
ORDER BY creation_date DESC
|
|
LIMIT 5;
|
|
EOF
|
|
|
|
echo -e "${GREEN}✅ Database test completed${NC}"
|
|
}
|
|
|
|
# Function to create a quick analysis script
|
|
create_analysis_script() {
|
|
echo -e "${YELLOW}📝 Creating analysis script...${NC}"
|
|
|
|
cat > analyze_ordrestyring_data.js << 'EOF'
|
|
#!/usr/bin/env node
|
|
/**
|
|
* Quick Analysis of Imported Ordrestyring Data
|
|
*/
|
|
|
|
const mysql = require('mysql2/promise');
|
|
|
|
const CONFIG = {
|
|
host: 'localhost',
|
|
user: 'ordrestyring_user',
|
|
password: process.env.DB_PASSWORD,
|
|
database: 'ordrestyring_local',
|
|
charset: 'utf8mb4'
|
|
};
|
|
|
|
async function analyzeData() {
|
|
const db = await mysql.createConnection(CONFIG);
|
|
|
|
console.log('📊 Ordrestyring Data Analysis');
|
|
console.log('=' .repeat(40));
|
|
|
|
// Basic counts
|
|
const queries = {
|
|
'Total Cases': 'SELECT COUNT(*) as count FROM ordrestyring_cases',
|
|
'Total Materials': 'SELECT COUNT(*) as count FROM ordrestyring_case_materials',
|
|
'Total Hours': 'SELECT COUNT(*) as count FROM ordrestyring_hours',
|
|
'Total Customers': 'SELECT COUNT(*) as count FROM ordrestyring_debtors',
|
|
'Total Invoices': 'SELECT COUNT(*) as count FROM ordrestyring_debtor_invoices'
|
|
};
|
|
|
|
for (const [label, query] of Object.entries(queries)) {
|
|
const [rows] = await db.execute(query);
|
|
console.log(`${label}: ${rows[0].count}`);
|
|
}
|
|
|
|
console.log('\n📈 Case Analysis by Year:');
|
|
const [yearlyData] = await db.execute(`
|
|
SELECT
|
|
YEAR(FROM_UNIXTIME(creation_date)) as year,
|
|
COUNT(*) as cases,
|
|
COUNT(DISTINCT customer_number) as unique_customers
|
|
FROM ordrestyring_cases
|
|
WHERE creation_date > 0
|
|
GROUP BY YEAR(FROM_UNIXTIME(creation_date))
|
|
ORDER BY year DESC
|
|
`);
|
|
|
|
yearlyData.forEach(row => {
|
|
console.log(` ${row.year}: ${row.cases} cases, ${row.unique_customers} customers`);
|
|
});
|
|
|
|
console.log('\n💰 Top 10 Cases by Material Value:');
|
|
const [topCases] = await db.execute(`
|
|
SELECT
|
|
c.case_number,
|
|
d.customer_name,
|
|
c.description,
|
|
cf.total_material_sales,
|
|
cf.total_hours
|
|
FROM ordrestyring_case_financials cf
|
|
JOIN ordrestyring_cases c ON cf.case_number = c.case_number
|
|
LEFT JOIN ordrestyring_debtors d ON cf.customer_number = d.customer_number
|
|
WHERE cf.total_material_sales > 0
|
|
ORDER BY cf.total_material_sales DESC
|
|
LIMIT 10
|
|
`);
|
|
|
|
topCases.forEach((row, i) => {
|
|
console.log(` ${i+1}. ${row.case_number} - ${row.customer_name}: ${row.total_material_sales} DKK (${row.total_hours}h)`);
|
|
});
|
|
|
|
await db.end();
|
|
}
|
|
|
|
analyzeData().catch(console.error);
|
|
EOF
|
|
|
|
chmod +x analyze_ordrestyring_data.js
|
|
echo -e "${GREEN}✅ Analysis script created: analyze_ordrestyring_data.js${NC}"
|
|
}
|
|
|
|
# Main execution
|
|
main() {
|
|
echo -e "${BLUE}Starting setup process...${NC}"
|
|
|
|
# Step 1: Check MySQL
|
|
check_mysql
|
|
|
|
# Step 2: Setup database
|
|
setup_database
|
|
|
|
# Step 3: Create tables
|
|
create_tables
|
|
|
|
# Step 4: Check dependencies
|
|
check_dependencies
|
|
|
|
# Step 5: Run import
|
|
run_import
|
|
|
|
# Step 6: Test database
|
|
test_database
|
|
|
|
# Step 7: Create analysis script
|
|
create_analysis_script
|
|
|
|
echo -e "${GREEN}🎉 Setup completed successfully!${NC}"
|
|
echo ""
|
|
echo -e "${BLUE}Next steps:${NC}"
|
|
echo "1. Run analysis: ./analyze_ordrestyring_data.js"
|
|
echo "2. Set up regular imports (cron job)"
|
|
echo "3. Update your application to use local data"
|
|
echo ""
|
|
echo -e "${YELLOW}Database connection details:${NC}"
|
|
echo "Host: localhost"
|
|
echo "Database: $DB_NAME"
|
|
echo "User: $DB_USER"
|
|
echo "Password: $DB_PASS"
|
|
}
|
|
|
|
# Run main function
|
|
main
|