Spaces:
Running
Running
File size: 10,106 Bytes
bad6218 | 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 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 | """Metadata cache for indicators and cube mappings."""
import asyncio
import os
from datetime import datetime, timedelta
from typing import Any
from .api_client import CubeJsClient, get_client
from .cube_resolver import CubeResolver, get_resolver
from .models import IndicatorMetadata, IndicatorListItem
class IndicatorCache:
"""Cache for indicator metadata and cube resolution.
This cache stores indicator metadata loaded at startup and periodically
refreshes to pick up new indicators. It also initializes the CubeResolver
for mapping indicator IDs to data cubes.
"""
def __init__(
self,
refresh_interval_seconds: int | None = None,
):
"""Initialize the cache.
Args:
refresh_interval_seconds: How often to refresh the cache.
Defaults to CACHE_REFRESH_SECONDS env var or 3600 (1 hour).
"""
self.refresh_interval = timedelta(
seconds=refresh_interval_seconds
or int(os.getenv("CACHE_REFRESH_SECONDS", "3600"))
)
# Indicator metadata by ID
self._indicators: dict[int, IndicatorMetadata] = {}
# Reference to the cube resolver
self._resolver: CubeResolver = get_resolver()
# Last refresh timestamp
self._last_refresh: datetime | None = None
# Lock for thread-safe refresh
self._refresh_lock = asyncio.Lock()
# Flag to indicate if initial load is complete
self._initialized = False
@property
def is_initialized(self) -> bool:
"""Check if the cache has been initialized."""
return self._initialized
@property
def needs_refresh(self) -> bool:
"""Check if the cache needs to be refreshed."""
if not self._initialized or self._last_refresh is None:
return True
return datetime.now() - self._last_refresh > self.refresh_interval
@property
def indicators(self) -> dict[int, IndicatorMetadata]:
"""Get all cached indicators."""
return self._indicators.copy()
@property
def resolver(self) -> CubeResolver:
"""Get the cube resolver instance."""
return self._resolver
async def initialize(self, client: CubeJsClient | None = None) -> None:
"""Initialize the cache with data from the API.
This should be called at application startup.
Args:
client: Optional CubeJsClient instance. If not provided,
uses the singleton instance.
"""
if client is None:
client = get_client()
await self.refresh(client)
async def refresh(self, client: CubeJsClient | None = None) -> None:
"""Refresh the cache from the API.
Args:
client: Optional CubeJsClient instance.
"""
async with self._refresh_lock:
if client is None:
client = get_client()
# Load indicator metadata
await self._load_indicators(client)
# Load and parse /meta for cube resolution
await self._load_cube_metadata(client)
self._last_refresh = datetime.now()
self._initialized = True
async def _load_indicators(self, client: CubeJsClient) -> None:
"""Load all indicator metadata from the API."""
# Note: Some dimensions listed in /meta may not exist in actual data
# Only include dimensions that have been validated to work
dimensions = [
"indicateur_metadata.id",
"indicateur_metadata.libelle",
"indicateur_metadata.unite",
"indicateur_metadata.description",
"indicateur_metadata.methode_calcul",
"indicateur_metadata.annees_disponibles",
"indicateur_metadata.mailles_disponibles",
"indicateur_metadata.maille_mini_disponible",
"indicateur_metadata.couverture_geographique",
"indicateur_metadata.completion_region",
"indicateur_metadata.completion_departement",
"indicateur_metadata.completion_epci",
"indicateur_metadata.completion_commune",
"indicateur_metadata.thematique_fnv",
# Note: secteur_fnv, enjeux_fnv, levier_fnv cause errors despite being in schema
]
data = await client.load_indicators_metadata(
dimensions=dimensions,
limit=1000, # Should be enough for all indicators
)
self._indicators.clear()
for row in data:
try:
indicator = IndicatorMetadata.from_api_response(row)
self._indicators[indicator.id] = indicator
except Exception as e:
# Log but don't fail on individual indicator parsing errors
print(f"Warning: Failed to parse indicator: {e}")
async def _load_cube_metadata(self, client: CubeJsClient) -> None:
"""Load cube metadata from /meta and initialize the resolver."""
meta = await client.get_meta()
self._resolver.load_from_meta(meta)
def get_indicator(self, indicator_id: int) -> IndicatorMetadata | None:
"""Get indicator metadata by ID.
Args:
indicator_id: The indicator ID.
Returns:
The indicator metadata, or None if not found.
"""
return self._indicators.get(indicator_id)
def get_cube_name(self, indicator_id: int, maille: str) -> str | None:
"""Get the data cube name for an indicator at a specific maille.
Args:
indicator_id: The indicator ID.
maille: The geographic level.
Returns:
The cube name, or None if not found.
"""
return self._resolver.find_cube_for_indicator(indicator_id, maille)
def list_indicators(
self,
thematique: str | None = None,
maille: str | None = None,
) -> list[IndicatorListItem]:
"""List indicators with optional filtering.
Args:
thematique: Filter by thematique_fnv (case-insensitive partial match).
maille: Filter by available geographic level.
Returns:
List of matching indicators.
"""
results = []
for indicator in self._indicators.values():
# Apply thematique filter
if thematique:
if not indicator.thematique_fnv:
continue
if thematique.lower() not in indicator.thematique_fnv.lower():
continue
# Apply maille filter
if maille:
if not indicator.has_geographic_level(maille):
continue
results.append(
IndicatorListItem(
id=indicator.id,
libelle=indicator.libelle,
unite=indicator.unite,
mailles_disponibles=indicator.mailles_disponibles,
thematique_fnv=indicator.thematique_fnv,
)
)
# Sort by ID for consistent ordering
results.sort(key=lambda x: x.id)
return results
def search_indicators(self, query: str) -> list[IndicatorListItem]:
"""Search indicators by keyword.
Searches in libelle and description fields (case-insensitive).
Args:
query: Search query string.
Returns:
List of matching indicators.
"""
if not query or not query.strip():
return self.list_indicators()
query_lower = query.lower().strip()
query_words = query_lower.split()
results = []
for indicator in self._indicators.values():
# Search in libelle and description
searchable = " ".join(
filter(None, [indicator.libelle, indicator.description])
).lower()
# Check if all query words are present
if all(word in searchable for word in query_words):
results.append(
IndicatorListItem(
id=indicator.id,
libelle=indicator.libelle,
unite=indicator.unite,
mailles_disponibles=indicator.mailles_disponibles,
thematique_fnv=indicator.thematique_fnv,
)
)
# Sort by relevance (exact match in libelle first, then by ID)
def sort_key(item: IndicatorListItem) -> tuple[int, int]:
exact_match = 0 if query_lower in item.libelle.lower() else 1
return (exact_match, item.id)
results.sort(key=sort_key)
return results
# Singleton cache instance
_cache_instance: IndicatorCache | None = None
def get_cache() -> IndicatorCache:
"""Get or create the singleton IndicatorCache instance.
Returns:
The shared IndicatorCache instance.
"""
global _cache_instance
if _cache_instance is None:
_cache_instance = IndicatorCache()
return _cache_instance
async def initialize_cache(client: CubeJsClient | None = None) -> IndicatorCache:
"""Initialize the singleton cache.
This should be called at application startup.
Args:
client: Optional CubeJsClient instance.
Returns:
The initialized cache.
"""
cache = get_cache()
if not cache.is_initialized:
await cache.initialize(client)
return cache
async def refresh_cache_if_needed(client: CubeJsClient | None = None) -> None:
"""Refresh the cache if it's stale.
Args:
client: Optional CubeJsClient instance.
"""
cache = get_cache()
if cache.needs_refresh:
await cache.refresh(client)
|