Improve sync logging

Add more detailed logging for sync functionality to improve issue tracking.
This commit is contained in:
gpt-engineer-app[bot]
2025-03-21 11:39:44 +00:00
parent 5903734503
commit e1c6875024
7 changed files with 427 additions and 189 deletions

View File

@@ -3,27 +3,83 @@ import { supabase } from '@/lib/supabase';
import { Transaction } from '@/components/TransactionCard';
import { isSyncEnabled } from '../syncSettings';
import { normalizeDate } from './dateUtils';
import { getDeletedTransactions, removeFromDeletedTransactions } from './deletedTransactionsTracker';
/**
* Upload transaction data from local storage to Supabase
* 로컬 데이터를 서버에 업로드 (새로운 또는 수정된 데이터만)
*/
export const uploadTransactions = async (userId: string): Promise<void> => {
if (!isSyncEnabled()) return;
if (!isSyncEnabled()) {
console.log('[동기화] 업로드: 동기화 비활성화 상태, 작업 건너뜀');
return;
}
try {
console.log('[동기화] 트랜잭션 업로드 시작');
const uploadStartTime = new Date().toISOString();
// 로컬 트랜잭션 데이터 로드
const localTransactions = localStorage.getItem('transactions');
if (!localTransactions) return;
if (!localTransactions) {
console.log('[동기화] 로컬 트랜잭션 데이터 없음, 업로드 건너뜀');
return;
}
// 트랜잭션 파싱
const transactions: Transaction[] = JSON.parse(localTransactions);
console.log(`로컬 트랜잭션 ${transactions.length}개 동기화 시작`);
console.log(`[동기화] 로컬 트랜잭션 ${transactions.length}개 동기화 시작`);
if (transactions.length === 0) return; // 트랜잭션이 없으면 처리하지 않음
if (transactions.length === 0) {
console.log('[동기화] 트랜잭션이 없음, 업로드 건너뜀');
return; // 트랜잭션이 없으면 처리하지 않음
}
// 삭제된 트랜잭션 처리
const deletedIds = getDeletedTransactions();
if (deletedIds.length > 0) {
console.log(`[동기화] 삭제된 트랜잭션 ${deletedIds.length}개 처리 시작`);
// 100개씩 나눠서 처리 (대용량 데이터 처리)
const batchSize = 100;
for (let i = 0; i < deletedIds.length; i += batchSize) {
const batch = deletedIds.slice(i, i + batchSize);
console.log(`[동기화] 삭제 배치 처리 중: ${i+1}~${Math.min(i+batch.length, deletedIds.length)}/${deletedIds.length}`);
// 각 삭제된 ID 처리 (병렬 처리)
const deletePromises = batch.map(async (id) => {
try {
const { error } = await supabase
.from('transactions')
.delete()
.eq('transaction_id', id)
.eq('user_id', userId);
if (error) {
console.error(`[동기화] 트랜잭션 삭제 실패 (ID: ${id}):`, error);
return { id, success: false };
} else {
console.log(`[동기화] 트랜잭션 삭제 성공: ${id}`);
removeFromDeletedTransactions(id);
return { id, success: true };
}
} catch (err) {
console.error(`[동기화] 트랜잭션 삭제 중 오류 (ID: ${id}):`, err);
return { id, success: false };
}
});
// 병렬 처리 대기
const results = await Promise.all(deletePromises);
const successCount = results.filter(r => r.success).length;
console.log(`[동기화] 삭제 배치 처리 결과: ${successCount}/${batch.length} 성공`);
}
}
// 먼저 서버에서 현재 트랜잭션 목록 가져오기
const { data: existingData, error: fetchError } = await supabase
.from('transactions')
.select('transaction_id')
.select('transaction_id, updated_at')
.eq('user_id', userId);
if (fetchError) {
@@ -33,8 +89,12 @@ export const uploadTransactions = async (userId: string): Promise<void> => {
}
// 서버에 이미 있는 트랜잭션 ID 맵 생성
const existingIds = new Set(existingData?.map(t => t.transaction_id) || []);
console.log(`서버에 이미 존재하는 트랜잭션: ${existingIds.size}`);
const existingMap = new Map();
existingData?.forEach(t => {
existingMap.set(t.transaction_id, t.updated_at);
});
console.log(`[동기화] 서버에 이미 존재하는 트랜잭션: ${existingMap.size}`);
// 삽입할 새 트랜잭션과 업데이트할 기존 트랜잭션 분리
const newTransactions = [];
@@ -42,9 +102,18 @@ export const uploadTransactions = async (userId: string): Promise<void> => {
for (const t of transactions) {
try {
// 삭제 목록에 있는 트랜잭션은 건너뜀
if (deletedIds.includes(t.id)) {
console.log(`[동기화] 삭제된 항목 건너뜀: ${t.id}`);
continue;
}
// 날짜 형식 정규화
const normalizedDate = normalizeDate(t.date);
// 현재 시간을 타임스탬프로 사용
const timestamp = t.localTimestamp || uploadStartTime;
const transactionData = {
user_id: userId,
title: t.title || '무제',
@@ -53,13 +122,24 @@ export const uploadTransactions = async (userId: string): Promise<void> => {
category: t.category || '기타',
type: t.type || 'expense',
transaction_id: t.id,
notes: t.notes || null
notes: t.notes || null,
updated_at: timestamp
};
if (existingIds.has(t.id)) {
updateTransactions.push(transactionData);
// 서버에 이미 존재하는지 확인
if (existingMap.has(t.id)) {
// 서버 타임스탬프와 비교
const serverTimestamp = existingMap.get(t.id);
// 로컬 데이터가 더 최신인 경우만 업데이트
if (!serverTimestamp || timestamp > serverTimestamp) {
updateTransactions.push(transactionData);
console.log(`[동기화] 업데이트 필요: ${t.id} - ${t.title} (로컬: ${timestamp}, 서버: ${serverTimestamp || '없음'})`);
} else {
console.log(`[동기화] 업데이트 불필요: ${t.id} - ${t.title} (로컬: ${timestamp}, 서버: ${serverTimestamp})`);
}
} else {
newTransactions.push(transactionData);
console.log(`[동기화] 새 항목 추가: ${t.id} - ${t.title}`);
}
} catch (err) {
console.error(`[동기화] 트랜잭션 처리 중 오류 (ID: ${t.id}):`, err);
@@ -69,33 +149,37 @@ export const uploadTransactions = async (userId: string): Promise<void> => {
// 새 트랜잭션 삽입 (있는 경우) - 배치 처리
if (newTransactions.length > 0) {
console.log(`${newTransactions.length}개의 새 트랜잭션 업로드`);
console.log(`[동기화] ${newTransactions.length}개의 새 트랜잭션 업로드`);
// 대용량 데이터 처리를 위해 배치 처리 (최대 100개씩)
const batchSize = 100;
for (let i = 0; i < newTransactions.length; i += batchSize) {
const batch = newTransactions.slice(i, i + batchSize);
console.log(`[동기화] 새 트랜잭션 배치 업로드 중: ${i+1}~${Math.min(i+batch.length, newTransactions.length)}/${newTransactions.length}`);
const { error: insertError } = await supabase
.from('transactions')
.insert(batch);
if (insertError) {
console.error(`[동기화] 새 트랜잭션 배치 업로드 실패 (${i}~${i + batch.length}):`, insertError);
console.error(`[동기화] 새 트랜잭션 배치 업로드 실패:`, insertError);
console.error('[동기화] 오류 상세:', JSON.stringify(insertError, null, 2));
// 배치 실패해도 다음 배치 계속 시도
} else {
console.log(`[동기화] 새 트랜잭션 배치 업로드 성공: ${batch.length}`);
}
}
}
// 기존 트랜잭션 업데이트 (있는 경우) - 배치 처리
if (updateTransactions.length > 0) {
console.log(`${updateTransactions.length}개의 기존 트랜잭션 업데이트`);
console.log(`[동기화] ${updateTransactions.length}개의 기존 트랜잭션 업데이트`);
// 대용량 데이터 처리를 위해 배치 처리 (최대 50개씩)
// 업데이트는 개별 쿼리보다 효율적이지만 삽입보다는 복잡하므로 더 작은 배치 크기 사용
const batchSize = 50;
for (let i = 0; i < updateTransactions.length; i += batchSize) {
const batch = updateTransactions.slice(i, i + batchSize);
console.log(`[동기화] 트랜잭션 업데이트 배치 처리 중: ${i+1}~${Math.min(i+batch.length, updateTransactions.length)}/${updateTransactions.length}`);
// 배치 내 트랜잭션을 병렬로 업데이트 (Promise.all 사용)
const updatePromises = batch.map(transaction =>
@@ -115,9 +199,11 @@ export const uploadTransactions = async (userId: string): Promise<void> => {
errors.forEach(err => {
console.error('[동기화] 업데이트 오류:', err.error);
});
} else {
console.log(`[동기화] 트랜잭션 업데이트 배치 성공: ${batch.length}`);
}
} catch (batchError) {
console.error(`[동기화] 트랜잭션 배치 업데이트 실패 (${i}~${i + batch.length}):`, batchError);
console.error(`[동기화] 트랜잭션 배치 업데이트 실패:`, batchError);
// 배치 실패해도 다음 배치 계속 시도
}
}