Skip to content

feat(spp_demo): curated PHL geodata + spp_demo_phl_luzon demo module (re-land from #76)#277

Open
gonzalesedwin1123 wants to merge 4 commits into
19.0from
reland/phl-demo
Open

feat(spp_demo): curated PHL geodata + spp_demo_phl_luzon demo module (re-land from #76)#277
gonzalesedwin1123 wants to merge 4 commits into
19.0from
reland/phl-demo

Conversation

@gonzalesedwin1123

Copy link
Copy Markdown
Member

Re-lands the PHL demo-data portion of reverted PR #76 (revert: #271).

Summary

  • spp_demo: curated PHL shapes/areas (phl_curated.geojson, areas.xml) and demo generator updates.
  • New spp_demo_phl_luzon module: Luzon administrative areas, population weights, area loader (depends on spp_demo's loader changes).
  • scripts/prepare_phl_geodata.py: the script that generates the curated geodata.

Verification

  • ./spp t spp_demo: 121 passed, 0 failed
  • ./spp t spp_demo_phl_luzon: 17 passed, 0 failed

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request adds a geodata preparation script (prepare_phl_geodata.py) and updates the spp_demo module with refreshed Philippine geodata using PSA/HDX p-codes. It also introduces a new companion module, spp_demo_phl_luzon, which provides Luzon-scale demo areas, population weights, and an area loader. The review feedback highlights three key improvements: optimizing an N+1 query pattern in the Luzon area loader's shape-loading method, wrapping the streaming HTTP request in a context manager to prevent connection leaks, and updating the script's documented dependencies to include pandas and openpyxl so that population data processing does not silently fail.

Important

The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.

Comment on lines +157 to +179
features = geojson_data.get("features", [])
shapes_loaded = 0

for feature in features:
properties = feature.get("properties", {})
geometry = feature.get("geometry")

code = properties.get("code")
if not code or not geometry:
continue

area = self.env["spp.area"].search([("code", "=", code)], limit=1)
if not area:
continue

try:
from shapely.geometry import shape

geom = shape(geometry)
area.write({"geo_polygon": geom.wkt})
shapes_loaded += 1
except Exception as e:
_logger.warning("Could not set shape for %s: %s", code, e)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

An N+1 query pattern is present in _load_shapes(). The method performs a database search query inside the loop for every single feature in the GeoJSON file:
area = self.env["spp.area"].search([("code", "=", code)], limit=1)

With over 700 municipalities and other administrative areas in Luzon, this results in hundreds of individual database queries, significantly degrading performance.

To optimize this, all relevant spp.area records should be fetched in a single batched query before the loop, and then mapped by their code.

        features = geojson_data.get("features", [])
        shapes_loaded = 0

        # Batch search all areas to avoid N+1 database queries inside the loop
        codes = [f.get("properties", {}).get("code") for f in features if f.get("properties", {}).get("code")]
        areas = self.env["spp.area"].search([("code", "in", codes)])
        area_by_code = {area.code: area for area in areas}

        for feature in features:
            properties = feature.get("properties", {})
            geometry = feature.get("geometry")

            code = properties.get("code")
            if not code or not geometry:
                continue

            area = area_by_code.get(code)
            if not area:
                continue

            try:
                from shapely.geometry import shape

                geom = shape(geometry)
                area.write({"geo_polygon": geom.wkt})
                shapes_loaded += 1
            except Exception as e:
                _logger.warning("Could not set shape for %s: %s", code, e)
References
  1. When processing records in bulk, avoid evaluating filters or permissions individually per record to prevent O(N * M) database query patterns. Instead, perform individual checks first, and then evaluate filters in a single batched query.

Comment thread scripts/prepare_phl_geodata.py Outdated
Comment on lines +118 to +130
resp = requests.get(url, stream=True, timeout=300)
resp.raise_for_status()

total = int(resp.headers.get("content-length", 0))
downloaded = 0
with open(cached_path, "wb") as f:
for chunk in resp.iter_content(chunk_size=1024 * 1024):
f.write(chunk)
downloaded += len(chunk)
if total:
pct = downloaded * 100 // total
print(f"\r Progress: {pct}% ({downloaded // (1024 * 1024)}MB / {total // (1024 * 1024)}MB)", end="")
print()

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The requests.get call is made with stream=True but is not wrapped in a with statement or explicitly closed. When stream=True is used, the connection is kept open until all data is read or the response is closed, which can lead to a connection leak if an exception occurs or if the stream is not fully consumed.

Wrapping the request in a with statement ensures that the connection is reliably closed and released back to the pool.

Suggested change
resp = requests.get(url, stream=True, timeout=300)
resp.raise_for_status()
total = int(resp.headers.get("content-length", 0))
downloaded = 0
with open(cached_path, "wb") as f:
for chunk in resp.iter_content(chunk_size=1024 * 1024):
f.write(chunk)
downloaded += len(chunk)
if total:
pct = downloaded * 100 // total
print(f"\r Progress: {pct}% ({downloaded // (1024 * 1024)}MB / {total // (1024 * 1024)}MB)", end="")
print()
with requests.get(url, stream=True, timeout=300) as resp:
resp.raise_for_status()
total = int(resp.headers.get("content-length", 0))
downloaded = 0
with open(cached_path, "wb") as f:
for chunk in resp.iter_content(chunk_size=1024 * 1024):
f.write(chunk)
downloaded += len(chunk)
if total:
pct = downloaded * 100 // total
print(f"\r Progress: {pct}% ({downloaded // (1024 * 1024)}MB / {total // (1024 * 1024)}MB)", end="")
print()

Comment thread scripts/prepare_phl_geodata.py Outdated
Comment on lines +23 to +24
# Requires: geopandas, shapely, requests
uv run --with geopandas --with requests scripts/prepare_phl_geodata.py

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The script imports pandas and uses pd.read_excel (which requires openpyxl or xlrd as an engine) to parse the population projection data. However, the documented uv run command only specifies --with geopandas --with requests.

If a user runs the script using the documented command, the population data download will silently fail (due to the broad except Exception block catching the ImportError) and fall back to equal weights.

Updating the usage instructions to include pandas and openpyxl ensures that the population weights are correctly processed.

Suggested change
# Requires: geopandas, shapely, requests
uv run --with geopandas --with requests scripts/prepare_phl_geodata.py
# Requires: geopandas, shapely, requests, pandas, openpyxl
uv run --with geopandas --with requests --with pandas --with openpyxl scripts/prepare_phl_geodata.py

@codecov

codecov Bot commented Jul 2, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 87.85714% with 17 lines in your changes missing coverage. Please review.
✅ Project coverage is 68.94%. Comparing base (bf61488) to head (77a1aca).
⚠️ Report is 1 commits behind head on 19.0.

Files with missing lines Patch % Lines
spp_demo_phl_luzon/models/area_loader.py 84.53% 15 Missing ⚠️
spp_demo_phl_luzon/__manifest__.py 0.00% 1 Missing ⚠️
spp_demo_phl_luzon/models/population_weights.py 97.43% 1 Missing ⚠️
Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff             @@
##             19.0     #277      +/-   ##
==========================================
- Coverage   74.86%   68.94%   -5.93%     
==========================================
  Files        1093      163     -930     
  Lines       63718    14244   -49474     
==========================================
- Hits        47701     9820   -37881     
+ Misses      16017     4424   -11593     
Flag Coverage Δ
endpoint_route_handler ?
fastapi ?
spp_aggregation ?
spp_alerts ?
spp_analytics ?
spp_api_v2 ?
spp_api_v2_change_request ?
spp_api_v2_cycles ?
spp_api_v2_data ?
spp_api_v2_entitlements ?
spp_api_v2_gis ?
spp_api_v2_products ?
spp_api_v2_programs ?
spp_api_v2_service_points ?
spp_api_v2_simulation ?
spp_api_v2_vocabulary ?
spp_approval ?
spp_area ?
spp_area_hdx ?
spp_attachment_av_scan ?
spp_audit ?
spp_audit_programs ?
spp_banking ?
spp_base_common 90.26% <ø> (ø)
spp_base_setting ?
spp_case_base ?
spp_case_cel ?
spp_case_demo 94.34% <ø> (ø)
spp_case_entitlements ?
spp_case_graduation ?
spp_case_programs ?
spp_case_registry ?
spp_case_session ?
spp_cel_domain ?
spp_cel_event ?
spp_cel_registry_search ?
spp_cel_vocabulary ?
spp_change_request_v2 ?
spp_claim_169 ?
spp_cr_type_assign_program ?
spp_cr_types_advanced ?
spp_cr_types_base ?
spp_dci ?
spp_dci_client ?
spp_dci_client_dr ?
spp_dci_client_ibr ?
spp_dci_client_sr ?
spp_dci_compliance ?
spp_dci_demo 69.23% <ø> (ø)
spp_dci_indicators ?
spp_dci_server ?
spp_dci_server_social ?
spp_demo 73.63% <ø> (+0.08%) ⬆️
spp_demo_phl_luzon 87.85% <87.85%> (+0.26%) ⬆️
spp_disability_registry ?
spp_drims ?
spp_drims_sl ?
spp_drims_sl_demo ?
spp_encryption ?
spp_farmer_registry ?
spp_farmer_registry_cr ?
spp_farmer_registry_demo 60.97% <ø> (+8.23%) ⬆️
spp_farmer_registry_vocabularies ?
spp_gis ?
spp_gis_indicators ?
spp_gis_report ?
spp_graduation ?
spp_grm ?
spp_grm_case_link ?
spp_grm_demo 81.16% <ø> (+1.29%) ⬆️
spp_hazard ?
spp_hazard_programs ?
spp_hxl_area ?
spp_import_match ?
spp_indicator ?
spp_irrigation ?
spp_land_record ?
spp_metric ?
spp_metric_service ?
spp_metrics_core ?
spp_metrics_services ?
spp_mis_demo_v2 70.44% <ø> (+2.51%) ⬆️
spp_oauth ?
spp_program_geofence ?
spp_programs 65.27% <ø> (ø)
spp_registrant_gis ?
spp_registry 86.83% <ø> (ø)
spp_registry_group_hierarchy ?
spp_scoring ?
spp_scoring_programs ?
spp_security 66.66% <ø> (ø)
spp_service_points ?
spp_simulation ?
spp_starter_disability_registry ?
spp_starter_farmer_registry ?
spp_starter_social_registry ?
spp_starter_sp_mis ?
spp_statistic ?
spp_storage_backend ?
spp_studio ?
spp_studio_change_requests ?

Flags with carried forward coverage won't be shown. Click here to find out more.

Files with missing lines Coverage Δ
spp_demo/__manifest__.py 0.00% <ø> (ø)
spp_demo/models/demo_data_generator.py 70.81% <ø> (+0.12%) ⬆️
spp_demo_phl_luzon/__init__.py 100.00% <100.00%> (ø)
spp_demo_phl_luzon/models/__init__.py 100.00% <100.00%> (ø)
spp_demo_phl_luzon/__manifest__.py 0.00% <0.00%> (ø)
spp_demo_phl_luzon/models/population_weights.py 97.43% <97.43%> (ø)
spp_demo_phl_luzon/models/area_loader.py 84.53% <84.53%> (+0.49%) ⬆️

... and 934 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

… module (from #76)

Re-lands the PHL demo data portion of reverted PR #76: curated PHL
geojson shapes and areas, demo data generator and area loader test
updates, the prepare_phl_geodata.py preparation script, and the new
spp_demo_phl_luzon module (Luzon demo areas + population weights).

Files restored verbatim from the pre-revert merged state (8bf9a3a).
spp_demo bumped to 19.0.2.1.0 with a HISTORY entry.
- area_loader: batch-fetch areas by code instead of per-feature search (N+1
  over ~700 Luzon features).
- prepare_phl_geodata.py: close streamed download via context manager; usage
  docs include pandas/openpyxl needed for population weights.
@gonzalesedwin1123

Copy link
Copy Markdown
Member Author

All three gemini-code-assist findings applied (commit 91c9e06): batched area lookup replacing the per-feature N+1 search, streamed download wrapped in a context manager, and usage docs now include pandas/openpyxl. ./spp t spp_demo_phl_luzon: 17/0.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant