Skip to content

Commit

Permalink
chore(compass-connections): split out connectionSupports into a funct…
Browse files Browse the repository at this point in the history
…ion that we can use outside of hooks (#6311)

split out connectionSupports into a function that we can use outside of hooks
  • Loading branch information
lerouxb authored Oct 1, 2024
1 parent 220067a commit 80f6ac0
Show file tree
Hide file tree
Showing 4 changed files with 253 additions and 44 deletions.
47 changes: 3 additions & 44 deletions packages/compass-connections/src/hooks/use-connection-supports.ts
Original file line number Diff line number Diff line change
@@ -1,39 +1,6 @@
import { useSelector } from '../stores/store-context';
import type { ConnectionState } from '../stores/connections-store-redux';

type ConnectionFeature = 'rollingIndexCreation' | 'globalWrites';

function isFreeOrSharedTierCluster(instanceSize: string | undefined): boolean {
if (!instanceSize) {
return false;
}

return ['M0', 'M2', 'M5'].includes(instanceSize);
}

function supportsRollingIndexCreation(connection: ConnectionState) {
const atlasMetadata = connection.info?.atlasMetadata;

if (!atlasMetadata) {
return false;
}

const { metricsType, instanceSize } = atlasMetadata;
return (
!isFreeOrSharedTierCluster(instanceSize) &&
(metricsType === 'cluster' || metricsType === 'replicaSet')
);
}

function supportsGlobalWrites(connection: ConnectionState) {
const atlasMetadata = connection.info?.atlasMetadata;

if (!atlasMetadata) {
return false;
}

return atlasMetadata.clusterType === 'GEOSHARDED';
}
import type { ConnectionFeature } from '../utils/connection-supports';
import { connectionSupports } from '../utils/connection-supports';

export function useConnectionSupports(
connectionId: string,
Expand All @@ -46,14 +13,6 @@ export function useConnectionSupports(
return false;
}

if (connectionFeature === 'rollingIndexCreation') {
return supportsRollingIndexCreation(connection);
}

if (connectionFeature === 'globalWrites') {
return supportsGlobalWrites(connection);
}

return false;
return connectionSupports(connection.info, connectionFeature);
});
}
2 changes: 2 additions & 0 deletions packages/compass-connections/src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ export { LegacyConnectionsModal } from './components/legacy-connections-modal';
export { useConnectionFormPreferences } from './hooks/use-connection-form-preferences';
import type { connect as devtoolsConnect } from 'mongodb-data-service';
import type { ExtraConnectionData as ExtraConnectionDataForTelemetry } from '@mongodb-js/compass-telemetry';
export type { ConnectionFeature } from './utils/connection-supports';
export { connectionSupports } from './utils/connection-supports';

