mirror of
				https://github.com/open-metadata/OpenMetadata.git
				synced 2025-10-31 02:29:03 +00:00 
			
		
		
		
	human readable retention period (#18119)
This commit is contained in:
		
							parent
							
								
									7ae81272b3
								
							
						
					
					
						commit
						304ee40eb0
					
				| @ -143,6 +143,41 @@ describe('Test Retention Period Component', () => { | |||||||
|     expect(mockOnUpdate).toHaveBeenCalledWith('69 days and 16 hours'); |     expect(mockOnUpdate).toHaveBeenCalledWith('69 days and 16 hours'); | ||||||
|   }); |   }); | ||||||
| 
 | 
 | ||||||
|  |   it('Should render correctly with ISO 8601 duration P0Y0M4D', () => { | ||||||
|  |     render( | ||||||
|  |       <RetentionPeriod | ||||||
|  |         {...mockRetentionPeriodProps} | ||||||
|  |         retentionPeriod="P0Y0M4D" | ||||||
|  |       /> | ||||||
|  |     ); | ||||||
|  | 
 | ||||||
|  |     expect(screen.getByText('4 days')).toBeInTheDocument(); | ||||||
|  |   }); | ||||||
|  | 
 | ||||||
|  |   it('Should render correctly with ISO 8601 duration P0Y0M4W', () => { | ||||||
|  |     render( | ||||||
|  |       <RetentionPeriod | ||||||
|  |         {...mockRetentionPeriodProps} | ||||||
|  |         retentionPeriod="P0Y0M4W" | ||||||
|  |       /> | ||||||
|  |     ); | ||||||
|  | 
 | ||||||
|  |     expect(screen.getByText('4 weeks')).toBeInTheDocument(); | ||||||
|  |   }); | ||||||
|  | 
 | ||||||
|  |   it('Should render correctly with ISO 8601 duration P0Y1M1DT1H30M', () => { | ||||||
|  |     render( | ||||||
|  |       <RetentionPeriod | ||||||
|  |         {...mockRetentionPeriodProps} | ||||||
|  |         retentionPeriod="P2Y1M1DT1H30M" | ||||||
|  |       /> | ||||||
|  |     ); | ||||||
|  | 
 | ||||||
|  |     expect( | ||||||
|  |       screen.getByText('2 years 1 month 1 day 1 hour 30 minutes') | ||||||
|  |     ).toBeInTheDocument(); | ||||||
|  |   }); | ||||||
|  | 
 | ||||||
