const { outputProgress, formatElapsedTime, estimateRemaining, calculateRate, logError } = require('./utils/progress'); const { getConnection } = require('./utils/db'); async function calculateBrandMetrics(startTime, totalProducts, processedCount = 0, isCancelled = false) { const connection = await getConnection(); let success = false; const BATCH_SIZE = 5000; try { // Get last calculation timestamp const [lastCalc] = await connection.query(` SELECT last_calculation_timestamp FROM calculate_status WHERE module_name = 'brand_metrics' `); const lastCalculationTime = lastCalc[0]?.last_calculation_timestamp || '1970-01-01'; // Get total count of brands needing updates const [brandCount] = await connection.query(` SELECT COUNT(DISTINCT p.brand) as count FROM products p LEFT JOIN orders o ON p.pid = o.pid AND o.updated > ? WHERE p.brand IS NOT NULL AND ( p.updated > ? OR o.id IS NOT NULL ) `, [lastCalculationTime, lastCalculationTime]); const totalBrands = brandCount[0].count; if (totalBrands === 0) { console.log('No brands need metric updates'); return { processedProducts: 0, processedOrders: 0, processedPurchaseOrders: 0, success: true }; } if (isCancelled) { outputProgress({ status: 'cancelled', operation: 'Brand metrics calculation cancelled', current: processedCount, total: totalBrands, elapsed: formatElapsedTime(startTime), remaining: null, rate: calculateRate(startTime, processedCount), percentage: ((processedCount / totalBrands) * 100).toFixed(1), timing: { start_time: new Date(startTime).toISOString(), end_time: new Date().toISOString(), elapsed_seconds: Math.round((Date.now() - startTime) / 1000) } }); return { processedProducts: processedCount, processedOrders: 0, processedPurchaseOrders: 0, success }; } outputProgress({ status: 'running', operation: 'Starting brand metrics calculation', current: processedCount, total: totalBrands, elapsed: formatElapsedTime(startTime), remaining: estimateRemaining(startTime, processedCount, totalBrands), rate: calculateRate(startTime, processedCount), percentage: ((processedCount / totalBrands) * 100).toFixed(1), timing: { start_time: new Date(startTime).toISOString(), end_time: new Date().toISOString(), elapsed_seconds: Math.round((Date.now() - startTime) / 1000) } }); // Process in batches let lastBrand = ''; while (true) { if (isCancelled) break; const [batch] = await connection.query(` SELECT DISTINCT p.brand FROM products p WHERE p.brand IS NOT NULL AND p.brand > ? AND ( p.updated > ? OR EXISTS ( SELECT 1 FROM orders o WHERE o.pid = p.pid AND o.updated > ? ) ) ORDER BY p.brand LIMIT ? `, [lastBrand, lastCalculationTime, lastCalculationTime, BATCH_SIZE]); if (batch.length === 0) break; // Update brand metrics for this batch await connection.query(` INSERT INTO brand_metrics ( brand, product_count, active_products, total_stock_units, total_stock_cost, total_stock_retail, total_revenue, avg_margin, growth_rate, last_calculated_at ) WITH product_stats AS ( SELECT p.brand, COUNT(DISTINCT p.pid) as product_count, COUNT(DISTINCT CASE WHEN p.visible = true THEN p.pid END) as active_products, SUM(p.stock_quantity) as total_stock_units, SUM(p.stock_quantity * p.cost_price) as total_stock_cost, SUM(p.stock_quantity * p.price) as total_stock_retail, SUM(pm.total_revenue) as total_revenue, AVG(pm.avg_margin_percent) as avg_margin FROM products p LEFT JOIN product_metrics pm ON p.pid = pm.pid WHERE p.brand IN (?) AND ( p.updated > ? OR EXISTS ( SELECT 1 FROM orders o WHERE o.pid = p.pid AND o.updated > ? ) ) GROUP BY p.brand ), sales_periods AS ( SELECT p.brand, SUM(CASE WHEN o.date >= DATE_SUB(CURRENT_DATE, INTERVAL 30 DAY) THEN o.quantity * o.price ELSE 0 END) as current_period_sales, SUM(CASE WHEN o.date BETWEEN DATE_SUB(CURRENT_DATE, INTERVAL 60 DAY) AND DATE_SUB(CURRENT_DATE, INTERVAL 30 DAY) THEN o.quantity * o.price ELSE 0 END) as previous_period_sales FROM products p INNER JOIN orders o ON p.pid = o.pid AND o.canceled = false AND o.date >= DATE_SUB(CURRENT_DATE, INTERVAL 60 DAY) AND o.updated > ? WHERE p.brand IN (?) GROUP BY p.brand ) SELECT ps.brand, COALESCE(ps.product_count, 0) as product_count, COALESCE(ps.active_products, 0) as active_products, COALESCE(ps.total_stock_units, 0) as total_stock_units, COALESCE(ps.total_stock_cost, 0) as total_stock_cost, COALESCE(ps.total_stock_retail, 0) as total_stock_retail, COALESCE(ps.total_revenue, 0) as total_revenue, COALESCE(ps.avg_margin, 0) as avg_margin, CASE WHEN COALESCE(sp.previous_period_sales, 0) = 0 AND COALESCE(sp.current_period_sales, 0) > 0 THEN 100 WHEN COALESCE(sp.previous_period_sales, 0) = 0 THEN 0 ELSE LEAST(999.99, GREATEST(-100, ((COALESCE(sp.current_period_sales, 0) / sp.previous_period_sales) - 1) * 100 )) END as growth_rate, NOW() as last_calculated_at FROM product_stats ps LEFT JOIN sales_periods sp ON ps.brand = sp.brand ON DUPLICATE KEY UPDATE product_count = VALUES(product_count), active_products = VALUES(active_products), total_stock_units = VALUES(total_stock_units), total_stock_cost = VALUES(total_stock_cost), total_stock_retail = VALUES(total_stock_retail), total_revenue = VALUES(total_revenue), avg_margin = VALUES(avg_margin), growth_rate = VALUES(growth_rate), last_calculated_at = NOW() `, [ batch.map(row => row.brand), // For first IN clause lastCalculationTime, // For p.updated > ? lastCalculationTime, // For o.updated > ? in EXISTS lastCalculationTime, // For o.updated > ? in sales_periods batch.map(row => row.brand) // For second IN clause ]); lastBrand = batch[batch.length - 1].brand; processedCount += batch.length; outputProgress({ status: 'running', operation: 'Processing brand metrics batch', current: processedCount, total: totalBrands, elapsed: formatElapsedTime(startTime), remaining: estimateRemaining(startTime, processedCount, totalBrands), rate: calculateRate(startTime, processedCount), percentage: ((processedCount / totalBrands) * 100).toFixed(1), timing: { start_time: new Date(startTime).toISOString(), end_time: new Date().toISOString(), elapsed_seconds: Math.round((Date.now() - startTime) / 1000) } }); } // If we get here, everything completed successfully success = true; // Update calculate_status await connection.query(` INSERT INTO calculate_status (module_name, last_calculation_timestamp) VALUES ('brand_metrics', NOW()) ON DUPLICATE KEY UPDATE last_calculation_timestamp = NOW() `); return { processedProducts: processedCount, processedOrders: 0, processedPurchaseOrders: 0, success }; } catch (error) { success = false; logError(error, 'Error calculating brand metrics'); throw error; } finally { if (connection) { connection.release(); } } } module.exports = calculateBrandMetrics;