Skip to content

Commit 722d35a

Browse files
authored
Merge pull request #182 from malmeloo:feat/key-alignment
feat: improve key alignment efficiency
2 parents 054fe9f + 3cc33f6 commit 722d35a

File tree

2 files changed

+16
-11
lines changed

2 files changed

+16
-11
lines changed

findmy/accessory.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,11 @@ def get_max_index(self, dt: datetime) -> int:
6161
@abstractmethod
6262
def update_alignment(self, dt: datetime, index: int) -> None:
6363
"""
64-
Update alignment of the accessory.
64+
Update alignment of the accessory based on a key index that was observed at a specific time.
6565
66-
Alignment can be updated based on a LocationReport that was observed at a specific index.
66+
Implementations of this method should consider that this method may be called
67+
multiple times, sometimes with seemingly conflicting data: the same index may be
68+
observed at different times, or multiple indices may be observed at the same time.
6769
"""
6870
raise NotImplementedError
6971

@@ -222,8 +224,13 @@ def get_max_index(self, dt: datetime) -> int:
222224

223225
@override
224226
def update_alignment(self, dt: datetime, index: int) -> None:
225-
if dt < self._alignment_date:
226-
# we only care about the most recent report
227+
if dt < self._alignment_date or index < self._alignment_index:
228+
# We only care about the most recent report and index.
229+
# Multiple calls to this method may be made with
230+
# possibly conflicting data, so we just ignore
231+
# anything that seems to go backwards in time or index.
232+
# Saving the newest data is at least likely to be stable
233+
# over multiple fetches.
227234
return
228235

229236
logger.info("Updating alignment based on report observed at index %i", index)

findmy/reports/reports.py

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -431,7 +431,7 @@ async def _fetch_accessory_report(
431431
# state variables
432432
cur_keys_primary: set[str] = set()
433433
cur_keys_secondary: set[str] = set()
434-
cur_index = accessory.get_min_index(start_date)
434+
cur_index = accessory.get_max_index(start_date)
435435
ret: set[LocationReport] = set()
436436

437437
async def _fetch() -> set[LocationReport]:
@@ -446,17 +446,15 @@ async def _fetch() -> set[LocationReport]:
446446
report.decrypt(key)
447447

448448
# update alignment data on every report
449-
# if a key maps to multiple indices, only feed it the maximum index,
450-
# since apple only returns the latest reports per request.
451-
# This makes the value more likely to be stable.
452-
accessory.update_alignment(report.timestamp, max(key_to_ind[key]))
449+
for i in key_to_ind[key]:
450+
accessory.update_alignment(report.timestamp, i)
453451

454452
cur_keys_primary.clear()
455453
cur_keys_secondary.clear()
456454

457455
return set(new_reports)
458456

459-
while cur_index <= accessory.get_max_index(end_date):
457+
while cur_index >= accessory.get_min_index(end_date):
460458
key_batch = accessory.keys_at(cur_index)
461459

462460
# split into primary and secondary keys
@@ -483,7 +481,7 @@ async def _fetch() -> set[LocationReport]:
483481
cur_keys_primary |= new_keys_primary
484482
cur_keys_secondary |= new_keys_secondary
485483

486-
cur_index += 1
484+
cur_index -= 1
487485

488486
if cur_keys_primary or cur_keys_secondary:
489487
# fetch remaining keys

0 commit comments

Comments
 (0)