@@ -7,13 +7,8 @@ import { MeetingType } from '@definition/app';
77const useYearlyAttendance = ( year : string ) => {
88 const attendances = useAtomValue ( meetingAttendanceState ) ;
99
10- const startMonth = useMemo ( ( ) => {
11- return `${ + year - 1 } /09` ;
12- } , [ year ] ) ;
13-
14- const endMonth = useMemo ( ( ) => {
15- return `${ year } /08` ;
16- } , [ year ] ) ;
10+ const startMonth = useMemo ( ( ) => `${ + year - 1 } /09` , [ year ] ) ;
11+ const endMonth = useMemo ( ( ) => `${ year } /08` , [ year ] ) ;
1712
1813 const reports = useMemo ( ( ) => {
1914 return attendances . filter (
@@ -25,36 +20,37 @@ const useYearlyAttendance = (year: string) => {
2520 const getMeetingAverage = ( meeting : MeetingType ) => {
2621 if ( reports . length === 0 ) return 0 ;
2722
28- const values : number [ ] = [ ] ;
29-
30- for ( const attendance of reports ) {
31- for ( let i = 1 ; i <= 5 ; i ++ ) {
32- let total = 0 ;
33-
34- const weekData = attendance [ `week_${ i } ` ] as WeeklyAttendance ;
35- const meetingData = weekData [ meeting ] ;
36-
37- total += meetingData . reduce ( ( acc , current ) => {
38- if ( current ?. online ) {
39- return acc + current . online ;
40- }
41-
42- if ( current ?. present ) {
43- return acc + current . present ;
44- }
45-
46- return acc ;
47- } , 0 ) ;
48-
49- if ( total > 0 ) values . push ( total ) ;
50- }
51- }
52-
53- const sum = values . reduce ( ( acc , current ) => acc + current , 0 ) ;
54-
55- if ( sum === 0 ) return 0 ;
56-
57- return Math . round ( sum / values . length ) ;
23+ const monthlyAverages = reports . map ( ( monthlyAttendance ) => {
24+ const weekKeys = Object . keys ( monthlyAttendance ) . filter ( ( key ) =>
25+ key . startsWith ( 'week_' )
26+ ) ;
27+
28+ const weeklyTotals = weekKeys
29+ . map ( ( weekKey ) => {
30+ const weeklyAttendanceData = monthlyAttendance [
31+ weekKey
32+ ] as WeeklyAttendance ;
33+ const meetingAttendances = weeklyAttendanceData [ meeting ] ?? [ ] ;
34+ return meetingAttendances . reduce (
35+ ( acc , current ) =>
36+ acc + ( current ?. online ?? 0 ) + ( current ?. present ?? 0 ) ,
37+ 0
38+ ) ;
39+ } )
40+ . filter ( ( total ) => total > 0 ) ;
41+
42+ if ( ! weeklyTotals . length ) return 0 ;
43+
44+ return weeklyTotals . reduce ( ( a , b ) => a + b , 0 ) / weeklyTotals . length ;
45+ } ) ;
46+
47+ const validMonthlyAverages = monthlyAverages . filter ( ( avg ) => avg > 0 ) ;
48+ if ( ! validMonthlyAverages . length ) return 0 ;
49+
50+ return Math . round (
51+ validMonthlyAverages . reduce ( ( a , b ) => a + b , 0 ) /
52+ validMonthlyAverages . length
53+ ) ;
5854 } ;
5955
6056 return { getMeetingAverage } ;
0 commit comments