const ConnectionsComponent: React.FunctionComponent<{
appName: string;
Expand Down
199 changes: 199 additions & 0 deletions packages/compass-connections/src/utils/connection-supports.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,199 @@
import { connectionSupports } from './connection-supports';
import { type ConnectionInfo } from '@mongodb-js/connection-storage/provider';
import { expect } from 'chai';

const mockConnections: ConnectionInfo[] = [
{
id: 'no-atlasMetadata',
connectionOptions: {
connectionString: 'mongodb://foo',
},
},
{
id: 'host-cluster',
connectionOptions: {
connectionString: 'mongodb://foo',
},
atlasMetadata: {
orgId: 'orgId',
projectId: 'projectId',
clusterName: 'clusterName',
regionalBaseUrl: 'https://example.com',
metricsId: 'metricsId',
metricsType: 'host',
instanceSize: 'M10',
clusterType: 'REPLICASET',
clusterUniqueId: 'clusterUniqueId',
},
},
{
id: 'free-cluster',
connectionOptions: {
connectionString: 'mongodb://foo',
},
atlasMetadata: {
orgId: 'orgId',
projectId: 'projectId',
clusterName: 'clusterName',
regionalBaseUrl: 'https://example.com',
metricsId: 'metricsId',
metricsType: 'replicaSet',
instanceSize: 'M0',
clusterType: 'REPLICASET',
clusterUniqueId: 'clusterUniqueId',
},
},
{
id: 'serverless-cluster',
connectionOptions: {
connectionString: 'mongodb://foo',
},
atlasMetadata: {
orgId: 'orgId',
projectId: 'projectId',
clusterName: 'clusterName',
regionalBaseUrl: 'https://example.com',
metricsId: 'metricsId',
metricsType: 'serverless',
instanceSize: 'SERVERLESS_V2',
clusterType: 'REPLICASET',
clusterUniqueId: 'clusterUniqueId',
},
},
{
id: 'dedicated-replicaSet',
connectionOptions: {
connectionString: 'mongodb://foo',
},
atlasMetadata: {
orgId: 'orgId',
projectId: 'projectId',
clusterName: 'clusterName',
regionalBaseUrl: 'https://example.com',
metricsId: 'metricsId',
metricsType: 'replicaSet',
instanceSize: 'M10',
clusterType: 'REPLICASET',
clusterUniqueId: 'clusterUniqueId',
},
},
{
id: 'dedicated-sharded',
connectionOptions: {
connectionString: 'mongodb://foo',
},
atlasMetadata: {
orgId: 'orgId',
projectId: 'projectId',
clusterName: 'clusterName',
regionalBaseUrl: 'https://example.com',
metricsId: 'metricsId',
metricsType: 'cluster',
instanceSize: 'M10',
clusterType: 'SHARDED',
clusterUniqueId: 'clusterUniqueId',
},
},
{
id: 'dedicated-geo-sharded',
connectionOptions: {
connectionString: 'mongodb://foo',
},
atlasMetadata: {
orgId: 'orgId',
projectId: 'projectId',
clusterName: 'clusterName',
regionalBaseUrl: 'https://example.com',
metricsId: 'metricsId',
metricsType: 'cluster',
instanceSize: 'M30',
clusterType: 'GEOSHARDED',
clusterUniqueId: 'clusterUniqueId',
},
},
];

function connectionInfoById(connectionId: string): ConnectionInfo {
const connectionInfo = mockConnections.find(({ id }) => id === connectionId);
if (!connectionInfo) {
throw new Error(`No connection for id "${connectionId}"`);
}
return connectionInfo;
}

describe('connectionSupports', function () {
context('rollingIndexCreation', function () {
it('should return false if the connection has no atlasMetadata', function () {
expect(
connectionSupports(
connectionInfoById('no-atlasMetadata'),
'rollingIndexCreation'
)
).to.be.false;
});

it('should return false for host cluster type', function () {
expect(
connectionSupports(
connectionInfoById('host-cluster'),
'rollingIndexCreation'
)
).to.be.false;
});

it('should return false for serverless cluster type', function () {
expect(
connectionSupports(
connectionInfoById('serverless-cluster'),
'rollingIndexCreation'
)
).to.be.false;
});

it('should return false for free/shared tier clusters', function () {
expect(
connectionSupports(
connectionInfoById('free-cluster'),
'rollingIndexCreation'
)
).to.be.false;
});

it('should return true for dedicated replicaSet clusters', function () {
expect(
connectionSupports(
connectionInfoById('dedicated-replicaSet'),
'rollingIndexCreation'
)
).to.be.true;
});

it('should return true for dedicated sharded clusters', function () {
expect(
connectionSupports(
connectionInfoById('dedicated-sharded'),
'rollingIndexCreation'
)
).to.be.true;
});
});
context('globalWrites', function () {
it('should return false if the connection has no atlasMetadata', function () {
expect(
connectionSupports(
connectionInfoById('no-atlasMetadata'),
'globalWrites'
)
).to.be.false;
});

it('should return true if the cluster type is geosharded', function () {
expect(
connectionSupports(
connectionInfoById('dedicated-geo-sharded'),
'globalWrites'
)
).to.be.true;
});
});
});
49 changes: 49 additions & 0 deletions packages/compass-connections/src/utils/connection-supports.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import type { ConnectionInfo } from '@mongodb-js/connection-info';
export type ConnectionFeature = 'rollingIndexCreation' | 'globalWrites';

function isFreeOrSharedTierCluster(instanceSize: string | undefined): boolean {
if (!instanceSize) {
return false;
}

return ['M0', 'M2', 'M5'].includes(instanceSize);
}

function supportsRollingIndexCreation(connectionInfo: ConnectionInfo) {
const atlasMetadata = connectionInfo.atlasMetadata;

if (!atlasMetadata) {
return false;
}

const { metricsType, instanceSize } = atlasMetadata;
return (
!isFreeOrSharedTierCluster(instanceSize) &&
(metricsType === 'cluster' || metricsType === 'replicaSet')
);
}

function supportsGlobalWrites(connectionInfo: ConnectionInfo) {
const atlasMetadata = connectionInfo.atlasMetadata;

if (!atlasMetadata) {
return false;
}

return atlasMetadata.clusterType === 'GEOSHARDED';
}

export function connectionSupports(
connectionInfo: ConnectionInfo,
connectionFeature: ConnectionFeature
): boolean {
if (connectionFeature === 'rollingIndexCreation') {
return supportsRollingIndexCreation(connectionInfo);
}

if (connectionFeature === 'globalWrites') {
return supportsGlobalWrites(connectionInfo);
}

return false;
}

0 comments on commit 80f6ac0

Please sign in to comment.