Skip to content

Commit 10e1c65

Browse files
committed
update covered queries with new links, esr order swap, copy change
1 parent d3a0ee7 commit 10e1c65

File tree

3 files changed

+100
-23
lines changed

3 files changed

+100
-23
lines changed

packages/compass-indexes/src/components/create-index-form/index-flow-section.spec.tsx

Lines changed: 57 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,55 @@ describe('IndexFlowSection', () => {
7575
});
7676
});
7777

78+
describe('when 4 index fields are filled in and user clicks on covered queries button', () => {
79+
const fields: Field[] = [
80+
{ name: 'field1', type: '1 (asc)' },
81+
{ name: 'field2', type: '-1 (desc)' },
82+
{ name: 'field3', type: '1 (asc)' },
83+
{ name: 'field4', type: '1 (asc)' },
84+
];
85+
86+
beforeEach(() => {
87+
renderComponent({ fields });
88+
screen.getByTestId('index-flow-section-covered-queries-button').click();
89+
});
90+
91+
it('renders the covered queries examples', () => {
92+
const coveredQueriesExamples = screen.getByTestId(
93+
'index-flow-section-covered-queries-examples'
94+
);
95+
expect(coveredQueriesExamples).to.exist;
96+
expect(coveredQueriesExamples).to.contain.text(
97+
JSON.stringify({
98+
field1: 1,
99+
field2: 2,
100+
field3: 3,
101+
field4: 4,
102+
})
103+
);
104+
});
105+
106+
it('renders the optimal query examples', () => {
107+
const optimalQueriesExamples = screen.getByTestId(
108+
'index-flow-section-optimal-queries-examples'
109+
);
110+
expect(optimalQueriesExamples).to.exist;
111+
expect(optimalQueriesExamples).to.contain.text(
112+
`{"field1":1,"field2":2,"field4":{"$gt":3}}.sort("field3": 1})`
113+
);
114+
});
115+
116+
it('renders the Covered Queries Learn More link', () => {
117+
const link = screen.getByText('Learn about covered queries');
118+
expect(link).to.be.visible;
119+
});
120+
121+
it('renders the ESR Learn More link', () => {
122+
const link = screen.getByText('Learn about ESR');
123+
expect(link).to.be.visible;
124+
});
125+
});
126+
78127
describe('when 3 index fields are filled in and user clicks on covered queries button', () => {
79128
const fields: Field[] = [
80129
{ name: 'field1', type: '1 (asc)' },
@@ -107,12 +156,17 @@ describe('IndexFlowSection', () => {
107156
);
108157
expect(optimalQueriesExamples).to.exist;
109158
expect(optimalQueriesExamples).to.contain.text(
110-
`{"field1":1,"field2":{"$gt":2}}.sort(field3: 1})`
159+
`{"field1":1,"field3":{"$gt":2}}.sort("field2": 1})`
111160
);
112161
});
113162

114-
it('renders the Learn More link', () => {
115-
const link = screen.getByText('Learn More');
163+
it('renders the Covered Queries Learn More link', () => {
164+
const link = screen.getByText('Learn about covered queries');
165+
expect(link).to.be.visible;
166+
});
167+
168+
it('renders the ESR Learn More link', () => {
169+
const link = screen.getByText('Learn about ESR');
116170
expect(link).to.be.visible;
117171
});
118172
});

packages/compass-indexes/src/components/create-index-form/index-flow-section.tsx

Lines changed: 35 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -156,23 +156,27 @@ const generateOptimalQueries = (
156156
}
157157

158158
// If there are more than two fields, we want to show a longer optimal query with gt and sort
159-
// i.e. {a:1, b:2, c:{gt:3}}.sort({d:1})
159+
// i.e. {a:1, b:2, d:{gt:3}}.sort({c:1})
160+
161+
const secondToLastField = coveredQueriesArr[numOfFields - 2];
162+
const secondToLastFieldKey = Object.keys(secondToLastField)[0];
163+
160164
const optimalQueries = coveredQueriesArr
161-
.slice(0, -1)
165+
.slice(0, -2)
162166
.reduce<Record<string, unknown>>((acc, obj, index) => {
163167
const key = Object.keys(obj)[0];
164168
const value = obj[key];
165169

166-
if (index === numOfFields - 2) {
167-
acc[key] = { $gt: value };
168-
} else {
169-
acc[key] = value;
170-
}
170+
acc[key] = value;
171171

172172
return acc;
173173
}, {});
174174

175-
return JSON.stringify(optimalQueries) + `.sort(${lastFieldKey}: 1})`;
175+
// Put last field in range and second to last field in sort
176+
optimalQueries[lastFieldKey] = { $gt: coveredQueriesArr.length - 1 };
177+
return (
178+
JSON.stringify(optimalQueries) + `.sort("${secondToLastFieldKey}": 1})`
179+
);
176180
};
177181

178182
const IndexFlowSection = ({
@@ -358,15 +362,25 @@ const IndexFlowSection = ({
358362
className={codeStyles}
359363
data-testid="index-flow-section-covered-queries-examples"
360364
>
361-
{coveredQueries}
365+
<p>{coveredQueries}</p>
362366
</Body>
367+
<Link
368+
href="https://www.mongodb.com/docs/manual/core/query-optimization/"
369+
onClick={() => {
370+
track('Covered Queries Learn More Clicked', {
371+
context: 'Create Index Modal',
372+
});
373+
}}
374+
>
375+
Learn about covered queries
376+
</Link>
363377

364378
{!!optimalQueries && (
365379
<>
366380
<p>
367381
<span className={underlineStyles}>
368382
Follow the Equality, Sort, Range (ESR) Rule. This index is
369-
optimal for queries that have this pattern:
383+
great for queries that have this pattern:
370384
</span>
371385
{/* Optimal queries */}
372386
<Body
@@ -376,18 +390,19 @@ const IndexFlowSection = ({
376390
{optimalQueries}
377391
</Body>
378392
</p>
379-
<Link
380-
href="https://www.mongodb.com/docs/manual/core/query-optimization/"
381-
onClick={() => {
382-
track('Covered Queries Learn More Clicked', {
383-
context: 'Create Index Modal',
384-
});
385-
}}
386-
>
387-
Learn More
388-
</Link>
389393
</>
390394
)}
395+
396+
<Link
397+
href="https://www.mongodb.com/docs/manual/tutorial/equality-sort-range-guideline/"
398+
onClick={() => {
399+
track('ESR Learn More Clicked', {
400+
context: 'Create Index Modal',
401+
});
402+
}}
403+
>
404+
Learn about ESR
405+
</Link>
391406
</div>
392407
</>
393408
)}

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2849,6 +2849,13 @@ type CreateIndexCoveredQueriesLearnMoreClicked = CommonEvent<{
28492849
};
28502850
}>;
28512851

2852+
type CreateIndexESRLearnMoreClicked = CommonEvent<{
2853+
name: 'ESR Learn More Clicked';
2854+
payload: {
2855+
context: CreateIndexModalContext;
2856+
};
2857+
}>;
2858+
28522859
type CreateIndexInputIndexCopied = CommonEvent<{
28532860
name: 'Input Index Copied';
28542861
payload: {
@@ -3003,6 +3010,7 @@ export type TelemetryEvent =
30033010
| CreateIndexCodeEquivalentToggled
30043011
| CreateIndexCoveredQueriesButtonClicked
30053012
| CreateIndexCoveredQueriesLearnMoreClicked
3013+
| CreateIndexESRLearnMoreClicked
30063014
| CreateIndexIndexTabClicked
30073015
| CreateIndexModalCancelled
30083016
| CreateIndexModalClosed

0 commit comments

Comments
 (0)