From 398377d68f717c75cadfbc01aafb3a0751b2f492 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sven=20G=C3=BCnther?= Date: Sat, 1 Nov 2025 10:35:20 +0100 Subject: [PATCH] Optimize min/max date calculation with single-pass loop --- CHANGELOG.md | 1 + .../market-data/market-data.service.ts | 34 +++++++++---------- 2 files changed, 17 insertions(+), 18 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a44518fa7..212fafb39 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,6 +12,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Improved the icon of the _View Holding_ menu item in the activities table - Ensured atomic data replacement during historical market data gathering - Refreshed the cryptocurrencies list +- Ensured atomic data replacement during historical market data gathering ## 2.213.0 - 2025-10-30 diff --git a/apps/api/src/services/market-data/market-data.service.ts b/apps/api/src/services/market-data/market-data.service.ts index 6f88dab8e..8cf6fef7c 100644 --- a/apps/api/src/services/market-data/market-data.service.ts +++ b/apps/api/src/services/market-data/market-data.service.ts @@ -144,26 +144,24 @@ export class MarketDataService { }: AssetProfileIdentifier & { data: Prisma.MarketDataUpdateInput[] }) { await this.prismaService.$transaction(async (prisma) => { if (data.length > 0) { - // Find the earliest and latest dates in the incoming data - const dates = data.map(({ date }) => { - return date as Date; - }); + // Find the earliest and latest dates in the incoming data using a single loop + let minTime = Infinity; + let maxTime = -Infinity; + + for (const item of data) { + const time = (item.date as Date).getTime(); + + if (time < minTime) { + minTime = time; + } - const minDate = new Date( - Math.min( - ...dates.map((date) => { - return date.getTime(); - }) - ) - ); + if (time > maxTime) { + maxTime = time; + } + } - const maxDate = new Date( - Math.max( - ...dates.map((date) => { - return date.getTime(); - }) - ) - ); + const minDate = new Date(minTime); + const maxDate = new Date(maxTime); await prisma.marketData.deleteMany({ where: {