|
| 1 | +/* |
| 2 | + * This Source Code Form is subject to the terms of the Mozilla Public |
| 3 | + * License, v. 2.0. If a copy of the MPL was not distributed with this |
| 4 | + * file, you can obtain one at https://mozilla.org/MPL/2.0/. |
| 5 | + * |
| 6 | + * Copyright Oxide Computer Company |
| 7 | + */ |
| 8 | + |
| 9 | +import { useMemo } from 'react' |
| 10 | +import * as R from 'remeda' |
| 11 | + |
| 12 | +import { |
| 13 | + Images24Icon, |
| 14 | + SoftwareUpdate16Icon, |
| 15 | + SoftwareUpdate24Icon, |
| 16 | + Time16Icon, |
| 17 | +} from '@oxide/design-system/icons/react' |
| 18 | +import { Badge } from '@oxide/design-system/ui' |
| 19 | + |
| 20 | +import { |
| 21 | + apiq, |
| 22 | + queryClient, |
| 23 | + useApiMutation, |
| 24 | + usePrefetchedQuery, |
| 25 | + type UpdateStatus, |
| 26 | +} from '~/api' |
| 27 | +import { DocsPopover } from '~/components/DocsPopover' |
| 28 | +import { HL } from '~/components/HL' |
| 29 | +import { MoreActionsMenu } from '~/components/MoreActionsMenu' |
| 30 | +import { RefreshButton } from '~/components/RefreshButton' |
| 31 | +import { makeCrumb } from '~/hooks/use-crumbs' |
| 32 | +import { confirmAction } from '~/stores/confirm-action' |
| 33 | +import { addToast } from '~/stores/toast' |
| 34 | +import { EmptyCell } from '~/table/cells/EmptyCell' |
| 35 | +import { CardBlock } from '~/ui/lib/CardBlock' |
| 36 | +import { DateTime } from '~/ui/lib/DateTime' |
| 37 | +import * as DropdownMenu from '~/ui/lib/DropdownMenu' |
| 38 | +import { PageHeader, PageTitle } from '~/ui/lib/PageHeader' |
| 39 | +import { PropertiesTable } from '~/ui/lib/PropertiesTable' |
| 40 | +import { TipIcon } from '~/ui/lib/TipIcon' |
| 41 | +import { ALL_ISH } from '~/util/consts' |
| 42 | +import { docLinks } from '~/util/links' |
| 43 | +import { percentage, round } from '~/util/math' |
| 44 | + |
| 45 | +export const handle = makeCrumb('System Update') |
| 46 | + |
| 47 | +const statusQuery = apiq('systemUpdateStatus', {}) |
| 48 | +const reposQuery = apiq('systemUpdateRepositoryList', { query: { limit: ALL_ISH } }) |
| 49 | + |
| 50 | +const refreshData = () => |
| 51 | + Promise.all([ |
| 52 | + queryClient.invalidateEndpoint('systemUpdateStatus'), |
| 53 | + queryClient.invalidateEndpoint('systemUpdateRepositoryList'), |
| 54 | + ]) |
| 55 | + |
| 56 | +export async function clientLoader() { |
| 57 | + await Promise.all([ |
| 58 | + queryClient.prefetchQuery(statusQuery), |
| 59 | + queryClient.prefetchQuery(reposQuery), |
| 60 | + ]) |
| 61 | + return null |
| 62 | +} |
| 63 | + |
| 64 | +function calcProgress(status: UpdateStatus) { |
| 65 | + const targetVersion = status.targetRelease?.version |
| 66 | + if (!targetVersion) return null |
| 67 | + |
| 68 | + const total = R.sum(Object.values(status.componentsByReleaseVersion)) |
| 69 | + const current = status.componentsByReleaseVersion[targetVersion] || 0 |
| 70 | + |
| 71 | + if (!total) return null // avoid dividing by zero |
| 72 | + |
| 73 | + return { |
| 74 | + current, |
| 75 | + total, |
| 76 | + // trunc prevents, e.g., 999/1000 being reported as 100% |
| 77 | + percentage: round(percentage(current, total), 0, 'trunc'), |
| 78 | + } |
| 79 | +} |
| 80 | + |
| 81 | +export default function UpdatePage() { |
| 82 | + const { data: status } = usePrefetchedQuery(statusQuery) |
| 83 | + const { data: repos } = usePrefetchedQuery(reposQuery) |
| 84 | + |
| 85 | + const { mutateAsync: setTargetRelease } = useApiMutation('targetReleaseUpdate', { |
| 86 | + onSuccess() { |
| 87 | + refreshData() |
| 88 | + addToast({ content: 'Target release updated' }) |
| 89 | + }, |
| 90 | + // error handled by confirm modal |
| 91 | + }) |
| 92 | + |
| 93 | + const componentProgress = useMemo(() => calcProgress(status), [status]) |
| 94 | + |
| 95 | + return ( |
| 96 | + <> |
| 97 | + <PageHeader> |
| 98 | + <PageTitle icon={<SoftwareUpdate24Icon />}>System Update</PageTitle> |
| 99 | + <div className="flex items-center gap-2"> |
| 100 | + <RefreshButton onClick={refreshData} /> |
| 101 | + <DocsPopover |
| 102 | + heading="system update" |
| 103 | + icon={<SoftwareUpdate16Icon />} |
| 104 | + summary="The update system automatically updates components to the target release." |
| 105 | + links={[docLinks.systemUpdate]} |
| 106 | + /> |
| 107 | + </div> |
| 108 | + </PageHeader> |
| 109 | + <PropertiesTable className="-mt-8 mb-8"> |
| 110 | + {/* targetRelease will never be null on a customer system after the |
| 111 | + first time it is set. */} |
| 112 | + <PropertiesTable.Row label="Target release"> |
| 113 | + {status.targetRelease?.version ?? <EmptyCell />} |
| 114 | + </PropertiesTable.Row> |
| 115 | + <PropertiesTable.Row label="Target set"> |
| 116 | + {status.targetRelease?.timeRequested ? ( |
| 117 | + <DateTime date={status.targetRelease.timeRequested} /> |
| 118 | + ) : ( |
| 119 | + <EmptyCell /> |
| 120 | + )} |
| 121 | + </PropertiesTable.Row> |
| 122 | + <PropertiesTable.Row |
| 123 | + label={ |
| 124 | + <> |
| 125 | + Progress |
| 126 | + <TipIcon className="ml-1.5"> |
| 127 | + Number of components updated to the target release |
| 128 | + </TipIcon> |
| 129 | + </> |
| 130 | + } |
| 131 | + > |
| 132 | + {componentProgress ? ( |
| 133 | + <> |
| 134 | + <div className="mr-1.5">{componentProgress.percentage}%</div> |
| 135 | + <div className="text-secondary"> |
| 136 | + ({componentProgress.current} of {componentProgress.total}) |
| 137 | + </div> |
| 138 | + </> |
| 139 | + ) : ( |
| 140 | + <EmptyCell /> |
| 141 | + )} |
| 142 | + </PropertiesTable.Row> |
| 143 | + <PropertiesTable.Row |
| 144 | + label={ |
| 145 | + <> |
| 146 | + Last step planned{' '} |
| 147 | + <TipIcon className="ml-1.5"> |
| 148 | + A rough indicator of the last time the update planner did something |
| 149 | + </TipIcon> |
| 150 | + </> |
| 151 | + } |
| 152 | + > |
| 153 | + <DateTime date={status.timeLastStepPlanned} /> |
| 154 | + </PropertiesTable.Row> |
| 155 | + <PropertiesTable.Row |
| 156 | + label={ |
| 157 | + <> |
| 158 | + Suspended{' '} |
| 159 | + <TipIcon className="ml-1.5"> |
| 160 | + Whether automatic update is suspended due to manual update activity |
| 161 | + </TipIcon> |
| 162 | + </> |
| 163 | + } |
| 164 | + > |
| 165 | + {status.suspended ? 'Yes' : 'No'} |
| 166 | + </PropertiesTable.Row> |
| 167 | + </PropertiesTable> |
| 168 | + |
| 169 | + <CardBlock> |
| 170 | + <CardBlock.Header title="Releases" /> |
| 171 | + <CardBlock.Body> |
| 172 | + <ul className="space-y-3"> |
| 173 | + {repos.items.map((repo) => { |
| 174 | + const isTarget = repo.systemVersion === status.targetRelease?.version |
| 175 | + return ( |
| 176 | + <li |
| 177 | + key={repo.hash} |
| 178 | + className="border-secondary flex items-center gap-4 rounded border p-4" |
| 179 | + > |
| 180 | + <Images24Icon className="text-secondary shrink-0" aria-hidden /> |
| 181 | + <div className="flex-1"> |
| 182 | + <div className="flex items-center gap-2"> |
| 183 | + <span className="text-sans-semi-lg text-raise"> |
| 184 | + {repo.systemVersion} |
| 185 | + </span> |
| 186 | + {isTarget && <Badge color="default">Target</Badge>} |
| 187 | + </div> |
| 188 | + <div className="text-secondary">{repo.fileName}</div> |
| 189 | + </div> |
| 190 | + <div className="flex items-center gap-4"> |
| 191 | + <div className="flex items-center gap-1"> |
| 192 | + <Time16Icon aria-hidden /> |
| 193 | + <DateTime date={repo.timeCreated} /> |
| 194 | + </div> |
| 195 | + </div> |
| 196 | + <MoreActionsMenu label={`${repo.systemVersion} actions`} isSmall> |
| 197 | + <DropdownMenu.Item |
| 198 | + label="Set as target release" |
| 199 | + onSelect={() => { |
| 200 | + confirmAction({ |
| 201 | + actionType: 'primary', |
| 202 | + doAction: () => |
| 203 | + setTargetRelease({ |
| 204 | + body: { systemVersion: repo.systemVersion }, |
| 205 | + }), |
| 206 | + modalTitle: 'Confirm set target release', |
| 207 | + modalContent: ( |
| 208 | + <p> |
| 209 | + Are you sure you want to set <HL>{repo.systemVersion}</HL> as |
| 210 | + the target release? |
| 211 | + </p> |
| 212 | + ), |
| 213 | + errorTitle: `Error setting target release to ${repo.systemVersion}`, |
| 214 | + }) |
| 215 | + }} |
| 216 | + // TODO: follow API logic, disabling for older releases. |
| 217 | + // Or maybe just have the API tell us by adding a field to |
| 218 | + // the TufRepo response type. |
| 219 | + disabled={isTarget && 'Already set as target'} |
| 220 | + /> |
| 221 | + </MoreActionsMenu> |
| 222 | + </li> |
| 223 | + ) |
| 224 | + })} |
| 225 | + </ul> |
| 226 | + </CardBlock.Body> |
| 227 | + </CardBlock> |
| 228 | + </> |
| 229 | + ) |
| 230 | +} |
0 commit comments