Skip to content

Commit 8fdefa1

Browse files
committed
feat: disable db and coll stats via preferences
1 parent 5a4c38e commit 8fdefa1

File tree

19 files changed

+162
-76
lines changed

19 files changed

+162
-76
lines changed

package-lock.json

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/collection-model/index.d.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -65,18 +65,18 @@ interface CollectionProps {
6565
specialish: boolean;
6666
normal: boolean;
6767
readonly: boolean;
68-
view_on: string;
68+
view_on: string | null;
6969
collation: unknown;
7070
pipeline: unknown[];
7171
validation: unknown;
72-
is_capped: boolean;
73-
document_count: number;
74-
document_size: number;
75-
avg_document_size: number;
76-
storage_size: number;
77-
free_storage_size: number;
78-
index_count: number;
79-
index_size: number;
72+
is_capped?: boolean;
73+
document_count?: number;
74+
document_size?: number;
75+
avg_document_size?: number;
76+
storage_size?: number;
77+
free_storage_size?: number;
78+
index_count?: number;
79+
index_size?: number;
8080
isTimeSeries: boolean;
8181
isView: boolean;
8282
/** Only relevant for a view and identifies collection/view from which this view was created. */

packages/collection-model/lib/model.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,7 @@ const CollectionModel = AmpersandModel.extend(debounceActions(['fetch']), {
260260

261261
try {
262262
console.log('[collection-model]', 'starting');
263-
const newStatus = this.status === 'initialed' ? 'fetching' : 'refreshing';
263+
const newStatus = this.status === 'initial' ? 'fetching' : 'refreshing';
264264
this.set({ status: newStatus });
265265
const [collStats, collectionInfo] = await Promise.all([
266266
enableDbAndCollStats

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

Lines changed: 28 additions & 11 deletions
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 { PreferencesAccess } from 'compass-preferences-model';
3031

3132
const WRITE_STAGE_LINK = {
3233
$merge:
@@ -225,12 +226,19 @@ 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+
preferences,
235+
}: {
236+
instance: MongoDBInstance;
237+
dataService: DataService;
238+
namespace: string;
239+
pipeline: Document[];
240+
preferences: PreferencesAccess;
241+
}) => {
234242
const lastStageOperator = getStageOperator(pipeline[pipeline.length - 1]);
235243
let typeOfWrite;
236244

@@ -252,6 +260,7 @@ const confirmWriteOperationIfNeeded = async (
252260
dataService,
253261
database,
254262
collection,
263+
preferences,
255264
}));
256265
} else {
257266
isOverwritingCollection = true;
@@ -289,17 +298,25 @@ export const runAggregation = (): PipelineBuilderThunkAction<Promise<void>> => {
289298
return async (
290299
dispatch,
291300
getState,
292-
{ pipelineBuilder, instance, dataService, track, connectionInfoRef }
301+
{
302+
pipelineBuilder,
303+
instance,
304+
dataService,
305+
track,
306+
connectionInfoRef,
307+
preferences,
308+
}
293309
) => {
294310
const pipeline = getPipelineFromBuilderState(getState(), pipelineBuilder);
295311

296312
if (
297-
!(await confirmWriteOperationIfNeeded(
313+
!(await confirmWriteOperationIfNeeded({
298314
instance,
299315
dataService,
300-
getState().namespace,
301-
pipeline
302-
))
316+
namespace: getState().namespace,
317+
pipeline,
318+
preferences,
319+
}))
303320
) {
304321
return;
305322
}

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import { MongoDBInstancesManager } from './instances-manager';
2020
import toNS from 'mongodb-ns';
2121
import type Collection from 'mongodb-collection-model';
2222
import type Database from 'mongodb-database-model';
23+
import { usePreferences } from 'compass-preferences-model/provider';
2324

2425
export {
2526
MongoDBInstancesManagerEvents,
@@ -130,6 +131,7 @@ export const NamespaceProvider = createServiceProvider(
130131
const ns = useMemo(() => {
131132
return toNS(namespace);
132133
}, [namespace]);
134+
const preferences = usePreferences(['enableDbAndCollStats']);
133135
const [namespaceModel, setNamespaceModel] = useState<
134136
Database | Collection | null
135137
>(() => {
@@ -166,7 +168,7 @@ export const NamespaceProvider = createServiceProvider(
166168
return;
167169
}
168170

169-
await db.fetchCollections({ dataService }).catch(() => {
171+
await db.fetchCollections({ dataService, preferences }).catch(() => {
170172
// See above
171173
});
172174
const coll = db.collections.get(ns.ns);
@@ -190,6 +192,7 @@ export const NamespaceProvider = createServiceProvider(
190192
ns.collection,
191193
ns.database,
192194
ns.ns,
195+
preferences,
193196
]);
194197

195198
if (!namespaceModel) {

packages/compass-app-stores/src/stores/instance-store.ts

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ export function createInstancesStore(
7272
await instance.fetchDatabases({ dataService });
7373
await Promise.all(
7474
instance.databases.map((db) => {
75-
return db.fetchCollections({ dataService });
75+
return db.fetchCollections({ dataService, preferences });
7676
})
7777
);
7878
} catch (error) {
@@ -91,7 +91,7 @@ export function createInstancesStore(
9191
const refreshInstance = async (
9292
refreshOptions: Omit<
9393
Parameters<MongoDBInstance['refresh']>[0],
94-
'dataService'
94+
'dataService' | 'preferences'
9595
> = {},
9696
{ connectionId }: { connectionId?: string } = {}
9797
) => {
@@ -105,16 +105,10 @@ export function createInstancesStore(
105105
instancesManager.getMongoDBInstanceForConnection(connectionId);
106106
const dataService = connections.getDataServiceForConnection(connectionId);
107107
isFirstRun = instance.status === 'initial';
108-
const { enableDbAndCollStats } = preferences.getPreferences();
109108
await instance.refresh({
110109
dataService,
111110
...refreshOptions,
112-
fetchDbStats: enableDbAndCollStats
113-
? refreshOptions.fetchDbStats
114-
: false,
115-
fetchCollStats: enableDbAndCollStats
116-
? refreshOptions.fetchCollStats
117-
: false,
111+
preferences,
118112
});
119113
} catch (err: any) {
120114
log.warn(
@@ -165,7 +159,7 @@ export function createInstancesStore(
165159
await instance.fetchDatabases({ dataService, force: true });
166160
await Promise.allSettled(
167161
instance.databases.map((db) =>
168-
db.fetchCollections({ dataService, force: true })
162+
db.fetchCollections({ dataService, preferences, force: true })
169163
)
170164
);
171165
} catch (err: any) {
@@ -353,7 +347,7 @@ export function createInstancesStore(
353347
connections.getDataServiceForConnection(connectionId);
354348
void instance.databases
355349
.get(databaseId)
356-
?.fetchCollections({ dataService });
350+
?.fetchCollections({ dataService, preferences });
357351
} catch (error) {
358352
log.warn(
359353
mongoLogId(1_001_000_323),

packages/compass-saved-aggregations-queries/src/stores/open-item.ts

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -461,7 +461,12 @@ export const openSavedItem =
461461
async (
462462
dispatch,
463463
getState,
464-
{ instancesManager, connections, logger: { log, mongoLogId } }
464+
{
465+
instancesManager,
466+
connections,
467+
logger: { log, mongoLogId },
468+
preferencesAccess: preferences,
469+
}
465470
) => {
466471
const {
467472
savedItems: { items },
@@ -494,6 +499,7 @@ export const openSavedItem =
494499
dataService,
495500
database,
496501
collection,
502+
preferences,
497503
});
498504

499505
if (coll) {
@@ -594,7 +600,11 @@ export const openSelectedItem =
594600

595601
export const databaseSelected =
596602
(database: string): SavedQueryAggregationThunkAction<Promise<void>> =>
597-
async (dispatch, getState, { instancesManager, connections }) => {
603+
async (
604+
dispatch,
605+
getState,
606+
{ instancesManager, connections, preferencesAccess: preferences }
607+
) => {
598608
const {
599609
openItem: { selectedDatabase, selectedConnection },
600610
} = getState();
@@ -621,7 +631,7 @@ export const databaseSelected =
621631
throw new Error('Database not found');
622632
}
623633

624-
await db.fetchCollections({ dataService });
634+
await db.fetchCollections({ dataService, preferences });
625635
// Check with the the current value in case db was re-selected while we
626636
// were fetching
627637
if (database === getState().openItem.selectedDatabase) {

packages/compass-workspaces/src/stores/workspaces.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -724,6 +724,7 @@ const fetchCollectionInfo = (
724724
dataService,
725725
database,
726726
collection,
727+
preferences,
727728
});
728729

729730
if (coll) {

packages/database-model/index.d.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import type { PreferencesAccess } from 'compass-preferences-model';
55
interface DatabaseProps {
66
_id: string;
77
name: string;
8-
hasDbStats: boolean;
98
status: 'initial' | 'fetching' | 'refreshing' | 'ready' | 'error';
109
statusError: string | null;
1110
collectionsStatus: 'initial' | 'fetching' | 'refreshing' | 'ready' | 'error';
@@ -29,11 +28,13 @@ interface Database extends DatabaseProps {
2928
}): Promise<void>;
3029
fetchCollections(opts: {
3130
dataService: DataService;
31+
preferences: PreferenceAccess;
3232
fetchInfo?: boolean;
3333
force?: boolean;
3434
}): Promise<void>;
3535
fetchCollectionsDetails(opts: {
3636
dataService: DataService;
37+
preferences: PreferenceAccess;
3738
nameOnly?: boolean;
3839
force?: boolean;
3940
}): Promise<void>;

packages/database-model/lib/model.js

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ const DatabaseModel = AmpersandModel.extend(
149149

150150
if (!enableDbAndCollStats) {
151151
console.log('[database-model]', 'Skipping');
152-
this.set({ status: 'ready', hasDbStats: 'false' });
152+
this.set({ status: 'ready' });
153153
return;
154154
} else {
155155
console.log('[database-model]', 'Not skipping');
@@ -162,7 +162,6 @@ const DatabaseModel = AmpersandModel.extend(
162162
this.set({
163163
status: 'ready',
164164
statusError: null,
165-
hasDbStats: true,
166165
...stats,
167166
});
168167
} catch (err) {
@@ -181,18 +180,25 @@ const DatabaseModel = AmpersandModel.extend(
181180
*/
182181
async fetchCollections({ dataService, preferences, force = false }) {
183182
if (!shouldFetch(this.collectionsStatus, force)) {
183+
console.log('[database-model]', 'skipping collection');
184184
return;
185185
}
186186

187187
try {
188188
const newStatus =
189189
this.collectionsStatus === 'initial' ? 'fetching' : 'refreshing';
190190
this.set({ collectionsStatus: newStatus });
191-
console.log('[database-model]', 'Fetching collections');
191+
console.log(
192+
'[database-model]',
193+
'Fetching collections',
194+
preferences,
195+
this.collections.fetch
196+
);
192197
await this.collections.fetch({ dataService, preferences, force });
193198
console.log('[database-model]', 'Done with collections');
194199
this.set({ collectionsStatus: 'ready', collectionsStatusError: null });
195200
} catch (err) {
201+
console.log('[database-model]', 'failure', err);
196202
this.set({
197203
collectionsStatus: 'error',
198204
collectionsStatusError: err.message,
@@ -203,11 +209,13 @@ const DatabaseModel = AmpersandModel.extend(
203209

204210
async fetchCollectionsDetails({
205211
dataService,
212+
preferences,
206213
nameOnly = false,
207214
force = false,
208215
}) {
209216
await this.fetchCollections({
210217
dataService,
218+
preferences,
211219
force,
212220
});
213221

@@ -221,6 +229,7 @@ const DatabaseModel = AmpersandModel.extend(
221229
this.collections.map((coll) => {
222230
return coll.fetch({
223231
dataService,
232+
preferences,
224233
// We already fetched it with fetchCollections
225234
fetchInfo: false,
226235
force,

0 commit comments

Comments
 (0)