File size: 2,193 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
"""
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)
    }