import { has } from 'lodash'; import type { Application } from '../../application/application.model'; import type { IConfirmationModalParams } from '../../confirmationModal/confirmationModal.service'; import type { ICluster, IServerGroup } from '../../domain'; export class ServerGroupWarningMessageService { public static addDestroyWarningMessage( application: Application, serverGroup: IServerGroup, params: IConfirmationModalParams, ): void { const remainingServerGroups: IServerGroup[] = this.getOtherServerGroupsInCluster(application, serverGroup); if (!remainingServerGroups.length) { params.body = `

You are destroying the last Server Group in the Cluster.

Account:
${serverGroup.account}
Region:
${serverGroup.region}
Cluster:
${serverGroup.cluster}
`; } } public static addDisableWarningMessage( application: Application, serverGroup: IServerGroup, params: IConfirmationModalParams, ): void { if (!serverGroup.instanceCounts.up) { return; } const otherServerGroupsInCluster: IServerGroup[] = this.getOtherServerGroupsInCluster(application, serverGroup); const remainingActiveServerGroups: IServerGroup[] = otherServerGroupsInCluster.filter( (s) => !s.isDisabled && s.instanceCounts.up > 0, ); const hasOtherInstances = otherServerGroupsInCluster.some((s) => s.instances.length > 0); if (hasOtherInstances || remainingActiveServerGroups.length === 0 || otherServerGroupsInCluster.length === 0) { const totalActiveInstances = remainingActiveServerGroups.reduce((acc: number, s: IServerGroup) => { return s.instanceCounts.up + acc; }, serverGroup.instanceCounts.up); const activeInstancesAfterDisable = totalActiveInstances - serverGroup.instanceCounts.up; const activePercentRemaining = Math.round((activeInstancesAfterDisable / totalActiveInstances) * 100); params.body = `

You are disabling ${serverGroup.instanceCounts.up} instance${serverGroup.instanceCounts.up === 1 ? '' : 's'}.

This will reduce the cluster to ${activePercentRemaining} percent of its current capacity, leaving ${activeInstancesAfterDisable} instance${activeInstancesAfterDisable === 1 ? '' : 's'} taking traffic.

`; params.verificationLabel = `Verify the number of remaining active instances (${activeInstancesAfterDisable}) after disabling this server group.`; params.textToVerify = `${activeInstancesAfterDisable}`; delete params.account; } } private static getOtherServerGroupsInCluster(application: Application, serverGroup: IServerGroup): IServerGroup[] { const cluster: ICluster = application.clusters.find( (c: ICluster) => c.account === serverGroup.account && c.name === serverGroup.cluster, ); return cluster ? cluster.serverGroups.filter((s) => s.region === serverGroup.region && s.name !== serverGroup.name) : []; } private static getRemainingServerGroupsForDisplay(serverGroups: IServerGroup[]): string { return serverGroups .sort((a, b) => b.name.localeCompare(a.name)) .map((sg) => { let label = sg.name; if (has(sg, 'buildInfo.jenkins.number')) { label = `${sg.name} (build #${sg.buildInfo.jenkins.number})`; } return `
  • ${label}: ${sg.instanceCounts.up} instance${sg.instanceCounts.up === 1 ? '' : 's'}
  • `; }) .join('\n'); } }