Skip to content

Commit

Permalink
Only overwrite data if it's good or we have no more retries
Browse files Browse the repository at this point in the history
  • Loading branch information
magico13 committed Mar 3, 2024
1 parent f0fe54e commit 02427e0
Showing 1 changed file with 9 additions and 6 deletions.
15 changes: 9 additions & 6 deletions pyemvue/pyemvue.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,13 +123,14 @@ def get_device_list_usage(
deviceGids=gids, instant=_format_time(instant), scale=scale, unit=unit
)
attempts = 0
success = False
update_failed = True
max_retry_attempts = max(max_retry_attempts, 1)
initial_retry_delay = max(initial_retry_delay, 0.5)
max_retry_delay = max(max_retry_delay, 0)
devices: dict[int, VueUsageDevice] = {}

while attempts < max_retry_attempts and not success:
while attempts < max_retry_attempts and update_failed:
update_failed = False # reset the flag, if we fail we'll set it again
if attempts > 0:
# if we're retrying, wait a bit before trying again using an exponential backoff
delay = min(
Expand All @@ -147,14 +148,16 @@ def get_device_list_usage(
populated = VueUsageDevice(
timestamp=timestamp
).from_json_dictionary(device)
devices[populated.device_gid] = populated
# data is missing if any usage is None for any channels. In that case we retry the request and merge results.
data_missing = any(channel_usage.usage is None for channel_usage in populated.channels.values())
success = success and not data_missing
update_failed = update_failed or data_missing
if not data_missing or attempts >= max_retry_attempts:
# only overwrite the device data if we have all the data or we've run out of retries
devices[populated.device_gid] = populated
else:
success = False
update_failed = True
else:
success = False
update_failed = True

# if we still haven't fully succeeded, return the data we did manage to get

Expand Down

0 comments on commit 02427e0

Please sign in to comment.