@@ -229,14 +229,40 @@ function isRestrictedLog(row) {
229229 return row . jsonPayload ?. [ "@type" ] ?. includes ( "Restricted" ) || false ;
230230}
231231
232+ function calculateRetentionDate ( logsArray ) {
233+ let oldestTimestamp = Infinity ;
234+ logsArray . forEach ( ( row ) => {
235+ const ts = new Date (
236+ row . timestamp || row . insertTimestamp || row . jsonPayload ?. timestamp || row . jsonpayload ?. timestamp
237+ ) . getTime ( ) ;
238+ if ( ! isNaN ( ts ) && ts < oldestTimestamp ) {
239+ oldestTimestamp = ts ;
240+ }
241+ } ) ;
242+
243+ let retentionDateIdentifier = null ;
244+ if ( oldestTimestamp !== Infinity ) {
245+ const fiftyFiveDaysMs = 55 * 24 * 60 * 60 * 1000 ;
246+ const oneHourMs = 60 * 60 * 1000 ;
247+ const expirationBasedOnLogs = oldestTimestamp + fiftyFiveDaysMs ;
248+ const minimumRetention = Date . now ( ) + oneHourMs ;
249+ const chosenRetention = new Date ( Math . max ( expirationBasedOnLogs , minimumRetention ) ) ;
250+ retentionDateIdentifier = chosenRetention . toISOString ( ) ;
251+ } else {
252+ retentionDateIdentifier = new Date ( Date . now ( ) + 60 * 60 * 1000 ) . toISOString ( ) ;
253+ }
254+ return retentionDateIdentifier ;
255+ }
256+
232257export function ensureCorrectFormat ( data ) {
233258 let logsArray ;
234259 //Handle if data is not array (like when reading a file).
235260 if ( ! Array . isArray ( data ) ) {
236- // If it's already in the correct format, return it as is.
261+ // If it's already in the correct format, return it as is, BUT RE-CALCULATE TTL for grace period .
237262 if ( data && data . rawLogs && Array . isArray ( data . rawLogs ) ) {
238263 return {
239264 ...data ,
265+ retentionDate : calculateRetentionDate ( data . rawLogs ) ,
240266 APIKEY : data . APIKEY || DEFAULT_API_KEY ,
241267 } ;
242268 } else {
@@ -337,26 +363,8 @@ export function ensureCorrectFormat(data) {
337363
338364 if ( ! hasPoints ) log ( "Bounds Calculation Failed: Could not find vehicle location data in any row." ) ;
339365
340- // Calculate retention date
341- let oldestTimestamp = Infinity ;
342- logsArray . forEach ( ( row ) => {
343- const ts = new Date ( row . timestamp || row . insertTimestamp || row . jsonPayload ?. timestamp ) . getTime ( ) ;
344- if ( ! isNaN ( ts ) && ts < oldestTimestamp ) {
345- oldestTimestamp = ts ;
346- }
347- } ) ;
348-
349- let retentionDateIdentifier = null ;
350- if ( oldestTimestamp !== Infinity ) {
351- const fiftyFiveDaysMs = 55 * 24 * 60 * 60 * 1000 ;
352- const oneHourMs = 60 * 60 * 1000 ;
353- const expirationBasedOnLogs = oldestTimestamp + fiftyFiveDaysMs ;
354- const minimumRetention = Date . now ( ) + oneHourMs ;
355- retentionDateIdentifier = new Date ( Math . max ( expirationBasedOnLogs , minimumRetention ) ) . toISOString ( ) ;
356- } else {
357- // If no valid timestamps found, default to 1 hour from now for safety
358- retentionDateIdentifier = new Date ( Date . now ( ) + 60 * 60 * 1000 ) . toISOString ( ) ;
359- }
366+ // Calculate retention date using the helper
367+ const retentionDateIdentifier = calculateRetentionDate ( logsArray ) ;
360368
361369 return {
362370 APIKEY : DEFAULT_API_KEY ,
0 commit comments