|   it('Should not render Retention Period Component if has no permission', () => { |   it('Should not render Retention Period Component if has no permission', () => { | ||||||
|     render( |     render( | ||||||
|       <RetentionPeriod {...mockRetentionPeriodProps} hasPermission={false} /> |       <RetentionPeriod {...mockRetentionPeriodProps} hasPermission={false} /> | ||||||
|  | |||||||
| @ -22,6 +22,7 @@ import { | |||||||
| } from 'antd'; | } from 'antd'; | ||||||
| import { useForm } from 'antd/lib/form/Form'; | import { useForm } from 'antd/lib/form/Form'; | ||||||
| import { AxiosError } from 'axios'; | import { AxiosError } from 'axios'; | ||||||
|  | import { Duration } from 'luxon'; | ||||||
| import React, { useCallback, useEffect, useState } from 'react'; | import React, { useCallback, useEffect, useState } from 'react'; | ||||||
| import { useTranslation } from 'react-i18next'; | import { useTranslation } from 'react-i18next'; | ||||||
| import { ReactComponent as EditIcon } from '../../../assets/svg/edit-new.svg'; | import { ReactComponent as EditIcon } from '../../../assets/svg/edit-new.svg'; | ||||||
| @ -34,6 +35,58 @@ import { showErrorToast } from '../../../utils/ToastUtils'; | |||||||
| import { ExtraInfoLabel } from '../../DataAssets/DataAssetsHeader/DataAssetsHeader.component'; | import { ExtraInfoLabel } from '../../DataAssets/DataAssetsHeader/DataAssetsHeader.component'; | ||||||
| import { RetentionPeriodProps } from './RetentionPeriod.interface'; | import { RetentionPeriodProps } from './RetentionPeriod.interface'; | ||||||
| 
 | 
 | ||||||
|  | // Helper function to detect and format ISO 8601 duration
 | ||||||
|  | const formatRetentionPeriod = (retentionPeriod: string | undefined) => { | ||||||
|  |   if (!retentionPeriod) { | ||||||
|  |     return NO_DATA_PLACEHOLDER; | ||||||
|  |   } | ||||||
|  | 
 | ||||||
|  |   const isoDurationRegex = | ||||||
|  |     /^P(?!$)(\d+Y)?(\d+M)?(\d+W)?(\d+D)?(T(\d+H)?(\d+M)?(\d+S)?)?$/; | ||||||
|  |   // Check if the string matches the ISO 8601 duration format
 | ||||||
|  |   if (isoDurationRegex.test(retentionPeriod)) { | ||||||
|  |     const duration = Duration.fromISO(retentionPeriod); | ||||||
|  | 
 | ||||||
|  |     const years = duration.years | ||||||
|  |       ? `${duration.years} year${duration.years > 1 ? 's' : ''}` | ||||||
|  |       : ''; | ||||||
|  |     const months = duration.months | ||||||
|  |       ? `${duration.months} month${duration.months > 1 ? 's' : ''}` | ||||||
|  |       : ''; | ||||||
|  |     const weeks = duration.weeks | ||||||
|  |       ? `${duration.weeks} week${duration.weeks > 1 ? 's' : ''}` | ||||||
|  |       : ''; | ||||||
|  |     const days = duration.days | ||||||
|  |       ? `${duration.days} day${duration.days > 1 ? 's' : ''}` | ||||||
|  |       : ''; | ||||||
|  |     const hours = duration.hours | ||||||
|  |       ? `${duration.hours} hour${duration.hours > 1 ? 's' : ''}` | ||||||
|  |       : ''; | ||||||
|  |     const minutes = duration.minutes | ||||||
|  |       ? `${duration.minutes} minute${duration.minutes > 1 ? 's' : ''}` | ||||||
|  |       : ''; | ||||||
|  |     const seconds = duration.seconds | ||||||
|  |       ? `${duration.seconds} second${duration.seconds > 1 ? 's' : ''}` | ||||||
|  |       : ''; | ||||||
|  | 
 | ||||||
|  |     const formattedDuration = [ | ||||||
|  |       years, | ||||||
|  |       months, | ||||||
|  |       weeks, | ||||||
|  |       days, | ||||||
|  |       hours, | ||||||
|  |       minutes, | ||||||
|  |       seconds, | ||||||
|  |     ] | ||||||
|  |       .filter(Boolean) | ||||||
|  |       .join(' '); | ||||||
|  | 
 | ||||||
|  |     return formattedDuration || NO_DATA_PLACEHOLDER; | ||||||
|  |   } | ||||||
|  | 
 | ||||||
|  |   // If it's not ISO, return the plain string
 | ||||||
|  |   return retentionPeriod; | ||||||
|  | }; | ||||||
| const RetentionPeriod = ({ | const RetentionPeriod = ({ | ||||||
|   retentionPeriod, |   retentionPeriod, | ||||||
|   onUpdate, |   onUpdate, | ||||||
| @ -67,7 +120,7 @@ const RetentionPeriod = ({ | |||||||
|       <Space data-testid="retention-period-container"> |       <Space data-testid="retention-period-container"> | ||||||
|         <ExtraInfoLabel |         <ExtraInfoLabel | ||||||
|           label={t('label.retention-period')} |           label={t('label.retention-period')} | ||||||
|           value={retentionPeriod ?? NO_DATA_PLACEHOLDER} |           value={formatRetentionPeriod(retentionPeriod) ?? NO_DATA_PLACEHOLDER} | ||||||
|         /> |         /> | ||||||
| 
 | 
 | ||||||
|         {hasPermission && ( |         {hasPermission && ( | ||||||
|  | |||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user
	 dechoma
						dechoma