Skip to content

Commit

Permalink
Force schema update, remove nested country label model
Browse files Browse the repository at this point in the history
  • Loading branch information
Zaczero committed Feb 19, 2024
1 parent 924f23c commit 8b01909
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 17 deletions.
2 changes: 1 addition & 1 deletion api/v1/tile.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ async def count_task(country: Country) -> None:
'name': 'defibrillators',
'features': [
{
'geometry': country.label.position,
'geometry': country.label_position,
'properties': {
'country_name': country.name,
'country_code': country.code,
Expand Down
8 changes: 1 addition & 7 deletions models/country.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,13 @@
from validators.geometry import GeometrySerializer, GeometryValidator


class CountryLabel(BaseModel):
model_config = ConfigDict(frozen=True, strict=True)

position: Annotated[Point, GeometryValidator, GeometrySerializer]


class Country(BaseModel):
model_config = ConfigDict(frozen=True, strict=True)

names: dict[str, str]
code: str
geometry: Annotated[BaseGeometry, GeometryValidator, GeometrySerializer]
label: CountryLabel
label_position: Annotated[Point, GeometryValidator, GeometrySerializer]

@property
def name(self) -> str:
Expand Down
4 changes: 2 additions & 2 deletions states/aed_state.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
@trace
async def _should_update_db() -> tuple[bool, float]:
doc = await get_state_doc('aed')
if doc is None or doc.get('version', 1) < 2:
if doc is None or doc.get('version', 1) < 3:
return True, 0

update_timestamp: float = doc['update_timestamp']
Expand Down Expand Up @@ -170,7 +170,7 @@ async def _update_db_diffs(last_update: float) -> None:
# keep transaction as short as possible: avoid doing any computation inside
async with Transaction() as s:
await AED_COLLECTION.bulk_write(bulk_write_arg, ordered=True, session=s)
await set_state_doc('aed', {'update_timestamp': data_timestamp, 'version': 2}, session=s)
await set_state_doc('aed', {'update_timestamp': data_timestamp, 'version': 3}, session=s)

if aeds:
print('🩺 Updating country codes')
Expand Down
13 changes: 6 additions & 7 deletions states/country_state.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

from config import COUNTRY_COLLECTION, COUNTRY_UPDATE_DELAY
from models.bbox import BBox
from models.country import Country, CountryLabel
from models.country import Country
from osm_countries import get_osm_countries
from state_utils import get_state_doc, set_state_doc
from transaction import Transaction
Expand Down Expand Up @@ -61,7 +61,7 @@ def _get_names(tags: dict[str, str]) -> dict[str, str]:
@trace
async def _should_update_db() -> tuple[bool, float]:
doc = await get_state_doc('country')
if doc is None:
if doc is None or doc.get('version', 1) < 2:
return True, 0

update_timestamp: float = doc['update_timestamp']
Expand Down Expand Up @@ -100,15 +100,15 @@ async def _update_db() -> None:
names=_get_names(c.tags),
code=country_code_assigner.get_unique(c.tags),
geometry=c.geometry,
label=CountryLabel(position=c.representative_point),
label_position=c.representative_point,
)
insert_many_arg.append(country.model_dump())

# keep transaction as short as possible: avoid doing any computation inside
async with Transaction() as s:
await COUNTRY_COLLECTION.delete_many({}, session=s)
await COUNTRY_COLLECTION.insert_many(insert_many_arg, session=s)
await set_state_doc('country', {'update_timestamp': data_timestamp}, session=s)
await set_state_doc('country', {'update_timestamp': data_timestamp, 'version': 2}, session=s)

shape_cache.clear()

Expand Down Expand Up @@ -147,14 +147,13 @@ async def get_all_countries(filter: dict | None = None) -> Sequence[Country]:
cached = shape_cache.get(doc['code'])
if cached is None:
geometry = geometry_validator(doc['geometry'])
label_position = geometry_validator(doc['label']['position'])
label_position = geometry_validator(doc['label_position'])
shape_cache[doc['code']] = (geometry, label_position)
else:
geometry, label_position = cached

doc['geometry'] = geometry
doc['label']['position'] = label_position
doc['label'] = CountryLabel.model_construct(**doc['label'])
doc['label_position'] = label_position
result.append(Country.model_construct(**doc))

return result
Expand Down

0 comments on commit 8b01909

Please sign in to comment.