Debug splitting normal and metrics tables
This commit is contained in:
@@ -41,6 +41,7 @@ async function calculateMetrics() {
|
||||
percentage: '0'
|
||||
});
|
||||
|
||||
// Create and truncate tables one at a time
|
||||
await connection.query(`
|
||||
CREATE TABLE IF NOT EXISTS temp_sales_metrics (
|
||||
product_id INT PRIMARY KEY,
|
||||
@@ -49,8 +50,10 @@ async function calculateMetrics() {
|
||||
average_price DECIMAL(10,2) DEFAULT 0.00,
|
||||
last_sale_date DATE,
|
||||
sales_rank INT
|
||||
);
|
||||
)
|
||||
`);
|
||||
|
||||
await connection.query(`
|
||||
CREATE TABLE IF NOT EXISTS temp_purchase_metrics (
|
||||
product_id INT PRIMARY KEY,
|
||||
total_quantity_purchased INT DEFAULT 0,
|
||||
@@ -58,12 +61,12 @@ async function calculateMetrics() {
|
||||
average_cost DECIMAL(10,2) DEFAULT 0.00,
|
||||
last_purchase_date DATE,
|
||||
purchase_rank INT
|
||||
);
|
||||
|
||||
TRUNCATE TABLE temp_sales_metrics;
|
||||
TRUNCATE TABLE temp_purchase_metrics;
|
||||
)
|
||||
`);
|
||||
|
||||
await connection.query('TRUNCATE TABLE temp_sales_metrics');
|
||||
await connection.query('TRUNCATE TABLE temp_purchase_metrics');
|
||||
|
||||
// Calculate sales metrics
|
||||
outputProgress({
|
||||
status: 'running',
|
||||
@@ -71,6 +74,7 @@ async function calculateMetrics() {
|
||||
percentage: '20'
|
||||
});
|
||||
|
||||
// First insert sales metrics
|
||||
await connection.query(`
|
||||
INSERT INTO temp_sales_metrics (
|
||||
product_id,
|
||||
@@ -87,21 +91,26 @@ async function calculateMetrics() {
|
||||
MAX(date) as last_sale_date
|
||||
FROM orders
|
||||
WHERE canceled = 0
|
||||
GROUP BY product_id;
|
||||
|
||||
UPDATE temp_sales_metrics
|
||||
SET sales_rank = (
|
||||
SELECT rank
|
||||
FROM (
|
||||
SELECT
|
||||
product_id,
|
||||
RANK() OVER (ORDER BY total_revenue DESC) as rank
|
||||
FROM temp_sales_metrics
|
||||
) rankings
|
||||
WHERE rankings.product_id = temp_sales_metrics.product_id
|
||||
);
|
||||
GROUP BY product_id
|
||||
`);
|
||||
|
||||
// Then update sales rank using a temporary table
|
||||
await connection.query(`
|
||||
CREATE TEMPORARY TABLE sales_rankings AS
|
||||
SELECT
|
||||
product_id,
|
||||
RANK() OVER (ORDER BY total_revenue DESC) as rank
|
||||
FROM temp_sales_metrics
|
||||
`);
|
||||
|
||||
await connection.query(`
|
||||
UPDATE temp_sales_metrics t
|
||||
JOIN sales_rankings r ON t.product_id = r.product_id
|
||||
SET t.sales_rank = r.rank
|
||||
`);
|
||||
|
||||
await connection.query(`DROP TEMPORARY TABLE sales_rankings`);
|
||||
|
||||
// Calculate purchase metrics
|
||||
outputProgress({
|
||||
status: 'running',
|
||||
@@ -109,6 +118,7 @@ async function calculateMetrics() {
|
||||
percentage: '40'
|
||||
});
|
||||
|
||||
// First insert purchase metrics
|
||||
await connection.query(`
|
||||
INSERT INTO temp_purchase_metrics (
|
||||
product_id,
|
||||
@@ -125,21 +135,26 @@ async function calculateMetrics() {
|
||||
MAX(received_date) as last_purchase_date
|
||||
FROM purchase_orders
|
||||
WHERE status = 'closed' AND received > 0
|
||||
GROUP BY product_id;
|
||||
|
||||
UPDATE temp_purchase_metrics
|
||||
SET purchase_rank = (
|
||||
SELECT rank
|
||||
FROM (
|
||||
SELECT
|
||||
product_id,
|
||||
RANK() OVER (ORDER BY total_cost DESC) as rank
|
||||
FROM temp_purchase_metrics
|
||||
) rankings
|
||||
WHERE rankings.product_id = temp_purchase_metrics.product_id
|
||||
);
|
||||
GROUP BY product_id
|
||||
`);
|
||||
|
||||
// Then update purchase rank using a temporary table
|
||||
await connection.query(`
|
||||
CREATE TEMPORARY TABLE purchase_rankings AS
|
||||
SELECT
|
||||
product_id,
|
||||
RANK() OVER (ORDER BY total_cost DESC) as rank
|
||||
FROM temp_purchase_metrics
|
||||
`);
|
||||
|
||||
await connection.query(`
|
||||
UPDATE temp_purchase_metrics t
|
||||
JOIN purchase_rankings r ON t.product_id = r.product_id
|
||||
SET t.purchase_rank = r.rank
|
||||
`);
|
||||
|
||||
await connection.query(`DROP TEMPORARY TABLE purchase_rankings`);
|
||||
|
||||
// Update product metrics
|
||||
outputProgress({
|
||||
status: 'running',
|
||||
|
||||
Reference in New Issue
Block a user