@@ -162,6 +162,8 @@ export function useLifetimeStatistics({ year, stats }: { year?: number, stats?:
162
162
return lifeTimeStatsMap
163
163
} , CACHE . LIFETIME_STATS_CACHE , { } , Number . MAX_SAFE_INTEGER )
164
164
165
+ const getRequiredSeconds = useGetRequiredSecondsForPeriod ( lifetimeYear )
166
+
165
167
useEffect ( ( ) => {
166
168
forceFetch ( )
167
169
// eslint-disable-next-line react-hooks/exhaustive-deps
@@ -196,6 +198,44 @@ export function useLifetimeStatistics({ year, stats }: { year?: number, stats?:
196
198
} , [ ] ) . sort ( ( a , b ) => a . workedSeconds - b . workedSeconds )
197
199
} , [ data , lifetimeYear ] )
198
200
201
+ const overhourStats : { totalDiffSeconds : number , secondsInLastWeek : number , secondsInLastMonth : number } = useMemo ( ( ) => {
202
+ const sorted = [ ...yearWeeksLifetime ] . sort ( ( a , b ) => `${ a . year } -${ a . week } ` . localeCompare ( `${ b . year } -${ b . week } ` ) )
203
+ const resolvedLastHalfYear = sorted . reduce ( ( array , { year, week, workedSeconds} , index ) => {
204
+ let currentDiff = workedSeconds - getRequiredSeconds ( year , week )
205
+ for ( let x = 0 ; x < index ; x ++ ) {
206
+ const oldWeek = array [ x ]
207
+ if ( currentDiff === 0 ) {
208
+ break
209
+ }
210
+ if ( oldWeek . diffSeconds === 0 || currentDiff > 0 === oldWeek . diffSeconds > 0 ) {
211
+ continue
212
+ }
213
+ if ( Math . abs ( currentDiff ) - Math . abs ( oldWeek . diffSeconds ) > 0 ) {
214
+ currentDiff += oldWeek . diffSeconds
215
+ oldWeek . diffSeconds = 0
216
+ }
217
+ else {
218
+ oldWeek . diffSeconds += currentDiff
219
+ currentDiff = 0
220
+ }
221
+ }
222
+
223
+ array . push ( { year, week, workedSeconds, diffSeconds : currentDiff } )
224
+ return array . slice ( - 25 )
225
+ } , [ ] )
226
+
227
+ return resolvedLastHalfYear . reduce ( ( stats , result , index ) => {
228
+ if ( index === 0 ) {
229
+ stats . secondsInLastWeek = result . diffSeconds
230
+ }
231
+ if ( index < 4 ) {
232
+ stats . secondsInLastMonth += result . diffSeconds
233
+ }
234
+ stats . totalDiffSeconds += result . diffSeconds
235
+ return stats
236
+ } , { totalDiffSeconds : 0 , secondsInLastWeek : 0 , secondsInLastMonth : 0 } )
237
+ } , [ yearWeeksLifetime , getRequiredSeconds ] )
238
+
199
239
const lifeTimeTotal = useMemo ( ( ) => {
200
240
const years = Array . from ( { length : ( new Date ( ) . getFullYear ( ) ) - lifetimeYear + 1 } , ( _v , idx ) => lifetimeYear + idx )
201
241
return years . reduce ( ( total , year ) => total + ( data [ year ] ?. total ?? 0 ) , 0 )
@@ -212,10 +252,11 @@ export function useLifetimeStatistics({ year, stats }: { year?: number, stats?:
212
252
} , [ yearWeeksLifetime ] )
213
253
214
254
return {
215
- data : { lifeTimeTotal, yearWeeksLifetime, lifeTimeMedianTop, lifeTimeMedianLow } ,
255
+ data : { lifeTimeTotal, yearWeeksLifetime, lifeTimeMedianTop, lifeTimeMedianLow, overhourStats } ,
216
256
actions : {
217
257
refresh : forceFetch
218
258
} ,
219
259
loading
220
260
}
221
261
}
262
+
0 commit comments