-
Notifications
You must be signed in to change notification settings - Fork 27
DA-1170 add index advisor support with two new tools #58
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
AayushTyagi1
merged 5 commits into
main
from
feature/DA-1170-add-support-for-index-advisor
Oct 22, 2025
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
f659939
feat: DA-1170 add index advisor support with two new tools
AayushTyagi1 805f792
test: DA-1170 Added Unit Tests for Index Advisor
AayushTyagi1 d408787
DA-1170: Fixed: Review comments
AayushTyagi1 190d0f7
DA-1170 Modified Review comment addressed
AayushTyagi1 c1bd8e4
DA-1170: Deleted: Tests
AayushTyagi1 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,84 @@ | ||
| """ | ||
| Tools for index operations and optimization. | ||
|
|
||
| This module contains tools for getting index recommendations using the Couchbase Index Advisor. | ||
| """ | ||
|
|
||
| import logging | ||
| from typing import Any | ||
|
|
||
| from mcp.server.fastmcp import Context | ||
|
|
||
| from tools.query import run_cluster_query | ||
| from utils.constants import MCP_SERVER_NAME | ||
|
|
||
| logger = logging.getLogger(f"{MCP_SERVER_NAME}.tools.index") | ||
|
|
||
|
|
||
| def get_index_advisor_recommendations(ctx: Context, query: str) -> dict[str, Any]: | ||
| """Get index recommendations from Couchbase Index Advisor for a given SQL++ query. | ||
|
|
||
| The Index Advisor analyzes the query and provides recommendations for optimal indexes. | ||
| This tool works with SELECT, UPDATE, DELETE, or MERGE queries. | ||
| The query should contain fully qualified keyspace (e.g., bucket.scope.collection). | ||
|
|
||
| Returns a dictionary with: | ||
| - current_used_indexes: Array of currently used indexes (if any) | ||
| - recommended_indexes: Array of recommended secondary indexes (if any) | ||
| - recommended_covering_indexes: Array of recommended covering indexes (if any) | ||
|
|
||
| Each index object contains: | ||
| - index: The CREATE INDEX SQL++ command | ||
| - statements: Array of statement objects with the query and run count | ||
| """ | ||
| try: | ||
| # Build the ADVISOR query | ||
| advisor_query = f"SELECT ADVISOR('{query}') AS advisor_result" | ||
|
|
||
| logger.info("Running Index Advisor for the provided query") | ||
|
|
||
| # Execute the ADVISOR function at cluster level using run_cluster_query | ||
| advisor_results = run_cluster_query(ctx, advisor_query) | ||
|
|
||
| if not advisor_results: | ||
| return { | ||
| "message": "No recommendations available", | ||
| "current_used_indexes": [], | ||
| "recommended_indexes": [], | ||
| "recommended_covering_indexes": [], | ||
| } | ||
|
|
||
| # The result is wrapped in advisor_result key | ||
| advisor_data = advisor_results[0].get("advisor_result", {}) | ||
|
|
||
| # Extract the relevant fields with defaults | ||
| response = { | ||
| "current_used_indexes": advisor_data.get("current_used_indexes", []), | ||
| "recommended_indexes": advisor_data.get("recommended_indexes", []), | ||
| "recommended_covering_indexes": advisor_data.get( | ||
| "recommended_covering_indexes", [] | ||
| ), | ||
| } | ||
|
|
||
| # Add summary information for better user experience | ||
| response["summary"] = { | ||
| "current_indexes_count": len(response["current_used_indexes"]), | ||
| "recommended_indexes_count": len(response["recommended_indexes"]), | ||
| "recommended_covering_indexes_count": len( | ||
| response["recommended_covering_indexes"] | ||
| ), | ||
| "has_recommendations": bool( | ||
| response["recommended_indexes"] | ||
| or response["recommended_covering_indexes"] | ||
| ), | ||
| } | ||
|
|
||
| logger.info( | ||
| f"Index Advisor completed. Found {response['summary']['recommended_indexes_count']} recommended indexes" | ||
| ) | ||
|
|
||
| return response | ||
|
|
||
| except Exception as e: | ||
| logger.error(f"Error running Index Advisor: {e!s}", exc_info=True) | ||
| raise | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.