Skip to content

Commit 7897097

Browse files
committed
traverse docs
1 parent 33401ca commit 7897097

File tree

9 files changed

+94
-59
lines changed

9 files changed

+94
-59
lines changed

packages/compass-components/src/components/bson-value.tsx

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,11 @@ import { spacing } from '@leafygreen-ui/tokens';
99
import { css, cx } from '@leafygreen-ui/emotion';
1010
import { Theme, useDarkMode } from '../hooks/use-theme';
1111

12-
type ValueProps = (
12+
type ValueProps =
1313
| {
1414
[type in keyof TypeCastMap]: { type: type; value: TypeCastMap[type] };
1515
}[keyof TypeCastMap]
16-
| { type: 'DBRef'; value: DBRef }
17-
) & { onUUIDEncountered?: (subtype: 3 | 4) => void };
16+
| { type: 'DBRef'; value: DBRef };
1817

1918
function truncate(str: string, length = 70): string {
2019
const truncated = str.slice(0, length);
@@ -123,7 +122,6 @@ const ObjectIdValue: React.FunctionComponent<PropsByValueType<'ObjectId'>> = ({
123122

124123
const BinaryValue: React.FunctionComponent<PropsByValueType<'Binary'>> = ({
125124
value,
126-
onUUIDEncountered,
127125
}) => {
128126
const { stringifiedValue, title, additionalHints } = useMemo(() => {
129127
if (value.sub_type === Binary.SUBTYPE_ENCRYPTED) {
@@ -146,7 +144,6 @@ const BinaryValue: React.FunctionComponent<PropsByValueType<'Binary'>> = ({
146144
}
147145
if (value.sub_type === Binary.SUBTYPE_UUID) {
148146
let uuid: string;
149-
onUUIDEncountered?.(4);
150147
try {
151148
// Try to get the pretty hex version of the UUID
152149
uuid = value.toUUID().toString();
@@ -190,9 +187,6 @@ const BinaryValue: React.FunctionComponent<PropsByValueType<'Binary'>> = ({
190187
};
191188
}
192189
}
193-
if (value.sub_type === Binary.SUBTYPE_UUID_OLD) {
194-
onUUIDEncountered?.(3);
195-
}
196190
return {
197191
stringifiedValue: `Binary.createFromBase64('${truncate(
198192
value.toString('base64'),
@@ -379,12 +373,7 @@ const BSONValue: React.FunctionComponent<ValueProps> = (props) => {
379373
case 'Date':
380374
return <DateValue value={props.value}></DateValue>;
381375
case 'Binary':
382-
return (
383-
<BinaryValue
384-
value={props.value}
385-
onUUIDEncountered={props.onUUIDEncountered}
386-
></BinaryValue>
387-
);
376+
return <BinaryValue value={props.value}></BinaryValue>;
388377
case 'Int32':
389378
case 'Double':
390379
return <NumberValue type={props.type} value={props.value}></NumberValue>;

packages/compass-components/src/components/document-list/document.tsx

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -85,15 +85,13 @@ const HadronDocument: React.FunctionComponent<{
8585
editable?: boolean;
8686
editing?: boolean;
8787
onEditStart?: () => void;
88-
onUUIDEncountered?: (subtype: 3 | 4) => void;
8988
extraGutterWidth?: number;
9089
}> = ({
9190
value: document,
9291
editable = false,
9392
editing = false,
9493
onEditStart,
9594
extraGutterWidth,
96-
onUUIDEncountered,
9795
}) => {
9896
const { elements, visibleElements } = useHadronDocument(document);
9997
const [autoFocus, setAutoFocus] = useState<{
@@ -150,7 +148,6 @@ const HadronDocument: React.FunctionComponent<{
150148
}
151149
: undefined
152150
}
153-
onUUIDEncountered={onUUIDEncountered}
154151
lineNumberSize={visibleElements.length}
155152
onAddElement={(el) => {
156153
setAutoFocus({

packages/compass-components/src/components/document-list/element.tsx

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -414,7 +414,6 @@ export const HadronElement: React.FunctionComponent<{
414414
editable: boolean;
415415
editingEnabled: boolean;
416416
onEditStart?: (id: string, field: 'key' | 'value' | 'type') => void;
417-
onUUIDEncountered?: (subtype: 3 | 4) => void;
418417
lineNumberSize: number;
419418
onAddElement(el: HadronElementType): void;
420419
extraGutterWidth?: number;
@@ -423,7 +422,6 @@ export const HadronElement: React.FunctionComponent<{
423422
editable,
424423
editingEnabled,
425424
onEditStart,
426-
onUUIDEncountered,
427425
lineNumberSize,
428426
onAddElement,
429427
extraGutterWidth = 0,
@@ -690,7 +688,6 @@ export const HadronElement: React.FunctionComponent<{
690688
<BSONValue
691689
type={type.value as any}
692690
value={value.originalValue}
693-
onUUIDEncountered={onUUIDEncountered}
694691
></BSONValue>
695692
</div>
696693
)}

packages/compass-crud/src/components/editable-document.tsx

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import { withPreferences } from 'compass-preferences-model/provider';
88
import { documentStyles, documentContentStyles } from './readonly-document';
99
import { getInsightsForDocument } from '../utils';
1010
import type { CrudActions } from '../stores/crud-store';
11-
import { TelemetryContext } from '@mongodb-js/compass-telemetry/provider';
1211

1312
const documentElementsContainerStyles = css({
1413
position: 'relative',
@@ -246,21 +245,12 @@ class EditableDocument extends React.Component<
246245
*/
247246
renderElements() {
248247
return (
249-
<TelemetryContext.Consumer>
250-
{(track) => (
251-
<DocumentList.Document
252-
value={this.props.doc}
253-
editable
254-
editing={this.state.editing}
255-
onEditStart={this.handleStartEditing.bind(this)}
256-
onUUIDEncountered={(subtype: 3 | 4) => {
257-
track('UUID Encountered', {
258-
subtype,
259-
});
260-
}}
261-
/>
262-
)}
263-
</TelemetryContext.Consumer>
248+
<DocumentList.Document
249+
value={this.props.doc}
250+
editable
251+
editing={this.state.editing}
252+
onEditStart={this.handleStartEditing.bind(this)}
253+
/>
264254
);
265255
}
266256

packages/compass-crud/src/components/readonly-document.tsx

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import type { TypeCastMap } from 'hadron-type-checker';
66
import { withPreferences } from 'compass-preferences-model/provider';
77
import { getInsightsForDocument } from '../utils';
88
import { DocumentEvents } from 'hadron-document';
9-
import { TelemetryContext } from '@mongodb-js/compass-telemetry/provider';
109
type BSONObject = TypeCastMap['Object'];
1110

1211
export const documentStyles = css({
@@ -133,22 +132,13 @@ class ReadonlyDocument extends React.Component<
133132
*/
134133
renderElements() {
135134
return (
136-
<TelemetryContext.Consumer>
137-
{(track) => (
138-
<>
139-
<DocumentList.Document
140-
value={this.props.doc}
141-
// Provide extra whitespace for the expand button
142-
extraGutterWidth={spacing[900]}
143-
onUUIDEncountered={(subtype: 3 | 4) => {
144-
track('UUID Encountered', {
145-
subtype,
146-
});
147-
}}
148-
/>
149-
</>
150-
)}
151-
</TelemetryContext.Consumer>
135+
<>
136+
<DocumentList.Document
137+
value={this.props.doc}
138+
// Provide extra whitespace for the expand button
139+
extraGutterWidth={spacing[900]}
140+
/>
141+
</>
152142
);
153143
}
154144

packages/compass-crud/src/stores/crud-store.ts

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -112,11 +112,13 @@ const INITIAL_BULK_UPDATE_TEXT = `{
112112

113113
export const fetchDocuments: (
114114
dataService: DataService,
115+
track: TrackFunction,
115116
serverVersion: string,
116117
isDataLake: boolean,
117118
...args: Parameters<DataService['find']>
118119
) => Promise<HadronDocument[]> = async (
119120
dataService: DataService,
121+
track: TrackFunction,
120122
serverVersion,
121123
isDataLake,
122124
ns,
@@ -145,17 +147,31 @@ export const fetchDocuments: (
145147
};
146148

147149
try {
148-
return (
150+
let uuidSubtype3Count = 0;
151+
let uuidSubtype4Count = 0;
152+
const docs = (
149153
await dataService.find(ns, filter, modifiedOptions, executionOptions)
150154
).map((doc) => {
151155
const { __doc, __size, ...rest } = doc;
156+
let hadronDoc: HadronDocument;
152157
if (__doc && __size && Object.keys(rest).length === 0) {
153-
const hadronDoc = new HadronDocument(__doc);
158+
hadronDoc = new HadronDocument(__doc);
154159
hadronDoc.size = Number(__size);
155-
return hadronDoc;
160+
} else {
161+
hadronDoc = new HadronDocument(doc);
156162
}
157-
return new HadronDocument(doc);
163+
const { subtype3Count, subtype4Count } = hadronDoc.findUUIDs();
164+
uuidSubtype3Count += subtype3Count;
165+
uuidSubtype4Count += subtype4Count;
166+
return hadronDoc;
158167
});
168+
if (uuidSubtype3Count > 0) {
169+
track('UUID Encountered', { subtype: 3, count: uuidSubtype3Count });
170+
}
171+
if (uuidSubtype4Count > 0) {
172+
track('UUID Encountered', { subtype: 4, count: uuidSubtype4Count });
173+
}
174+
return docs;
159175
} catch (err) {
160176
// We are handling all the cases where the size calculating projection might
161177
// not work, but just in case we run into some other environment or use-case
@@ -896,6 +912,7 @@ class CrudStoreImpl
896912
try {
897913
documents = await fetchDocuments(
898914
this.dataService,
915+
this.track,
899916
this.state.version,
900917
this.state.isDataLake,
901918
ns,
@@ -1733,6 +1750,7 @@ class CrudStoreImpl
17331750
),
17341751
fetchDocuments(
17351752
this.dataService,
1753+
this.track,
17361754
this.state.version,
17371755
this.state.isDataLake,
17381756
ns,

packages/compass-telemetry/src/telemetry-events.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2698,6 +2698,7 @@ type UUIDEncounteredEvent = CommonEvent<{
26982698
name: 'UUID Encountered';
26992699
payload: {
27002700
subtype: 3 | 4;
2701+
count: number;
27012702
};
27022703
}>;
27032704

packages/hadron-document/src/document.ts

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import type { BSONArray, BSONObject, BSONValue } from './utils';
1212
import { objectToIdiomaticEJSON } from './utils';
1313
import type { HadronEJSONOptions } from './utils';
1414
import { DocumentEvents } from '.';
15-
import type { MongoServerError } from 'mongodb';
15+
import type { Binary, MongoServerError } from 'mongodb';
1616

1717
/**
1818
* The event constant.
@@ -450,6 +450,30 @@ export class Document extends EventEmitter {
450450
}, 0);
451451
}
452452

453+
findUUIDs() {
454+
let subtype4Count = 0;
455+
let subtype3Count = 0;
456+
for (const element of this.elements) {
457+
if (element.currentType === 'Binary') {
458+
if ((element.value as Binary).sub_type === 4) {
459+
subtype4Count++;
460+
}
461+
if ((element.value as Binary).sub_type === 3) {
462+
subtype3Count++;
463+
}
464+
} else if (
465+
element.currentType === 'Object' ||
466+
element.currentType === 'Array'
467+
) {
468+
const { subtype3Count: sub3, subtype4Count: sub4 } =
469+
element.findUUIDs();
470+
subtype3Count += sub3;
471+
subtype4Count += sub4;
472+
}
473+
}
474+
return { subtype3Count, subtype4Count };
475+
}
476+
453477
startEditing(elementId?: string, field?: 'key' | 'value' | 'type'): void {
454478
if (!this.editing) {
455479
this.editing = true;

packages/hadron-document/src/element.ts

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import DateEditor from './editor/date';
1010
import Events from './element-events';
1111
import type Document from './document';
1212
import type { TypeCastTypes } from 'hadron-type-checker';
13-
import type { ObjectId } from 'bson';
13+
import type { Binary, ObjectId } from 'bson';
1414
import type { BSONArray, BSONObject, BSONValue } from './utils';
1515
import { getDefaultValueForType } from './utils';
1616
import { DocumentEvents, ElementEvents } from '.';
@@ -879,6 +879,35 @@ export class Element extends EventEmitter {
879879
);
880880
}
881881

882+
findUUIDs(): { subtype4Count: number; subtype3Count: number } {
883+
let subtype4Count = 0;
884+
let subtype3Count = 0;
885+
886+
if (!this.elements) {
887+
return { subtype4Count, subtype3Count };
888+
}
889+
for (const element of this.elements) {
890+
if (element.currentType === 'Binary') {
891+
if ((element.currentValue as Binary).sub_type === 4) {
892+
subtype4Count++;
893+
}
894+
if ((element.currentValue as Binary).sub_type === 3) {
895+
subtype3Count++;
896+
}
897+
} else if (
898+
element.currentType === 'Object' ||
899+
element.currentType === 'Array'
900+
) {
901+
const { subtype3Count: sub3, subtype4Count: sub4 } =
902+
element.findUUIDs();
903+
subtype3Count += sub3;
904+
subtype4Count += sub4;
905+
}
906+
}
907+
908+
return { subtype4Count, subtype3Count };
909+
}
910+
882911
private emitVisibleElementsChanged(targetElement: Element | Document = this) {
883912
if (targetElement.isRoot()) {
884913
targetElement.emit(DocumentEvents.VisibleElementsChanged, targetElement);

0 commit comments

Comments
 (0)