const { Client } = require('pg'); async function main() { const conn = new Client({ host: 'localhost', port: 5432, user: 'postgres', password: 'turbo123', database: 'genome_db' }); await conn.connect(); const cowId = 'KOR002191643715'; const farmNo = 1; console.log('======================================================='); console.log('선발지수 계산 방식 비교 분석'); console.log('=======================================================\n'); // 1. 해당 개체의 35개 형질 EBV 확인 const traitsResult = await conn.query(` SELECT trait_name, trait_ebv FROM tb_genome_trait_detail WHERE cow_id = $1 AND del_dt IS NULL ORDER BY trait_name `, [cowId]); const traits = traitsResult.rows; console.log('=== 개체 형질 데이터 ==='); console.log('형질 수:', traits.length); // EBV 합계 const ebvSum = traits.reduce((sum, t) => sum + Number(t.trait_ebv || 0), 0); console.log('EBV 합계:', ebvSum.toFixed(2)); // EBV 평균 const ebvAvg = ebvSum / traits.length; console.log('EBV 평균:', ebvAvg.toFixed(2)); console.log('\n=== 선발지수 계산 방식 비교 ===\n'); // 방식 1: EBV 합계 (getSelectionIndex 방식) console.log('방식1 - EBV 합계 (weightedSum):', ebvSum.toFixed(2)); // 방식 2: EBV 평균 console.log('방식2 - EBV 평균 (sum/count):', ebvAvg.toFixed(2)); // 농가/보은군 평균도 각 방식으로 계산 console.log('\n=== 농가 평균 계산 방식 비교 ===\n'); // 농가 내 모든 개체 const farmCowsResult = await conn.query(` SELECT c.cow_id, SUM(gtd.trait_ebv) as sum_ebv, AVG(gtd.trait_ebv) as avg_ebv, COUNT(*) as cnt FROM tb_genome_request gr JOIN tb_cow c ON gr.fk_cow_no = c.pk_cow_no JOIN tb_genome_trait_detail gtd ON gtd.cow_id = c.cow_id AND gtd.del_dt IS NULL WHERE gr.fk_farm_no = $1 AND gr.del_dt IS NULL AND c.del_dt IS NULL AND gr.chip_sire_name = '일치' GROUP BY c.cow_id HAVING COUNT(*) = 35 `, [farmNo]); const farmCows = farmCowsResult.rows; // 방식 1: 개체별 합계의 평균 const farmSumAvg = farmCows.reduce((sum, c) => sum + Number(c.sum_ebv), 0) / farmCows.length; console.log('방식1 - 개체별 합계의 평균:', farmSumAvg.toFixed(2)); // 방식 2: 개체별 평균의 평균 const farmAvgAvg = farmCows.reduce((sum, c) => sum + Number(c.avg_ebv), 0) / farmCows.length; console.log('방식2 - 개체별 평균의 평균:', farmAvgAvg.toFixed(2)); console.log('\n=== 보은군 평균 계산 방식 비교 ===\n'); // 보은군 전체 개체 const allCowsResult = await conn.query(` SELECT c.cow_id, SUM(gtd.trait_ebv) as sum_ebv, AVG(gtd.trait_ebv) as avg_ebv, COUNT(*) as cnt FROM tb_genome_request gr JOIN tb_cow c ON gr.fk_cow_no = c.pk_cow_no JOIN tb_genome_trait_detail gtd ON gtd.cow_id = c.cow_id AND gtd.del_dt IS NULL WHERE gr.del_dt IS NULL AND c.del_dt IS NULL AND gr.chip_sire_name = '일치' GROUP BY c.cow_id HAVING COUNT(*) = 35 `); const allCows = allCowsResult.rows; // 방식 1: 개체별 합계의 평균 const regionSumAvg = allCows.reduce((sum, c) => sum + Number(c.sum_ebv), 0) / allCows.length; console.log('방식1 - 개체별 합계의 평균:', regionSumAvg.toFixed(2)); // 방식 2: 개체별 평균의 평균 const regionAvgAvg = allCows.reduce((sum, c) => sum + Number(c.avg_ebv), 0) / allCows.length; console.log('방식2 - 개체별 평균의 평균:', regionAvgAvg.toFixed(2)); console.log('\n======================================================='); console.log('결과 비교'); console.log('=======================================================\n'); console.log('만약 "합계" 방식 사용 시:'); console.log(' 내 개체:', ebvSum.toFixed(2)); console.log(' 농가 평균:', farmSumAvg.toFixed(2)); console.log(' 보은군 평균:', regionSumAvg.toFixed(2)); console.log(' → 내 개체 vs 농가: +', (ebvSum - farmSumAvg).toFixed(2)); console.log(' → 내 개체 vs 보은군: +', (ebvSum - regionSumAvg).toFixed(2)); console.log('\n만약 "평균" 방식 사용 시:'); console.log(' 내 개체:', ebvAvg.toFixed(2)); console.log(' 농가 평균:', farmAvgAvg.toFixed(2)); console.log(' 보은군 평균:', regionAvgAvg.toFixed(2)); console.log(' → 내 개체 vs 농가: +', (ebvAvg - farmAvgAvg).toFixed(2)); console.log(' → 내 개체 vs 보은군: +', (ebvAvg - regionAvgAvg).toFixed(2)); console.log('\n======================================================='); console.log('리스트 선발지수 확인 (page.tsx의 GENOMIC_TRAITS)'); console.log('=======================================================\n'); // 리스트에서 보여주는 선발지수는 어떻게 계산되나? // page.tsx:350-356 확인 필요 console.log('리스트의 overallScore 계산식 확인 필요:'); console.log(' - selectionIndex.score 사용 시: 합계 방식'); console.log(' - GENOMIC_TRAITS.reduce / length 사용 시: 평균 방식'); await conn.end(); } main().catch(console.error);