This commit is contained in:
Benjamin Wilson
2025-02-25 19:17:13 -05:00
parent 19f49b74e6
commit 903aa2628c
@@ -44,11 +44,12 @@ export class ClientStatisticsService {
public async doBulkAsyncUpdate(){ public async doBulkAsyncUpdate(){
// Step 1: Prepare data for bulk update // Step 1: Prepare data for bulk update
const values = this.bulkAsyncUpdates.map(stat => const values = this.bulkAsyncUpdates.map(stat =>
`('${stat.clientId}', ${stat.time}, ${stat.shares || 0}, ${stat.acceptedCount || 0}, NOW())` `('${stat.clientId}', ${stat.time}, ${stat.shares ?? 0}, ${stat.acceptedCount ?? 0}, NOW())`
).join(','); ).join(',');
// Step 2: Use a temp table and single UPDATE const query = `
await this.clientStatisticsRepository.query(` DO $$
BEGIN
CREATE TEMP TABLE temp_stats ( CREATE TEMP TABLE temp_stats (
"clientId" UUID, "clientId" UUID,
time BIGINT, time BIGINT,
@@ -66,7 +67,16 @@ export class ClientStatisticsService {
"updatedAt" = ts."updatedAt" "updatedAt" = ts."updatedAt"
FROM temp_stats ts FROM temp_stats ts
WHERE cse."clientId" = ts."clientId" AND cse.time = ts.time; WHERE cse."clientId" = ts."clientId" AND cse.time = ts.time;
`); END;
$$;
`;
try {
await this.clientStatisticsRepository.query(query);
} catch (error) {
console.error('Bulk update failed:', error.message, query);
throw error;
}
this.bulkAsyncUpdates = []; this.bulkAsyncUpdates = [];
} }