GerardCB's picture
Deploy to Spaces (Final Clean)
4851501
"""
Schema endpoint - Provides data catalog information to users.
Shows available tables, columns, and data descriptions.
"""
from fastapi import APIRouter
from pydantic import BaseModel
from typing import Optional, List, Any
from backend.core.data_catalog import get_data_catalog
router = APIRouter()
class ColumnInfo(BaseModel):
name: str
type: str
description: Optional[str] = None
class TableInfo(BaseModel):
name: str
description: str
row_count: int
columns: List[ColumnInfo]
class SchemaResponse(BaseModel):
tables: List[TableInfo]
last_updated: str
data_source: str
@router.get("/", response_model=SchemaResponse)
async def get_schema():
"""
Returns the dynamic data catalog with all available tables and their schemas.
"""
catalog = get_data_catalog()
tables = []
for table_name, meta in catalog.catalog.items():
# Map catalog columns to Schema columns
# Catalog columns are just a list of strings usually
cols = []
raw_cols = meta.get("columns", [])
# Helper to guess type
def guess_type(col_name):
if col_name == "geom": return "geometry"
if "id" in col_name: return "integer"
if "name" in col_name: return "text"
return "text" # Default
for col in raw_cols:
cols.append(ColumnInfo(
name=col,
type=guess_type(col),
description=None
))
tables.append(TableInfo(
name=table_name,
description=meta.get("semantic_description") or meta.get("description", ""),
row_count=meta.get("row_count") or 0,
columns=cols
))
return SchemaResponse(
tables=tables,
last_updated="Dynamic",
data_source="GeoQuery Data Catalog (OSM, Overture, HDX, INEC)"
)
@router.get("/tables")
async def list_tables():
"""
Returns a simple list of available table names.
"""
catalog = get_data_catalog()
return {
"tables": list(catalog.catalog.keys()),
"count": len(catalog.catalog)
}