2021-11-08 20:55:38 +01:00
|
|
|
import { RedisCacheService } from '@ghostfolio/api/app/redis-cache/redis-cache.service';
|
2021-04-21 20:27:39 +02:00
|
|
|
import { ConfigurationService } from '@ghostfolio/api/services/configuration.service';
|
2021-08-09 21:11:35 +02:00
|
|
|
import { DataGatheringService } from '@ghostfolio/api/services/data-gathering.service';
|
2021-10-15 22:22:45 +02:00
|
|
|
import { DataProviderService } from '@ghostfolio/api/services/data-provider/data-provider.service';
|
2021-09-24 21:09:48 +02:00
|
|
|
import { ExchangeRateDataService } from '@ghostfolio/api/services/exchange-rate-data.service';
|
2021-04-21 20:27:39 +02:00
|
|
|
import { PrismaService } from '@ghostfolio/api/services/prisma.service';
|
2021-12-04 21:05:11 +01:00
|
|
|
import { PROPERTY_STRIPE_CONFIG } from '@ghostfolio/common/config';
|
2021-05-16 22:11:14 +02:00
|
|
|
import { InfoItem } from '@ghostfolio/common/interfaces';
|
2021-11-08 20:55:38 +01:00
|
|
|
import { Statistics } from '@ghostfolio/common/interfaces/statistics.interface';
|
2021-06-21 20:03:36 +02:00
|
|
|
import { Subscription } from '@ghostfolio/common/interfaces/subscription.interface';
|
2021-05-16 22:11:14 +02:00
|
|
|
import { permissions } from '@ghostfolio/common/permissions';
|
2021-11-07 21:25:18 +01:00
|
|
|
import { Injectable, Logger } from '@nestjs/common';
|
2021-04-13 21:53:58 +02:00
|
|
|
import { JwtService } from '@nestjs/jwt';
|
2021-06-17 22:59:48 +02:00
|
|
|
import * as bent from 'bent';
|
|
|
|
import { subDays } from 'date-fns';
|
2021-04-13 21:53:58 +02:00
|
|
|
|
|
|
|
@Injectable()
|
|
|
|
export class InfoService {
|
|
|
|
private static DEMO_USER_ID = '9b112b4d-3b7d-4bad-9bdd-3b0f7b4dac2f';
|
2021-11-08 20:55:38 +01:00
|
|
|
private static CACHE_KEY_STATISTICS = 'STATISTICS';
|
2021-04-13 21:53:58 +02:00
|
|
|
|
|
|
|
public constructor(
|
2021-04-18 19:06:54 +02:00
|
|
|
private readonly configurationService: ConfigurationService,
|
2021-10-15 22:22:45 +02:00
|
|
|
private readonly dataProviderService: DataProviderService,
|
2021-09-24 21:09:48 +02:00
|
|
|
private readonly exchangeRateDataService: ExchangeRateDataService,
|
2021-08-09 21:11:35 +02:00
|
|
|
private readonly dataGatheringService: DataGatheringService,
|
2021-08-07 22:38:07 +02:00
|
|
|
private readonly jwtService: JwtService,
|
2021-11-08 20:55:38 +01:00
|
|
|
private readonly prismaService: PrismaService,
|
|
|
|
private readonly redisCacheService: RedisCacheService
|
2021-04-13 21:53:58 +02:00
|
|
|
) {}
|
|
|
|
|
|
|
|
public async get(): Promise<InfoItem> {
|
2021-07-17 11:04:43 +02:00
|
|
|
const info: Partial<InfoItem> = {};
|
2021-08-07 22:38:07 +02:00
|
|
|
const platforms = await this.prismaService.platform.findMany({
|
2021-04-13 21:53:58 +02:00
|
|
|
orderBy: { name: 'asc' },
|
|
|
|
select: { id: true, name: true }
|
|
|
|
});
|
|
|
|
|
2021-04-18 19:06:54 +02:00
|
|
|
const globalPermissions: string[] = [];
|
|
|
|
|
2021-07-31 11:21:32 +02:00
|
|
|
if (this.configurationService.get('ENABLE_FEATURE_BLOG')) {
|
|
|
|
globalPermissions.push(permissions.enableBlog);
|
|
|
|
}
|
|
|
|
|
2021-07-14 20:54:05 +02:00
|
|
|
if (this.configurationService.get('ENABLE_FEATURE_IMPORT')) {
|
|
|
|
globalPermissions.push(permissions.enableImport);
|
|
|
|
}
|
|
|
|
|
2021-04-18 19:06:54 +02:00
|
|
|
if (this.configurationService.get('ENABLE_FEATURE_SOCIAL_LOGIN')) {
|
2021-04-24 22:01:38 +02:00
|
|
|
globalPermissions.push(permissions.enableSocialLogin);
|
|
|
|
}
|
|
|
|
|
2021-06-17 22:59:48 +02:00
|
|
|
if (this.configurationService.get('ENABLE_FEATURE_STATISTICS')) {
|
|
|
|
globalPermissions.push(permissions.enableStatistics);
|
|
|
|
}
|
|
|
|
|
2021-04-24 22:01:38 +02:00
|
|
|
if (this.configurationService.get('ENABLE_FEATURE_SUBSCRIPTION')) {
|
|
|
|
globalPermissions.push(permissions.enableSubscription);
|
2021-07-17 11:04:43 +02:00
|
|
|
|
|
|
|
info.stripePublicKey = this.configurationService.get('STRIPE_PUBLIC_KEY');
|
2021-04-18 19:06:54 +02:00
|
|
|
}
|
|
|
|
|
2021-04-13 21:53:58 +02:00
|
|
|
return {
|
2021-07-17 11:04:43 +02:00
|
|
|
...info,
|
2021-04-18 19:06:54 +02:00
|
|
|
globalPermissions,
|
2021-04-13 21:53:58 +02:00
|
|
|
platforms,
|
2021-09-24 21:09:48 +02:00
|
|
|
currencies: this.exchangeRateDataService.getCurrencies(),
|
2021-04-13 21:53:58 +02:00
|
|
|
demoAuthToken: this.getDemoAuthToken(),
|
2021-06-17 22:59:48 +02:00
|
|
|
lastDataGathering: await this.getLastDataGathering(),
|
2021-10-15 22:22:45 +02:00
|
|
|
primaryDataSource: this.dataProviderService.getPrimaryDataSource(),
|
2021-06-21 20:03:36 +02:00
|
|
|
statistics: await this.getStatistics(),
|
|
|
|
subscriptions: await this.getSubscriptions()
|
2021-06-17 22:59:48 +02:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
private async countActiveUsers(aDays: number) {
|
2021-08-07 22:38:07 +02:00
|
|
|
return await this.prismaService.user.count({
|
2021-06-17 22:59:48 +02:00
|
|
|
orderBy: {
|
|
|
|
Analytics: {
|
|
|
|
updatedAt: 'desc'
|
|
|
|
}
|
|
|
|
},
|
|
|
|
where: {
|
|
|
|
AND: [
|
|
|
|
{
|
|
|
|
NOT: {
|
|
|
|
Analytics: null
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Analytics: {
|
|
|
|
updatedAt: {
|
|
|
|
gt: subDays(new Date(), aDays)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-09-04 19:46:24 +02:00
|
|
|
private async countGitHubContributors(): Promise<number> {
|
|
|
|
try {
|
|
|
|
const get = bent(
|
|
|
|
`https://api.github.com/repos/ghostfolio/ghostfolio/contributors`,
|
|
|
|
'GET',
|
|
|
|
'json',
|
|
|
|
200,
|
|
|
|
{
|
|
|
|
'User-Agent': 'request'
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
const contributors = await get();
|
|
|
|
return contributors?.length;
|
|
|
|
} catch (error) {
|
2021-11-07 21:25:18 +01:00
|
|
|
Logger.error(error);
|
2021-09-04 19:46:24 +02:00
|
|
|
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-17 22:59:48 +02:00
|
|
|
private async countGitHubStargazers(): Promise<number> {
|
|
|
|
try {
|
|
|
|
const get = bent(
|
|
|
|
`https://api.github.com/repos/ghostfolio/ghostfolio`,
|
|
|
|
'GET',
|
|
|
|
'json',
|
|
|
|
200,
|
|
|
|
{
|
|
|
|
'User-Agent': 'request'
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
const { stargazers_count } = await get();
|
|
|
|
return stargazers_count;
|
|
|
|
} catch (error) {
|
2021-11-07 21:25:18 +01:00
|
|
|
Logger.error(error);
|
2021-06-17 22:59:48 +02:00
|
|
|
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
}
|
2021-06-21 20:03:36 +02:00
|
|
|
|
2021-11-01 21:15:09 +01:00
|
|
|
private async countNewUsers(aDays: number) {
|
|
|
|
return await this.prismaService.user.count({
|
|
|
|
orderBy: {
|
|
|
|
createdAt: 'desc'
|
|
|
|
},
|
|
|
|
where: {
|
|
|
|
AND: [
|
|
|
|
{
|
|
|
|
NOT: {
|
|
|
|
Analytics: null
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
createdAt: {
|
|
|
|
gt: subDays(new Date(), aDays)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-06-21 20:03:36 +02:00
|
|
|
private getDemoAuthToken() {
|
|
|
|
return this.jwtService.sign({
|
|
|
|
id: InfoService.DEMO_USER_ID
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
private async getLastDataGathering() {
|
2021-08-09 21:11:35 +02:00
|
|
|
const lastDataGathering =
|
|
|
|
await this.dataGatheringService.getLastDataGathering();
|
2021-06-21 20:03:36 +02:00
|
|
|
|
2021-08-09 21:11:35 +02:00
|
|
|
return lastDataGathering ?? null;
|
2021-06-21 20:03:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private async getStatistics() {
|
|
|
|
if (!this.configurationService.get('ENABLE_FEATURE_STATISTICS')) {
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
|
2021-11-08 20:55:38 +01:00
|
|
|
let statistics: Statistics;
|
|
|
|
|
|
|
|
try {
|
|
|
|
statistics = JSON.parse(
|
|
|
|
await this.redisCacheService.get(InfoService.CACHE_KEY_STATISTICS)
|
|
|
|
);
|
|
|
|
|
|
|
|
if (statistics) {
|
|
|
|
return statistics;
|
|
|
|
}
|
|
|
|
} catch {}
|
|
|
|
|
2021-06-21 20:03:36 +02:00
|
|
|
const activeUsers1d = await this.countActiveUsers(1);
|
2021-11-01 21:15:09 +01:00
|
|
|
const activeUsers7d = await this.countActiveUsers(7);
|
2021-06-21 20:03:36 +02:00
|
|
|
const activeUsers30d = await this.countActiveUsers(30);
|
2021-11-01 21:15:09 +01:00
|
|
|
const newUsers30d = await this.countNewUsers(30);
|
2021-09-04 19:46:24 +02:00
|
|
|
const gitHubContributors = await this.countGitHubContributors();
|
2021-06-21 20:03:36 +02:00
|
|
|
const gitHubStargazers = await this.countGitHubStargazers();
|
|
|
|
|
2021-11-08 20:55:38 +01:00
|
|
|
statistics = {
|
2021-06-21 20:03:36 +02:00
|
|
|
activeUsers1d,
|
2021-11-01 21:15:09 +01:00
|
|
|
activeUsers7d,
|
2021-06-21 20:03:36 +02:00
|
|
|
activeUsers30d,
|
2021-09-04 19:46:24 +02:00
|
|
|
gitHubContributors,
|
2021-11-01 21:15:09 +01:00
|
|
|
gitHubStargazers,
|
|
|
|
newUsers30d
|
2021-06-21 20:03:36 +02:00
|
|
|
};
|
2021-11-08 20:55:38 +01:00
|
|
|
|
|
|
|
await this.redisCacheService.set(
|
|
|
|
InfoService.CACHE_KEY_STATISTICS,
|
|
|
|
JSON.stringify(statistics)
|
|
|
|
);
|
|
|
|
|
|
|
|
return statistics;
|
2021-06-21 20:03:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private async getSubscriptions(): Promise<Subscription[]> {
|
|
|
|
if (!this.configurationService.get('ENABLE_FEATURE_SUBSCRIPTION')) {
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
|
2021-08-07 22:38:07 +02:00
|
|
|
const stripeConfig = await this.prismaService.property.findUnique({
|
2021-12-04 21:05:11 +01:00
|
|
|
where: { key: PROPERTY_STRIPE_CONFIG }
|
2021-06-21 20:03:36 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
if (stripeConfig) {
|
|
|
|
return [JSON.parse(stripeConfig.value)];
|
|
|
|
}
|
|
|
|
|
|
|
|
return [];
|
|
|
|
}
|
2021-04-13 21:53:58 +02:00
|
|
|
}
|