From 4de68046ce35c54ec12f12cf7942c3bfab7de779 Mon Sep 17 00:00:00 2001 From: Mate Vago Date: Tue, 26 Nov 2024 10:31:20 +0100 Subject: [PATCH] refactor: remove unused ws messages --- .../deployment-container-status-list.tsx | 10 +- .../instances/use-instance-state.ts | 100 ----------- .../deployments/use-deployment-state.tsx | 160 +----------------- web/crux-ui/src/models/deployment.ts | 24 +-- web/crux/src/app/deploy/deploy.message.ts | 18 +- web/crux/src/app/deploy/deploy.ws.gateway.ts | 36 ---- 6 files changed, 10 insertions(+), 338 deletions(-) delete mode 100644 web/crux-ui/src/components/deployments/instances/use-instance-state.ts diff --git a/web/crux-ui/src/components/deployments/deployment-container-status-list.tsx b/web/crux-ui/src/components/deployments/deployment-container-status-list.tsx index 391ce58ca..6121ec5b9 100644 --- a/web/crux-ui/src/components/deployments/deployment-container-status-list.tsx +++ b/web/crux-ui/src/components/deployments/deployment-container-status-list.tsx @@ -32,7 +32,7 @@ interface DeploymentContainerStatusListProps { progress: Record } -type ContainerWithInstance = Container & { +type ContainerWithConfigId = Container & { configId: string } @@ -44,7 +44,7 @@ const DeploymentContainerStatusList = (props: DeploymentContainerStatusListProps const now = utcNow() - const [containers, setContainers] = useState(() => + const [containers, setContainers] = useState(() => deployment.instances.map(it => ({ configId: it.config.id, id: { @@ -73,7 +73,7 @@ const DeploymentContainerStatusList = (props: DeploymentContainerStatusListProps } as WatchContainerStatusMessage), }) - const merge = (weak: ContainerWithInstance[], strong: Container[]): ContainerWithInstance[] => { + const merge = (weak: ContainerWithConfigId[], strong: Container[]): ContainerWithConfigId[] => { if (!strong || strong.length === 0) { return weak } @@ -121,7 +121,7 @@ const DeploymentContainerStatusList = (props: DeploymentContainerStatusListProps /> + body={(it: ContainerWithConfigId) => progress[it.configId]?.progress < 1 ? ( ) : ( @@ -136,7 +136,7 @@ const DeploymentContainerStatusList = (props: DeploymentContainerStatusListProps /> ( + body={(it: ContainerWithConfigId) => ( <> {it.state && (
diff --git a/web/crux-ui/src/components/deployments/instances/use-instance-state.ts b/web/crux-ui/src/components/deployments/instances/use-instance-state.ts deleted file mode 100644 index 8298ad966..000000000 --- a/web/crux-ui/src/components/deployments/instances/use-instance-state.ts +++ /dev/null @@ -1,100 +0,0 @@ -// import { -// ContainerConfigData, -// GetInstanceSecretsMessage, -// ContainerConfigProperty, -// Instance, -// InstanceSecretsMessage, -// mergeConfigs, -// ConcreteContainerConfigData, -// WS_TYPE_GET_INSTANCE_SECRETS, -// WS_TYPE_INSTANCE_SECRETS, -// } from '@app/models' -// import { createContainerConfigSchema, getValidationError } from '@app/validations' -// import { useEffect, useState } from 'react' -// import { DeploymentActions, DeploymentState } from '../use-deployment-state' -// import useTranslation from 'next-translate/useTranslation' - -// export type InstanceStateOptions = { -// deploymentState: DeploymentState -// deploymentActions: DeploymentActions -// instance: Instance -// } - -// export type InstanceState = { -// config: ConcreteContainerConfigData -// resetableConfig: ContainerConfigData -// definedSecrets: string[] -// errorMessage: string -// } - -// export type InstanceActions = { -// resetSection: (section: ContainerConfigProperty) => void -// onPatch: (newConfig: Partial) => void -// onParseError: (error: Error) => void -// } - -// const useInstanceState = (options: InstanceStateOptions) => { -// const { t } = useTranslation('container') - -// const { instance, deploymentState, deploymentActions } = options -// const { sock } = deploymentState - -// const [parseError, setParseError] = useState(null) -// const [definedSecrets, setDefinedSecrets] = useState([]) - -// sock.on(WS_TYPE_INSTANCE_SECRETS, (message: InstanceSecretsMessage) => { -// if (message.instanceId !== instance.id) { -// return -// } - -// setDefinedSecrets(message.keys) -// }) - -// useEffect(() => { -// sock.send(WS_TYPE_GET_INSTANCE_SECRETS, { -// id: instance.id, -// } as GetInstanceSecretsMessage) -// }, [instance.id, sock]) - -// const mergedConfig = mergeConfigs(instance.image.config, instance.config) - -// const errorMessage = -// parseError ?? getValidationError(createContainerConfigSchema(instance.image.labels), mergedConfig, null, t)?.message - -// const resetSection = (section: ContainerConfigProperty): ConcreteContainerConfigData => { -// const newConfig = { ...instance.config } as any -// newConfig[section] = null - -// deploymentActions.updateInstanceConfig(instance.id, newConfig) - -// sock.send(WS_TYPE_PATCH_INSTANCE, { -// instanceId: instance.id, -// resetSection: section, -// } as PatchInstanceMessage) - -// return newConfig -// } - -// const onPatch = (id: string, newConfig: ConcreteContainerConfigData) => { -// deploymentActions.onPatchInstance(id, newConfig) -// setParseError(null) -// } - -// const onParseError = (err: Error) => setParseError(err.message) - -// return [ -// { -// config: mergedConfig, -// resetableConfig: instance.config, -// definedSecrets, -// errorMessage, -// }, -// { -// onPatch, -// resetSection, -// onParseError, -// }, -// ] -// } - -// export default useInstanceState diff --git a/web/crux-ui/src/components/deployments/use-deployment-state.tsx b/web/crux-ui/src/components/deployments/use-deployment-state.tsx index 6d04bb1db..e1e4885cf 100644 --- a/web/crux-ui/src/components/deployments/use-deployment-state.tsx +++ b/web/crux-ui/src/components/deployments/use-deployment-state.tsx @@ -7,9 +7,7 @@ import usePersistedViewMode from '@app/hooks/use-persisted-view-mode' import useTeamRoutes from '@app/hooks/use-team-routes' import useWebSocket from '@app/hooks/use-websocket' import { - ConcreteContainerConfigData, DeploymentDetails, - DeploymentInvalidatedSecrets, deploymentIsCopiable, deploymentIsDeletable, deploymentIsDeployable, @@ -21,14 +19,12 @@ import { ImageDeletedMessage, Instance, instanceCreatedMessageToInstance, - InstanceMessage, InstancesAddedMessage, NodeEventMessage, ProjectDetails, VersionDetails, WebSocketSaveState, WS_TYPE_IMAGE_DELETED, - WS_TYPE_INSTANCE, WS_TYPE_INSTANCES_ADDED, WS_TYPE_NODE_EVENT, } from '@app/models' @@ -68,24 +64,13 @@ export type DeploymentState = { export type DeploymentActions = { setEditState: (state: DeploymentEditState) => void onDeploymentEdited: (editedDeployment: DeploymentDetails) => void - onPatchInstance: (id: string, newConfig: ConcreteContainerConfigData) => void - updateInstanceConfig: (id: string, newConfig: ConcreteContainerConfigData) => void setViewMode: (viewMode: ViewMode) => void - onInvalidateSecrets: (secrets: DeploymentInvalidatedSecrets[]) => void onDeploymentTokenCreated: (token: DeploymentToken) => void onRevokeDeploymentToken: VoidFunction onInstanceSelected: (id: string, deploy: boolean) => void onAllInstancesToggled: (deploy: boolean) => void } -// const mergeInstancePatch = (instance: Instance, message: InstanceUpdatedMessage): Instance => ({ -// ...instance, -// config: { -// ...instance.config, -// ...message, -// }, -// }) - const useDeploymentState = (options: DeploymentStateOptions): [DeploymentState, DeploymentActions] => { const { t } = useTranslation('deployments') const routes = useTeamRoutes() @@ -93,13 +78,9 @@ const useDeploymentState = (options: DeploymentStateOptions): [DeploymentState, const { deployment: optionDeploy, onWsError, onApiError } = options const { project, version } = optionDeploy - // const throttle = useThrottling(DEPLOYMENT_EDIT_WS_REQUEST_DELAY) - - // const patch = useRef>({}) - const [deployment, setDeployment] = useState(optionDeploy) const [node, setNode] = useNodeState(optionDeploy.node) - const [saveState] = useState(null) + const [saveState, setSaveState] = useState('disconnected') const [editState, setEditState] = useState('details') const [instances, setInstances] = useState(deployment.instances ?? []) const [viewMode, setViewMode] = usePersistedViewMode({ initialViewMode: 'list', pageName: 'deployments' }) @@ -126,50 +107,13 @@ const useDeploymentState = (options: DeploymentStateOptions): [DeploymentState, }) const sock = useWebSocket(routes.deployment.detailsSocket(deployment.id), { - // onOpen: () => setSaveState('connected'), - // onClose: () => setSaveState('disconnected'), - // onSend: message => { - // if ([WS_TYPE_PATCH_INSTANCE, WS_TYPE_PATCH_DEPLOYMENT_ENV].includes(message.type)) { - // setSaveState('saving') - // } - // }, - // onReceive: message => { - // if (WS_TYPE_PATCH_RECEIVED === message.type) { - // setSaveState('saved') - // } - // }, + onOpen: () => setSaveState('connected'), + onClose: () => setSaveState('disconnected'), onError: onWsError, }) const editor = useEditorState(sock) - // sock.on(WS_TYPE_DEPLOYMENT_ENV_UPDATED, (message: DeploymentEnvUpdatedMessage) => { - // setDeployment({ - // ...deployment, - // ...message, - // }) - // }) - - // sock.on(WS_TYPE_INSTANCE_UPDATED, (message: InstanceUpdatedMessage) => { - // const index = instances.findIndex(it => it.id === message.instanceId) - // if (index < 0) { - // sock.send(WS_TYPE_GET_INSTANCE, { - // id: message.instanceId, - // } as GetInstanceMessage) - // return - // } - - // const oldOne = instances[index] - // const instance = mergeInstancePatch(oldOne, message) - - // const newInstances = [...instances] - // newInstances[index] = instance - - // setInstances(newInstances) - // }) - - sock.on(WS_TYPE_INSTANCE, (message: InstanceMessage) => setInstances([...instances, message])) - sock.on(WS_TYPE_INSTANCES_ADDED, (message: InstancesAddedMessage) => setInstances([...instances, ...message.map(it => instanceCreatedMessageToInstance(it))]), ) @@ -183,101 +127,6 @@ const useDeploymentState = (options: DeploymentStateOptions): [DeploymentState, setEditState('details') } - // const onEnvironmentEdited = environment => { - // setSaveState('saving') - // setDeployment({ - // ...deployment, - // environment, - // }) - // throttle(() => { - // sock.send(WS_TYPE_PATCH_DEPLOYMENT_ENV, { - // environment, - // }) - // }) - // } - - const onInvalidateSecrets = (secrets: DeploymentInvalidatedSecrets[]) => { - const newInstances = instances.map(it => { - const invalidated = secrets.find(sec => sec.instanceId === it.id) - if (!invalidated) { - return it - } - - return { - ...it, - config: { - ...it.config, - secrets: (it.config.secrets ?? []).map(secret => { - if (invalidated.invalid.includes(secret.id)) { - return { - ...secret, - encrypted: false, - publicKey: '', - value: '', - } - } - - return secret - }), - }, - } - }) - - setInstances(newInstances) - } - - const onPatchInstance = (_: string, __: ConcreteContainerConfigData) => { - // const onPatchInstance = (id: string, newConfig: ConcreteContainerConfigData) => { - // const index = instances.findIndex(it => it.id === id) - // if (index < 0) { - // return - // } - // setSaveState('saving') - // const newPatch = { - // ...patch.current, - // ...newConfig, - // } - // patch.current = newPatch - // const newInstances = [...instances] - // const instance = newInstances[index] - // newInstances[index] = { - // ...instance, - // config: { - // ...instance.config, - // ...newConfig, - // }, - // } - // setInstances(newInstances) - // throttle(() => { - // sock.send(WS_TYPE_PATCH_INSTANCE, { - // instanceId: id, - // config: patch.current, - // } as PatchInstanceMessage) - // patch.current = {} - // }) - } - - const updateInstanceConfig = (_: string, __: ConcreteContainerConfigData) => { - // const updateInstanceConfig = (id: string, newConfig: ConcreteContainerConfigData) => { - // const index = instances.findIndex(it => it.id === id) - // if (index < 0) { - // return - // } - // setSaveState('saving') - // const newInstances = [...instances] - // const instance = newInstances[index] - // newInstances[index] = { - // ...instance, - // config: instance.config - // ? { - // ...instance.config, - // ...newConfig, - // } - // : newConfig, - // } - // setInstances(newInstances) - } - const onDeploymentTokenCreated = (token: DeploymentToken) => { setDeployment({ ...deployment, @@ -348,13 +197,10 @@ const useDeploymentState = (options: DeploymentStateOptions): [DeploymentState, setEditState, onDeploymentEdited, setViewMode, - onInvalidateSecrets, - onPatchInstance, onDeploymentTokenCreated, onRevokeDeploymentToken, onInstanceSelected, onAllInstancesToggled, - updateInstanceConfig, }, ] } diff --git a/web/crux-ui/src/models/deployment.ts b/web/crux-ui/src/models/deployment.ts index 3f90d526c..181c888a2 100644 --- a/web/crux-ui/src/models/deployment.ts +++ b/web/crux-ui/src/models/deployment.ts @@ -1,7 +1,7 @@ import { Audit } from './audit' import { DeploymentStatus, DyoApiError, slugify } from './common' import { ConfigBundleDetails } from './config-bundle' -import { ConcreteContainerConfig, ContainerIdentifier, ContainerState } from './container' +import { ConcreteContainerConfig, ContainerState } from './container' import { ImageDeletedMessage, VersionImage } from './image' import { Instance } from './instance' import { DyoNode } from './node' @@ -150,9 +150,6 @@ export type GetInstanceMessage = { id: string } -export const WS_TYPE_INSTANCE = 'instance' -export type InstanceMessage = Instance & {} - export const WS_TYPE_INSTANCES_ADDED = 'instances-added' type InstanceCreatedMessage = { id: string @@ -180,25 +177,6 @@ export type DeploymentEventMessage = DeploymentEvent export const WS_TYPE_DEPLOYMENT_FINISHED = 'deployment-finished' -export const WS_TYPE_GET_INSTANCE_SECRETS = 'get-instance-secrets' -export type GetInstanceSecretsMessage = { - id: string -} - -export type InstanceSecrets = { - container: ContainerIdentifier - - publicKey: string - - keys?: string[] -} - -export const WS_TYPE_INSTANCE_SECRETS = 'instance-secrets' -export type InstanceSecretsMessage = { - instanceId: string - keys: string[] -} - export const deploymentIsMutable = (status: DeploymentStatus, type: VersionType): boolean => { switch (status) { case 'preparing': diff --git a/web/crux/src/app/deploy/deploy.message.ts b/web/crux/src/app/deploy/deploy.message.ts index 634d30b27..9575ce4b5 100644 --- a/web/crux/src/app/deploy/deploy.message.ts +++ b/web/crux/src/app/deploy/deploy.message.ts @@ -15,28 +15,12 @@ export type DeploymentBundlesUpdatedMessage = { bundles: ConfigBundleDto[] } -export const WS_TYPE_GET_DEPLOYMENT_SECRETS = 'get-deployment-secrets' -export const WS_TYPE_GET_INSTANCE_SECRETS = 'get-instance-secrets' -export type GetInstanceSecretsMessage = { - id: string -} - -export const WS_TYPE_DEPLOYMENT_SECRETS = 'deployment-secrets' -export type DeploymentSecretsMessage = { - keys: string[] -} - -export const WS_TYPE_INSTANCE_SECRETS = 'instance-secrets' -export type InstanceSecretsMessage = { - instanceId: string - keys: string[] -} - type InstanceCreatedMessage = { id: string configId: string image: ImageDetailsDto } + export const WS_TYPE_INSTANCES_ADDED = 'instances-added' export type InstancesAddedMessage = InstanceCreatedMessage[] diff --git a/web/crux/src/app/deploy/deploy.ws.gateway.ts b/web/crux/src/app/deploy/deploy.ws.gateway.ts index 3a50f8b3c..1296f4e3b 100644 --- a/web/crux/src/app/deploy/deploy.ws.gateway.ts +++ b/web/crux/src/app/deploy/deploy.ws.gateway.ts @@ -30,15 +30,8 @@ import { IdentityFromSocket } from '../token/jwt-auth.guard' import { DeploymentEventListMessage, DeploymentEventMessage, - DeploymentSecretsMessage, - GetInstanceSecretsMessage, - InstanceSecretsMessage, WS_TYPE_DEPLOYMENT_EVENT_LIST, - WS_TYPE_DEPLOYMENT_SECRETS, WS_TYPE_FETCH_DEPLOYMENT_EVENTS, - WS_TYPE_GET_DEPLOYMENT_SECRETS, - WS_TYPE_GET_INSTANCE_SECRETS, - WS_TYPE_INSTANCE_SECRETS, } from './deploy.message' import DeployService from './deploy.service' @@ -140,35 +133,6 @@ export default class DeployWebSocketGateway { ) } - @AuditLogLevel('disabled') - @SubscribeMessage(WS_TYPE_GET_DEPLOYMENT_SECRETS) - async getDeploymentSecrets(@DeploymentId() deploymentId: string): Promise> { - const secrets = await this.service.getDeploymentSecrets(deploymentId) - - return { - type: WS_TYPE_DEPLOYMENT_SECRETS, - data: { - keys: secrets.keys ?? [], - }, - } - } - - @AuditLogLevel('disabled') - @SubscribeMessage(WS_TYPE_GET_INSTANCE_SECRETS) - async getInstanceSecrets( - @SocketMessage() message: GetInstanceSecretsMessage, - ): Promise> { - const secrets = await this.service.getInstanceSecrets(message.id) - - return { - type: WS_TYPE_INSTANCE_SECRETS, - data: { - instanceId: message.id, - keys: secrets.keys ?? [], - }, - } - } - @AuditLogLevel('disabled') @SubscribeMessage(WS_TYPE_FOCUS_INPUT) async onFocusInput(