-
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: ✨ personal general 에 dailyActivities 추가
- 기존과는 다른 구조로 제작하였습니다. 앞으로 db 정리하고 이런 방식으로 고치면 어떨까 하는 생각입니다. - daily_logtimes collection 을 추가했습니다. - scale_teams, events_users 로 인해 성능 저하가 심각한 수준이면 별도 collection 으로 분리할 예정입니다. 현재 자체 테스트에선 dev 기준 100 ~ 300ms 정도 나옵니다. - close #388 - close #389
- Loading branch information
Showing
17 changed files
with
484 additions
and
2 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,11 @@ | ||
import { | ||
lookupStage, | ||
type CollectionLookup, | ||
} from 'src/database/mongoose/database.mongoose.aggregation'; | ||
import { events } from './event.database.schema'; | ||
|
||
export const lookupEvents: CollectionLookup = ( | ||
localField, | ||
foreignField, | ||
pipeline, | ||
) => lookupStage(events.name, localField, foreignField, pipeline); |
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,27 @@ | ||
import { Prop, Schema, SchemaFactory } from '@nestjs/mongoose'; | ||
import type { HydratedDocument } from 'mongoose'; | ||
|
||
export type eventDocument = HydratedDocument<events>; | ||
|
||
@Schema({ collection: 'events' }) | ||
export class events { | ||
@Prop({ required: true }) | ||
id: number; | ||
|
||
@Prop({ required: true }) | ||
beginAt: Date; | ||
|
||
@Prop({ required: true }) | ||
endAt: Date; | ||
|
||
@Prop({ required: true }) | ||
name: string; | ||
|
||
@Prop({ required: true }) | ||
description: string; | ||
|
||
@Prop({ required: true }) | ||
location: string; | ||
} | ||
|
||
export const eventSchema = SchemaFactory.createForClass(events); |
11 changes: 11 additions & 0 deletions
11
app/src/api/eventsUser/db/eventsUser.database.aggregate.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,11 @@ | ||
import { | ||
lookupStage, | ||
type CollectionLookup, | ||
} from 'src/database/mongoose/database.mongoose.aggregation'; | ||
import { events_users } from './eventsUser.database.schema'; | ||
|
||
export const lookupEventsUsers: CollectionLookup = ( | ||
localField, | ||
foreignField, | ||
pipeline, | ||
) => lookupStage(events_users.name, localField, foreignField, pipeline); |
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 @@ | ||
import { Prop, Schema } from '@nestjs/mongoose'; | ||
|
||
@Schema({ collection: 'events_users' }) | ||
export class events_users { | ||
@Prop({ required: true }) | ||
id: number; | ||
|
||
@Prop({ required: true }) | ||
eventId: number; | ||
|
||
@Prop({ required: true }) | ||
userId: 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
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,118 @@ | ||
import { Injectable } from '@nestjs/common'; | ||
import { InjectModel } from '@nestjs/mongoose'; | ||
import type { Model } from 'mongoose'; | ||
import { daily_logtimes } from 'src/dailyLogtime/db/dailyLogtime.database.schema'; | ||
import { lookupEvents } from 'src/api/event/db/event.database.aggregate'; | ||
import { events } from 'src/api/event/db/event.database.schema'; | ||
import { events_users } from 'src/api/eventsUser/db/eventsUser.database.schema'; | ||
import { scale_team } from 'src/api/scaleTeam/db/scaleTeam.database.schema'; | ||
import { | ||
DailyActivityType, | ||
type DailyDefaultRecord, | ||
type DailyLogtimeRecord, | ||
type FindDailyActivityRecordInput, | ||
type FindDailyActivityRecordOutput, | ||
} from './dailyActivity.dto'; | ||
|
||
export type DailyActivityDao = { | ||
findAllRecordByDate: ( | ||
args: FindDailyActivityRecordInput, | ||
) => Promise<FindDailyActivityRecordOutput[]>; | ||
}; | ||
|
||
@Injectable() | ||
export class DailyActivityDaoImpl implements DailyActivityDao { | ||
constructor( | ||
@InjectModel(scale_team.name) | ||
private readonly scaleTeamModel: Model<scale_team>, | ||
) {} | ||
|
||
async findAllRecordByDate({ | ||
userId, | ||
start, | ||
end, | ||
}: FindDailyActivityRecordInput): Promise<FindDailyActivityRecordOutput[]> { | ||
return await this.scaleTeamModel | ||
.aggregate<DailyLogtimeRecord | DailyDefaultRecord>() | ||
.match({ | ||
$or: [{ 'corrector.id': userId }, { 'correcteds.id': userId }], | ||
filledAt: { | ||
$gte: start, | ||
$lt: end, | ||
}, | ||
}) | ||
.sort({ filledAt: 1 }) | ||
.project({ | ||
_id: 0, | ||
id: 1, | ||
at: '$filledAt', | ||
type: { | ||
$cond: { | ||
if: { $eq: ['$corrector.id', userId] }, | ||
then: DailyActivityType.CORRECTOR, | ||
else: DailyActivityType.CORRECTED, | ||
}, | ||
}, | ||
}) | ||
.unionWith({ | ||
coll: daily_logtimes.name, | ||
pipeline: [ | ||
{ | ||
$match: { | ||
userId, | ||
date: { | ||
$gte: start, | ||
$lt: end, | ||
}, | ||
}, | ||
}, | ||
{ | ||
$project: { | ||
_id: 0, | ||
date: '$date', | ||
type: { $literal: DailyActivityType.LOGTIME }, | ||
value: 1, | ||
}, | ||
}, | ||
], | ||
}) | ||
.unionWith({ | ||
coll: events_users.name, | ||
pipeline: [ | ||
{ | ||
$match: { | ||
userId, | ||
}, | ||
}, | ||
lookupEvents('eventId', 'id', [ | ||
{ | ||
$match: { | ||
date: { | ||
$gte: start, | ||
$lt: end, | ||
}, | ||
}, | ||
}, | ||
]), | ||
{ | ||
$project: { | ||
_id: 0, | ||
id: 1, | ||
at: { $first: `$${events.name}.endAt` }, | ||
type: { $literal: DailyActivityType.EVENT }, | ||
}, | ||
}, | ||
{ | ||
$match: { | ||
at: { $ne: null }, | ||
}, | ||
}, | ||
{ | ||
$sort: { | ||
at: 1, | ||
}, | ||
}, | ||
], | ||
}); | ||
} | ||
} |
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,39 @@ | ||
export enum DailyActivityType { | ||
CORRECTOR, | ||
CORRECTED, | ||
EVENT, | ||
LOGTIME, | ||
} | ||
|
||
export type DailyLogtimeRecord = { | ||
type: DailyActivityType.LOGTIME; | ||
value: number; | ||
date: Date; | ||
}; | ||
|
||
export type DailyDefaultRecord = { | ||
type: Exclude<DailyActivityType, DailyActivityType.LOGTIME>; | ||
id: number; | ||
at: Date; | ||
}; | ||
|
||
export type FindDailyActivityRecordInput = { | ||
userId: number; | ||
start: Date; | ||
end: Date; | ||
}; | ||
|
||
export type FindDailyActivityRecordOutput = | ||
| DailyLogtimeRecord | ||
| DailyDefaultRecord; | ||
|
||
export type FindUserDailyActivityByDateInput = { | ||
userId: number; | ||
start: Date; | ||
end: Date; | ||
}; | ||
|
||
export type FindUserDailyActivityByDateOutput = { | ||
date: Date; | ||
records: (Omit<DailyLogtimeRecord, 'date'> | DailyDefaultRecord)[]; | ||
}; |
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,20 @@ | ||
import { Module } from '@nestjs/common'; | ||
import { MongooseModule } from '@nestjs/mongoose'; | ||
import { | ||
ScaleTeamSchema, | ||
scale_team, | ||
} from 'src/api/scaleTeam/db/scaleTeam.database.schema'; | ||
import { DailyActivityDaoImpl } from './dailyActivity.dao'; | ||
import { DailyActivityService } from './dailyActivity.service'; | ||
|
||
@Module({ | ||
imports: [ | ||
MongooseModule.forFeature([ | ||
{ name: scale_team.name, schema: ScaleTeamSchema }, | ||
]), | ||
], | ||
providers: [DailyActivityDaoImpl, DailyActivityService], | ||
exports: [DailyActivityService], | ||
}) | ||
// eslint-disable-next-line @typescript-eslint/no-extraneous-class | ||
export class DailyActivityModule {} |
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,60 @@ | ||
import { Injectable } from '@nestjs/common'; | ||
import { DateWrapper } from 'src/dateWrapper/dateWrapper'; | ||
import { DailyActivityDaoImpl } from './dailyActivity.dao'; | ||
import { | ||
DailyActivityType, | ||
type DailyDefaultRecord, | ||
type DailyLogtimeRecord, | ||
type FindUserDailyActivityByDateInput, | ||
type FindUserDailyActivityByDateOutput, | ||
} from './dailyActivity.dto'; | ||
|
||
@Injectable() | ||
export class DailyActivityService { | ||
constructor(private readonly dailyActivityDao: DailyActivityDaoImpl) {} | ||
|
||
async findAllUserDailyActivityByDate( | ||
args: FindUserDailyActivityByDateInput, | ||
): Promise<FindUserDailyActivityByDateOutput[]> { | ||
const records = await this.dailyActivityDao.findAllRecordByDate(args); | ||
|
||
const recordMapByDate = records.reduce((recordMap, record) => { | ||
if (isDailyLogtimeRecord(record)) { | ||
const timestamp = record.date.getTime(); | ||
const prevRecords = recordMap.get(timestamp) ?? []; | ||
|
||
recordMap.set(timestamp, [ | ||
...prevRecords, | ||
{ type: record.type, value: record.value }, | ||
]); | ||
|
||
return recordMap; | ||
} | ||
|
||
const timestamp = new DateWrapper(record.at) | ||
.startOfDate() | ||
.toDate() | ||
.getTime(); | ||
|
||
const prevRecords = recordMap.get(timestamp) ?? []; | ||
|
||
recordMap.set(timestamp, [ | ||
...prevRecords, | ||
{ type: record.type, id: record.id, at: record.at }, | ||
]); | ||
|
||
return recordMap; | ||
}, new Map<number, FindUserDailyActivityByDateOutput['records']>()); | ||
|
||
return Array.from(recordMapByDate.entries()) | ||
.sort(([a], [b]) => a - b) | ||
.map(([timestamp, records]) => ({ | ||
date: new Date(timestamp), | ||
records, | ||
})); | ||
} | ||
} | ||
|
||
const isDailyLogtimeRecord = ( | ||
record: DailyLogtimeRecord | DailyDefaultRecord, | ||
): record is DailyLogtimeRecord => record.type === DailyActivityType.LOGTIME; |
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,18 @@ | ||
import { Prop, Schema, SchemaFactory } from '@nestjs/mongoose'; | ||
import type { HydratedDocument } from 'mongoose'; | ||
|
||
export type DailyLogtimeDocument = HydratedDocument<daily_logtimes>; | ||
|
||
@Schema({ collection: 'daily_logtimes' }) | ||
export class daily_logtimes { | ||
@Prop({ required: true }) | ||
userId: number; | ||
|
||
@Prop({ required: true }) | ||
date: Date; | ||
|
||
@Prop({ required: true }) | ||
value: number; | ||
} | ||
|
||
export const dailyLogtimeSchema = SchemaFactory.createForClass(daily_logtimes); |
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.