Skip to content

Commit f1a3722

Browse files
authored
feat: disable db and coll stats via preferences COMPASS-5387 (#6852)
1 parent e7b7d3e commit f1a3722

File tree

41 files changed

+756
-212
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+756
-212
lines changed

package-lock.json

+8
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/collection-model/index.d.ts

+20-11
Original file line numberDiff line numberDiff line change
@@ -64,18 +64,18 @@ interface CollectionProps {
6464
specialish: boolean;
6565
normal: boolean;
6666
readonly: boolean;
67-
view_on: string;
67+
view_on: string | null;
6868
collation: unknown;
6969
pipeline: unknown[];
7070
validation: unknown;
71-
is_capped: boolean;
72-
document_count: number;
73-
document_size: number;
74-
avg_document_size: number;
75-
storage_size: number;
76-
free_storage_size: number;
77-
index_count: number;
78-
index_size: number;
71+
is_capped: boolean | undefined;
72+
document_count: number | undefined;
73+
document_size: number | undefined;
74+
avg_document_size: number | undefined;
75+
storage_size: number | undefined;
76+
free_storage_size: number | undefined;
77+
index_count: number | undefined;
78+
index_size: number | undefined;
7979
isTimeSeries: boolean;
8080
isView: boolean;
8181
/** Only relevant for a view and identifies collection/view from which this view was created. */
@@ -85,7 +85,13 @@ interface CollectionProps {
8585
is_non_existent: boolean;
8686
}
8787

88-
type CollectionDataService = Pick<DataService, 'collectionStats' | 'collectionInfo' | 'listCollections' | 'isListSearchIndexesSupported'>;
88+
type CollectionDataService = Pick<
89+
DataService,
90+
| 'collectionStats'
91+
| 'collectionInfo'
92+
| 'listCollections'
93+
| 'isListSearchIndexesSupported'
94+
>;
8995

9096
interface Collection extends CollectionProps {
9197
fetch(opts: {
@@ -106,7 +112,10 @@ interface Collection extends CollectionProps {
106112
}
107113

108114
interface CollectionCollection extends Array<Collection> {
109-
fetch(opts: { dataService: CollectionDataService; fetchInfo?: boolean }): Promise<void>;
115+
fetch(opts: {
116+
dataService: CollectionDataService;
117+
fetchInfo?: boolean;
118+
}): Promise<void>;
110119
toJSON(opts?: { derived: boolean }): Array<CollectionProps>;
111120
at(index: number): Collection | undefined;
112121
get(id: string, key?: '_id' | 'name'): Collection | undefined;

packages/collection-model/lib/model.js

+29-5
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,17 @@ function pickCollectionInfo({
104104
fle2,
105105
is_non_existent,
106106
}) {
107-
return { type, readonly, view_on, collation, pipeline, validation, clustered, fle2, is_non_existent };
107+
return {
108+
type,
109+
readonly,
110+
view_on,
111+
collation,
112+
pipeline,
113+
validation,
114+
clustered,
115+
fle2,
116+
is_non_existent,
117+
};
108118
}
109119

110120
/**
@@ -232,18 +242,30 @@ const CollectionModel = AmpersandModel.extend(debounceActions(['fetch']), {
232242
},
233243

234244
/**
235-
* @param {{ dataService: import('mongodb-data-service').DataService }} dataService
245+
* @param {{
246+
* dataService: import('mongodb-data-service').DataService,
247+
* fetchInfo: boolean,
248+
* force: boolean
249+
* }} options
236250
* @returns
237251
*/
238252
async fetch({ dataService, fetchInfo = true, force = false }) {
239253
if (!shouldFetch(this.status, force)) {
240254
return;
241255
}
256+
257+
const shouldFetchDbAndCollStats = getParentByType(
258+
this,
259+
'Instance'
260+
).shouldFetchDbAndCollStats;
261+
242262
try {
243263
const newStatus = this.status === 'initial' ? 'fetching' : 'refreshing';
244264
this.set({ status: newStatus });
245265
const [collStats, collectionInfo] = await Promise.all([
246-
dataService.collectionStats(this.database, this.name),
266+
shouldFetchDbAndCollStats
267+
? dataService.collectionStats(this.database, this.name)
268+
: null,
247269
fetchInfo ? dataService.collectionInfo(this.database, this.name) : null,
248270
]);
249271
this.set({
@@ -255,7 +277,7 @@ const CollectionModel = AmpersandModel.extend(debounceActions(['fetch']), {
255277
// If the collection is not unprovisioned `is_non_existent` anymore,
256278
// let's update the parent database model to reflect the change.
257279
// This happens when a user tries to insert first document into a
258-
// collection that doesn't exist yet or creates a new collection
280+
// collection that doesn't exist yet or creates a new collection
259281
// for an unprovisioned database.
260282
if (!this.is_non_existent) {
261283
getParentByType(this, 'Database').set({
@@ -271,7 +293,9 @@ const CollectionModel = AmpersandModel.extend(debounceActions(['fetch']), {
271293
/**
272294
* Fetches collection info and returns a special format of collection metadata
273295
* that events like open-in-new-tab, select-namespace, edit-view require
274-
* @param {{ dataService: import('mongodb-data-service').DataService }} dataService
296+
* @param {{
297+
* dataService: import('mongodb-data-service').DataService,
298+
* }} options
275299
*/
276300
async fetchMetadata({ dataService }) {
277301
try {

packages/compass-aggregations/src/modules/aggregation.ts

+26-11
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import { runPipelineConfirmationDescription } from '../utils/modal-descriptions'
2727
import type { MongoDBInstance } from 'mongodb-instance-model';
2828
import type { DataService } from '../modules/data-service';
2929
import toNS from 'mongodb-ns';
30+
import type { PreferencesAccess } from 'compass-preferences-model';
3031

3132
const WRITE_STAGE_LINK = {
3233
$merge:
@@ -225,12 +226,18 @@ const reducer: Reducer<State, Action> = (state = INITIAL_STATE, action) => {
225226
return state;
226227
};
227228

228-
const confirmWriteOperationIfNeeded = async (
229-
instance: MongoDBInstance,
230-
dataService: DataService,
231-
namespace: string,
232-
pipeline: Document[]
233-
) => {
229+
const confirmWriteOperationIfNeeded = async ({
230+
instance,
231+
dataService,
232+
namespace,
233+
pipeline,
234+
}: {
235+
instance: MongoDBInstance;
236+
dataService: DataService;
237+
namespace: string;
238+
pipeline: Document[];
239+
preferences: PreferencesAccess;
240+
}) => {
234241
const lastStageOperator = getStageOperator(pipeline[pipeline.length - 1]);
235242
let typeOfWrite;
236243

@@ -289,17 +296,25 @@ export const runAggregation = (): PipelineBuilderThunkAction<Promise<void>> => {
289296
return async (
290297
dispatch,
291298
getState,
292-
{ pipelineBuilder, instance, dataService, track, connectionInfoRef }
299+
{
300+
pipelineBuilder,
301+
instance,
302+
dataService,
303+
track,
304+
connectionInfoRef,
305+
preferences,
306+
}
293307
) => {
294308
const pipeline = getPipelineFromBuilderState(getState(), pipelineBuilder);
295309

296310
if (
297-
!(await confirmWriteOperationIfNeeded(
311+
!(await confirmWriteOperationIfNeeded({
298312
instance,
299313
dataService,
300-
getState().namespace,
301-
pipeline
302-
))
314+
namespace: getState().namespace,
315+
pipeline,
316+
preferences,
317+
}))
303318
) {
304319
return;
305320
}

packages/compass-app-stores/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@
7979
"mongodb-collection-model": "^5.25.8",
8080
"mongodb-database-model": "^2.25.8",
8181
"mongodb-instance-model": "^12.26.8",
82+
"compass-preferences-model": "^2.33.8",
8283
"mongodb-ns": "^2.4.2",
8384
"react": "^17.0.2"
8485
},

packages/compass-app-stores/src/instances-manager.spec.ts

+13-1
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,19 @@ import {
66
} from './instances-manager';
77
import { MongoDBInstance } from 'mongodb-instance-model';
88
import { createDefaultConnectionInfo } from '@mongodb-js/testing-library-compass';
9+
import {
10+
type PreferencesAccess,
11+
createSandboxFromDefaultPreferences,
12+
} from 'compass-preferences-model';
913

1014
const TEST_CONNECTION_INFO = createDefaultConnectionInfo();
1115

1216
describe('InstancesManager', function () {
1317
let instancesManager: MongoDBInstancesManager;
14-
beforeEach(function () {
18+
let preferences: PreferencesAccess;
19+
beforeEach(async function () {
1520
instancesManager = new MongoDBInstancesManager();
21+
preferences = await createSandboxFromDefaultPreferences();
1622
});
1723

1824
it('should be able to create and return a MongoDB instance', function () {
@@ -27,6 +33,7 @@ describe('InstancesManager', function () {
2733
servers: [],
2834
setName: '',
2935
},
36+
preferences,
3037
}
3138
);
3239
expect(instance).to.be.instanceOf(MongoDBInstance);
@@ -44,6 +51,7 @@ describe('InstancesManager', function () {
4451
servers: [],
4552
setName: '',
4653
},
54+
preferences,
4755
}
4856
);
4957
expect(instancesManager.listMongoDBInstances()).to.have.lengthOf(1);
@@ -66,6 +74,7 @@ describe('InstancesManager', function () {
6674
servers: [],
6775
setName: '',
6876
},
77+
preferences,
6978
}
7079
);
7180
expect(onInstanceCreatedStub).to.be.calledOnceWithExactly(
@@ -89,6 +98,7 @@ describe('InstancesManager', function () {
8998
servers: [],
9099
setName: '',
91100
},
101+
preferences,
92102
}
93103
);
94104
expect(() =>
@@ -108,6 +118,7 @@ describe('InstancesManager', function () {
108118
servers: [],
109119
setName: '',
110120
},
121+
preferences,
111122
}
112123
);
113124
expect(() =>
@@ -138,6 +149,7 @@ describe('InstancesManager', function () {
138149
servers: [],
139150
setName: '',
140151
},
152+
preferences,
141153
}
142154
);
143155
instancesManager.removeMongoDBInstanceForConnection(

packages/compass-app-stores/src/plugin.tsx

+6
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ import { createInstancesStore } from './stores';
99
import type { ConnectionsService } from '@mongodb-js/compass-connections/provider';
1010
import { connectionsLocator } from '@mongodb-js/compass-connections/provider';
1111
import { type MongoDBInstancesManager } from './instances-manager';
12+
import type { PreferencesAccess } from 'compass-preferences-model';
13+
import { preferencesLocator } from 'compass-preferences-model/provider';
1214

1315
interface MongoDBInstancesProviderProps {
1416
children?: React.ReactNode;
@@ -37,10 +39,12 @@ export const CompassInstanceStorePlugin = registerHadronPlugin(
3739
{
3840
connections,
3941
logger,
42+
preferences,
4043
globalAppRegistry,
4144
}: {
4245
connections: ConnectionsService;
4346
logger: Logger;
47+
preferences: PreferencesAccess;
4448
globalAppRegistry: AppRegistry;
4549
},
4650
helpers: ActivateHelpers
@@ -49,6 +53,7 @@ export const CompassInstanceStorePlugin = registerHadronPlugin(
4953
{
5054
connections,
5155
logger,
56+
preferences,
5257
globalAppRegistry,
5358
},
5459
helpers
@@ -63,6 +68,7 @@ export const CompassInstanceStorePlugin = registerHadronPlugin(
6368
},
6469
{
6570
logger: createLoggerLocator('COMPASS-INSTANCE-STORE'),
71+
preferences: preferencesLocator,
6672
connections: connectionsLocator,
6773
}
6874
);

0 commit comments

Comments
 (0)