|
|
@ -369,11 +369,15 @@ export class DataProviderService implements OnModuleInit { |
|
|
}); |
|
|
}); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
public async getHistorical( |
|
|
public async getHistorical({ |
|
|
aItems: AssetProfileIdentifier[], |
|
|
assetProfileIdentifiers, |
|
|
from: Date, |
|
|
from, |
|
|
to: Date |
|
|
to |
|
|
): Promise<{ |
|
|
}: { |
|
|
|
|
|
assetProfileIdentifiers: AssetProfileIdentifier[]; |
|
|
|
|
|
from: Date; |
|
|
|
|
|
to: Date; |
|
|
|
|
|
}): Promise<{ |
|
|
[assetProfileIdentifier: string]: { |
|
|
[assetProfileIdentifier: string]: { |
|
|
[date: string]: DataProviderHistoricalResponse; |
|
|
[date: string]: DataProviderHistoricalResponse; |
|
|
}; |
|
|
}; |
|
|
@ -384,35 +388,29 @@ export class DataProviderService implements OnModuleInit { |
|
|
}; |
|
|
}; |
|
|
} = {}; |
|
|
} = {}; |
|
|
|
|
|
|
|
|
if (isEmpty(aItems) || !isValid(from) || !isValid(to)) { |
|
|
if (isEmpty(assetProfileIdentifiers) || !isValid(from) || !isValid(to)) { |
|
|
return response; |
|
|
return response; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
const rangeQuery = |
|
|
const assetProfileIdentifierTuples = assetProfileIdentifiers.map( |
|
|
from && to |
|
|
({ dataSource, symbol }) => { |
|
|
? Prisma.sql`AND date >= ${format(from, DATE_FORMAT, { |
|
|
return Prisma.sql`(${dataSource}::"DataSource", ${symbol})`; |
|
|
in: utc |
|
|
} |
|
|
})}::timestamp AND date <= ${format(to, DATE_FORMAT, { |
|
|
); |
|
|
in: utc |
|
|
|
|
|
})}::timestamp` |
|
|
|
|
|
: Prisma.empty; |
|
|
|
|
|
|
|
|
|
|
|
const assetProfileIdentifiers = aItems.map(({ dataSource, symbol }) => { |
|
|
|
|
|
return Prisma.sql`(${dataSource}::"DataSource", ${symbol})`; |
|
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
try { |
|
|
try { |
|
|
const marketDataByGranularity: Pick< |
|
|
const marketDataItems: Pick< |
|
|
MarketData, |
|
|
MarketData, |
|
|
'dataSource' | 'date' | 'marketPrice' | 'symbol' |
|
|
'dataSource' | 'date' | 'marketPrice' | 'symbol' |
|
|
>[] = await this.prismaService.$queryRaw` |
|
|
>[] = await this.prismaService.$queryRaw` |
|
|
SELECT "dataSource", "date", "marketPrice", "symbol" |
|
|
SELECT "dataSource", "date", "marketPrice", "symbol" |
|
|
FROM "MarketData" |
|
|
FROM "MarketData" |
|
|
WHERE ("dataSource", "symbol") IN (${Prisma.join(assetProfileIdentifiers)}) |
|
|
WHERE ("dataSource", "symbol") IN (${Prisma.join(assetProfileIdentifierTuples)}) |
|
|
${rangeQuery} |
|
|
AND date >= ${format(from, DATE_FORMAT, { in: utc })}::timestamp |
|
|
|
|
|
AND date <= ${format(to, DATE_FORMAT, { in: utc })}::timestamp |
|
|
ORDER BY date;`;
|
|
|
ORDER BY date;`;
|
|
|
|
|
|
|
|
|
response = marketDataByGranularity.reduce((r, marketData) => { |
|
|
response = marketDataItems.reduce((r, marketData) => { |
|
|
const { dataSource, date, marketPrice, symbol } = marketData; |
|
|
const { dataSource, date, marketPrice, symbol } = marketData; |
|
|
|
|
|
|
|
|
const assetProfileIdentifier = getAssetProfileIdentifier({ |
|
|
const assetProfileIdentifier = getAssetProfileIdentifier({ |
|
|
|