Skip to content

Commit

Permalink
feat: add features in walletService
Browse files Browse the repository at this point in the history
  • Loading branch information
everGreenGH committed Jun 2, 2023
1 parent 9b2e902 commit 4a42d26
Show file tree
Hide file tree
Showing 6 changed files with 158 additions and 25 deletions.
3 changes: 1 addition & 2 deletions src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import * as path from "path";
import { ThrottlerModule } from "@nestjs/throttler";
import { TypeOrmModule } from "@nestjs/typeorm";
import { ProtocolModule } from "./protocol/protocol.module";
import { WalletModule } from './wallet/wallet.module';
import { WalletModule } from "./wallet/wallet.module";
import postgresConfig from "@common/config/postgres.config";

@Module({
Expand All @@ -24,7 +24,6 @@ import postgresConfig from "@common/config/postgres.config";
TypeOrmModule.forRoot({
...postgresConfig().postgres,
}),
TransactionModule,
ProtocolModule,
WalletModule,
],
Expand Down
13 changes: 8 additions & 5 deletions src/protocol/protocol.module.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import { Module } from '@nestjs/common';
import { ProtocolController } from './protocol.controller';
import { ProtocolService } from './protocol.service';
import { Module } from "@nestjs/common";
import { ProtocolService } from "./protocol.service";
import { TypeOrmModule } from "@nestjs/typeorm";
import { Transaction } from "@common/database/entities/transaction.entity";
import { Wallet } from "@common/database/entities/wallet.entity";
import { Protocol } from "@common/database/entities/protocol.entity";

@Module({
controllers: [ProtocolController],
providers: [ProtocolService]
imports: [TypeOrmModule.forFeature([Transaction, Wallet, Protocol])],
providers: [ProtocolService],
})
export class ProtocolModule {}
18 changes: 0 additions & 18 deletions src/protocol/protocol.service.spec.ts

This file was deleted.

7 changes: 7 additions & 0 deletions src/wallet/wallet.dtos.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { Wallet } from "@common/database/entities/wallet.entity";

export class WalletsByTimestampInterval {
startTimestamp: number;
endTimestamp: number;
wallet: Wallet[];
}
11 changes: 11 additions & 0 deletions src/wallet/wallet.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { Module } from "@nestjs/common";
import { WalletService } from "./wallet.service";
import { TypeOrmModule } from "@nestjs/typeorm";
import { Wallet } from "@common/database/entities/wallet.entity";
import { Transaction } from "@common/database/entities/transaction.entity";

@Module({
imports: [TypeOrmModule.forFeature([Transaction, Wallet])],
providers: [WalletService],
})
export class WalletModule {}
131 changes: 131 additions & 0 deletions src/wallet/wallet.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
import { Injectable } from "@nestjs/common";
import { InjectRepository } from "@nestjs/typeorm";
import { Repository } from "typeorm";
import { Wallet } from "../common/database/entities/wallet.entity";
import { Transaction } from "@common/database/entities/transaction.entity";
import { WalletsByTimestampInterval } from "./wallet.dtos";

@Injectable()
export class WalletService {
constructor(
@InjectRepository(Transaction)
private _transactionRepository: Repository<Transaction>,
@InjectRepository(Wallet)
private _walletRepository: Repository<Wallet>,
) {}

async getWalletByTransactionId(transactionId: number): Promise<Wallet> {
/* 현재 interval에 해당하는 transaction이면 wallets에 추가 */
const wallet = await this._walletRepository
.createQueryBuilder("wallet")
.leftJoinAndSelect("wallet.transaction", "transaction")
.where("transaction.id = :transactionId", { transactionId })
.getOne();

return wallet;
}

async getWalletsByTimestampInterval(interval: number): Promise<WalletsByTimestampInterval[]> {
/* transaction을 오름차순으로 정렬 */
const transactions = await this._transactionRepository.find({ order: { timestamp: "ASC" } });

const walletTimestamps: WalletsByTimestampInterval[] = [];

let currentIntervalStart = transactions[0].timestamp;
let currentIntervalEnd = currentIntervalStart + interval;

let currentWallets: Wallet[] = [];

for (const transaction of transactions) {
if (transaction.timestamp >= currentIntervalStart && transaction.timestamp < currentIntervalEnd) {
/* 현재 interval에 해당하는 transaction이면 wallets에 추가 */
const wallet = await this.getWalletByTransactionId(transaction.id);

if (wallet) {
currentWallets.push(wallet);
}
} else {
/* 현재 interval이 끝났으므로 결과에 추가하고 다음 interval로 넘어감 */
walletTimestamps.push({
startTimestamp: currentIntervalStart,
endTimestamp: currentIntervalEnd,
wallet: currentWallets,
});

currentIntervalStart = currentIntervalEnd;
currentIntervalEnd = currentIntervalStart + interval;

currentWallets = [];

/* 현재 interval에 해당하는 transaction도 현재 wallets에 추가 */
const wallet = await this.getWalletByTransactionId(transaction.id);

if (wallet) {
currentWallets.push(wallet);
}
}
}

/* 마지막 interval 결과 추가 */
walletTimestamps.push({
startTimestamp: currentIntervalStart,
endTimestamp: currentIntervalEnd,
wallet: currentWallets,
});

return walletTimestamps;
}

async getDailyWalletCounts(): Promise<{ date: string; count: number }[]> {
const interval = 24 * 60 * 60 * 1000; // 1일을 밀리초로 표현
const walletsByInterval = await this.getWalletsByTimestampInterval(interval);

const dailyCounts: { date: string; count: number }[] = [];

for (const wallets of walletsByInterval) {
const startDate = new Date(wallets.startTimestamp).toISOString().split("T")[0];
const endDate = new Date(wallets.endTimestamp).toISOString().split("T")[0];
const count = wallets.wallet.length;

dailyCounts.push({ date: startDate, count });
}

return dailyCounts;
}

async getWeeklyWalletCounts(): Promise<{ startDate: string; endDate: string; count: number }[]> {
const interval = 7 * 24 * 60 * 60 * 1000; // 1주일을 밀리초로 표현
const walletsByInterval = await this.getWalletsByTimestampInterval(interval);

const weeklyCounts: { startDate: string; endDate: string; count: number }[] = [];

for (const wallets of walletsByInterval) {
const startDate = new Date(wallets.startTimestamp).toISOString().split("T")[0];
const endDate = new Date(wallets.endTimestamp).toISOString().split("T")[0];
const count = wallets.wallet.length;

weeklyCounts.push({ startDate, endDate, count });
}

return weeklyCounts;
}

async getMonthlyWalletCounts(): Promise<{ year: number; month: number; count: number }[]> {
const interval = 30 * 24 * 60 * 60 * 1000; // 30일을 밀리초로 표현
const walletsByInterval = await this.getWalletsByTimestampInterval(interval);

const monthlyCounts: { year: number; month: number; count: number }[] = [];

for (const wallets of walletsByInterval) {
const startDate = new Date(wallets.startTimestamp);
const endDate = new Date(wallets.endTimestamp);
const year = startDate.getFullYear();
const month = startDate.getMonth() + 1;
const count = wallets.wallet.length;

monthlyCounts.push({ year, month, count });
}

return monthlyCounts;
}
}

0 comments on commit 4a42d26

Please sign in to comment.