ghostfolio/apps/api/src/app/admin/admin.service.ts

153 lines
4.3 KiB
TypeScript
Raw Normal View History

import { SubscriptionService } from '@ghostfolio/api/app/subscription/subscription.service';
import { ConfigurationService } from '@ghostfolio/api/services/configuration.service';
import { DataGatheringService } from '@ghostfolio/api/services/data-gathering.service';
import { ExchangeRateDataService } from '@ghostfolio/api/services/exchange-rate-data.service';
import { MarketDataService } from '@ghostfolio/api/services/market-data.service';
import { PrismaService } from '@ghostfolio/api/services/prisma.service';
import { baseCurrency } from '@ghostfolio/common/config';
import {
AdminData,
AdminMarketData,
AdminMarketDataDetails
} from '@ghostfolio/common/interfaces';
2021-04-13 21:53:58 +02:00
import { Injectable } from '@nestjs/common';
import { differenceInDays } from 'date-fns';
2021-04-13 21:53:58 +02:00
@Injectable()
export class AdminService {
public constructor(
private readonly configurationService: ConfigurationService,
private readonly dataGatheringService: DataGatheringService,
2021-08-07 22:38:07 +02:00
private readonly exchangeRateDataService: ExchangeRateDataService,
private readonly marketDataService: MarketDataService,
private readonly prismaService: PrismaService,
private readonly subscriptionService: SubscriptionService
2021-04-13 21:53:58 +02:00
) {}
public async get(): Promise<AdminData> {
return {
dataGatheringProgress:
await this.dataGatheringService.getDataGatheringProgress(),
exchangeRates: this.exchangeRateDataService
.getCurrencies()
.filter((currency) => {
return currency !== baseCurrency;
})
.map((currency) => {
return {
label1: baseCurrency,
label2: currency,
value: this.exchangeRateDataService.toCurrency(
1,
baseCurrency,
currency
)
};
}),
2021-04-13 21:53:58 +02:00
lastDataGathering: await this.getLastDataGathering(),
2021-08-07 22:38:07 +02:00
transactionCount: await this.prismaService.order.count(),
userCount: await this.prismaService.user.count(),
2021-04-18 20:12:58 +02:00
users: await this.getUsersWithAnalytics()
2021-04-13 21:53:58 +02:00
};
}
public async getMarketData(): Promise<AdminMarketData> {
return {
marketData: await (
await this.dataGatheringService.getSymbolsMax()
).map((symbol) => {
return symbol;
})
};
}
public async getMarketDataBySymbol(
aSymbol: string
): Promise<AdminMarketDataDetails> {
return {
marketData: await this.marketDataService.marketDataItems({
orderBy: {
date: 'asc'
},
where: {
symbol: aSymbol
}
})
};
}
2021-04-13 21:53:58 +02:00
private async getLastDataGathering() {
const lastDataGathering =
await this.dataGatheringService.getLastDataGathering();
2021-04-13 21:53:58 +02:00
if (lastDataGathering) {
return lastDataGathering;
2021-04-13 21:53:58 +02:00
}
2021-08-07 22:38:07 +02:00
const dataGatheringInProgress =
await this.dataGatheringService.getIsInProgress();
2021-04-13 21:53:58 +02:00
if (dataGatheringInProgress) {
return 'IN_PROGRESS';
}
return undefined;
2021-04-13 21:53:58 +02:00
}
private async getUsersWithAnalytics(): Promise<AdminData['users']> {
const usersWithAnalytics = await this.prismaService.user.findMany({
2021-04-18 20:12:58 +02:00
orderBy: {
Analytics: {
updatedAt: 'desc'
}
},
2021-04-13 21:53:58 +02:00
select: {
2021-04-18 20:12:58 +02:00
_count: {
select: { Account: true, Order: true }
2021-04-18 20:12:58 +02:00
},
alias: true,
Analytics: {
2021-04-13 21:53:58 +02:00
select: {
2021-04-18 20:12:58 +02:00
activityCount: true,
updatedAt: true
2021-04-13 21:53:58 +02:00
}
2021-04-18 20:12:58 +02:00
},
createdAt: true,
id: true,
Subscription: true
2021-04-13 21:53:58 +02:00
},
take: 30,
2021-04-22 20:55:05 +02:00
where: {
NOT: {
Analytics: null
}
}
2021-04-13 21:53:58 +02:00
});
return usersWithAnalytics.map(
({ _count, alias, Analytics, createdAt, id, Subscription }) => {
const daysSinceRegistration =
differenceInDays(new Date(), createdAt) + 1;
const engagement = Analytics.activityCount / daysSinceRegistration;
const subscription = this.configurationService.get(
'ENABLE_FEATURE_SUBSCRIPTION'
)
? this.subscriptionService.getSubscription(Subscription)
: undefined;
return {
alias,
createdAt,
engagement,
id,
subscription,
accountCount: _count.Account || 0,
lastActivity: Analytics.updatedAt,
transactionCount: _count.Order || 0
};
}
);
2021-04-13 21:53:58 +02:00
}
}