Browse Source

Optimize min/max date calculation with single-pass loop

pull/5858/head
Sven Günther 21 hours ago
parent
commit
398377d68f
  1. 1
      CHANGELOG.md
  2. 34
      apps/api/src/services/market-data/market-data.service.ts

1
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

34
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: {

Loading…
Cancel
Save