Browse Source

Use db decimation for benchmark data

pull/3139/head
helgehatt 1 year ago
parent
commit
72cc04caba
  1. 33
      apps/api/src/app/benchmark/benchmark.service.ts

33
apps/api/src/app/benchmark/benchmark.service.ts

@ -27,7 +27,7 @@ import { BenchmarkTrend } from '@ghostfolio/common/types';
import { Injectable, Logger } from '@nestjs/common'; import { Injectable, Logger } from '@nestjs/common';
import { SymbolProfile } from '@prisma/client'; import { SymbolProfile } from '@prisma/client';
import Big from 'big.js'; import Big from 'big.js';
import { format, isSameDay, subDays } from 'date-fns'; import { differenceInDays, format, isSameDay, subDays } from 'date-fns';
import { isNumber, last, uniqBy } from 'lodash'; import { isNumber, last, uniqBy } from 'lodash';
import ms from 'ms'; import ms from 'ms';
@ -217,6 +217,10 @@ export class BenchmarkService {
} & UniqueAsset): Promise<BenchmarkMarketDataDetails> { } & UniqueAsset): Promise<BenchmarkMarketDataDetails> {
const marketData: { date: string; value: number }[] = []; const marketData: { date: string; value: number }[] = [];
const endDate = new Date(Date.now());
const days = differenceInDays(endDate, startDate);
const step = Math.round(days / Math.min(days, MAX_CHART_ITEMS));
const [currentSymbolItem, marketDataItems] = await Promise.all([ const [currentSymbolItem, marketDataItems] = await Promise.all([
this.symbolService.get({ this.symbolService.get({
dataGatheringItem: { dataGatheringItem: {
@ -224,17 +228,10 @@ export class BenchmarkService {
symbol symbol
} }
}), }),
this.marketDataService.marketDataItems({ this.marketDataService.getDecimatedRange({
orderBy: { dateRange: { start: startDate, end: endDate, step },
date: 'asc' symbols: [symbol],
}, dataSources: [dataSource]
where: {
dataSource,
symbol,
date: {
gte: startDate
}
}
}) })
]); ]);
@ -266,17 +263,7 @@ export class BenchmarkService {
return { marketData }; return { marketData };
} }
const step = Math.round( for (const marketDataItem of marketDataItems) {
marketDataItems.length / Math.min(marketDataItems.length, MAX_CHART_ITEMS)
);
let i = 0;
for (let marketDataItem of marketDataItems) {
if (i % step !== 0) {
continue;
}
const exchangeRate = const exchangeRate =
exchangeRates[`${currentSymbolItem.currency}${userCurrency}`]?.[ exchangeRates[`${currentSymbolItem.currency}${userCurrency}`]?.[
format(marketDataItem.date, DATE_FORMAT) format(marketDataItem.date, DATE_FORMAT)

Loading…
Cancel
Save