Skip to content

Commit 8352d18

Browse files
committed
Use promise.allSettled
1 parent 7b4922f commit 8352d18

File tree

1 file changed

+23
-12
lines changed

1 file changed

+23
-12
lines changed

src/tools/atlas/read/getPerformanceAdvisor.ts

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ export class GetPerformanceAdvisorTool extends AtlasToolBase {
5252
}: ToolArgs<typeof this.argsShape>): Promise<CallToolResult> {
5353
try {
5454
const [suggestedIndexesResult, dropIndexSuggestionsResult, slowQueryLogsResult, schemaSuggestionsResult] =
55-
await Promise.all([
55+
await Promise.allSettled([
5656
operations.includes("suggestedIndexes")
5757
? getSuggestedIndexes(this.session.apiClient, projectId, clusterName)
5858
: Promise.resolve(undefined),
@@ -73,26 +73,37 @@ export class GetPerformanceAdvisorTool extends AtlasToolBase {
7373
: Promise.resolve(undefined),
7474
]);
7575

76-
const hasSuggestedIndexes = suggestedIndexesResult && suggestedIndexesResult?.suggestedIndexes?.length > 0;
76+
const hasSuggestedIndexes =
77+
suggestedIndexesResult.status === "fulfilled" &&
78+
suggestedIndexesResult.value?.suggestedIndexes &&
79+
suggestedIndexesResult.value.suggestedIndexes.length > 0;
7780
const hasDropIndexSuggestions =
78-
dropIndexSuggestionsResult &&
79-
dropIndexSuggestionsResult?.hiddenIndexes?.length > 0 &&
80-
dropIndexSuggestionsResult?.redundantIndexes?.length > 0 &&
81-
dropIndexSuggestionsResult?.unusedIndexes?.length > 0;
82-
const hasSlowQueryLogs = slowQueryLogsResult && slowQueryLogsResult?.slowQueryLogs?.length > 0;
81+
dropIndexSuggestionsResult.status === "fulfilled" &&
82+
dropIndexSuggestionsResult.value?.hiddenIndexes &&
83+
dropIndexSuggestionsResult.value?.redundantIndexes &&
84+
dropIndexSuggestionsResult.value?.unusedIndexes &&
85+
dropIndexSuggestionsResult.value.hiddenIndexes.length > 0 &&
86+
dropIndexSuggestionsResult.value.redundantIndexes.length > 0 &&
87+
dropIndexSuggestionsResult.value.unusedIndexes.length > 0;
88+
const hasSlowQueryLogs =
89+
slowQueryLogsResult.status === "fulfilled" &&
90+
slowQueryLogsResult.value?.slowQueryLogs &&
91+
slowQueryLogsResult.value.slowQueryLogs.length > 0;
8392
const hasSchemaSuggestions =
84-
schemaSuggestionsResult && schemaSuggestionsResult?.recommendations?.length > 0;
93+
schemaSuggestionsResult.status === "fulfilled" &&
94+
schemaSuggestionsResult.value?.recommendations &&
95+
schemaSuggestionsResult.value.recommendations.length > 0;
8596

8697
// Inserts the performance advisor data with the relevant section header if it exists
8798
const performanceAdvisorData = [
8899
`## Suggested Indexes\n${
89100
hasSuggestedIndexes
90-
? `Note: The "Weight" field is measured in bytes, and represents the estimated number of bytes saved in disk reads per executed read query that would be saved by implementing an index suggestion. Please convert this to MB or GB for easier readability.\n${JSON.stringify(suggestedIndexesResult.suggestedIndexes)}`
101+
? `Note: The "Weight" field is measured in bytes, and represents the estimated number of bytes saved in disk reads per executed read query that would be saved by implementing an index suggestion. Please convert this to MB or GB for easier readability.\n${JSON.stringify(suggestedIndexesResult.value?.suggestedIndexes)}`
91102
: "No suggested indexes found."
92103
}`,
93-
`## Drop Index Suggestions\n${hasDropIndexSuggestions ? JSON.stringify(dropIndexSuggestionsResult) : "No drop index suggestions found."}`,
94-
`## Slow Query Logs\n${hasSlowQueryLogs ? JSON.stringify(slowQueryLogsResult.slowQueryLogs) : "No slow query logs found."}`,
95-
`## Schema Suggestions\n${hasSchemaSuggestions ? JSON.stringify(schemaSuggestionsResult.recommendations) : "No schema suggestions found."}`,
104+
`## Drop Index Suggestions\n${hasDropIndexSuggestions ? JSON.stringify(dropIndexSuggestionsResult.value) : "No drop index suggestions found."}`,
105+
`## Slow Query Logs\n${hasSlowQueryLogs ? JSON.stringify(slowQueryLogsResult.value?.slowQueryLogs) : "No slow query logs found."}`,
106+
`## Schema Suggestions\n${hasSchemaSuggestions ? JSON.stringify(schemaSuggestionsResult.value?.recommendations) : "No schema suggestions found."}`,
96107
];
97108

98109
if (performanceAdvisorData.length === 0) {

0 commit comments

Comments
 (0)