GWM : iip calculation logs
This commit is contained in:
parent
266a2f1dbf
commit
e8cda34479
@ -15,10 +15,8 @@ class AutoFillSubmissionService {
|
||||
|
||||
try {
|
||||
await connection.beginTransaction();
|
||||
|
||||
console.log(`\n========================================`);
|
||||
console.log(`Auto-filling Missing Submissions for ${year} - ${quarter}`);
|
||||
console.log(`========================================\n`);
|
||||
|
||||
logger.info(`**************************** Auto-filling Missing Submissions for ${year} - ${quarter} ****************************`);
|
||||
|
||||
// Validate quarter
|
||||
const validQuarters = ['Q1', 'Q2', 'Q3', 'Q4'];
|
||||
@ -27,22 +25,22 @@ class AutoFillSubmissionService {
|
||||
}
|
||||
|
||||
// Step 1: Find establishments that don't have submission for this specific quarter
|
||||
console.log(`Step 1: Finding establishments missing ${quarter} submissions...`);
|
||||
logger.info(`Step 1: Finding establishments missing ${quarter} submissions...`);
|
||||
const missingEstablishments = await this.findMissingEstablishments(connection, year, quarter);
|
||||
|
||||
if (missingEstablishments.length === 0) {
|
||||
console.log(`✓ All establishments have submitted ${quarter} for ${year}!`);
|
||||
logger.info(`All establishments have submitted ${quarter} for ${year}!`);
|
||||
await connection.commit();
|
||||
return { success: true, establishmentsProcessed: 0, submissionsCreated: 0 };
|
||||
}
|
||||
|
||||
console.log(`Found ${missingEstablishments.length} establishments missing ${quarter} submission\n`);
|
||||
logger.info(`Found ${missingEstablishments.length} establishments missing ${quarter} submission`);
|
||||
|
||||
let submissionsCreated = 0;
|
||||
|
||||
// Step 2: Process each establishment
|
||||
for (const establishment of missingEstablishments) {
|
||||
console.log(`Processing Establishment ID: ${establishment.establishment_id} (${establishment.factory_name})`);
|
||||
logger.info(`Processing Establishment ID: ${establishment.establishment_id} (${establishment.factory_name})`);
|
||||
|
||||
const created = await this.createSubmissionForQuarter(
|
||||
connection,
|
||||
@ -53,19 +51,20 @@ class AutoFillSubmissionService {
|
||||
|
||||
if (created) {
|
||||
submissionsCreated++;
|
||||
console.log(` ✓ Created submission for ${quarter}\n`);
|
||||
logger.info(`Created submission for ${quarter}`);
|
||||
} else {
|
||||
console.log(` ⚠ Could not create submission (no source found)\n`);
|
||||
logger.info(`Could not create submission (no source found)`);
|
||||
}
|
||||
}
|
||||
|
||||
await connection.commit();
|
||||
|
||||
logger.info(`========================================`);
|
||||
logger.info(`Auto-fill Completed Successfully`);
|
||||
logger.info(`Establishments Processed: ${missingEstablishments.length}`);
|
||||
logger.info(`Submissions Created: ${submissionsCreated}`);
|
||||
logger.info(`========================================`);
|
||||
|
||||
console.log(`========================================`);
|
||||
console.log(`✓ Auto-fill Completed Successfully`);
|
||||
console.log(` Establishments Processed: ${missingEstablishments.length}`);
|
||||
console.log(` Submissions Created: ${submissionsCreated}`);
|
||||
console.log(`========================================\n`);
|
||||
|
||||
return {
|
||||
success: true,
|
||||
@ -76,6 +75,7 @@ class AutoFillSubmissionService {
|
||||
} catch (error) {
|
||||
await connection.rollback();
|
||||
console.error('Error in auto-fill process:', error);
|
||||
logger.error(`Error in auto-fill process: ${error}`);
|
||||
throw error;
|
||||
} finally {
|
||||
connection.release();
|
||||
@ -117,7 +117,7 @@ class AutoFillSubmissionService {
|
||||
);
|
||||
|
||||
if (!sourceSubmissionId) {
|
||||
console.log(` ⚠ No previous submission found for establishment ${establishmentId}`);
|
||||
logger.info(`No previous submission found for establishment ${establishmentId}`);
|
||||
return false;
|
||||
}
|
||||
|
||||
@ -151,7 +151,7 @@ class AutoFillSubmissionService {
|
||||
);
|
||||
|
||||
if (result.length > 0) {
|
||||
console.log(` Using source: ${year} ${previousQuarter} (submission ID: ${result[0].id})`);
|
||||
logger.info(`Using source: ${year} ${previousQuarter} (submission ID: ${result[0].id})`);
|
||||
return result[0].id;
|
||||
}
|
||||
}
|
||||
@ -167,7 +167,7 @@ class AutoFillSubmissionService {
|
||||
);
|
||||
|
||||
if (result.length > 0) {
|
||||
console.log(` Using source: ${year - 1} Q4 (submission ID: ${result[0].id})`);
|
||||
logger.info(`Using source: ${year - 1} Q4 (submission ID: ${result[0].id})`);
|
||||
return result[0].id;
|
||||
}
|
||||
}
|
||||
@ -182,7 +182,7 @@ class AutoFillSubmissionService {
|
||||
);
|
||||
|
||||
if (result.length > 0) {
|
||||
console.log(` Using source: ${year - 1} ${quarter} (submission ID: ${result[0].id})`);
|
||||
logger.info(`Using source: ${year - 1} ${quarter} (submission ID: ${result[0].id})`);
|
||||
return result[0].id;
|
||||
}
|
||||
|
||||
@ -196,7 +196,7 @@ class AutoFillSubmissionService {
|
||||
);
|
||||
|
||||
if (lastResult.length > 0) {
|
||||
console.log(` Using source: ${lastResult[0].year} ${lastResult[0].quarter} (submission ID: ${lastResult[0].id})`);
|
||||
logger.info(`Using source: ${lastResult[0].year} ${lastResult[0].quarter} (submission ID: ${lastResult[0].id})`);
|
||||
return lastResult[0].id;
|
||||
}
|
||||
|
||||
@ -332,7 +332,8 @@ class AutoFillSubmissionService {
|
||||
);
|
||||
}
|
||||
|
||||
console.log(` ✓ Created submission ID ${newSubmissionId} for ${quarter} using forecast from submission ${sourceSubmissionId}`);
|
||||
logger.info(`Created submission ID ${newSubmissionId} for ${quarter} using forecast from submission ${sourceSubmissionId}`);
|
||||
|
||||
|
||||
return newSubmissionId;
|
||||
}
|
||||
@ -382,15 +383,14 @@ class AutoFillSubmissionService {
|
||||
`;
|
||||
|
||||
const [results] = await connection.query(query, params);
|
||||
|
||||
logger.info(`Auto-Fill Report for Year ${year}${quarter ? ' - ' + quarter : ''}`);
|
||||
|
||||
console.log('\n========================================');
|
||||
console.log(`Auto-Fill Report for Year ${year}${quarter ? ' - ' + quarter : ''}`);
|
||||
console.log('========================================\n');
|
||||
|
||||
if (results.length === 0) {
|
||||
console.log('No auto-generated submissions found.');
|
||||
logger.info(`No auto-generated submissions found.`);
|
||||
} else {
|
||||
console.table(results);
|
||||
logger.info(`results: ${results}`);
|
||||
}
|
||||
|
||||
return results;
|
||||
@ -421,14 +421,15 @@ class AutoFillSubmissionService {
|
||||
`, [year, quarter]);
|
||||
|
||||
const stats = results[0];
|
||||
|
||||
console.log('\n========================================');
|
||||
console.log(`Completeness Report for ${year} - ${quarter}`);
|
||||
console.log('========================================');
|
||||
console.log(`Total Establishments: ${stats.total_establishments}`);
|
||||
console.log(`Submitted ${quarter}: ${stats.submitted_establishments}`);
|
||||
console.log(`Missing ${quarter}: ${stats.missing_establishments}`);
|
||||
console.log('========================================\n');
|
||||
|
||||
logger.info(`========================================`);
|
||||
logger.info(`Completeness Report for ${year} - ${quarter}`);
|
||||
logger.info(`========================================`);
|
||||
logger.info(`Total Establishments: ${stats.total_establishments}`);
|
||||
logger.info(`Submitted ${quarter}: ${stats.submitted_establishments}`);
|
||||
logger.info(`Missing ${quarter}: ${stats.missing_establishments}`);
|
||||
logger.info(`========================================`);
|
||||
|
||||
|
||||
return stats;
|
||||
|
||||
@ -451,14 +452,14 @@ class AutoFillSubmissionService {
|
||||
`, [year]);
|
||||
|
||||
const stats = results[0];
|
||||
|
||||
console.log('\n========================================');
|
||||
console.log(`Completeness Report for Year ${year}`);
|
||||
console.log('========================================');
|
||||
console.log(`Total Establishments: ${stats.total_establishments}`);
|
||||
console.log(`Complete (4 quarters): ${stats.complete_establishments}`);
|
||||
console.log(`Incomplete (< 4 quarters): ${stats.incomplete_establishments}`);
|
||||
console.log('========================================\n');
|
||||
|
||||
logger.info(`========================================`);
|
||||
logger.info(`Completeness Report for Year ${year}`);
|
||||
logger.info(`========================================`);
|
||||
logger.info(`Total Establishments: ${stats.total_establishments}`);
|
||||
logger.info(`Complete (4 quarters): ${stats.complete_establishments}`);
|
||||
logger.info(`Incomplete (< 4 quarters): ${stats.incomplete_establishments}`);
|
||||
logger.info(`========================================`);
|
||||
|
||||
return stats;
|
||||
}
|
||||
@ -474,48 +475,6 @@ class AutoFillSubmissionService {
|
||||
}
|
||||
}
|
||||
|
||||
// ==========================================================================
|
||||
// USAGE EXAMPLE
|
||||
// ==========================================================================
|
||||
|
||||
const dbConfig = {
|
||||
host: 'localhost',
|
||||
user: 'your_user',
|
||||
password: 'your_password',
|
||||
database: 'your_database',
|
||||
waitForConnections: true,
|
||||
connectionLimit: 10,
|
||||
queueLimit: 0
|
||||
};
|
||||
|
||||
// Example usage
|
||||
async function main() {
|
||||
const service = new AutoFillSubmissionService(dbConfig);
|
||||
|
||||
try {
|
||||
const year = 2022;
|
||||
const quarter = 'Q1';
|
||||
|
||||
// Auto-fill missing submissions for specific year and quarter
|
||||
const result = await service.autoFillMissingSubmissions(year, quarter);
|
||||
|
||||
// Get report for specific quarter
|
||||
await service.getAutoFillReport(year, quarter);
|
||||
|
||||
// Verify completeness for specific quarter
|
||||
await service.verifyCompleteness(year, quarter);
|
||||
|
||||
// Or verify entire year
|
||||
// await service.verifyCompleteness(year);
|
||||
|
||||
} catch (error) {
|
||||
console.error('Error:', error);
|
||||
} finally {
|
||||
await service.close();
|
||||
}
|
||||
}
|
||||
|
||||
// Uncomment to run
|
||||
// main();
|
||||
|
||||
module.exports = AutoFillSubmissionService;
|
||||
@ -634,9 +634,8 @@ class IPICalculationService {
|
||||
|
||||
// MASTER FUNCTION: Run Complete Calculation Pipeline
|
||||
async runCompleteCalculation(year, month) {
|
||||
console.log(`\n========================================`);
|
||||
console.log(`Starting IPI Calculation for ${year}-${month}`);
|
||||
console.log(`========================================\n`);
|
||||
|
||||
logger.info(`**************************** Starting IPI Calculation for ${year}-${month} ****************************`);
|
||||
|
||||
try {
|
||||
// Step 1: Aggregate monthly production
|
||||
@ -657,14 +656,14 @@ class IPICalculationService {
|
||||
// Step 6: Calculate Manufacturing IPI
|
||||
const result = await this.calculateManufacturingIPI(year, month);
|
||||
|
||||
console.log(`\n========================================`);
|
||||
console.log(`✓ IPI Calculation Completed Successfully`);
|
||||
console.log(`========================================\n`);
|
||||
logger.info(`**************************** IPI Calculation Completed Successfully ****************************`);
|
||||
|
||||
|
||||
return result;
|
||||
|
||||
} catch (error) {
|
||||
console.error('Error in complete calculation:', error);
|
||||
logger.error(` Error in complete calculation: ${error}`);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
@ -698,6 +697,7 @@ async logRecord(calculation_type, reference_year, reference_month = null, status
|
||||
|
||||
} catch (err) {
|
||||
console.error('[LOG ERROR]', err.message);
|
||||
logger.error(` Log record error: ${err.message} `);
|
||||
} finally {
|
||||
if (!useConnection) connection.release();
|
||||
}
|
||||
@ -709,7 +709,8 @@ async logRecord(calculation_type, reference_year, reference_month = null, status
|
||||
const connection = await this.pool.getConnection();
|
||||
|
||||
try {
|
||||
console.log(`\nVerifying Base Year ${baseYear} Data...`);
|
||||
|
||||
logger.info(` Verifying Base Year ${baseYear} Data...`);
|
||||
|
||||
// Count products
|
||||
const [countResult] = await connection.query(
|
||||
@ -738,11 +739,11 @@ async logRecord(calculation_type, reference_year, reference_month = null, status
|
||||
WHERE base_year = ? AND (avg_by_production = 0 OR avg_by_production IS NULL)`,
|
||||
[baseYear]
|
||||
);
|
||||
|
||||
console.log(`\n✓ Total products in base year: ${countResult[0].count}`);
|
||||
console.log(`✓ Products with zero/null average: ${zeroAvgResult[0].count}`);
|
||||
console.log(`\nSample data (first 5 products):`);
|
||||
console.table(sampleData);
|
||||
|
||||
logger.info(` Total products in base year: ${countResult[0].count}`);
|
||||
logger.info(` Products with zero/null average: ${zeroAvgResult[0].count}`);
|
||||
logger.info(` Sample data (first 5 products): ${sampleData} `);
|
||||
|
||||
|
||||
return {
|
||||
totalProducts: countResult[0].count,
|
||||
@ -763,7 +764,7 @@ async logRecord(calculation_type, reference_year, reference_month = null, status
|
||||
try {
|
||||
await connection.beginTransaction();
|
||||
|
||||
console.log(`Clearing calculated data for ${year}-${month}...`);
|
||||
logger.info(` Clearing calculated data for ${year}-${month} `);
|
||||
|
||||
await connection.query(
|
||||
`DELETE FROM monthly_production WHERE year = ? AND month = ?`,
|
||||
@ -796,13 +797,14 @@ async logRecord(calculation_type, reference_year, reference_month = null, status
|
||||
);
|
||||
|
||||
await connection.commit();
|
||||
console.log(`✓ Data cleared for ${year}-${month}`);
|
||||
logger.info(` Data cleared for ${year}-${month} `);
|
||||
|
||||
return { success: true };
|
||||
|
||||
} catch (error) {
|
||||
await connection.rollback();
|
||||
console.error('Error clearing data:', error);
|
||||
logger.error(` Error clearing data: ${error} `);
|
||||
throw error;
|
||||
} finally {
|
||||
connection.release();
|
||||
|
||||
Loading…
Reference in New Issue
Block a user