Skip to content

Commit 3217e95

Browse files
committed
Typing
1 parent f0b24bb commit 3217e95

File tree

2 files changed

+32
-95
lines changed

2 files changed

+32
-95
lines changed

src/common/atlas/performanceAdvisorUtils.ts

Lines changed: 5 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -4,97 +4,20 @@ import { getProcessIdFromCluster } from "./cluster.js";
44
import type { components } from "./openapi.js";
55

66
export type SuggestedIndex = components["schemas"]["PerformanceAdvisorIndex"];
7-
87
export type DropIndexSuggestion = components["schemas"]["DropIndexSuggestionsIndex"];
9-
108
export type SlowQueryLogMetrics = components["schemas"]["PerformanceAdvisorSlowQueryMetrics"];
11-
129
export type SlowQueryLog = components["schemas"]["PerformanceAdvisorSlowQuery"];
1310

1411
interface SuggestedIndexesResponse {
15-
content: {
16-
suggestedIndexes?: Array<SuggestedIndex>;
17-
};
12+
content: components["schemas"]["PerformanceAdvisorResponse"];
1813
}
19-
2014
interface DropIndexesResponse {
21-
content: {
22-
hiddenIndexes?: Array<DropIndexSuggestion>;
23-
redundantIndexes?: Array<DropIndexSuggestion>;
24-
unusedIndexes?: Array<DropIndexSuggestion>;
25-
};
15+
content: components["schemas"]["DropIndexSuggestionsResponse"];
2616
}
27-
2817
interface SchemaAdviceResponse {
29-
content: {
30-
recommendations?: Array<SchemaRecommendation>;
31-
};
32-
}
33-
34-
interface SlowQueriesResponse {
35-
slowQueries?: Array<SlowQueryLog>;
36-
}
37-
38-
export type SchemaTriggerType =
39-
| "PERCENT_QUERIES_USE_LOOKUP"
40-
| "NUMBER_OF_QUERIES_USE_LOOKUP"
41-
| "DOCS_CONTAIN_UNBOUNDED_ARRAY"
42-
| "NUMBER_OF_NAMESPACES"
43-
| "DOC_SIZE_TOO_LARGE"
44-
| "NUM_INDEXES"
45-
| "QUERIES_CONTAIN_CASE_INSENSITIVE_REGEX";
46-
47-
export const SCHEMA_TRIGGER_DESCRIPTIONS: Record<SchemaTriggerType, string> = {
48-
PERCENT_QUERIES_USE_LOOKUP: "High percentage of queries (>50%) use $lookup operations",
49-
NUMBER_OF_QUERIES_USE_LOOKUP: "High number of queries (>100) use $lookup operations",
50-
DOCS_CONTAIN_UNBOUNDED_ARRAY: "Arrays with over 10000 entries detected in the collection(s)",
51-
NUMBER_OF_NAMESPACES: "Too many namespaces (collections) in the database (>100)",
52-
DOC_SIZE_TOO_LARGE: "Documents larger than 2 MB found in the collection(s)",
53-
NUM_INDEXES: "More than 30 indexes detected in the collection(s) scanned",
54-
QUERIES_CONTAIN_CASE_INSENSITIVE_REGEX: "Queries use case-insensitive regular expressions",
55-
};
56-
57-
type SchemaRecommedationType =
58-
| "REDUCE_LOOKUP_OPS"
59-
| "AVOID_UNBOUNDED_ARRAY"
60-
| "REDUCE_DOCUMENT_SIZE"
61-
| "REMOVE_UNNECESSARY_INDEXES"
62-
| "REDUCE_NUMBER_OF_NAMESPACES"
63-
| "OPTIMIZE_CASE_INSENSITIVE_REGEX_QUERIES"
64-
| "OPTIMIZE_TEXT_QUERIES";
65-
66-
export const SCHEMA_RECOMMENDATION_DESCRIPTIONS: Record<SchemaRecommedationType, string> = {
67-
REDUCE_LOOKUP_OPS: "Reduce the use of $lookup operations",
68-
AVOID_UNBOUNDED_ARRAY: "Avoid using unbounded arrays in documents",
69-
REDUCE_DOCUMENT_SIZE: "Reduce the size of documents",
70-
REMOVE_UNNECESSARY_INDEXES: "Remove unnecessary indexes",
71-
REDUCE_NUMBER_OF_NAMESPACES: "Reduce the number of collections in the database",
72-
OPTIMIZE_CASE_INSENSITIVE_REGEX_QUERIES: "Optimize case-insensitive regex queries",
73-
OPTIMIZE_TEXT_QUERIES: "Optimize text search queries",
74-
};
75-
76-
export interface SchemaRecommendation {
77-
affectedNamespaces?: Array<{
78-
namespace?: string | null;
79-
triggers?: Array<{
80-
description?: string;
81-
triggerType?: SchemaTriggerType;
82-
}>;
83-
}>;
84-
description?: string;
85-
recommendation?: SchemaRecommedationType;
86-
}
87-
88-
export interface PerformanceAdvisorData {
89-
suggestedIndexes: Array<SuggestedIndex>;
90-
dropIndexSuggestions: {
91-
hiddenIndexes: Array<DropIndexSuggestion>;
92-
redundantIndexes: Array<DropIndexSuggestion>;
93-
unusedIndexes: Array<DropIndexSuggestion>;
94-
};
95-
slowQueryLogs: Array<SlowQueryLog>;
96-
schemaSuggestions: Array<SchemaRecommendation>;
18+
content: components["schemas"]["SchemaAdvisorResponse"];
9719
}
20+
export type SchemaRecommendation = components["schemas"]["SchemaAdvisorItemRecommendation"];
9821

