Debug metric calculations and reset scripts (still broken)

This commit is contained in:
2025-01-11 22:16:43 -05:00
parent 1eccfe0b2c
commit e48911ae24
6 changed files with 455 additions and 591 deletions

View File

@@ -15,47 +15,14 @@ function outputProgress(data) {
console.log(JSON.stringify(data));
}
function getMetricsTablesFromSchema() {
const schemaPath = path.join(__dirname, '../db/metrics-schema.sql');
const schemaSQL = fs.readFileSync(schemaPath, 'utf8');
// Extract table names from CREATE TABLE statements
const createTableRegex = /CREATE TABLE.*?`(\w+)`/g;
const tables = [];
let match;
while ((match = createTableRegex.exec(schemaSQL)) !== null) {
tables.push(match[1]);
}
return tables;
}
async function checkIndexExists(connection, tableName, indexName) {
const [rows] = await connection.query(`
SELECT COUNT(*) as count
FROM information_schema.statistics
WHERE table_schema = DATABASE()
AND table_name = ?
AND index_name = ?`,
[tableName, indexName]
);
return rows[0].count > 0;
}
async function createMetricsIndexes(connection) {
// Check and create orders index
const ordersIndexExists = await checkIndexExists(connection, 'orders', 'idx_orders_metrics');
if (!ordersIndexExists) {
await connection.query('CREATE INDEX idx_orders_metrics ON orders (product_id, date, canceled, quantity, price)');
}
// Check and create purchase_orders index
const poIndexExists = await checkIndexExists(connection, 'purchase_orders', 'idx_purchase_orders_metrics');
if (!poIndexExists) {
await connection.query('CREATE INDEX idx_purchase_orders_metrics ON purchase_orders (product_id, date, status, ordered, received)');
}
}
// Explicitly define all metrics-related tables
const METRICS_TABLES = [
'temp_sales_metrics',
'temp_purchase_metrics',
'product_metrics',
'product_time_aggregates',
'vendor_metrics'
];
async function resetMetrics() {
outputProgress({
@@ -67,55 +34,92 @@ async function resetMetrics() {
const connection = await mysql.createConnection(dbConfig);
try {
// Get list of metrics tables from schema
const metricsTables = getMetricsTablesFromSchema();
// Disable foreign key checks first
await connection.query('SET FOREIGN_KEY_CHECKS = 0');
// Get list of existing metrics tables
if (metricsTables.length > 0) {
const [tables] = await connection.query(`
SELECT GROUP_CONCAT(table_name) as tables
FROM information_schema.tables
WHERE table_schema = DATABASE()
AND table_name IN (${metricsTables.map(table => `'${table}'`).join(',')})`
);
const [tables] = await connection.query(`
SELECT GROUP_CONCAT(table_name) as tables
FROM information_schema.tables
WHERE table_schema = DATABASE()
AND table_name IN (${METRICS_TABLES.map(table => `'${table}'`).join(',')})`
);
if (tables[0].tables) {
outputProgress({
status: 'running',
operation: 'Dropping metrics tables',
percentage: '40'
});
if (tables[0].tables) {
outputProgress({
status: 'running',
operation: 'Dropping existing metrics tables',
percentage: '20'
});
// Drop all metrics tables in one query
const dropQuery = `DROP TABLE IF EXISTS ${tables[0].tables.split(',').map(table => '`' + table + '`').join(', ')}`;
await connection.query(dropQuery);
}
// Drop all existing metrics tables in one query
const dropQuery = `DROP TABLE IF EXISTS ${tables[0].tables.split(',').map(table => '`' + table + '`').join(', ')}`;
await connection.query(dropQuery);
}
// Read and execute metrics schema (without the index creation)
// Read metrics schema
outputProgress({
status: 'running',
operation: 'Creating metrics tables',
percentage: '60'
percentage: '40'
});
const schemaPath = path.join(__dirname, '../db/metrics-schema.sql');
let schemaSQL = fs.readFileSync(schemaPath, 'utf8');
const schemaSQL = fs.readFileSync(schemaPath, 'utf8');
// Remove the index creation statements from the schema
schemaSQL = schemaSQL.split('-- Create optimized indexes')[0];
await connection.query(schemaSQL);
// Split schema into parts
const parts = schemaSQL.split('-- Create optimized indexes');
const tableSchema = parts[0];
// Execute table creation first
await connection.query(tableSchema);
// Create indexes if they don't exist
// Verify all tables were created
outputProgress({
status: 'running',
operation: 'Checking and creating indexes',
operation: 'Verifying tables',
percentage: '60'
});
const [verifyTables] = await connection.query(`
SELECT table_name
FROM information_schema.tables
WHERE table_schema = DATABASE()
AND table_name IN (${METRICS_TABLES.map(table => `'${table}'`).join(',')})`
);
const missingTables = METRICS_TABLES.filter(table =>
!verifyTables.find(t => t.table_name === table)
);
if (missingTables.length > 0) {
throw new Error(`Failed to create tables: ${missingTables.join(', ')}`);
}
// Create indexes
outputProgress({
status: 'running',
operation: 'Creating indexes',
percentage: '80'
});
await createMetricsIndexes(connection);
// Drop existing indexes if they exist
try {
await connection.query('DROP INDEX IF EXISTS idx_orders_metrics ON orders');
await connection.query('DROP INDEX IF EXISTS idx_purchase_orders_metrics ON purchase_orders');
} catch (err) {
// Ignore any errors dropping indexes
console.warn('Warning dropping indexes:', err.message);
}
// Create new indexes
try {
await connection.query('CREATE INDEX idx_orders_metrics ON orders (product_id, date, canceled, quantity, price)');
await connection.query('CREATE INDEX idx_purchase_orders_metrics ON purchase_orders (product_id, date, status, ordered, received)');
} catch (err) {
// Log index creation errors but don't fail
console.warn('Warning creating indexes:', err.message);
}
// Re-enable foreign key checks
await connection.query('SET FOREIGN_KEY_CHECKS = 1');