File size: 7,216 Bytes
4851501 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 |
"""
Catalog Management Endpoints
Provides API for viewing and enriching the data catalog.
"""
from fastapi import APIRouter, HTTPException, BackgroundTasks
from pydantic import BaseModel
from typing import List, Optional, Dict, Any
router = APIRouter()
class CatalogStatsResponse(BaseModel):
total_datasets: int
enriched_datasets: int
by_category: Dict[str, int]
by_tag: Dict[str, int]
class TableMetadataResponse(BaseModel):
name: str
path: str
description: str
semantic_description: Optional[str]
tags: List[str]
data_type: str
columns: List[str]
row_count: Optional[int]
category: str
last_indexed: Optional[str]
last_enriched: Optional[str]
class EnrichmentRequest(BaseModel):
table_names: Optional[List[str]] = None # None = all tables
force_refresh: bool = False
class EnrichmentResponse(BaseModel):
status: str
message: str
tables_queued: int
@router.get("/stats", response_model=CatalogStatsResponse)
async def get_catalog_stats():
"""Get statistics about the data catalog."""
from backend.core.data_catalog import get_data_catalog
catalog = get_data_catalog()
stats = catalog.get_stats()
return CatalogStatsResponse(
total_datasets=stats["total_datasets"],
enriched_datasets=stats.get("enriched_datasets", 0),
by_category=stats["by_category"],
by_tag=stats["by_tag"]
)
@router.get("/tables", response_model=List[TableMetadataResponse])
async def list_catalog_tables():
"""List all tables in the catalog with their metadata."""
from backend.core.data_catalog import get_data_catalog
catalog = get_data_catalog()
tables = []
for name, meta in catalog.catalog.items():
tables.append(TableMetadataResponse(
name=name,
path=meta.get("path", ""),
description=meta.get("description", ""),
semantic_description=meta.get("semantic_description"),
tags=meta.get("tags", []),
data_type=meta.get("data_type", "static"),
columns=meta.get("columns", []),
row_count=meta.get("row_count"),
category=meta.get("category", "unknown"),
last_indexed=meta.get("last_indexed"),
last_enriched=meta.get("last_enriched")
))
return tables
@router.get("/tables/{table_name}", response_model=TableMetadataResponse)
async def get_table_metadata(table_name: str):
"""Get metadata for a specific table."""
from backend.core.data_catalog import get_data_catalog
catalog = get_data_catalog()
meta = catalog.get_table_metadata(table_name)
if not meta:
raise HTTPException(status_code=404, detail=f"Table '{table_name}' not found")
return TableMetadataResponse(
name=table_name,
path=meta.get("path", ""),
description=meta.get("description", ""),
semantic_description=meta.get("semantic_description"),
tags=meta.get("tags", []),
data_type=meta.get("data_type", "static"),
columns=meta.get("columns", []),
row_count=meta.get("row_count"),
category=meta.get("category", "unknown"),
last_indexed=meta.get("last_indexed"),
last_enriched=meta.get("last_enriched")
)
@router.post("/enrich", response_model=EnrichmentResponse)
async def enrich_catalog(request: EnrichmentRequest, background_tasks: BackgroundTasks):
"""
Trigger LLM enrichment for catalog tables.
Enrichment generates semantic descriptions and refined tags.
Runs in the background to avoid blocking.
"""
from backend.core.data_catalog import get_data_catalog
catalog = get_data_catalog()
if request.table_names:
# Validate table names
invalid = [t for t in request.table_names if t not in catalog.catalog]
if invalid:
raise HTTPException(
status_code=400,
detail=f"Unknown tables: {invalid}"
)
tables_to_enrich = request.table_names
else:
tables_to_enrich = list(catalog.catalog.keys())
# Queue enrichment in background
async def run_enrichment():
for table_name in tables_to_enrich:
await catalog.enrich_table(table_name, request.force_refresh)
background_tasks.add_task(run_enrichment)
return EnrichmentResponse(
status="queued",
message=f"Enrichment started for {len(tables_to_enrich)} tables",
tables_queued=len(tables_to_enrich)
)
@router.post("/enrich/{table_name}")
async def enrich_single_table(table_name: str, force: bool = False):
"""
Immediately enrich a single table (synchronous).
Use for testing or when you need the result right away.
"""
from backend.core.data_catalog import get_data_catalog
catalog = get_data_catalog()
if table_name not in catalog.catalog:
raise HTTPException(status_code=404, detail=f"Table '{table_name}' not found")
success = await catalog.enrich_table(table_name, force)
if success:
meta = catalog.get_table_metadata(table_name)
return {
"status": "success",
"table": table_name,
"semantic_description": meta.get("semantic_description"),
"tags": meta.get("tags", [])
}
else:
raise HTTPException(status_code=500, detail=f"Failed to enrich table '{table_name}'")
@router.get("/search")
async def search_tables(query: str, top_k: int = 10):
"""
Search for tables using semantic search.
Returns the most relevant tables for a natural language query.
"""
from backend.core.semantic_search import get_semantic_search
from backend.core.data_catalog import get_data_catalog
semantic = get_semantic_search()
catalog = get_data_catalog()
results = semantic.search(query, top_k=top_k)
response = []
for table_name, score in results:
meta = catalog.get_table_metadata(table_name)
if meta:
response.append({
"table": table_name,
"score": round(score, 4),
"description": meta.get("semantic_description") or meta.get("description"),
"tags": meta.get("tags", [])
})
return {"query": query, "results": response}
@router.post("/rebuild-embeddings")
async def rebuild_embeddings():
"""
Rebuild all semantic search embeddings from current catalog.
Use after bulk enrichment or catalog updates.
"""
from backend.core.semantic_search import get_semantic_search
from backend.core.data_catalog import get_data_catalog
semantic = get_semantic_search()
catalog = get_data_catalog()
# Force re-embed all tables
count = 0
for table_name, metadata in catalog.catalog.items():
if semantic.embed_table(table_name, metadata):
count += 1
semantic._save_embeddings()
return {
"status": "success",
"message": f"Rebuilt embeddings for {count} tables",
"total_embeddings": len(semantic.embeddings)
}
|