Skip to content

Commit

Permalink
fix(web, connections): filter out paused clusters; cleanup connection…
Browse files Browse the repository at this point in the history
…s on plugin deactivate COMPASS-8510 CLOUDP-284226 (#6500)

* fix(web, connections): filter out paused clusters; cleanup connections on plugin deactivate

* chore(connections): fix test
  • Loading branch information
gribnoysup authored Nov 19, 2024
1 parent 206341c commit 8512aa7
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 9 deletions.
32 changes: 32 additions & 0 deletions packages/compass-connections/src/index.spec.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import React from 'react';
import {
createDefaultConnectionInfo,
render,
} from '@mongodb-js/testing-library-compass';
import { expect } from 'chai';

describe('CompassConnections', function () {
it('cleans-up connections when unmounted', async function () {
const conn1 = createDefaultConnectionInfo();
const conn2 = createDefaultConnectionInfo();

const result = render(
<div>
{/* it's a bit weird, but testing-library-compass already renders CompassConnections for us */}
</div>,
{ connections: [conn1, conn2] }
);

await result.connectionsStore.actions.connect(conn1);

expect(
result.connectionsStore.getState().connections.byId[conn1.id]
).to.have.property('status', 'connected');

result.unmount();

expect(
result.connectionsStore.getState().connections.byId[conn1.id]
).to.have.property('status', 'disconnected');
});
});
12 changes: 10 additions & 2 deletions packages/compass-connections/src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { registerHadronPlugin } from 'hadron-app-registry';
import {
autoconnectCheck,
configureStore,
disconnect,
loadConnections,
} from './stores/connections-store-redux';
import React, { useContext, useRef } from 'react';
Expand Down Expand Up @@ -46,7 +47,7 @@ const CompassConnectionsPlugin = registerHadronPlugin(
activate(
initialProps,
{ logger, preferences, connectionStorage, track },
helpers
{ addCleanup, cleanup }
) {
const store = configureStore(initialProps.preloadStorageConnectionInfos, {
logger,
Expand All @@ -67,9 +68,16 @@ const CompassConnectionsPlugin = registerHadronPlugin(
}
});

// Stop all connections on disconnect
addCleanup(() => {
for (const connectionId of store.getState().connections.ids) {
store.dispatch(disconnect(connectionId));
}
});

return {
store,
deactivate: helpers.cleanup,
deactivate: cleanup,
context: ConnectionsStoreContext,
};
},
Expand Down
22 changes: 16 additions & 6 deletions packages/compass-connections/src/stores/connections-store-redux.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1423,6 +1423,10 @@ function getCurrentConnectionInfo(
return state.connections.byId[connectionId]?.info;
}

function getCurrentConnectionStatus(state: State, connectionId: ConnectionId) {
return state.connections.byId[connectionId]?.status;
}

/**
* Returns the number of active connections. We count in-progress connections
* as "active" to make sure that the maximum connection allowed check takes
Expand Down Expand Up @@ -1967,15 +1971,21 @@ const cleanupConnection = (
'Initiating disconnect attempt'
);

const currentStatus = getCurrentConnectionStatus(getState(), connectionId);

// We specifically want to track Disconnected even when it's not really
// triggered by user at all, so we put it in the cleanup function that is
// called every time you disconnect, or remove a connection, or all of them,
// or close the app
track(
'Connection Disconnected',
{},
getCurrentConnectionInfo(getState(), connectionId)
);
// or close the app. Only track when connection is either connected or
// connecting, we might be calling this on something that was never
// connected
if (currentStatus === 'connected' || currentStatus === 'connecting') {
track(
'Connection Disconnected',
{},
getCurrentConnectionInfo(getState(), connectionId)
);
}

const { closeConnectionStatusToast } = getNotificationTriggers(
preferences.getPreferences().enableMultipleConnectionSystem
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -470,6 +470,8 @@ describe('useConnections', function () {
preferences: defaultPreferences,
});

await connectionsStore.actions.connect(mockConnections[0]);

result.current.removeConnection(mockConnections[0].id);

await waitFor(() => {
Expand Down
3 changes: 2 additions & 1 deletion packages/compass-web/src/connection-storage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ type ClusterDescription = {
state: string;
deploymentItemName: string;
replicationSpecList?: ReplicationSpec[];
isPaused?: boolean;
};

export type ClusterDescriptionWithDataProcessingRegion = ClusterDescription & {
Expand Down Expand Up @@ -250,7 +251,7 @@ class AtlasCloudConnectionStorage
// account in the UI for a special state of a deployment as
// clusters can become inactive during their runtime and it's
// valuable UI info to display
return !!description.srvAddress;
return !description.isPaused && !!description.srvAddress;
})
.map(async (description) => {
// Even though nds/clusters will list serverless clusters, to get
Expand Down

0 comments on commit 8512aa7

Please sign in to comment.