Skip to content

Commit

Permalink
feat: โœจ blackholed user record last ์ฆ๊ฐ€
Browse files Browse the repository at this point in the history
๊ธฐ์กด ๋ฐฉ์‹๋Œ€๋กœ ์ฒ˜๋ฆฌํ•ด๋„ ํฌ๊ฒŒ ๋ฌด๋ฆฌ ์—†๋Š” ์ˆ˜์ค€์ด๋ผ ํŒ๋‹จํ–ˆ์Šต๋‹ˆ๋‹ค.

- #392
  • Loading branch information
jpham005 committed Nov 14, 2023
1 parent f01f2f9 commit f855879
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 35 deletions.
37 changes: 31 additions & 6 deletions app/src/page/home/user/home.user.resolver.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
import { UseFilters, UseGuards } from '@nestjs/common';
import { Args, Query, ResolveField, Resolver } from '@nestjs/graphql';
import { StatAuthGuard } from 'src/auth/statAuthGuard';
import { CacheUtilService } from 'src/cache/cache.util.service';
import { Rate } from 'src/common/models/common.rate.model';
import { UserRank } from 'src/common/models/common.user.model';
import { IntRecord } from 'src/common/models/common.valueRecord.model';
import { DateWrapper } from 'src/dateWrapper/dateWrapper';
import { HttpExceptionFilter } from 'src/http-exception.filter';
import { HomeUserService } from './home.user.service';
import {
GetHomeUserBlackholedCountRecordsArgs,
HomeUser,
IntPerCircle,
UserCountPerLevel,
Expand All @@ -16,7 +19,10 @@ import {
@UseGuards(StatAuthGuard)
@Resolver((_of: unknown) => HomeUser)
export class HomeUserResolver {
constructor(private readonly homeUserService: HomeUserService) {}
constructor(
private readonly homeUserService: HomeUserService,
private readonly cacheUtilService: CacheUtilService,
) {}

@Query((_of) => HomeUser)
async getHomeUser() {
Expand All @@ -43,13 +49,32 @@ export class HomeUserResolver {
return await this.homeUserService.blackholedRate();
}

@ResolveField((_returns) => [IntRecord], { description: '1 ~ 24 ๊ฐœ์›”' })
@ResolveField((_returns) => [IntRecord], {
description: '1 ~ 120 ๊ฐœ์›”',
})
async blackholedCountRecords(
@Args('last') last: number,
@Args() { last }: GetHomeUserBlackholedCountRecordsArgs,
): Promise<IntRecord[]> {
return await this.homeUserService.blackholedCountRecords(
Math.max(1, Math.min(last, 24)),
);
const nextMonth = DateWrapper.nextMonth().toDate();
const start = DateWrapper.currMonth()
.moveMonth(1 - last)
.toDate();

const cacheKey = `homeUserBlackholedCountRecords:${start.getTime()}:${nextMonth.getTime()}`;

const cached = await this.cacheUtilService.get<IntRecord[]>(cacheKey);
if (cached) {
return cached;
}

const result = await this.homeUserService.blackholedCountRecords({
start,
end: nextMonth,
});

await this.cacheUtilService.set(cacheKey, result);

return result;
}

@ResolveField((_returns) => [IntPerCircle])
Expand Down
42 changes: 14 additions & 28 deletions app/src/page/home/user/home.user.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import { assertExist } from 'src/common/assertExist';
import type { Rate } from 'src/common/models/common.rate.model';
import type { UserRank } from 'src/common/models/common.user.model';
import type { IntRecord } from 'src/common/models/common.valueRecord.model';
import { DateRangeService } from 'src/dateRange/dateRange.service';
import type { DateRange } from 'src/dateRange/dtos/dateRange.dto';
import { DateWrapper } from 'src/dateWrapper/dateWrapper';
import type { IntPerCircle, UserCountPerLevel } from './models/home.user.model';
Expand All @@ -27,7 +26,6 @@ export class HomeUserService {
private readonly cursusUserService: CursusUserService,
private readonly cursusUserCacheService: CursusUserCacheService,
private readonly questsUserService: QuestsUserService,
private readonly dateRangeService: DateRangeService,
) {}

@CacheOnReturn()
Expand Down Expand Up @@ -90,46 +88,34 @@ export class HomeUserService {
};
}

@CacheOnReturn()
async blackholedCountRecords(last: number): Promise<IntRecord[]> {
const startDate = new DateWrapper()
.startOfMonth()
.moveMonth(1 - last)
.toDate();

const blackholeds: { blackholedAt?: Date }[] =
async blackholedCountRecords({
start,
end,
}: DateRange): Promise<IntRecord[]> {
const blackholed: Pick<cursus_user, 'blackholedAt'>[] =
await this.cursusUserService.findAllAndLean({
filter: blackholedUserFilterByDateRange({
start: startDate,
end: new Date(),
}),
filter: blackholedUserFilterByDateRange({ start, end }),
select: { blackholedAt: 1 },
});

const res = blackholeds.reduce((acc, { blackholedAt }) => {
const groupedRecord = blackholed.reduce((acc, { blackholedAt }) => {
assertExist(blackholedAt);

const date = new DateWrapper(blackholedAt)
const timestamp = new DateWrapper(blackholedAt)
.startOfMonth()
.toDate()
.getTime();

const prev = acc.get(date);
const prev = acc.get(timestamp) ?? 0;

acc.set(date, (prev ?? 0) + 1);
acc.set(timestamp, prev + 1);

return acc;
}, new Map() as Map<number, number>);

const records: IntRecord[] = [];

for (let i = 0; i < last; i++) {
const currDate = new DateWrapper(startDate).moveMonth(i).toDate();

records.push({ at: currDate, value: res.get(currDate.getTime()) ?? 0 });
}
}, new Map<number, number>());

return records;
return [...groupedRecord.entries()]
.sort(([a], [b]) => a - b)
.map(([timestamp, value]) => ({ at: new Date(timestamp), value }));
}

@CacheOnReturn()
Expand Down
11 changes: 10 additions & 1 deletion app/src/page/home/user/models/home.user.model.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Field, ObjectType } from '@nestjs/graphql';
import { ArgsType, Field, ObjectType } from '@nestjs/graphql';
import { Max, Min } from 'class-validator';
import { Rate } from 'src/common/models/common.rate.model';
import { UserRank } from 'src/common/models/common.user.model';
import { IntRecord } from 'src/common/models/common.valueRecord.model';
Expand Down Expand Up @@ -50,3 +51,11 @@ export class HomeUser {
@Field((_type) => [IntPerCircle])
averageDurationPerCircle: IntPerCircle[];
}

@ArgsType()
export class GetHomeUserBlackholedCountRecordsArgs {
@Min(1)
@Max(750)
@Field()
last: number;
}

0 comments on commit f855879

Please sign in to comment.