Line data Source code
1 : import type { WorkspaceDeployment } from '@/platform/workspace/api/workspaceApi'
2 : import { deploymentStatusTone } from '@/platform/workspace/composables/useDeploymentLabels'
3 :
4 : /** Why the server would not start a deployment. */
5 : export type DeploymentStartRefusal =
6 : | 'noCredits'
7 : | 'tooManyRunning'
8 : | 'unavailable'
9 : | 'failed'
10 :
11 : /**
12 : * A Start this page asked for, until the listing after it lands, or the
13 : * server's refusal of it.
14 : */
15 : export type DeploymentStart =
16 : | { phase: 'requested' }
17 : | { phase: 'refused'; reason: DeploymentStartRefusal; message: string | null }
18 :
19 : /** The Starts this page asked for, by deployment id. */
20 : export type DeploymentStarts = ReadonlyMap<string, DeploymentStart>
21 :
22 : export type DeploymentStartEvent =
23 : | { type: 'requested'; deploymentId: string }
24 : | { type: 'taken'; deploymentId: string }
25 : | {
26 : type: 'refused'
27 : deploymentId: string
28 : reason: DeploymentStartRefusal
29 : message: string | null
30 : }
31 : | { type: 'listed'; deployments: readonly WorkspaceDeployment[] }
32 :
33 : /** What choosing a deployment's row does. */
34 : export type DeploymentRowAction = 'pick' | 'start' | 'retry' | 'open' | 'none'
35 :
36 0 : function withoutStart(
37 : starts: DeploymentStarts,
38 : deploymentId: string
39 : ): DeploymentStarts {
40 0 : const next = new Map(starts)
41 0 : next.delete(deploymentId)
42 0 : return next
43 : }
44 :
45 0 : function lastsThrough(
46 : start: DeploymentStart,
47 : deployment: WorkspaceDeployment
48 : ): boolean {
49 0 : if (start.phase === 'requested') return true
50 0 : return deployment.can_start
51 : }
52 :
53 9 : function listed(
54 : starts: DeploymentStarts,
55 : deployments: readonly WorkspaceDeployment[]
56 : ): DeploymentStarts {
57 9 : const next = new Map<string, DeploymentStart>()
58 9 : for (const [deploymentId, start] of starts) {
59 0 : const deployment = deployments.find((d) => d.deployment_id === deploymentId)
60 0 : if (deployment && lastsThrough(start, deployment)) {
61 0 : next.set(deploymentId, start)
62 : }
63 : }
64 9 : return next
65 : }
66 :
67 9 : export function reduceDeploymentStarts(
68 : starts: DeploymentStarts,
69 : event: DeploymentStartEvent
70 : ): DeploymentStarts {
71 9 : switch (event.type) {
72 : case 'requested':
73 0 : if (starts.get(event.deploymentId)?.phase === 'requested') return starts
74 0 : return new Map(starts).set(event.deploymentId, { phase: 'requested' })
75 : case 'taken':
76 0 : if (starts.get(event.deploymentId)?.phase !== 'requested') return starts
77 0 : return withoutStart(starts, event.deploymentId)
78 : case 'refused':
79 0 : if (starts.get(event.deploymentId)?.phase !== 'requested') return starts
80 0 : return new Map(starts).set(event.deploymentId, {
81 : phase: 'refused',
82 : reason: event.reason,
83 : message: event.message
84 : })
85 : case 'listed':
86 9 : return listed(starts, event.deployments)
87 : }
88 : }
89 :
90 : /**
91 : * Picks a ready row, starts one the listing says can start (or opens it on the
92 : * Developer Platform while Start is not offered), and retries a refused Start.
93 : */
94 0 : export function deploymentRowAction(
95 : deployment: WorkspaceDeployment,
96 : start: DeploymentStart | undefined,
97 : { startOffered }: { startOffered: boolean }
98 : ): DeploymentRowAction {
99 0 : if (start?.phase === 'requested') return 'none'
100 0 : if (start?.phase === 'refused') {
101 0 : return start.reason === 'unavailable' || !startOffered ? 'open' : 'retry'
102 : }
103 0 : if (deployment.status === 'ready') return 'pick'
104 0 : if (!deployment.can_start) return 'none'
105 0 : return startOffered ? 'start' : 'open'
106 : }
107 :
108 : /** Why a row cannot be picked, when the row itself does not already say. */
109 : export type DeploymentRowReason =
110 : | 'stopped'
111 : | 'failed'
112 : | 'starting'
113 : | 'startElsewhere'
114 : | 'notReady'
115 :
116 0 : export function deploymentRowReason(
117 : deployment: WorkspaceDeployment,
118 : action: DeploymentRowAction,
119 : start: DeploymentStart | undefined
120 : ): DeploymentRowReason | null {
121 0 : if (start?.phase === 'refused' || action === 'pick') return null
122 0 : if (start || deploymentStatusTone(deployment.status) === 'pending') {
123 0 : return 'starting'
124 : }
125 0 : if (action === 'start') {
126 0 : return deployment.status === 'failed' ? 'failed' : 'stopped'
127 : }
128 0 : return action === 'open' ? 'startElsewhere' : 'notReady'
129 : }
|