Skip to content

Commit

Permalink
Node versions
Browse files Browse the repository at this point in the history
  • Loading branch information
Zaczero committed Feb 13, 2024
1 parent 69b6d41 commit 8b670f9
Show file tree
Hide file tree
Showing 8 changed files with 26 additions and 21 deletions.
7 changes: 6 additions & 1 deletion api/v1/countries.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,12 @@ async def get_geojson(
{
'type': 'Feature',
'geometry': mapping(aed.position.shapely),
'properties': {'@osm_type': 'node', '@osm_id': int(aed.id), **aed.tags},
'properties': {
'@osm_type': 'node',
'@osm_id': aed.id,
'@osm_version': aed.version,
**aed.tags,
},
}
for aed in aeds
],
Expand Down
4 changes: 2 additions & 2 deletions api/v1/node.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,11 +96,11 @@ async def get_node(node_id: str, aed_state: AEDStateDep, photo_state: PhotoState
**photo_dict,
**timezone_dict,
'type': 'node',
'id': int(aed.id),
'id': aed.id,
'lat': aed.position.lat,
'lon': aed.position.lon,
'tags': aed.tags,
'version': 0,
'version': aed.version,
}
],
}
2 changes: 1 addition & 1 deletion api/v1/tile.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ async def _get_tile_aed(z: int, bbox: BBox, aed_state: AEDState) -> bytes:
{
'geometry': aed.position.shapely,
'properties': {
'node_id': int(aed.id),
'node_id': aed.id,
'access': aed.access,
},
}
Expand Down
1 change: 0 additions & 1 deletion config.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@

NAME = 'openaedmap-backend'
VERSION = '2.6.1'
VERSION_TIMESTAMP = 0
CREATED_BY = f'{NAME} {VERSION}'
WEBSITE = 'https://openaedmap.org'
USER_AGENT = f'{NAME}/{VERSION} (+{WEBSITE})'
Expand Down
3 changes: 2 additions & 1 deletion models/aed.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@

@dataclass(frozen=True, slots=True)
class AED:
id: str
id: int
position: LonLat
country_codes: list[str] | None
tags: dict[str, str]
version: int

@property
def access(self) -> str:
Expand Down
1 change: 0 additions & 1 deletion shell.nix
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ let
# -- Misc
(writeShellScriptBin "make-version" ''
sed -i -r "s|VERSION = '([0-9.]+)'|VERSION = '\1.$(date +%y%m%d)'|g" config.py
sed -i -r "s|VERSION_TIMESTAMP = ([0-9.]+)|VERSION_TIMESTAMP = $(date +%s)|g" config.py
'')
] ++ lib.optionals isDevelopment [
# Development packages
Expand Down
24 changes: 14 additions & 10 deletions states/aed_state.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,15 @@
from transaction import Transaction
from utils import as_dict, print_run_time, retry_exponential

_AED_QUERY = 'node[emergency=defibrillator];out body qt;'
_AED_QUERY = 'node[emergency=defibrillator];out meta qt;'


async def _should_update_db() -> tuple[bool, float]:
doc = await get_state_doc('aed')
if doc is None:
if doc is None or doc.get('version', 1) < 2:
return True, 0

update_timestamp = doc['update_timestamp']
# if update_timestamp < VERSION_TIMESTAMP:
# return True, update_timestamp

update_timestamp: float = doc['update_timestamp']
update_age = time() - update_timestamp
if update_age > AED_UPDATE_DELAY.total_seconds():
return True, update_timestamp
Expand Down Expand Up @@ -92,7 +89,13 @@ def _process_overpass_node(node: dict) -> AED:
tags = node.get('tags', {})
is_valid = _is_defibrillator(tags)
assert is_valid, 'Unexpected non-defibrillator node'
return AED(id=str(node['id']), position=LonLat(node['lon'], node['lat']), country_codes=None, tags=tags)
return AED(
id=node['id'],
position=LonLat(node['lon'], node['lat']),
country_codes=None,
tags=tags,
version=node['version'],
)


async def _update_db_snapshot() -> None:
Expand All @@ -104,7 +107,7 @@ async def _update_db_snapshot() -> None:
async with Transaction() as s:
await AED_COLLECTION.delete_many({}, session=s)
await AED_COLLECTION.insert_many(insert_many_arg, session=s)
await set_state_doc('aed', {'update_timestamp': data_timestamp}, session=s)
await set_state_doc('aed', {'update_timestamp': data_timestamp, 'version': 2}, session=s)

if aeds:
print('🩺 Updating country codes')
Expand All @@ -122,13 +125,14 @@ def _process_action(action: dict) -> Iterable[AED | str]:
def _process_create_or_modify(node: dict) -> AED | str:
node_tags = _parse_xml_tags(node)
node_valid = _is_defibrillator(node_tags)
node_id = str(node['@id'])
node_id = int(node['@id'])
if node_valid:
return AED(
id=node_id,
position=LonLat(float(node['@lon']), float(node['@lat'])),
country_codes=None,
tags=node_tags,
version=int(node['@version']),
)
else:
return node_id
Expand Down Expand Up @@ -169,7 +173,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}, session=s)
await set_state_doc('aed', {'update_timestamp': data_timestamp, 'version': 2}, session=s)

if aeds:
print('🩺 Updating country codes')
Expand Down
5 changes: 1 addition & 4 deletions states/country_state.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,7 @@ async def _should_update_db() -> tuple[bool, float]:
if doc is None:
return True, 0

update_timestamp = doc['update_timestamp']
# if update_timestamp < VERSION_TIMESTAMP:
# return True, update_timestamp

update_timestamp: float = doc['update_timestamp']
update_age = time() - update_timestamp
if update_age > COUNTRY_UPDATE_DELAY.total_seconds():
return True, update_timestamp
Expand Down

0 comments on commit 8b670f9

Please sign in to comment.