const mysql = require('mysql2/promise'); const path = require('path'); const dotenv = require('dotenv'); const fs = require('fs'); dotenv.config({ path: path.join(__dirname, '../.env') }); const dbConfig = { host: process.env.DB_HOST, user: process.env.DB_USER, password: process.env.DB_PASSWORD, database: process.env.DB_NAME, multipleStatements: true, waitForConnections: true, connectionLimit: 10, queueLimit: 0, namedPlaceholders: true }; // Set up logging const LOG_DIR = path.join(__dirname, '../logs'); const ERROR_LOG = path.join(LOG_DIR, 'import-errors.log'); const IMPORT_LOG = path.join(LOG_DIR, 'import.log'); // Ensure log directory exists if (!fs.existsSync(LOG_DIR)) { fs.mkdirSync(LOG_DIR, { recursive: true }); } // Helper function to log errors function logError(error, context = '') { const timestamp = new Date().toISOString(); const errorMessage = `[${timestamp}] ${context}\nError: ${error.message}\nStack: ${error.stack}\n\n`; fs.appendFileSync(ERROR_LOG, errorMessage); console.error(`\n${context}\nError: ${error.message}`); } // Helper function to log progress function outputProgress(data) { const timestamp = new Date().toISOString(); const logMessage = `[${timestamp}] ${JSON.stringify(data)}\n`; fs.appendFileSync(IMPORT_LOG, logMessage); console.log(JSON.stringify(data)); } async function resetMetrics() { let pool; try { pool = mysql.createPool(dbConfig); const connection = await pool.getConnection(); try { outputProgress({ status: 'running', operation: 'Starting metrics reset', message: 'Creating/resetting metrics tables...', percentage: '0' }); // Create tables if they don't exist and then truncate them await connection.query(` CREATE TABLE IF NOT EXISTS temp_sales_metrics ( product_id INT PRIMARY KEY, total_quantity_sold INT DEFAULT 0, total_revenue DECIMAL(10,2) DEFAULT 0.00, average_price DECIMAL(10,2) DEFAULT 0.00, last_sale_date DATE, sales_rank INT ); CREATE TABLE IF NOT EXISTS temp_purchase_metrics ( product_id INT PRIMARY KEY, total_quantity_purchased INT DEFAULT 0, total_cost DECIMAL(10,2) DEFAULT 0.00, average_cost DECIMAL(10,2) DEFAULT 0.00, last_purchase_date DATE, purchase_rank INT ); CREATE TABLE IF NOT EXISTS product_metrics ( product_id INT PRIMARY KEY, total_quantity_sold INT DEFAULT 0, total_revenue DECIMAL(10,2) DEFAULT 0.00, average_price DECIMAL(10,2) DEFAULT 0.00, total_quantity_purchased INT DEFAULT 0, total_cost DECIMAL(10,2) DEFAULT 0.00, average_cost DECIMAL(10,2) DEFAULT 0.00, profit_margin DECIMAL(5,2) DEFAULT 0.00, turnover_rate DECIMAL(5,2) DEFAULT 0.00, abc_class CHAR(1), last_calculated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, last_sale_date DATE, last_purchase_date DATE, sales_rank INT, purchase_rank INT ); CREATE TABLE IF NOT EXISTS product_time_aggregates ( product_id INT, period_type ENUM('daily', 'weekly', 'monthly', 'quarterly', 'yearly'), period_start DATE, quantity_sold INT DEFAULT 0, revenue DECIMAL(10,2) DEFAULT 0.00, quantity_purchased INT DEFAULT 0, purchase_cost DECIMAL(10,2) DEFAULT 0.00, PRIMARY KEY (product_id, period_type, period_start) ); CREATE TABLE IF NOT EXISTS vendor_metrics ( vendor VARCHAR(255) PRIMARY KEY, total_orders INT DEFAULT 0, total_items_ordered INT DEFAULT 0, total_items_received INT DEFAULT 0, total_spend DECIMAL(10,2) DEFAULT 0.00, average_order_value DECIMAL(10,2) DEFAULT 0.00, fulfillment_rate DECIMAL(5,2) DEFAULT 0.00, average_delivery_days DECIMAL(5,1), last_order_date DATE, last_delivery_date DATE ); TRUNCATE TABLE temp_sales_metrics; TRUNCATE TABLE temp_purchase_metrics; TRUNCATE TABLE product_metrics; TRUNCATE TABLE product_time_aggregates; TRUNCATE TABLE vendor_metrics; `); outputProgress({ status: 'complete', operation: 'Metrics reset completed', message: 'All metrics tables have been created/cleared', percentage: '100' }); } catch (error) { logError(error, 'Error resetting metrics tables'); outputProgress({ status: 'error', error: error.message, operation: 'Failed to reset metrics' }); throw error; } finally { connection.release(); } } catch (error) { logError(error, 'Fatal error during metrics reset'); outputProgress({ status: 'error', error: error.message, operation: 'Failed to reset metrics' }); throw error; } finally { if (pool) { await pool.end(); } } } // Run if called directly if (require.main === module) { resetMetrics().catch(error => { logError(error, 'Unhandled error in main process'); process.exit(1); }); } module.exports = resetMetrics;