Optimize assistant context by requested themes
This commit is contained in:
@@ -23,6 +23,7 @@ from app.services.geo_assistant_service import GeoAssistantService
|
||||
from app.services.thematic_raster_acquisition_service import ThematicRasterAcquisitionService
|
||||
from app.services.thematic_raster_analysis_service import ThematicRasterAnalysisService
|
||||
from app.services.dataset_service import DatasetService
|
||||
from app.services.vector_feature_service import VectorFeatureService
|
||||
|
||||
|
||||
ROOT = Path(__file__).resolve().parents[2]
|
||||
@@ -277,6 +278,104 @@ def test_assistant_context_receives_persisted_thematic_metrics(tmp_path) -> None
|
||||
assert context["rules"]["thematic_policy_rasters_available"] is True
|
||||
|
||||
|
||||
def test_assistant_context_skips_unrequested_expensive_themes(monkeypatch) -> None:
|
||||
project_id = uuid4()
|
||||
soil_id, agriculture_id = uuid4(), uuid4()
|
||||
population_id, space_id = uuid4(), uuid4()
|
||||
project = Project(id=project_id, name="Kempen", region="Kempen")
|
||||
datasets = [
|
||||
Dataset(
|
||||
id=soil_id,
|
||||
project_id=project_id,
|
||||
name="soil.geojson",
|
||||
dataset_type="vector",
|
||||
source="official",
|
||||
source_name="dov",
|
||||
source_metadata={"theme": "soil"},
|
||||
status="ready",
|
||||
),
|
||||
Dataset(
|
||||
id=agriculture_id,
|
||||
project_id=project_id,
|
||||
name="agriculture.geojson",
|
||||
dataset_type="vector",
|
||||
source="official",
|
||||
source_name="lv",
|
||||
source_metadata={"theme": "agriculture"},
|
||||
status="ready",
|
||||
),
|
||||
Dataset(
|
||||
id=population_id,
|
||||
project_id=project_id,
|
||||
name="population.tif",
|
||||
dataset_type="raster",
|
||||
source="official",
|
||||
source_name=ThematicRasterAcquisitionService.PROVIDER,
|
||||
source_metadata={"product_key": "population_density_2019"},
|
||||
status="ready",
|
||||
),
|
||||
Dataset(
|
||||
id=space_id,
|
||||
project_id=project_id,
|
||||
name="space.tif",
|
||||
dataset_type="raster",
|
||||
source="official",
|
||||
source_name=ThematicRasterAcquisitionService.PROVIDER,
|
||||
source_metadata={"product_key": "space_occupation_2025"},
|
||||
status="ready",
|
||||
),
|
||||
]
|
||||
db = FakeSession({(Project, project_id): project}, query_result=datasets)
|
||||
summarized: list = []
|
||||
analyzed: list = []
|
||||
|
||||
def summarize(_db, *, dataset, **_kwargs):
|
||||
summarized.append(dataset.id)
|
||||
return {
|
||||
"metric_label": "Gekarteerde bodemoppervlakte",
|
||||
"metric_value": 12.5,
|
||||
"metric_unit": "ha",
|
||||
"is_estimate": False,
|
||||
"warning": "Historische bodemkaart",
|
||||
}
|
||||
|
||||
def analyze(_db, _project_id, dataset_id, _payload, **_kwargs):
|
||||
analyzed.append(dataset_id)
|
||||
return {
|
||||
"theme": "population",
|
||||
"summary": {
|
||||
"metrics": [
|
||||
{
|
||||
"metric_label": "Geraamd aantal inwoners (2019)",
|
||||
"metric_value": 100.0,
|
||||
"metric_unit": "inwoners",
|
||||
"is_estimate": True,
|
||||
}
|
||||
]
|
||||
},
|
||||
"unsupported_metrics": ["current_population"],
|
||||
"limitation_message": "Rasterraming",
|
||||
}
|
||||
|
||||
monkeypatch.setattr(VectorFeatureService, "summarize_features_by_bbox", summarize)
|
||||
monkeypatch.setattr(ThematicRasterAnalysisService, "analyze", analyze)
|
||||
|
||||
context, metrics, _series, dataset_ids, _warnings, _scope = GeoAssistantService(Settings(_env_file=None))._build_context(
|
||||
db,
|
||||
project_id=project_id,
|
||||
payload=AssistantQueryRequest(
|
||||
question="Hoeveel inwoners zijn er en welke bodemtypes komen voor?",
|
||||
bbox=payload("population_density_2019", side_m=200.0).bbox,
|
||||
),
|
||||
)
|
||||
|
||||
assert summarized == [soil_id]
|
||||
assert analyzed == [population_id]
|
||||
assert {metric.theme for metric in metrics} == {"soil", "population"}
|
||||
assert set(dataset_ids) == {soil_id, population_id}
|
||||
assert context["scope"]["requested_themes"] == ["population", "soil"]
|
||||
|
||||
|
||||
def test_index_renderer_returns_browser_png(tmp_path) -> None:
|
||||
project_id, dataset_id = uuid4(), uuid4()
|
||||
values = np.linspace(0.1, 4.0, 100, dtype="float32").reshape((10, 10))
|
||||
|
||||
Reference in New Issue
Block a user