|
| 1 | +<script> |
| 2 | + import { getAlertErrorFromResponse } from '$lib/common/errors'; |
| 3 | + import { extractJobErrorParts } from '$lib/common/job_utilities'; |
| 4 | + import ExpandableLog from '../common/ExpandableLog.svelte'; |
| 5 | + import Modal from '../common/Modal.svelte'; |
| 6 | + import Paginator from '../common/Paginator.svelte'; |
| 7 | +
|
| 8 | + let loading = false; |
| 9 | + let page = 1; |
| 10 | + let pageSize = 10; |
| 11 | + let totalCount = 0; |
| 12 | +
|
| 13 | + /** @type {number} */ |
| 14 | + let projectId; |
| 15 | + /** @type {number} */ |
| 16 | + let historyRunId; |
| 17 | + /** @type {number} */ |
| 18 | + let datasetId; |
| 19 | + /** @type {number} */ |
| 20 | + let workflowTaskId; |
| 21 | + /** @type {number} */ |
| 22 | + let historyRunIndex; |
| 23 | +
|
| 24 | + /** @type {Modal} */ |
| 25 | + let modal; |
| 26 | +
|
| 27 | + /** @type {(import('fractal-components/types/api').Pagination<import('fractal-components/types/api').HistoryUnit>)|undefined} */ |
| 28 | + let data = undefined; |
| 29 | +
|
| 30 | + let loadingLogs = false; |
| 31 | + /** @type {number|undefined} */ |
| 32 | + let selectedLogUnit = undefined; |
| 33 | + /** @type {Array<{text: string, highlight: boolean}>} */ |
| 34 | + let logParts = []; |
| 35 | + let loadedLogsStatus = ''; |
| 36 | +
|
| 37 | + /** |
| 38 | + * @param {number} _projectId |
| 39 | + * @param {number} _historyRunId |
| 40 | + * @param {number} _datasetId |
| 41 | + * @param {number} _workflowTaskId |
| 42 | + * @param {number} _historyRunIndex |
| 43 | + */ |
| 44 | + export async function open( |
| 45 | + _projectId, |
| 46 | + _historyRunId, |
| 47 | + _datasetId, |
| 48 | + _workflowTaskId, |
| 49 | + _historyRunIndex |
| 50 | + ) { |
| 51 | + loading = true; |
| 52 | + projectId = _projectId; |
| 53 | + historyRunId = _historyRunId; |
| 54 | + datasetId = _datasetId; |
| 55 | + workflowTaskId = _workflowTaskId; |
| 56 | + historyRunIndex = _historyRunIndex; |
| 57 | + data = undefined; |
| 58 | + page = 1; |
| 59 | + pageSize = 10; |
| 60 | + modal.show(); |
| 61 | + await loadRun(page, pageSize); |
| 62 | + } |
| 63 | +
|
| 64 | + function onClose() { |
| 65 | + data = undefined; |
| 66 | + } |
| 67 | +
|
| 68 | + /** |
| 69 | + * @param {number} currentPage |
| 70 | + * @param {number} selectedPageSize |
| 71 | + */ |
| 72 | + async function loadRun(currentPage, selectedPageSize) { |
| 73 | + loading = true; |
| 74 | + const url = `/api/v2/project/${projectId}/status/run/${historyRunId}/units?workflowtask_id=${workflowTaskId}&dataset_id=${datasetId}&page=${currentPage}&page_size=${selectedPageSize}`; |
| 75 | + const response = await fetch(url); |
| 76 | + if (!response.ok) { |
| 77 | + loading = false; |
| 78 | + modal.displayErrorAlert(await getAlertErrorFromResponse(response)); |
| 79 | + return; |
| 80 | + } |
| 81 | + data = await response.json(); |
| 82 | + if (data) { |
| 83 | + page = data.current_page; |
| 84 | + pageSize = data.page_size; |
| 85 | + totalCount = data.total_count; |
| 86 | + } |
| 87 | + loading = false; |
| 88 | + } |
| 89 | +
|
| 90 | + /** |
| 91 | + * @param {number} unitId |
| 92 | + * @param {string} status |
| 93 | + */ |
| 94 | + async function loadLogs(unitId, status) { |
| 95 | + loadingLogs = true; |
| 96 | + const response = await fetch( |
| 97 | + `/api/v2/project/${projectId}/status/unit-log?history_run_id=${historyRunId}&history_unit_id=${unitId}&workflowtask_id=${workflowTaskId}&dataset_id=${datasetId}`, |
| 98 | + { |
| 99 | + method: 'GET' |
| 100 | + } |
| 101 | + ); |
| 102 | + if (!response.ok) { |
| 103 | + modal.displayErrorAlert(await getAlertErrorFromResponse(response)); |
| 104 | + loadingLogs = false; |
| 105 | + return; |
| 106 | + } |
| 107 | + const log = await response.json(); |
| 108 | + loadedLogsStatus = status; |
| 109 | + if (status === 'failed') { |
| 110 | + logParts = extractJobErrorParts(log, false, true); |
| 111 | + } else { |
| 112 | + logParts = [{ text: log, highlight: false }]; |
| 113 | + } |
| 114 | + selectedLogUnit = unitId; |
| 115 | + loadingLogs = false; |
| 116 | + } |
| 117 | +</script> |
| 118 | + |
| 119 | +<Modal id="runStatusModal" bind:this={modal} fullscreen={true} bodyCss="p-0" {onClose}> |
| 120 | + <svelte:fragment slot="header"> |
| 121 | + <h1 class="modal-title fs-5"> |
| 122 | + {#if selectedLogUnit && !loadingLogs} |
| 123 | + Run {historyRunIndex} - Logs for unit #{selectedLogUnit} |
| 124 | + {:else} |
| 125 | + Run {historyRunIndex} |
| 126 | + {/if} |
| 127 | + </h1> |
| 128 | + </svelte:fragment> |
| 129 | + <svelte:fragment slot="body"> |
| 130 | + <div id="errorAlert-runStatusModal" class="mb-2" /> |
| 131 | + {#if loading && !data} |
| 132 | + <span class="spinner-border spinner-border-sm" role="status" aria-hidden="true" /> |
| 133 | + {:else if selectedLogUnit && !loadingLogs} |
| 134 | + <div class="row"> |
| 135 | + <div class="col"> |
| 136 | + <ExpandableLog bind:logParts highlight={loadedLogsStatus === 'failed'} /> |
| 137 | + </div> |
| 138 | + </div> |
| 139 | + <div class="row"> |
| 140 | + <div class="col"> |
| 141 | + <button class="m-2 ms-3 btn btn-primary" on:click={() => (selectedLogUnit = undefined)}> |
| 142 | + Back |
| 143 | + </button> |
| 144 | + </div> |
| 145 | + </div> |
| 146 | + {:else if data} |
| 147 | + {#if data.items.length > 0} |
| 148 | + <table class="table table-striped"> |
| 149 | + <thead> |
| 150 | + <tr> |
| 151 | + <th>Unit id</th> |
| 152 | + <th>Status</th> |
| 153 | + <th /> |
| 154 | + </tr> |
| 155 | + </thead> |
| 156 | + <tbody> |
| 157 | + {#each data.items as run} |
| 158 | + <tr> |
| 159 | + <td>{run.id}</td> |
| 160 | + <td>{run.status || '-'}</td> |
| 161 | + <td> |
| 162 | + <button class="btn btn-light" on:click={() => loadLogs(run.id, run.status)}> |
| 163 | + {#if loadingLogs} |
| 164 | + <span |
| 165 | + class="spinner-border spinner-border-sm" |
| 166 | + role="status" |
| 167 | + aria-hidden="true" |
| 168 | + /> |
| 169 | + {/if} |
| 170 | + <i class="bi-list-columns-reverse" /> Logs |
| 171 | + </button> |
| 172 | + </td> |
| 173 | + </tr> |
| 174 | + {/each} |
| 175 | + </tbody> |
| 176 | + </table> |
| 177 | + <Paginator |
| 178 | + currentPage={page} |
| 179 | + {pageSize} |
| 180 | + {totalCount} |
| 181 | + onPageChange={(currentPage, pageSize) => loadRun(currentPage, pageSize)} |
| 182 | + /> |
| 183 | + {:else} |
| 184 | + <p class="ps-3">No images</p> |
| 185 | + {/if} |
| 186 | + {/if} |
| 187 | + </svelte:fragment> |
| 188 | +</Modal> |
0 commit comments