|
| 1 | +""" |
| 2 | +Analytics Catalog tools. |
| 3 | +
|
| 4 | +Tools: |
| 5 | + - list_epochs – recent AnalysisEpoch records |
| 6 | + - get_epoch – full detail for one epoch |
| 7 | + - query_executions – paginated execution search with filters |
| 8 | + - get_lineage – complete lineage for an execution |
| 9 | + - get_catalog_stats – summary statistics |
| 10 | +""" |
| 11 | + |
| 12 | +from typing import Optional |
| 13 | + |
| 14 | +from ..server import mcp |
| 15 | + |
| 16 | + |
| 17 | +def _get_catalog(): |
| 18 | + """Build an AnalysisCatalog connected to the current database.""" |
| 19 | + from graph_analytics_ai.db_connection import get_db_connection |
| 20 | + from graph_analytics_ai.catalog import AnalysisCatalog |
| 21 | + from graph_analytics_ai.catalog.storage import ArangoDBStorage |
| 22 | + |
| 23 | + db = get_db_connection() |
| 24 | + storage = ArangoDBStorage(db) |
| 25 | + return AnalysisCatalog(storage), storage |
| 26 | + |
| 27 | + |
| 28 | +# --------------------------------------------------------------------------- |
| 29 | +# list_epochs |
| 30 | +# --------------------------------------------------------------------------- |
| 31 | +@mcp.tool() |
| 32 | +def list_epochs(limit: int = 20) -> list: |
| 33 | + """List the most recent analysis epochs tracked in the catalog. |
| 34 | +
|
| 35 | + Args: |
| 36 | + limit: Maximum number of epochs to return (default 20). |
| 37 | +
|
| 38 | + Returns a list of epoch summary dicts (id, name, status, created_at). |
| 39 | + """ |
| 40 | + from graph_analytics_ai.catalog import CatalogQueries, EpochFilter |
| 41 | + |
| 42 | + _, storage = _get_catalog() |
| 43 | + queries = CatalogQueries(storage) |
| 44 | + result = queries.query_with_pagination( |
| 45 | + filter=EpochFilter(), |
| 46 | + page=1, |
| 47 | + page_size=limit, |
| 48 | + ) |
| 49 | + epochs = result.items if hasattr(result, "items") else result |
| 50 | + out = [] |
| 51 | + for e in epochs: |
| 52 | + out.append( |
| 53 | + { |
| 54 | + "id": getattr(e, "id", None) or getattr(e, "epoch_id", None), |
| 55 | + "name": getattr(e, "name", None), |
| 56 | + "status": str(getattr(e, "status", "")), |
| 57 | + "created_at": str(getattr(e, "created_at", "")), |
| 58 | + "execution_count": getattr(e, "execution_count", None), |
| 59 | + } |
| 60 | + ) |
| 61 | + return out |
| 62 | + |
| 63 | + |
| 64 | +# --------------------------------------------------------------------------- |
| 65 | +# get_epoch |
| 66 | +# --------------------------------------------------------------------------- |
| 67 | +@mcp.tool() |
| 68 | +def get_epoch(epoch_id: str) -> dict: |
| 69 | + """Get full details for a single analysis epoch. |
| 70 | +
|
| 71 | + Args: |
| 72 | + epoch_id: The ID of the epoch to retrieve. |
| 73 | + """ |
| 74 | + catalog, _ = _get_catalog() |
| 75 | + epoch = catalog.get_epoch(epoch_id) |
| 76 | + if epoch is None: |
| 77 | + return {"error": f"Epoch {epoch_id!r} not found"} |
| 78 | + return { |
| 79 | + "id": getattr(epoch, "id", None) or getattr(epoch, "epoch_id", None), |
| 80 | + "name": getattr(epoch, "name", None), |
| 81 | + "status": str(getattr(epoch, "status", "")), |
| 82 | + "tags": getattr(epoch, "tags", []), |
| 83 | + "created_at": str(getattr(epoch, "created_at", "")), |
| 84 | + "metadata": getattr(epoch, "metadata", {}), |
| 85 | + } |
| 86 | + |
| 87 | + |
| 88 | +# --------------------------------------------------------------------------- |
| 89 | +# query_executions |
| 90 | +# --------------------------------------------------------------------------- |
| 91 | +@mcp.tool() |
| 92 | +def query_executions( |
| 93 | + algorithm: Optional[str] = None, |
| 94 | + epoch_id: Optional[str] = None, |
| 95 | + status: Optional[str] = None, |
| 96 | + page: int = 1, |
| 97 | + page_size: int = 20, |
| 98 | +) -> dict: |
| 99 | + """Search execution records in the analytics catalog. |
| 100 | +
|
| 101 | + Args: |
| 102 | + algorithm: Filter by algorithm name (e.g. 'pagerank', 'wcc'). |
| 103 | + epoch_id: Filter by epoch ID. |
| 104 | + status: Filter by execution status string (e.g. 'completed', 'failed'). |
| 105 | + page: Page number (1-indexed, default 1). |
| 106 | + page_size: Results per page (default 20). |
| 107 | +
|
| 108 | + Returns a dict with keys: items, total, page, page_size. |
| 109 | + """ |
| 110 | + from graph_analytics_ai.catalog import CatalogQueries, ExecutionFilter |
| 111 | + |
| 112 | + _, storage = _get_catalog() |
| 113 | + queries = CatalogQueries(storage) |
| 114 | + |
| 115 | + f = ExecutionFilter() |
| 116 | + if algorithm: |
| 117 | + f.algorithm = algorithm |
| 118 | + if epoch_id: |
| 119 | + f.epoch_id = epoch_id |
| 120 | + if status: |
| 121 | + f.status = status |
| 122 | + |
| 123 | + result = queries.query_with_pagination(filter=f, page=page, page_size=page_size) |
| 124 | + items = result.items if hasattr(result, "items") else result |
| 125 | + return { |
| 126 | + "items": [ |
| 127 | + { |
| 128 | + "id": getattr(e, "id", None) or getattr(e, "execution_id", None), |
| 129 | + "algorithm": getattr(e, "algorithm", None), |
| 130 | + "status": str(getattr(e, "status", "")), |
| 131 | + "epoch_id": getattr(e, "epoch_id", None), |
| 132 | + "started_at": str(getattr(e, "started_at", "")), |
| 133 | + "duration_ms": getattr(e, "duration_ms", None), |
| 134 | + } |
| 135 | + for e in items |
| 136 | + ], |
| 137 | + "total": getattr(result, "total", len(items)), |
| 138 | + "page": page, |
| 139 | + "page_size": page_size, |
| 140 | + } |
| 141 | + |
| 142 | + |
| 143 | +# --------------------------------------------------------------------------- |
| 144 | +# get_lineage |
| 145 | +# --------------------------------------------------------------------------- |
| 146 | +@mcp.tool() |
| 147 | +def get_lineage(execution_id: str) -> dict: |
| 148 | + """Get the complete lineage chain for an execution. |
| 149 | +
|
| 150 | + Traces backwards through: Execution → Template → Use Case → Requirements. |
| 151 | +
|
| 152 | + Args: |
| 153 | + execution_id: The ID of the execution to trace lineage for. |
| 154 | + """ |
| 155 | + from graph_analytics_ai.catalog import LineageTracker |
| 156 | + from graph_analytics_ai.catalog.storage import ArangoDBStorage |
| 157 | + from graph_analytics_ai.db_connection import get_db_connection |
| 158 | + |
| 159 | + db = get_db_connection() |
| 160 | + storage = ArangoDBStorage(db) |
| 161 | + tracker = LineageTracker(storage) |
| 162 | + lineage = tracker.get_complete_lineage(execution_id) |
| 163 | + |
| 164 | + if lineage is None: |
| 165 | + return {"error": f"No lineage found for execution {execution_id!r}"} |
| 166 | + |
| 167 | + return ( |
| 168 | + lineage.to_dict() |
| 169 | + if hasattr(lineage, "to_dict") |
| 170 | + else {"execution_id": execution_id, "lineage": str(lineage)} |
| 171 | + ) |
| 172 | + |
| 173 | + |
| 174 | +# --------------------------------------------------------------------------- |
| 175 | +# get_catalog_stats |
| 176 | +# --------------------------------------------------------------------------- |
| 177 | +@mcp.tool() |
| 178 | +def get_catalog_stats() -> dict: |
| 179 | + """Return summary statistics for the analytics catalog. |
| 180 | +
|
| 181 | + Includes epoch count, execution count, algorithm breakdown, and more. |
| 182 | + """ |
| 183 | + from graph_analytics_ai.catalog import CatalogManager |
| 184 | + from graph_analytics_ai.catalog.storage import ArangoDBStorage |
| 185 | + from graph_analytics_ai.db_connection import get_db_connection |
| 186 | + |
| 187 | + db = get_db_connection() |
| 188 | + storage = ArangoDBStorage(db) |
| 189 | + manager = CatalogManager(storage) |
| 190 | + stats = manager.get_statistics() if hasattr(manager, "get_statistics") else {} |
| 191 | + |
| 192 | + return stats if isinstance(stats, dict) else (stats.to_dict() if hasattr(stats, "to_dict") else str(stats)) |
0 commit comments