Browse Source

Refactoring

pull/3575/head
Thomas Kaul 1 year ago
parent
commit
39527299ae
  1. 10
      apps/api/src/app/admin/admin.controller.ts
  2. 5
      apps/api/src/services/cron.service.ts
  3. 74
      apps/api/src/services/data-gathering/data-gathering.service.ts

10
apps/api/src/app/admin/admin.controller.ts

@ -81,10 +81,11 @@ export class AdminController {
@Post('gather/max') @Post('gather/max')
@UseGuards(AuthGuard('jwt'), HasPermissionGuard) @UseGuards(AuthGuard('jwt'), HasPermissionGuard)
public async gatherMax(): Promise<void> { public async gatherMax(): Promise<void> {
const uniqueAssets = await this.dataGatheringService.getUniqueAssets(); const assetProfileIdentifiers =
await this.dataGatheringService.getAllAssetProfileIdentifiers();
await this.dataGatheringService.addJobsToQueue( await this.dataGatheringService.addJobsToQueue(
uniqueAssets.map(({ dataSource, symbol }) => { assetProfileIdentifiers.map(({ dataSource, symbol }) => {
return { return {
data: { data: {
dataSource, dataSource,
@ -107,10 +108,11 @@ export class AdminController {
@Post('gather/profile-data') @Post('gather/profile-data')
@UseGuards(AuthGuard('jwt'), HasPermissionGuard) @UseGuards(AuthGuard('jwt'), HasPermissionGuard)
public async gatherProfileData(): Promise<void> { public async gatherProfileData(): Promise<void> {
const uniqueAssets = await this.dataGatheringService.getUniqueAssets(); const assetProfileIdentifiers =
await this.dataGatheringService.getAllAssetProfileIdentifiers();
await this.dataGatheringService.addJobsToQueue( await this.dataGatheringService.addJobsToQueue(
uniqueAssets.map(({ dataSource, symbol }) => { assetProfileIdentifiers.map(({ dataSource, symbol }) => {
return { return {
data: { data: {
dataSource, dataSource,

5
apps/api/src/services/cron.service.ts

@ -45,10 +45,11 @@ export class CronService {
@Cron(CronService.EVERY_SUNDAY_AT_LUNCH_TIME) @Cron(CronService.EVERY_SUNDAY_AT_LUNCH_TIME)
public async runEverySundayAtTwelvePm() { public async runEverySundayAtTwelvePm() {
if (await this.isDataGatheringEnabled()) { if (await this.isDataGatheringEnabled()) {
const uniqueAssets = await this.dataGatheringService.getUniqueAssets(); const assetProfileIdentifiers =
await this.dataGatheringService.getAllAssetProfileIdentifiers();
await this.dataGatheringService.addJobsToQueue( await this.dataGatheringService.addJobsToQueue(
uniqueAssets.map(({ dataSource, symbol }) => { assetProfileIdentifiers.map(({ dataSource, symbol }) => {
return { return {
data: { data: {
dataSource, dataSource,

74
apps/api/src/services/data-gathering/data-gathering.service.ts

@ -152,7 +152,7 @@ export class DataGatheringService {
}); });
if (!uniqueAssets) { if (!uniqueAssets) {
uniqueAssets = await this.getUniqueAssets(); uniqueAssets = await this.getAllAssetProfileIdentifiers();
} }
if (uniqueAssets.length <= 0) { if (uniqueAssets.length <= 0) {
@ -284,7 +284,7 @@ export class DataGatheringService {
); );
} }
public async getUniqueAssets(): Promise<UniqueAsset[]> { public async getAllAssetProfileIdentifiers(): Promise<UniqueAsset[]> {
const symbolProfiles = await this.prismaService.symbolProfile.findMany({ const symbolProfiles = await this.prismaService.symbolProfile.findMany({
orderBy: [{ symbol: 'asc' }] orderBy: [{ symbol: 'asc' }]
}); });
@ -304,22 +304,44 @@ export class DataGatheringService {
}); });
} }
private async getCurrencies7D(): Promise<IDataGatheringItem[]> { private async getAssetProfileIdentifiersWithCompleteMarketData(): Promise<
const startDate = subDays(resetHours(new Date()), 7); UniqueAsset[]
> {
return (
await this.prismaService.marketData.groupBy({
_count: true,
by: ['dataSource', 'symbol'],
orderBy: [{ symbol: 'asc' }],
where: {
date: { gt: subDays(resetHours(new Date()), 7) },
state: 'CLOSE'
}
})
)
.filter(({ _count }) => {
return _count >= 6;
})
.map(({ dataSource, symbol }) => {
return { dataSource, symbol };
});
}
const symbolsWithCompleteMarketData = private async getCurrencies7D(): Promise<IDataGatheringItem[]> {
await this.getSymbolsWithCompleteMarketData(); const assetProfileIdentifiersWithCompleteMarketData =
await this.getAssetProfileIdentifiersWithCompleteMarketData();
return this.exchangeRateDataService return this.exchangeRateDataService
.getCurrencyPairs() .getCurrencyPairs()
.filter(({ symbol }) => { .filter(({ dataSource, symbol }) => {
return !symbolsWithCompleteMarketData.includes(symbol); return !assetProfileIdentifiersWithCompleteMarketData.some((item) => {
return item.dataSource === dataSource && item.symbol === symbol;
});
}) })
.map(({ dataSource, symbol }) => { .map(({ dataSource, symbol }) => {
return { return {
dataSource, dataSource,
symbol, symbol,
date: startDate date: subDays(resetHours(new Date()), 7)
}; };
}); });
} }
@ -333,15 +355,13 @@ export class DataGatheringService {
}: { }: {
withUserSubscription?: boolean; withUserSubscription?: boolean;
}): Promise<IDataGatheringItem[]> { }): Promise<IDataGatheringItem[]> {
const startDate = subDays(resetHours(new Date()), 7);
const symbolProfiles = const symbolProfiles =
await this.symbolProfileService.getSymbolProfilesByUserSubscription({ await this.symbolProfileService.getSymbolProfilesByUserSubscription({
withUserSubscription withUserSubscription
}); });
const symbolsWithCompleteMarketData = const assetProfileIdentifiersWithCompleteMarketData =
await this.getSymbolsWithCompleteMarketData(); await this.getAssetProfileIdentifiersWithCompleteMarketData();
return symbolProfiles return symbolProfiles
.filter(({ dataSource, scraperConfiguration, symbol }) => { .filter(({ dataSource, scraperConfiguration, symbol }) => {
@ -349,14 +369,16 @@ export class DataGatheringService {
dataSource === 'MANUAL' && !isEmpty(scraperConfiguration); dataSource === 'MANUAL' && !isEmpty(scraperConfiguration);
return ( return (
!symbolsWithCompleteMarketData.includes(symbol) && !assetProfileIdentifiersWithCompleteMarketData.some((item) => {
return item.dataSource === dataSource && item.symbol === symbol;
}) &&
(dataSource !== 'MANUAL' || manualDataSourceWithScraperConfiguration) (dataSource !== 'MANUAL' || manualDataSourceWithScraperConfiguration)
); );
}) })
.map((symbolProfile) => { .map((symbolProfile) => {
return { return {
...symbolProfile, ...symbolProfile,
date: startDate date: subDays(resetHours(new Date()), 7)
}; };
}); });
} }
@ -428,26 +450,4 @@ export class DataGatheringService {
return [...currencyPairsToGather, ...symbolProfilesToGather]; return [...currencyPairsToGather, ...symbolProfilesToGather];
} }
private async getSymbolsWithCompleteMarketData() {
const startDate = subDays(resetHours(new Date()), 7);
return (
await this.prismaService.marketData.groupBy({
_count: true,
by: ['symbol'],
orderBy: [{ symbol: 'asc' }],
where: {
date: { gt: startDate },
state: 'CLOSE'
}
})
)
.filter(({ _count }) => {
return _count >= 6;
})
.map(({ symbol }) => {
return symbol;
});
}
} }

Loading…
Cancel
Save