All files / components/admin CronJobStatusPanel.tsx

70.37% Statements 19/27
76.66% Branches 23/30
100% Functions 5/5
80% Lines 16/20

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160                                                  2x               8x 7x 7x   7x 7x 7x                   8x 7x 7x 7x               113x   113x                                           8x                               8x   8x                                                                                                                  
import {type ReactElement} from 'react';
import {Alert, Box, Chip, CircularProgress, Divider, Paper, Tooltip, Typography} from '@mui/material';
import {
	CheckCircle as HealthyIcon,
	HelpOutline as NeverRunIcon,
	Warning as WarningIcon,
	Error as StaleIcon
} from '@mui/icons-material';
 
import {
	type CronJobInfo,
	type CronJobStatus,
	useGetCronJobStatusesQuery
} from '../../services/cronJobsApi';
 
// ============================================================================
// Display helpers
// ============================================================================
 
interface StatusDisplay {
	label: string;
	color: 'success' | 'warning' | 'error' | 'default';
	Icon: typeof HealthyIcon;
}
 
const STATUS_DISPLAY: Record<CronJobStatus, StatusDisplay> = {
	healthy: {label: 'Healthy', color: 'success', Icon: HealthyIcon},
	warning: {label: 'Stale soon', color: 'warning', Icon: WarningIcon},
	stale: {label: 'Stale', color: 'error', Icon: StaleIcon},
	never_run: {label: 'Never run', color: 'default', Icon: NeverRunIcon}
};
 
function formatRelativeTime(iso: string | null): string {
	if (iso === null) return 'never';
	const then = new Date(iso).getTime();
	Iif (Number.isNaN(then)) return 'unknown';
 
	const minutes = Math.round((Date.now() - then) / 60000);
	Iif (minutes < 1) return 'just now';
	Eif (minutes < 60) return `${minutes}m ago`;
 
	const hours = Math.round(minutes / 60);
	if (hours < 24) return `${hours}h ago`;
 
	const days = Math.round(hours / 24);
	return `${days}d ago`;
}
 
function formatAbsoluteTime(iso: string | null): string {
	if (iso === null) return '—';
	const d = new Date(iso);
	Iif (Number.isNaN(d.getTime())) return iso;
	return d.toLocaleString();
}
 
// ============================================================================
// Component
// ============================================================================
 
export default function CronJobStatusPanel(): ReactElement {
	const {data: jobs, isLoading, error} = useGetCronJobStatusesQuery();
 
	return (
		<Paper sx={{p: 3}}>
			<Box sx={{mb: 2}}>
				<Typography variant="h6" fontWeight="bold">
					Scheduled Jobs
				</Typography>
				<Typography variant="body2" color="text.secondary">
					Last run state for the three nightly / monthly cron commands.
				</Typography>
			</Box>
 
			{isLoading && (
				<Box sx={{display: 'flex', justifyContent: 'center', py: 3}}>
					<CircularProgress size={24}/>
				</Box>
			)}
 
			{error !== undefined && (
				<Alert severity="error">Failed to load scheduled job status.</Alert>
			)}
 
			{jobs?.map((job, i) => (
				<JobRow key={job.jobName} job={job} showDivider={i > 0}/>
			))}
		</Paper>
	);
}
 
// ============================================================================
// Subcomponent
// ============================================================================
 
interface JobRowProps {
	job: CronJobInfo;
	showDivider: boolean;
}
 
function JobRow({job, showDivider}: JobRowProps): ReactElement {
	const display = STATUS_DISPLAY[job.status];
 
	return (
		<Box>
			{showDivider && <Divider sx={{my: 2}}/>}
			<Box
				sx={{
					display: 'grid',
					gridTemplateColumns: {xs: '1fr', sm: 'minmax(0, 2fr) minmax(0, 1fr) minmax(0, 1.5fr) minmax(0, 1fr)'},
					gap: 2,
					alignItems: 'center'
				}}
			>
				<Box>
					<Typography variant="body1" fontWeight="medium">
						{job.displayName}
					</Typography>
					<Typography variant="caption" color="text.secondary">
						{job.expectedFrequency === 'daily' ? 'Runs nightly' : 'Runs on the 1st of each month'}
					</Typography>
				</Box>
 
				<Box>
					<Tooltip title={formatAbsoluteTime(job.lastRunAt)} arrow>
						<Typography variant="body2">
							{formatRelativeTime(job.lastRunAt)}
						</Typography>
					</Tooltip>
					{job.lastRunFor !== null && (
						<Typography variant="caption" color="text.secondary">
							for {job.lastRunFor}
						</Typography>
					)}
				</Box>
 
				<Box>
					<Typography variant="body2">
						{job.accountsAffected} account{job.accountsAffected === 1 ? '' : 's'}
					</Typography>
					{job.totalAmount !== null && (
						<Typography variant="caption" color="text.secondary">
							${job.totalAmount}
						</Typography>
					)}
				</Box>
 
				<Box sx={{justifySelf: {xs: 'start', sm: 'end'}}}>
					<Chip
						icon={<display.Icon sx={{fontSize: 18}}/>}
						label={display.label}
						color={display.color}
						size="small"
						variant={job.status === 'healthy' ? 'filled' : 'outlined'}
					/>
				</Box>
			</Box>
		</Box>
	);
}