GWM : iip calculation logs
This commit is contained in:
parent
266a2f1dbf
commit
e8cda34479
@ -15,10 +15,8 @@ class AutoFillSubmissionService {
|
|||||||
|
|
||||||
try {
|
try {
|
||||||
await connection.beginTransaction();
|
await connection.beginTransaction();
|
||||||
|
|
||||||
console.log(`\n========================================`);
|
logger.info(`**************************** Auto-filling Missing Submissions for ${year} - ${quarter} ****************************`);
|
||||||
console.log(`Auto-filling Missing Submissions for ${year} - ${quarter}`);
|
|
||||||
console.log(`========================================\n`);
|
|
||||||
|
|
||||||
// Validate quarter
|
// Validate quarter
|
||||||
const validQuarters = ['Q1', 'Q2', 'Q3', 'Q4'];
|
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
|
// 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);
|
const missingEstablishments = await this.findMissingEstablishments(connection, year, quarter);
|
||||||
|
|
||||||
if (missingEstablishments.length === 0) {
|
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();
|
await connection.commit();
|
||||||
return { success: true, establishmentsProcessed: 0, submissionsCreated: 0 };
|
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;
|
let submissionsCreated = 0;
|
||||||
|
|
||||||
// Step 2: Process each establishment
|
// Step 2: Process each establishment
|
||||||
for (const establishment of missingEstablishments) {
|
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(
|
const created = await this.createSubmissionForQuarter(
|
||||||
connection,
|
connection,
|
||||||
@ -53,19 +51,20 @@ class AutoFillSubmissionService {
|
|||||||
|
|
||||||
if (created) {
|
if (created) {
|
||||||
submissionsCreated++;
|
submissionsCreated++;
|
||||||
console.log(` ✓ Created submission for ${quarter}\n`);
|
logger.info(`Created submission for ${quarter}`);
|
||||||
} else {
|
} else {
|
||||||
console.log(` ⚠ Could not create submission (no source found)\n`);
|
logger.info(`Could not create submission (no source found)`);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
await connection.commit();
|
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 {
|
return {
|
||||||
success: true,
|
success: true,
|
||||||
@ -76,6 +75,7 @@ class AutoFillSubmissionService {
|
|||||||
} catch (error) {
|
} catch (error) {
|
||||||
await connection.rollback();
|
await connection.rollback();
|
||||||
console.error('Error in auto-fill process:', error);
|
console.error('Error in auto-fill process:', error);
|
||||||
|
logger.error(`Error in auto-fill process: ${error}`);
|
||||||
throw error;
|
throw error;
|
||||||
} finally {
|
} finally {
|
||||||
connection.release();
|
connection.release();
|
||||||
@ -117,7 +117,7 @@ class AutoFillSubmissionService {
|
|||||||
);
|
);
|
||||||
|
|
||||||
if (!sourceSubmissionId) {
|
if (!sourceSubmissionId) {
|
||||||
console.log(` ⚠ No previous submission found for establishment ${establishmentId}`);
|
logger.info(`No previous submission found for establishment ${establishmentId}`);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -151,7 +151,7 @@ class AutoFillSubmissionService {
|
|||||||
);
|
);
|
||||||
|
|
||||||
if (result.length > 0) {
|
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;
|
return result[0].id;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -167,7 +167,7 @@ class AutoFillSubmissionService {
|
|||||||
);
|
);
|
||||||
|
|
||||||
if (result.length > 0) {
|
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;
|
return result[0].id;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -182,7 +182,7 @@ class AutoFillSubmissionService {
|
|||||||
);
|
);
|
||||||
|
|
||||||
if (result.length > 0) {
|
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;
|
return result[0].id;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -196,7 +196,7 @@ class AutoFillSubmissionService {
|
|||||||
);
|
);
|
||||||
|
|
||||||
if (lastResult.length > 0) {
|
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;
|
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;
|
return newSubmissionId;
|
||||||
}
|
}
|
||||||
@ -382,15 +383,14 @@ class AutoFillSubmissionService {
|
|||||||
`;
|
`;
|
||||||
|
|
||||||
const [results] = await connection.query(query, params);
|
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) {
|
if (results.length === 0) {
|
||||||
console.log('No auto-generated submissions found.');
|
logger.info(`No auto-generated submissions found.`);
|
||||||
} else {
|
} else {
|
||||||
console.table(results);
|
logger.info(`results: ${results}`);
|
||||||
}
|
}
|
||||||
|
|
||||||
return results;
|
return results;
|
||||||
@ -421,14 +421,15 @@ class AutoFillSubmissionService {
|
|||||||
`, [year, quarter]);
|
`, [year, quarter]);
|
||||||
|
|
||||||
const stats = results[0];
|
const stats = results[0];
|
||||||
|
|
||||||
console.log('\n========================================');
|
logger.info(`========================================`);
|
||||||
console.log(`Completeness Report for ${year} - ${quarter}`);
|
logger.info(`Completeness Report for ${year} - ${quarter}`);
|
||||||
console.log('========================================');
|
logger.info(`========================================`);
|
||||||
console.log(`Total Establishments: ${stats.total_establishments}`);
|
logger.info(`Total Establishments: ${stats.total_establishments}`);
|
||||||
console.log(`Submitted ${quarter}: ${stats.submitted_establishments}`);
|
logger.info(`Submitted ${quarter}: ${stats.submitted_establishments}`);
|
||||||
console.log(`Missing ${quarter}: ${stats.missing_establishments}`);
|
logger.info(`Missing ${quarter}: ${stats.missing_establishments}`);
|
||||||
console.log('========================================\n');
|
logger.info(`========================================`);
|
||||||
|
|
||||||
|
|
||||||
return stats;
|
return stats;
|
||||||
|
|
||||||
@ -451,14 +452,14 @@ class AutoFillSubmissionService {
|
|||||||
`, [year]);
|
`, [year]);
|
||||||
|
|
||||||
const stats = results[0];
|
const stats = results[0];
|
||||||
|
|
||||||
console.log('\n========================================');
|
logger.info(`========================================`);
|
||||||
console.log(`Completeness Report for Year ${year}`);
|
logger.info(`Completeness Report for Year ${year}`);
|
||||||
console.log('========================================');
|
logger.info(`========================================`);
|
||||||
console.log(`Total Establishments: ${stats.total_establishments}`);
|
logger.info(`Total Establishments: ${stats.total_establishments}`);
|
||||||
console.log(`Complete (4 quarters): ${stats.complete_establishments}`);
|
logger.info(`Complete (4 quarters): ${stats.complete_establishments}`);
|
||||||
console.log(`Incomplete (< 4 quarters): ${stats.incomplete_establishments}`);
|
logger.info(`Incomplete (< 4 quarters): ${stats.incomplete_establishments}`);
|
||||||
console.log('========================================\n');
|
logger.info(`========================================`);
|
||||||
|
|
||||||
return stats;
|
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;
|
module.exports = AutoFillSubmissionService;
|
||||||
@ -634,9 +634,8 @@ class IPICalculationService {
|
|||||||
|
|
||||||
// MASTER FUNCTION: Run Complete Calculation Pipeline
|
// MASTER FUNCTION: Run Complete Calculation Pipeline
|
||||||
async runCompleteCalculation(year, month) {
|
async runCompleteCalculation(year, month) {
|
||||||
console.log(`\n========================================`);
|
|
||||||
console.log(`Starting IPI Calculation for ${year}-${month}`);
|
logger.info(`**************************** Starting IPI Calculation for ${year}-${month} ****************************`);
|
||||||
console.log(`========================================\n`);
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
// Step 1: Aggregate monthly production
|
// Step 1: Aggregate monthly production
|
||||||
@ -657,14 +656,14 @@ class IPICalculationService {
|
|||||||
// Step 6: Calculate Manufacturing IPI
|
// Step 6: Calculate Manufacturing IPI
|
||||||
const result = await this.calculateManufacturingIPI(year, month);
|
const result = await this.calculateManufacturingIPI(year, month);
|
||||||
|
|
||||||
console.log(`\n========================================`);
|
logger.info(`**************************** IPI Calculation Completed Successfully ****************************`);
|
||||||
console.log(`✓ IPI Calculation Completed Successfully`);
|
|
||||||
console.log(`========================================\n`);
|
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('Error in complete calculation:', error);
|
console.error('Error in complete calculation:', error);
|
||||||
|
logger.error(` Error in complete calculation: ${error}`);
|
||||||
throw error;
|
throw error;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -698,6 +697,7 @@ async logRecord(calculation_type, reference_year, reference_month = null, status
|
|||||||
|
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.error('[LOG ERROR]', err.message);
|
console.error('[LOG ERROR]', err.message);
|
||||||
|
logger.error(` Log record error: ${err.message} `);
|
||||||
} finally {
|
} finally {
|
||||||
if (!useConnection) connection.release();
|
if (!useConnection) connection.release();
|
||||||
}
|
}
|
||||||
@ -709,7 +709,8 @@ async logRecord(calculation_type, reference_year, reference_month = null, status
|
|||||||
const connection = await this.pool.getConnection();
|
const connection = await this.pool.getConnection();
|
||||||
|
|
||||||
try {
|
try {
|
||||||
console.log(`\nVerifying Base Year ${baseYear} Data...`);
|
|
||||||
|
logger.info(` Verifying Base Year ${baseYear} Data...`);
|
||||||
|
|
||||||
// Count products
|
// Count products
|
||||||
const [countResult] = await connection.query(
|
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)`,
|
WHERE base_year = ? AND (avg_by_production = 0 OR avg_by_production IS NULL)`,
|
||||||
[baseYear]
|
[baseYear]
|
||||||
);
|
);
|
||||||
|
|
||||||
console.log(`\n✓ Total products in base year: ${countResult[0].count}`);
|
logger.info(` Total products in base year: ${countResult[0].count}`);
|
||||||
console.log(`✓ Products with zero/null average: ${zeroAvgResult[0].count}`);
|
logger.info(` Products with zero/null average: ${zeroAvgResult[0].count}`);
|
||||||
console.log(`\nSample data (first 5 products):`);
|
logger.info(` Sample data (first 5 products): ${sampleData} `);
|
||||||
console.table(sampleData);
|
|
||||||
|
|
||||||
return {
|
return {
|
||||||
totalProducts: countResult[0].count,
|
totalProducts: countResult[0].count,
|
||||||
@ -763,7 +764,7 @@ async logRecord(calculation_type, reference_year, reference_month = null, status
|
|||||||
try {
|
try {
|
||||||
await connection.beginTransaction();
|
await connection.beginTransaction();
|
||||||
|
|
||||||
console.log(`Clearing calculated data for ${year}-${month}...`);
|
logger.info(` Clearing calculated data for ${year}-${month} `);
|
||||||
|
|
||||||
await connection.query(
|
await connection.query(
|
||||||
`DELETE FROM monthly_production WHERE year = ? AND month = ?`,
|
`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();
|
await connection.commit();
|
||||||
console.log(`✓ Data cleared for ${year}-${month}`);
|
logger.info(` Data cleared for ${year}-${month} `);
|
||||||
|
|
||||||
return { success: true };
|
return { success: true };
|
||||||
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
await connection.rollback();
|
await connection.rollback();
|
||||||
console.error('Error clearing data:', error);
|
console.error('Error clearing data:', error);
|
||||||
|
logger.error(` Error clearing data: ${error} `);
|
||||||
throw error;
|
throw error;
|
||||||
} finally {
|
} finally {
|
||||||
connection.release();
|
connection.release();
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user