Calculate script optimizations, reset metrics script fixes, calculate script fixes
This commit is contained in:
@@ -58,7 +58,7 @@ CREATE TABLE IF NOT EXISTS product_metrics (
|
||||
abc_class CHAR(1),
|
||||
stock_status VARCHAR(20),
|
||||
-- Turnover metrics
|
||||
turnover_rate DECIMAL(10,3),
|
||||
turnover_rate DECIMAL(12,3),
|
||||
-- Lead time metrics
|
||||
current_lead_time INT,
|
||||
target_lead_time INT,
|
||||
@@ -68,7 +68,10 @@ CREATE TABLE IF NOT EXISTS product_metrics (
|
||||
INDEX idx_metrics_revenue (total_revenue),
|
||||
INDEX idx_metrics_stock_status (stock_status),
|
||||
INDEX idx_metrics_lead_time (lead_time_status),
|
||||
INDEX idx_metrics_turnover (turnover_rate)
|
||||
INDEX idx_metrics_turnover (turnover_rate),
|
||||
INDEX idx_metrics_last_calculated (last_calculated_at),
|
||||
INDEX idx_metrics_abc (abc_class),
|
||||
INDEX idx_metrics_sales (daily_sales_avg, weekly_sales_avg, monthly_sales_avg)
|
||||
);
|
||||
|
||||
-- New table for time-based aggregates
|
||||
@@ -117,7 +120,9 @@ CREATE TABLE IF NOT EXISTS vendor_metrics (
|
||||
PRIMARY KEY (vendor),
|
||||
FOREIGN KEY (vendor) REFERENCES vendor_details(vendor) ON DELETE CASCADE,
|
||||
INDEX idx_vendor_performance (on_time_delivery_rate),
|
||||
INDEX idx_vendor_status (status)
|
||||
INDEX idx_vendor_status (status),
|
||||
INDEX idx_metrics_last_calculated (last_calculated_at),
|
||||
INDEX idx_vendor_metrics_orders (total_orders, total_late_orders)
|
||||
);
|
||||
|
||||
-- New table for category metrics
|
||||
@@ -130,14 +135,16 @@ CREATE TABLE IF NOT EXISTS category_metrics (
|
||||
-- Financial metrics
|
||||
total_value DECIMAL(10,3) DEFAULT 0,
|
||||
avg_margin DECIMAL(5,2),
|
||||
turnover_rate DECIMAL(10,3),
|
||||
turnover_rate DECIMAL(12,3),
|
||||
growth_rate DECIMAL(5,2),
|
||||
-- Status
|
||||
status VARCHAR(20) DEFAULT 'active',
|
||||
PRIMARY KEY (category_id),
|
||||
FOREIGN KEY (category_id) REFERENCES categories(id) ON DELETE CASCADE,
|
||||
INDEX idx_category_status (status),
|
||||
INDEX idx_category_growth (growth_rate)
|
||||
INDEX idx_category_growth (growth_rate),
|
||||
INDEX idx_metrics_last_calculated (last_calculated_at),
|
||||
INDEX idx_category_metrics_products (product_count, active_products)
|
||||
);
|
||||
|
||||
-- New table for vendor time-based metrics
|
||||
@@ -170,7 +177,7 @@ CREATE TABLE IF NOT EXISTS category_time_metrics (
|
||||
total_value DECIMAL(10,3) DEFAULT 0,
|
||||
total_revenue DECIMAL(10,3) DEFAULT 0,
|
||||
avg_margin DECIMAL(5,2),
|
||||
turnover_rate DECIMAL(10,3),
|
||||
turnover_rate DECIMAL(12,3),
|
||||
PRIMARY KEY (category_id, year, month),
|
||||
FOREIGN KEY (category_id) REFERENCES categories(id) ON DELETE CASCADE,
|
||||
INDEX idx_category_date (year, month)
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -51,18 +51,23 @@ async function resetMetrics() {
|
||||
connection = await mysql.createConnection(dbConfig);
|
||||
await connection.beginTransaction();
|
||||
|
||||
// Reset all metrics tables
|
||||
// Drop all metrics tables
|
||||
for (const table of METRICS_TABLES) {
|
||||
console.log(`Truncating table: ${table}`);
|
||||
console.log(`Dropping table: ${table}`);
|
||||
try {
|
||||
await connection.query(`TRUNCATE TABLE ${table}`);
|
||||
console.log(`Successfully truncated: ${table}`);
|
||||
await connection.query(`DROP TABLE IF EXISTS ${table}`);
|
||||
console.log(`Successfully dropped: ${table}`);
|
||||
} catch (err) {
|
||||
console.error(`Error truncating ${table}:`, err.message);
|
||||
console.error(`Error dropping ${table}:`, err.message);
|
||||
throw err;
|
||||
}
|
||||
}
|
||||
|
||||
// Recreate all metrics tables from schema
|
||||
const schemaSQL = fs.readFileSync(path.resolve(__dirname, '../db/metrics-schema.sql'), 'utf8');
|
||||
await connection.query(schemaSQL);
|
||||
console.log('All metrics tables recreated successfully');
|
||||
|
||||
await connection.commit();
|
||||
console.log('All metrics tables reset successfully');
|
||||
} catch (error) {
|
||||
|
||||
@@ -487,7 +487,7 @@ export function DataManagement() {
|
||||
}
|
||||
};
|
||||
|
||||
// Check status on mount and periodically
|
||||
// Check status on mount
|
||||
useEffect(() => {
|
||||
const checkStatus = async () => {
|
||||
console.log('Checking status...');
|
||||
@@ -590,14 +590,8 @@ export function DataManagement() {
|
||||
}
|
||||
};
|
||||
|
||||
console.log('Setting up status check interval');
|
||||
console.log('Checking status on page load');
|
||||
checkStatus();
|
||||
const interval = setInterval(checkStatus, 5000);
|
||||
|
||||
return () => {
|
||||
console.log('Cleaning up status check interval');
|
||||
clearInterval(interval);
|
||||
};
|
||||
}, []);
|
||||
|
||||
const handleUpdateCSV = async () => {
|
||||
|
||||
Reference in New Issue
Block a user