All files / features / runs RunsPage.tsx

45.71% Statements 32/70
31.25% Branches 20/64
45.45% Functions 10/22
50.79% Lines 32/63

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 161 162 163 164 165 166 167 168 169            67x                 14x 14x                                                                                                                                                                                     14x               616x                   616x                           616x                           616x                   616x          
import { Outlet } from 'react-router-dom'
import { Page, DataTable, Crud } from '@/layers/L2R'
import type { PageConfig, ListConfig, CrudConfig } from '@/layers/interfaces'
import { RunPhaseIcon } from '@/shared/components/RunPhaseIcon'
import { SourceBadge } from '@/shared/components/SourceBadge'
import { DurationDisplay } from '@/shared/components/DurationDisplay'
import { formatRelativeTime, abbreviateNumber } from '@/shared/utils/format'
import type { FilterDef } from '@/layers/L2R/surfaces/FilterBar'

function presetToIso(preset: string): string | undefined {
  const now = Date.now()
  const ms =
    preset === '1h' ? 60 * 60 * 1000 :
    preset === '24h' ? 24 * 60 * 60 * 1000 :
    preset === '7d' ? 7 * 24 * 60 * 60 * 1000 :
    preset === '30d' ? 30 * 24 * 60 * 60 * 1000 :
    undefined
  if (ms === undefined) return undefined
  return new Date(now - ms).toISOString()
}
 
export function RunsPage() {
  const pageConfig: PageConfig = { title: 'Runs' }
 
  const filterDefs: FilterDef[] = [
    {
      key: 'phase',
      label: 'Phase',
      type: 'multi-select',
      options: [
        { value: 'pending', label: 'Pending' },
        { value: 'running', label: 'Running' },
        { value: 'succeeded', label: 'Succeeded' },
        { value: 'warning', label: 'Warning' },
        { value: 'failed', label: 'Failed' },
      ],
    },
    {
      key: 'project_id',
      label: 'Project',
      type: 'multi-select',
      entitySource: { entityPath: 'projects' },
    },
    {
      key: 'source_type',
      label: 'Source Type',
      type: 'multi-select',
      options: [
        { value: 'manual', label: 'Manual' },
        { value: 'api', label: 'API' },
        { value: 'schedule', label: 'Schedule' },
        { value: 'vikunja', label: 'Vikunja' },
        { value: 'gitea', label: 'Gitea' },
        { value: 'github', label: 'GitHub' },
      ],
    },
    {
      key: 'source',
      label: 'Source',
      type: 'multi-select',
      options: [
        { value: 'vikunja', label: 'Vikunja' },
        { value: 'gitea', label: 'Gitea' },
        { value: 'github', label: 'GitHub' },
      ],
    },
    {
      key: 'created_after',
      label: 'Created After',
      type: 'date-range',
      toApiValue: presetToIso,
    },
  ]
 
  const listConfig: ListConfig = {
    entityPath: 'runs',
    prefix: 'runs',
    columns: [
      {
        field: 'phase',
        label: 'Phase',
        sortable: true,
        render: (row) => <RunPhaseIcon phase={(row.phase ?? 'pending') as 'pending' | 'running' | 'succeeded' | 'failed'} />,
      },
      {
        field: 'project_name',
        label: 'Project',
        sortable: true,
        render: (row) => (
          <a href={`#/projects/${row.project_id}`} className="text-primary hover:underline">
            {row.project_name as string}
          </a>
        ),
      },
      {
        field: 'pipeline_name',
        label: 'Pipeline',
        sortable: true,
        render: (row) => (
          <a href={`#/pipelines/${row.pipeline_id}`} className="text-primary hover:underline">
            {row.pipeline_name as string}
          </a>
        ),
      },
      {
        field: 'source_type',
        label: 'Source',
        sortable: true,
        render: (row) => <SourceBadge source={(row.source_type ?? 'manual') as 'manual' | 'api' | 'schedule' | 'vikunja' | 'gitea' | 'github'} />,
      },
      {
        field: 'created_at',
        label: 'Created',
        sortable: true,
        render: (row) => formatRelativeTime(row.created_at as string | null | undefined),
      },
      {
        field: 'tokens',
        label: 'Tokens',
        sortable: true,
        render: (row) => {
          const input = row.token_input as number | undefined
          const output = row.token_output as number | undefined
          const total = (input ?? 0) + (output ?? 0)
          return total ? abbreviateNumber(total) : '\u2014'
        },
      },
      {
        field: 'duration',
        label: 'Duration',
        sortable: true,
        render: (row) => {
          const started = row.started_at as string | undefined
          const completed = row.completed_at as string | undefined
          const phase = row.phase as string | undefined
          const ms = started && completed ? new Date(completed).getTime() - new Date(started).getTime() : null
          if (phase === 'running') {
            return <span className="text-sm text-blue-500" data-testid="duration-running">running</span>
          }
          return <DurationDisplay ms={ms} />
        },
      },
    ],
    sort: { field: 'created_at', order: 'desc' },
    sse: { enabled: true },
    createTarget: '/runs/add',
    createLabel: 'Trigger run',
    rowTarget: '/runs/:id',
  }
 
  const crudConfig: CrudConfig = {
    entityPath: 'runs',
    fieldSet: [
      { field: 'pipeline_id', title: 'Pipeline' },
      { field: 'source_type', title: 'Source Type' },
    ],
    successTarget: '/runs/:id',
    cancelTarget: '/runs',
  }
 
  return (
    <Page config={pageConfig}>
      <DataTable config={listConfig} filterDefs={filterDefs} />
      <Crud config={crudConfig} />
      <Outlet />
    </Page>
  )
}