Skip to content

Commit 51dbe87

Browse files
authored
Chore/better cron status (supabase#30818)
* Better cron statuses * Adjust time display * Make status badge a proper component
1 parent 3a30f45 commit 51dbe87

File tree

3 files changed

+51
-12
lines changed

3 files changed

+51
-12
lines changed

apps/studio/components/interfaces/Integrations/CronJobs/CronJobs.utils.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,12 @@ export function calculateDuration(start: string, end: string): string {
117117
const startTime = new Date(start).getTime()
118118
const endTime = new Date(end).getTime()
119119
const duration = endTime - startTime
120-
return isNaN(duration) ? 'Invalid Date' : `${duration} ms`
120+
121+
if (isNaN(duration)) return 'Invalid Date'
122+
123+
if (duration < 1000) return `${duration}ms`
124+
if (duration < 60000) return `${(duration / 1000).toFixed(1)}s`
125+
return `${(duration / 60000).toFixed(1)}m`
121126
}
122127

123128
export function formatDate(dateString: string): string {

apps/studio/components/interfaces/Integrations/CronJobs/PreviousRunsTab.tsx

Lines changed: 40 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { toString as CronToString } from 'cronstrue'
2-
import { List } from 'lucide-react'
2+
import { CircleCheck, CircleX, List, Loader } from 'lucide-react'
33
import Link from 'next/link'
44
import { UIEvent, useCallback, useEffect, useMemo } from 'react'
55
import DataGrid, { Column, Row } from 'react-data-grid'
@@ -12,7 +12,6 @@ import {
1212
useCronJobRunsInfiniteQuery,
1313
} from 'data/database-cron-jobs/database-cron-jobs-runs-infinite-query'
1414
import {
15-
Badge,
1615
Button,
1716
cn,
1817
LoadingLine,
@@ -66,9 +65,7 @@ const cronJobColumns = [
6665
id: 'status',
6766
name: 'Status',
6867
minWidth: 75,
69-
value: (row: CronJobRun) => (
70-
<Badge variant={row.status === 'succeeded' ? 'success' : 'warning'}>{row.status}</Badge>
71-
),
68+
value: (row: CronJobRun) => <StatusBadge status={row.status} />,
7269
},
7370
{
7471
id: 'start_time',
@@ -80,15 +77,19 @@ const cronJobColumns = [
8077
id: 'end_time',
8178
name: 'End Time',
8279
minWidth: 120,
83-
value: (row: CronJobRun) => <div className="text-xs">{formatDate(row.end_time)}</div>,
80+
value: (row: CronJobRun) => (
81+
<div className="text-xs">{row.status === 'succeeded' ? formatDate(row.end_time) : '-'}</div>
82+
),
8483
},
8584

8685
{
8786
id: 'duration',
8887
name: 'Duration',
8988
minWidth: 100,
9089
value: (row: CronJobRun) => (
91-
<div className="text-xs">{calculateDuration(row.start_time, row.end_time)}</div>
90+
<span className="text-xs">
91+
{row.status === 'succeeded' ? calculateDuration(row.start_time, row.end_time) : ''}
92+
</span>
9293
),
9394
},
9495
]
@@ -291,3 +292,35 @@ export const PreviousRunsTab = () => {
291292
</div>
292293
)
293294
}
295+
296+
interface StatusBadgeProps {
297+
status: string
298+
}
299+
300+
function StatusBadge({ status }: StatusBadgeProps) {
301+
if (status === 'succeeded') {
302+
return (
303+
<span className="text-brand-600 flex items-center gap-1">
304+
<CircleCheck size={14} /> Succeeded
305+
</span>
306+
)
307+
}
308+
309+
if (status === 'failed') {
310+
return (
311+
<span className="text-destructive flex items-center gap-1">
312+
<CircleX size={14} /> Failed
313+
</span>
314+
)
315+
}
316+
317+
if (['running', 'starting', 'sending', 'connecting'].includes(status)) {
318+
return (
319+
<span className="text-_secondary flex items-center gap-1">
320+
<Loader size={14} className="animate-spin" /> Running
321+
</span>
322+
)
323+
}
324+
325+
return null
326+
}

apps/studio/data/database-cron-jobs/database-cron-jobs-runs-infinite-query.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ export type CronJobRun = {
1818
database: string
1919
username: string
2020
command: string
21-
status: 'succeeded' | 'failed'
21+
// statuses https://github.com/citusdata/pg_cron/blob/f5d111117ddc0f4d83a1bad34d61b857681b6720/include/job_metadata.h#L20
22+
status: 'starting' | 'running' | 'sending' | 'connecting' | 'succeeded' | 'failed'
2223
return_message: string
2324
start_time: string
2425
end_time: string
@@ -35,9 +36,9 @@ export async function getDatabaseCronJobRuns({
3536
if (!projectRef) throw new Error('Project ref is required')
3637

3738
let query = `
38-
SELECT * FROM cron.job_run_details
39-
WHERE
40-
jobid = '${jobId}'
39+
SELECT * FROM cron.job_run_details
40+
WHERE
41+
jobid = '${jobId}'
4142
${afterTimestamp ? `AND start_time < '${afterTimestamp}'` : ''}
4243
ORDER BY start_time DESC
4344
LIMIT ${CRON_JOB_RUNS_PAGE_SIZE}`

0 commit comments

Comments
 (0)