@@ -8,6 +8,14 @@ import ReactTooltip from 'react-tooltip';
88import TotalReportBarGraph from './TotalReportBarGraph' ;
99import Loading from '../../common/Loading' ;
1010import { generateBarData as generateBarDataUtil } from './generateBarData' ;
11+ import {
12+ getCachedData ,
13+ setCachedData ,
14+ validateUserList ,
15+ logApiRequest ,
16+ logApiResponse ,
17+ checkIncompleteResponse ,
18+ } from './cacheUtils' ;
1119
1220function TotalPeopleReport ( props ) {
1321 const { startDate, endDate, userProfiles, darkMode } = props ;
@@ -39,104 +47,60 @@ function TotalPeopleReport(props) {
3947
4048 const loadTimeEntriesForPeriod = useCallback (
4149 async controller => {
50+ const reportName = 'TotalPeopleReport' ;
4251 const url = ENDPOINTS . TIME_ENTRIES_REPORTS ;
4352
4453 if ( ! url ) {
4554 setTotalPeopleReportDataLoading ( false ) ;
4655 return ;
4756 }
4857
49- // Don't make API call if userList is empty
50- if ( ! userList || userList . length === 0 ) {
51- // eslint-disable-next-line no-console
52- console . warn ( 'TotalPeopleReport: Skipping API call - userList is empty' , {
53- userProfilesLength : userProfiles ?. length ,
54- userListLength : userList ?. length ,
55- } ) ;
58+ // Validate userList
59+ if ( ! validateUserList ( userList , userProfiles , reportName ) ) {
5660 setTotalPeopleReportDataLoading ( false ) ;
5761 setAllTimeEntries ( [ ] ) ;
5862 return ;
5963 }
6064
61- // Check cache with date range key to ensure cache is valid for current date range
62- const cacheKey = `TotalPeopleReport_${ fromDate } _${ toDate } ` ;
63- const cachedData = localStorage . getItem ( cacheKey ) ;
64- if ( cachedData ) {
65- try {
66- const parsedData = JSON . parse ( cachedData ) ;
67- if ( parsedData && Array . isArray ( parsedData ) && parsedData . length > 0 ) {
68- // eslint-disable-next-line no-console
69- console . log ( 'TotalPeopleReport: Using cached data' , {
70- cacheKey,
71- dataLength : parsedData . length ,
72- } ) ;
73- setAllTimeEntries ( parsedData ) ;
74- setTotalPeopleReportDataLoading ( false ) ;
75- setTotalPeopleReportDataReady ( true ) ;
76- return ;
77- }
78- } catch ( e ) {
79- // eslint-disable-next-line no-console
80- console . warn ( 'TotalPeopleReport: Failed to parse cached data' , e ) ;
81- }
65+ // Check cache with date range key
66+ const cacheKey = `${ reportName } _${ fromDate } _${ toDate } ` ;
67+ const cached = getCachedData ( cacheKey , reportName ) ;
68+ if ( cached . data ) {
69+ setAllTimeEntries ( cached . data ) ;
70+ setTotalPeopleReportDataLoading ( false ) ;
71+ setTotalPeopleReportDataReady ( true ) ;
72+ return ;
8273 }
8374
8475 try {
85- // eslint-disable-next-line no-console
86- console . log ( 'TotalPeopleReport API Request:' , {
87- url,
88- payload : { users : userList , fromDate, toDate } ,
76+ logApiRequest ( reportName , url , { users : userList , fromDate, toDate } , {
8977 usersCount : userList ?. length ,
9078 userProfilesCount : userProfiles ?. length ,
91- timestamp : new Date ( ) . toISOString ( ) ,
9279 } ) ;
80+
9381 const res = await axios . post (
9482 url ,
9583 { users : userList , fromDate, toDate } ,
9684 { signal : controller . signal } ,
9785 ) ;
98- // eslint-disable-next-line no-console
99- console . log ( 'TotalPeopleReport API Response:' , {
100- dataLength : res . data ?. length ,
101- sampleData : res . data ?. slice ( 0 , 2 ) ,
102- timestamp : new Date ( ) . toISOString ( ) ,
103- responseTime : new Date ( ) . toISOString ( ) ,
104- } ) ;
105-
86+
87+ logApiResponse ( reportName , res . data ?. length , res . data ?. slice ( 0 , 2 ) ) ;
88+
10689 const timeEntries = res . data . map ( entry => ( {
10790 userId : entry . personId ,
10891 hours : entry . hours ,
10992 minutes : entry . minutes ,
11093 isTangible : entry . isTangible ,
11194 date : entry . dateOfWork ,
11295 } ) ) ;
113-
114- // Log if response seems incomplete (very few entries for many users)
115- if ( timeEntries . length > 0 && userList . length > 100 && timeEntries . length < 10 ) {
116- // eslint-disable-next-line no-console
117- console . warn ( 'TotalPeopleReport: Response may be incomplete' , {
118- timeEntriesCount : timeEntries . length ,
119- usersCount : userList . length ,
120- ratio : ( timeEntries . length / userList . length * 100 ) . toFixed ( 2 ) + '%' ,
121- message : 'If data seems incomplete, backend may still be processing. Try refreshing in a few minutes.' ,
122- } ) ;
123- }
124-
96+
97+ checkIncompleteResponse ( reportName , timeEntries . length , userList . length ) ;
98+
12599 setAllTimeEntries ( timeEntries ) ;
126-
127- // Cache the data with date range key
128- if ( timeEntries . length > 0 ) {
129- localStorage . setItem ( cacheKey , JSON . stringify ( timeEntries ) ) ;
130- // eslint-disable-next-line no-console
131- console . log ( 'TotalPeopleReport: Data cached' , { cacheKey, dataLength : timeEntries . length } ) ;
132- } else {
133- // eslint-disable-next-line no-console
134- console . warn ( 'TotalPeopleReport: Empty response - clearing cache' , { cacheKey } ) ;
135- localStorage . removeItem ( cacheKey ) ;
136- }
100+ setCachedData ( cacheKey , timeEntries , reportName ) ;
137101 } catch ( error ) {
138102 // eslint-disable-next-line no-console
139- console . error ( 'TotalPeopleReport API Error:' , error ) ;
103+ console . error ( ` ${ reportName } API Error:` , error ) ;
140104 setTotalPeopleReportDataLoading ( false ) ;
141105 }
142106 } ,
0 commit comments