|
| 1 | +import { |
| 2 | + InfoCard, |
| 3 | + Progress, |
| 4 | + Table, |
| 5 | + type TableColumn, |
| 6 | + StatusOK, |
| 7 | + StatusError, |
| 8 | + Link, |
| 9 | +} from '@backstage/core-components'; |
| 10 | +import Alert from '@material-ui/lab/Alert'; |
| 11 | +import { useEntity } from '@backstage/plugin-catalog-react'; |
| 12 | +import { useTemplateTaskRuns } from '../../../hooks/useTemplateTaskRuns'; |
| 13 | +import { DateTime } from 'luxon'; |
| 14 | +import { useApi, configApiRef } from '@backstage/core-plugin-api'; |
| 15 | +import { TaskRun } from '../../../types'; |
| 16 | + |
| 17 | +export const TemplateTaskRunsCard = () => { |
| 18 | + const { entity } = useEntity(); |
| 19 | + const templateName = entity.metadata.name; |
| 20 | + const { taskRuns, loading, error } = useTemplateTaskRuns(templateName); |
| 21 | + const config = useApi(configApiRef); |
| 22 | + const backendUrl = config.getOptionalString('app.baseUrl'); |
| 23 | + if (!backendUrl) { |
| 24 | + throw new Error("app.baseUrl is not configured in Backstage config."); |
| 25 | + } |
| 26 | + if (loading) return <Progress />; |
| 27 | + if (error) { |
| 28 | + return ( |
| 29 | + <InfoCard title="Template Task Runs"> |
| 30 | + <Alert severity="info">No task runs found for this template.</Alert> |
| 31 | + </InfoCard> |
| 32 | + ); |
| 33 | + } |
| 34 | + if (!taskRuns || taskRuns.length === 0) { |
| 35 | + return ( |
| 36 | + <InfoCard title="Template Task Runs"> |
| 37 | + <Alert severity="info">No task runs found for this template.</Alert> |
| 38 | + </InfoCard> |
| 39 | + ); |
| 40 | + } |
| 41 | + const columns: TableColumn<TaskRun>[] = [ |
| 42 | + { |
| 43 | + title: 'ID', |
| 44 | + field: 'id', |
| 45 | + render: (row: TaskRun) => { |
| 46 | + const taskUrl = `${backendUrl}/create/tasks/${row.id}`; |
| 47 | + return <Link to={taskUrl}>{row.id}</Link>; |
| 48 | + }, |
| 49 | + }, |
| 50 | + { |
| 51 | + title: 'Status', |
| 52 | + field: 'status', |
| 53 | + render: (row: TaskRun) => { |
| 54 | + if (row.status === 'completed') { |
| 55 | + return ( |
| 56 | + <div style={{ display: 'flex', alignItems: 'center', gap: '8px' }}> |
| 57 | + <StatusOK /> |
| 58 | + <span>completed</span> |
| 59 | + </div> |
| 60 | + ); |
| 61 | + } |
| 62 | + if (row.status === 'failed') { |
| 63 | + return ( |
| 64 | + <div style={{ display: 'flex', alignItems: 'center', gap: '8px' }}> |
| 65 | + <StatusError /> |
| 66 | + <span>failed</span> |
| 67 | + </div> |
| 68 | + ); |
| 69 | + } |
| 70 | + return row.status; |
| 71 | + }, |
| 72 | + }, |
| 73 | + { |
| 74 | + title: 'Created At', |
| 75 | + field: 'created_at', |
| 76 | + render: (row: TaskRun) => |
| 77 | + DateTime.fromISO(row.created_at).toLocaleString( |
| 78 | + DateTime.DATETIME_SHORT, |
| 79 | + ), |
| 80 | + }, |
| 81 | + { |
| 82 | + title: 'Duration', |
| 83 | + field: 'duration', |
| 84 | + render: (row: TaskRun) => { |
| 85 | + if (row.status === 'failed') { |
| 86 | + return <StatusError />; |
| 87 | + } |
| 88 | + |
| 89 | + if (!row.created_at || !row.last_heartbeat_at) { |
| 90 | + return 'N/A'; |
| 91 | + } |
| 92 | + |
| 93 | + const start = new Date(row.created_at).getTime(); |
| 94 | + const end = new Date(row.last_heartbeat_at).getTime(); |
| 95 | + const durationMs = end - start; |
| 96 | + |
| 97 | + if (durationMs <= 0) { |
| 98 | + return 'N/A'; |
| 99 | + } |
| 100 | + |
| 101 | + const minutes = Math.floor(durationMs / (1000 * 60)); |
| 102 | + const seconds = Math.floor((durationMs % (1000 * 60)) / 1000); |
| 103 | + |
| 104 | + if (minutes > 0) { |
| 105 | + return `${minutes}m ${seconds}s`; |
| 106 | + } |
| 107 | + return `${seconds}s`; |
| 108 | + }, |
| 109 | + }, |
| 110 | + { |
| 111 | + title: 'Created By', |
| 112 | + field: 'created_by', |
| 113 | + render: (row: TaskRun) => { |
| 114 | + if (!row.created_by) return 'N/A'; |
| 115 | + |
| 116 | + // Parse user:development/guest format |
| 117 | + const userMatch = row.created_by.match(/^user:(.+)\/(.+)$/); |
| 118 | + if (userMatch) { |
| 119 | + const [, namespace, username] = userMatch; |
| 120 | + const userUrl = `${backendUrl}/catalog/${namespace}/user/${username}`; |
| 121 | + return ( |
| 122 | + <Link to={userUrl}> |
| 123 | + user:{namespace}/{username} |
| 124 | + </Link> |
| 125 | + ); |
| 126 | + } |
| 127 | + |
| 128 | + // Fallback for other formats |
| 129 | + return row.created_by; |
| 130 | + }, |
| 131 | + }, |
| 132 | + ]; |
| 133 | + return ( |
| 134 | + <InfoCard title="Task Runs"> |
| 135 | + <Table |
| 136 | + options={{ paging: true, search: true }} |
| 137 | + columns={columns} |
| 138 | + data={taskRuns} |
| 139 | + /> |
| 140 | + </InfoCard> |
| 141 | + ); |
| 142 | +}; |
0 commit comments