9922
export async function getSuggestedIndexes(
10023
apiClient: ApiClient,
@@ -204,7 +127,7 @@ export async function getSlowQueries(
204127
},
205128
});
206129

207-
return { slowQueryLogs: (response as SlowQueriesResponse).slowQueries ?? [] };
130+
return { slowQueryLogs: response.slowQueries ?? [] };
208131
} catch (err) {
209132
apiClient.logger.debug({
210133
id: LogId.atlasPaSlowQueryLogsFailure,

src/tools/atlas/read/listPerformanceAdvisor.ts

Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,10 @@ import {
88
getDropIndexSuggestions,
99
getSchemaAdvice,
1010
getSlowQueries,
11-
type PerformanceAdvisorData,
11+
type SuggestedIndex,
12+
type DropIndexSuggestion,
13+
type SlowQueryLog,
14+
type SchemaRecommendation,
1215
} from "../../../common/atlas/performanceAdvisorUtils.js";
1316

1417
const PerformanceAdvisorOperationType = z.enum([
@@ -17,9 +20,21 @@ const PerformanceAdvisorOperationType = z.enum([
1720
"slowQueryLogs",
1821
"schemaSuggestions",
1922
]);
23+
24+
interface PerformanceAdvisorData {
25+
suggestedIndexes?: Array<SuggestedIndex>;
26+
dropIndexSuggestions?: {
27+
hiddenIndexes?: Array<DropIndexSuggestion>;
28+
redundantIndexes?: Array<DropIndexSuggestion>;
29+
unusedIndexes?: Array<DropIndexSuggestion>;
30+
};
31+
slowQueryLogs?: Array<SlowQueryLog>;
32+
schemaSuggestions?: Array<SchemaRecommendation>;
33+
}
2034
export class ListPerformanceAdvisorTool extends AtlasToolBase {
2135
public name = "atlas-list-performance-advisor";
22-
protected description = "List MongoDB Atlas performance advisor recommendations";
36+
protected description =
37+
"List MongoDB Atlas performance advisor recommendations, which includes the operations: suggested indexes, drop index suggestions, slow query logs, and schema suggestions";
2338
public operationType: OperationType = "read";
2439
protected argsShape = {
2540
projectId: z.string().describe("Atlas project ID to list performance advisor recommendations"),
@@ -29,7 +44,10 @@ export class ListPerformanceAdvisorTool extends AtlasToolBase {
2944
.default(PerformanceAdvisorOperationType.options)
3045
.describe("Operations to list performance advisor recommendations"),
3146
since: z.date().describe("Date to list slow query logs since").optional(),
32-
namespaces: z.array(z.string()).describe("Namespaces to list slow query logs").optional(),
47+
namespaces: z
48+
.array(z.string())
49+
.describe("Namespaces to list slow query logs. Only relevant for the slowQueryLogs operation.")
50+
.optional(),
3351
};
3452

3553
protected async execute({
@@ -60,8 +78,8 @@ export class ListPerformanceAdvisorTool extends AtlasToolBase {
6078
if (operations.includes("dropIndexSuggestions")) {
6179
performanceAdvisorPromises.push(
6280
getDropIndexSuggestions(this.session.apiClient, projectId, clusterName).then(
63-
({ hiddenIndexes, redundantIndexes, unusedIndexes }) => {
64-
data.dropIndexSuggestions = { hiddenIndexes, redundantIndexes, unusedIndexes };
81+
(dropIndexSuggestions) => {
82+
data.dropIndexSuggestions = dropIndexSuggestions;
6583
}
6684
)
6785
);
@@ -99,23 +117,19 @@ export class ListPerformanceAdvisorTool extends AtlasToolBase {
99117

100118
let formattedOutput = "";
101119

102-
if (data.suggestedIndexes.length > 0) {
120+
if (data.suggestedIndexes && data.suggestedIndexes.length > 0) {
103121
formattedOutput += `\n## Suggested Indexes\n${JSON.stringify(data.suggestedIndexes)}\n`;
104122
}
105123

106-
if (
107-
data.dropIndexSuggestions.hiddenIndexes.length > 0 ||
108-
data.dropIndexSuggestions.redundantIndexes.length > 0 ||
109-
data.dropIndexSuggestions.unusedIndexes.length > 0
110-
) {
124+
if (data.dropIndexSuggestions) {
111125
formattedOutput += `\n## Drop Index Suggestions\n${JSON.stringify(data.dropIndexSuggestions)}\n`;
112126
}
113127

114-
if (data.slowQueryLogs.length > 0) {
128+
if (data.slowQueryLogs && data.slowQueryLogs.length > 0) {
115129
formattedOutput += `\n## Slow Query Logs\n${JSON.stringify(data.slowQueryLogs)}\n`;
116130
}
117131

118-
if (data.schemaSuggestions.length > 0) {
132+
if (data.schemaSuggestions && data.schemaSuggestions.length > 0) {
119133
formattedOutput += `\n## Schema Suggestions\n${JSON.stringify(data.schemaSuggestions)}\n`;
120134
}
121135

0 commit comments

Comments
 (0)