-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: 특정 주차의 세션 출석률 통계 조회 기능 구현 (#37)
* feat: 특정 주차의 세션 출석률 통계 조회 기능 구현 * refactor: 통계 집계 로직 usecase로 이동, Output 으로 넘기기
- Loading branch information
Showing
6 changed files
with
174 additions
and
32 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
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
84 changes: 84 additions & 0 deletions
84
src/main/kotlin/com/depromeet/makers/domain/usecase/GetAttendancesByWeek.kt
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,84 @@ | ||
package com.depromeet.makers.domain.usecase | ||
|
||
import com.depromeet.makers.domain.exception.SessionNotFoundException | ||
import com.depromeet.makers.domain.gateway.AttendanceGateway | ||
import com.depromeet.makers.domain.gateway.SessionGateway | ||
import com.depromeet.makers.domain.model.AttendanceStatus | ||
import com.depromeet.makers.domain.model.MemberRole | ||
import com.depromeet.makers.domain.model.Session | ||
import com.depromeet.makers.domain.usecase.GetAttendancesByWeek.GetAttendancesByWeekOutput | ||
import com.depromeet.makers.domain.usecase.GetAttendancesByWeek.GetAttendancesByWeekOutput.TeamAttendancesStats | ||
|
||
class GetAttendancesByWeek( | ||
private val attendanceGateway: AttendanceGateway, | ||
private val sessionGateway: SessionGateway, | ||
) : UseCase<GetAttendancesByWeek.GetAttendancesByWeekInput, GetAttendancesByWeekOutput> { | ||
data class GetAttendancesByWeekInput( | ||
val generation: Int, | ||
val week: Int, | ||
) | ||
|
||
data class GetAttendancesByWeekOutput( | ||
val session: Session, | ||
val attendancePercentage: Double, | ||
val totalAttendance: Int, | ||
val totalMember: Int, | ||
val teamAttendances: Map<Int, TeamAttendancesStats>, | ||
) { | ||
data class TeamAttendancesStats( | ||
val teamNumber: Int, | ||
var attendanceCount: Int, | ||
var memberCount: Int, | ||
) | ||
} | ||
|
||
|
||
override fun execute(input: GetAttendancesByWeekInput): GetAttendancesByWeekOutput { | ||
val attendances = attendanceGateway.findAllByGenerationAndWeek(input.generation, input.week) | ||
val session = sessionGateway.findByGenerationAndWeek(input.generation, input.week) ?: throw SessionNotFoundException() | ||
|
||
var totalAttendanceCount = 0 | ||
var totalMemberCount = 0 | ||
val teams = mutableMapOf<Int, TeamAttendancesStats>() | ||
|
||
attendances | ||
.map { Pair(it.member.generations.find { generation -> generation.generationId == it.generation }, it) } | ||
.filter { (memberGeneration, _) -> | ||
// 현재 기수의 ROLE이 MEMBER인 경우만 집계 | ||
memberGeneration?.role == MemberRole.MEMBER | ||
} | ||
.filter { (_, attendance) -> | ||
// 출석의 대상이 되는 인원들만 집계 | ||
attendance.attendanceStatus == AttendanceStatus.ATTENDANCE || attendance.attendanceStatus == AttendanceStatus.ATTENDANCE_ON_HOLD | ||
} | ||
.forEach { (memberGeneration, attendance) -> | ||
val teamId = memberGeneration!!.groupId!! | ||
val team = teams.getOrDefault(teamId, TeamAttendancesStats(teamId, 0, 0)) | ||
when (attendance.attendanceStatus) { | ||
AttendanceStatus.ATTENDANCE -> { | ||
team.attendanceCount++ | ||
team.memberCount++ | ||
totalAttendanceCount++ | ||
totalMemberCount++ | ||
} | ||
|
||
AttendanceStatus.ATTENDANCE_ON_HOLD -> { | ||
team.memberCount++ | ||
totalMemberCount++ | ||
} | ||
|
||
else -> { | ||
// do nothing | ||
} | ||
} | ||
teams[teamId] = team | ||
} | ||
return GetAttendancesByWeekOutput( | ||
session = session, | ||
attendancePercentage = if (totalMemberCount == 0) 0.0 else (totalAttendanceCount.toDouble() / totalMemberCount.toDouble() * 100), | ||
totalAttendance = totalAttendanceCount, | ||
totalMember = totalMemberCount, | ||
teamAttendances = teams, | ||
) | ||
} | ||
} |
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
69 changes: 69 additions & 0 deletions
69
.../kotlin/com/depromeet/makers/presentation/restapi/dto/response/AttendanceStatsResponse.kt
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,69 @@ | ||
package com.depromeet.makers.presentation.restapi.dto.response | ||
|
||
import com.depromeet.makers.domain.usecase.GetAttendancesByWeek.GetAttendancesByWeekOutput | ||
import io.swagger.v3.oas.annotations.media.Schema | ||
import java.time.LocalDateTime | ||
|
||
@Schema(description = "출석 조회 결과 DTO") | ||
data class AttendanceStatsResponse( | ||
|
||
@Schema(description = "기수", example = "15") | ||
val generation: Int, | ||
|
||
@Schema(description = "주차", example = "1") | ||
val week: Int, | ||
|
||
@Schema(description = "세션 날짜", example = "2021-10-10T10:00:00") | ||
val sessionDate: LocalDateTime, | ||
|
||
@Schema(description = "전체 출석률", example = "50") | ||
val attendancePercentage: Int, | ||
|
||
@Schema(description = "출석 인원", example = "20") | ||
val attendanceCount: Int, | ||
|
||
@Schema(description = "오늘 세션에 출석 할 인원", example = "40") | ||
val memberCount: Int, | ||
|
||
@Schema(description = "출석한 팀 목록") | ||
val teams: Map<Int, AttendanceStatsByTeamResponse>, | ||
) { | ||
data class AttendanceStatsByTeamResponse( | ||
@Schema(description = "팀 번호", example = "1") | ||
val teamNumber: Int, | ||
|
||
@Schema(description = "팀 출석 인원", example = "4") | ||
var attendanceCount: Int, | ||
|
||
@Schema(description = "팀원 인원", example = "10") | ||
var memberCount: Int, | ||
) { | ||
companion object { | ||
fun new(teamNumber: Int, attendanceCount: Int, memberCount: Int) = AttendanceStatsByTeamResponse( | ||
teamNumber = teamNumber, | ||
attendanceCount = attendanceCount, | ||
memberCount = memberCount, | ||
) | ||
} | ||
} | ||
|
||
companion object { | ||
fun fromDomain(output: GetAttendancesByWeekOutput): AttendanceStatsResponse { | ||
return AttendanceStatsResponse( | ||
generation = output.session.generation, | ||
week = output.session.week, | ||
sessionDate = output.session.startTime, | ||
attendancePercentage = output.attendancePercentage.toInt(), | ||
attendanceCount = output.totalAttendance, | ||
memberCount = output.totalMember, | ||
teams = output.teamAttendances.mapValues { (teamId, teamAttendancesStats) -> | ||
AttendanceStatsByTeamResponse.new( | ||
teamNumber = teamId, | ||
attendanceCount = teamAttendancesStats.attendanceCount, | ||
memberCount = teamAttendancesStats.memberCount, | ||
) | ||
} | ||
) | ||
} | ||
} | ||
} |
32 changes: 0 additions & 32 deletions
32
...lin/com/depromeet/makers/presentation/restapi/dto/response/ViewAttendanceStatsResponse.kt
This file was deleted.
Oops, something went wrong.