-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: ✨ dailyEvalCount 추가를 통해 eval count record last 지원 범위 증가
- #392
- Loading branch information
Showing
14 changed files
with
385 additions
and
59 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
export type FindEvalCountByDateInput = { | ||
start: Date; | ||
end: Date; | ||
}; | ||
|
||
export type FindUserEvalCountByDateInput = FindEvalCountByDateInput & { | ||
userId: number; | ||
}; | ||
|
||
export type FindEvalCountByDateOutput = { | ||
date: Date; | ||
count: number; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import { Module } from '@nestjs/common'; | ||
import { MongooseModule } from '@nestjs/mongoose'; | ||
import { DailyEvalCountService } from './dailyEvalCount.service'; | ||
import { DailyEvalCountDaoImpl } from './db/dailyEvalCount.database.dao'; | ||
import { | ||
dailyUserEvalCountSchema, | ||
mv_daily_user_scale_team_counts, | ||
} from './db/dailyEvalCount.database.schema'; | ||
|
||
@Module({ | ||
imports: [ | ||
MongooseModule.forFeature([ | ||
{ | ||
name: mv_daily_user_scale_team_counts.name, | ||
schema: dailyUserEvalCountSchema, | ||
}, | ||
]), | ||
], | ||
providers: [DailyEvalCountService, DailyEvalCountDaoImpl], | ||
exports: [DailyEvalCountService], | ||
}) | ||
// eslint-disable-next-line @typescript-eslint/no-extraneous-class | ||
export class DailyEvalCountModule {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
import { Test } from '@nestjs/testing'; | ||
import type { FindEvalCountByDateOutput } from './dailyEvalCount.dto'; | ||
import { DailyEvalCountService } from './dailyEvalCount.service'; | ||
import { | ||
DailyEvalCountDaoImpl, | ||
type DailyEvalCountDao, | ||
} from './db/dailyEvalCount.database.dao'; | ||
|
||
describe(DailyEvalCountService.name, () => { | ||
let dailyEvalCountService: DailyEvalCountService; | ||
|
||
beforeEach(async () => { | ||
const moduleRef = await Test.createTestingModule({ | ||
providers: [DailyEvalCountService], | ||
}) | ||
.useMocker((token) => { | ||
if (token === DailyEvalCountDaoImpl) { | ||
return { | ||
findEvalCountsByDate: async ({ | ||
start, | ||
end, | ||
}): Promise<FindEvalCountByDateOutput[]> => { | ||
return [ | ||
{ | ||
date: new Date('2022-12-31T15:00:00.000Z'), | ||
count: 1, | ||
}, | ||
{ | ||
date: new Date('2023-01-31T15:00:00.000Z'), | ||
count: 2, | ||
}, | ||
{ | ||
date: new Date('2023-06-31T15:00:00.000Z'), | ||
count: 3, | ||
}, | ||
{ | ||
date: new Date('2024-05-31T15:00:00.000Z'), | ||
count: 4, | ||
}, | ||
].filter(({ date }) => date >= start && date <= end); | ||
}, | ||
findUserEvalCountsByDatePerMonth: async ({ | ||
start, | ||
end, | ||
}): Promise<FindEvalCountByDateOutput[]> => { | ||
return [ | ||
{ | ||
date: new Date('2022-12-31T15:00:00.000Z'), | ||
count: 1, | ||
}, | ||
{ | ||
date: new Date('2023-01-31T15:00:00.000Z'), | ||
count: 2, | ||
}, | ||
{ | ||
date: new Date('2023-06-31T15:00:00.000Z'), | ||
count: 3, | ||
}, | ||
{ | ||
date: new Date('2024-05-31T15:00:00.000Z'), | ||
count: 4, | ||
}, | ||
].filter(({ date }) => date >= start && date <= end); | ||
}, | ||
} satisfies DailyEvalCountDao; | ||
} | ||
}) | ||
.compile(); | ||
|
||
dailyEvalCountService = moduleRef.get<DailyEvalCountService>( | ||
DailyEvalCountService, | ||
); | ||
}); | ||
|
||
describe('evalCountRecordsByDate', () => { | ||
const testDate = { | ||
start: new Date('2022-12-31T15:00:00.000Z'), | ||
end: new Date('2023-12-31T15:00:00.000Z'), | ||
}; | ||
|
||
it('should return eval count records', async () => { | ||
const evalCountRecords = | ||
await dailyEvalCountService.evalCountRecordsByDate(testDate); | ||
|
||
evalCountRecords.forEach(({ at, value }) => { | ||
expect(isNaN(new Date(at).getTime())).toBe(false); | ||
expect(typeof value).toBe('number'); | ||
}); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import { Injectable } from '@nestjs/common'; | ||
import { DailyEvalCountDaoImpl } from './db/dailyEvalCount.database.dao'; | ||
import type { DateRange } from 'src/dateRange/dtos/dateRange.dto'; | ||
import type { IntRecord } from 'src/common/models/common.valueRecord.model'; | ||
|
||
@Injectable() | ||
export class DailyEvalCountService { | ||
constructor(private readonly dailyEvalCountDao: DailyEvalCountDaoImpl) {} | ||
|
||
async evalCountRecordsByDate({ | ||
start, | ||
end, | ||
}: DateRange): Promise<IntRecord[]> { | ||
const evalCounts = await this.dailyEvalCountDao.findEvalCountsByDate({ | ||
start, | ||
end, | ||
}); | ||
|
||
return evalCounts.map(({ date, count }) => ({ at: date, value: count })); | ||
} | ||
|
||
async userEvalCountRecordsByDatePerMonth( | ||
userId: number, | ||
{ start, end }: DateRange, | ||
): Promise<IntRecord[]> { | ||
const userEvalCounts = | ||
await this.dailyEvalCountDao.findUserEvalCountsByDatePerMonth({ | ||
userId, | ||
start, | ||
end, | ||
}); | ||
|
||
return userEvalCounts.map(({ date, count }) => ({ | ||
at: date, | ||
value: count, | ||
})); | ||
} | ||
} |
101 changes: 101 additions & 0 deletions
101
app/src/dailyEvalCount/db/dailyEvalCount.database.dao.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
import { Inject, Injectable } from '@nestjs/common'; | ||
import { InjectModel } from '@nestjs/mongoose'; | ||
import type { Model } from 'mongoose'; | ||
import type { | ||
FindEvalCountByDateInput, | ||
FindEvalCountByDateOutput, | ||
FindUserEvalCountByDateInput, | ||
} from '../dailyEvalCount.dto'; | ||
import { mv_daily_user_scale_team_counts } from './dailyEvalCount.database.schema'; | ||
import { RUNTIME_CONFIG } from 'src/config/runtime'; | ||
import type { ConfigType } from '@nestjs/config'; | ||
|
||
export type DailyEvalCountDao = { | ||
findEvalCountsByDate: ( | ||
args: FindEvalCountByDateInput, | ||
) => Promise<FindEvalCountByDateOutput[]>; | ||
findUserEvalCountsByDatePerMonth: ( | ||
args: FindUserEvalCountByDateInput, | ||
) => Promise<FindEvalCountByDateOutput[]>; | ||
}; | ||
|
||
@Injectable() | ||
export class DailyEvalCountDaoImpl implements DailyEvalCountDao { | ||
constructor( | ||
@InjectModel(mv_daily_user_scale_team_counts.name) | ||
private readonly dailyUserEvalCountModel: Model<mv_daily_user_scale_team_counts>, | ||
@Inject(RUNTIME_CONFIG.KEY) | ||
private readonly runtimeConfig: ConfigType<typeof RUNTIME_CONFIG>, | ||
) {} | ||
|
||
async findEvalCountsByDate({ | ||
start, | ||
end, | ||
}: FindEvalCountByDateInput): Promise<FindEvalCountByDateOutput[]> { | ||
return await this.dailyUserEvalCountModel | ||
.aggregate<FindEvalCountByDateOutput>() | ||
.match({ | ||
date: { | ||
$gte: start, | ||
$lt: end, | ||
}, | ||
}) | ||
.group({ | ||
// timezone 달라지면 dateFromParts 등으로 변환할것 | ||
_id: '$date', | ||
count: { | ||
$sum: '$count', | ||
}, | ||
}) | ||
.sort({ _id: 1 }) | ||
.project({ | ||
_id: 0, | ||
date: '$_id', | ||
count: 1, | ||
}); | ||
} | ||
|
||
async findUserEvalCountsByDatePerMonth({ | ||
userId, | ||
start, | ||
end, | ||
}: FindUserEvalCountByDateInput): Promise<FindEvalCountByDateOutput[]> { | ||
return await this.dailyUserEvalCountModel | ||
.aggregate<FindEvalCountByDateOutput>() | ||
.match({ | ||
userId, | ||
date: { | ||
$gte: start, | ||
$lt: end, | ||
}, | ||
}) | ||
.group({ | ||
_id: { | ||
$dateFromParts: { | ||
year: { | ||
$year: { | ||
date: '$date', | ||
timezone: this.runtimeConfig.TIMEZONE, | ||
}, | ||
}, | ||
month: { | ||
$month: { | ||
date: '$date', | ||
timezone: this.runtimeConfig.TIMEZONE, | ||
}, | ||
}, | ||
timezone: this.runtimeConfig.TIMEZONE, | ||
}, | ||
}, | ||
count: { | ||
$sum: '$count', | ||
}, | ||
}) | ||
.sort({ _id: 1 }) | ||
.project({ | ||
_id: 0, | ||
date: '$_id', | ||
count: 1, | ||
}); | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
app/src/dailyEvalCount/db/dailyEvalCount.database.schema.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { Prop, Schema, SchemaFactory } from '@nestjs/mongoose'; | ||
import { HydratedDocument } from 'mongoose'; | ||
|
||
export type DailyUserEvalCountDocument = | ||
HydratedDocument<mv_daily_user_scale_team_counts>; | ||
|
||
@Schema({ collection: 'mv_daily_user_scale_team_counts' }) | ||
export class mv_daily_user_scale_team_counts { | ||
@Prop({ required: true }) | ||
count: number; | ||
|
||
@Prop({ required: true }) | ||
date: Date; | ||
|
||
@Prop({ required: true }) | ||
userId: number; | ||
} | ||
|
||
export const dailyUserEvalCountSchema = SchemaFactory.createForClass( | ||
mv_daily_user_scale_team_counts, | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.