forked from BasedHardware/omi
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlanes.py
More file actions
138 lines (115 loc) · 4.33 KB
/
Copy pathlanes.py
File metadata and controls
138 lines (115 loc) · 4.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
"""Authoritative fresh/backfill classification for Offline Sync uploads."""
from __future__ import annotations
import os
import time
from dataclasses import dataclass
from enum import Enum
from typing import Iterable, Optional
from utils.request_validation import parse_sync_filename_timestamp
class SyncLane(str, Enum):
FRESH = 'fresh'
BACKFILL = 'backfill'
class CaptureTimeTrust(str, Enum):
DEVICE_BOUND = 'device_bound'
LEGACY = 'legacy'
UNTRUSTED = 'untrusted'
@dataclass(frozen=True)
class SyncLaneDecision:
lane: SyncLane
trust: CaptureTimeTrust
reason: str
oldest_capture_at: Optional[float]
newest_capture_at: Optional[float]
maximum_age_seconds: Optional[int]
automatic_recovery_allowed: bool = True
def fresh_cutoff_seconds() -> int:
return max(60, int(os.getenv('SYNC_FRESH_MAX_AGE_SECONDS', str(6 * 60 * 60))))
def maximum_backfill_age_seconds() -> int:
return max(fresh_cutoff_seconds(), int(os.getenv('SYNC_BACKFILL_MAX_AGE_SECONDS', str(30 * 24 * 60 * 60))))
def maximum_future_skew_seconds() -> int:
return max(0, int(os.getenv('SYNC_CAPTURE_MAX_FUTURE_SKEW_SECONDS', '300')))
def capture_times_within_window(filenames: Iterable[str], lower: float, upper: float) -> bool:
try:
capture_times = [float(parse_sync_filename_timestamp(filename)) for filename in filenames]
except (IndexError, ValueError):
return False
return bool(capture_times) and all(lower <= capture_time <= upper for capture_time in capture_times)
def classify_sync_lane(
filenames: Iterable[str],
*,
client_device_id: Optional[str],
now: Optional[float] = None,
) -> SyncLaneDecision:
"""Classify a whole upload batch; mixed batches conservatively become backfill."""
capture_times: list[float] = []
for filename in filenames:
try:
capture_times.append(float(parse_sync_filename_timestamp(filename)))
except (IndexError, ValueError):
return SyncLaneDecision(
lane=SyncLane.BACKFILL,
trust=CaptureTimeTrust.UNTRUSTED,
reason='invalid_capture_time',
oldest_capture_at=None,
newest_capture_at=None,
maximum_age_seconds=None,
)
if not capture_times:
return SyncLaneDecision(
lane=SyncLane.BACKFILL,
trust=CaptureTimeTrust.UNTRUSTED,
reason='missing_capture_time',
oldest_capture_at=None,
newest_capture_at=None,
maximum_age_seconds=None,
)
effective_now = time.time() if now is None else now
oldest = min(capture_times)
newest = max(capture_times)
maximum_age = max(0, int(effective_now - oldest))
trust = CaptureTimeTrust.DEVICE_BOUND if client_device_id else CaptureTimeTrust.LEGACY
if newest > effective_now + maximum_future_skew_seconds():
return SyncLaneDecision(
lane=SyncLane.BACKFILL,
trust=CaptureTimeTrust.UNTRUSTED,
reason='future_capture_time',
oldest_capture_at=oldest,
newest_capture_at=newest,
maximum_age_seconds=maximum_age,
)
if maximum_age > maximum_backfill_age_seconds():
return SyncLaneDecision(
lane=SyncLane.BACKFILL,
trust=trust,
reason='lookback_exceeded',
oldest_capture_at=oldest,
newest_capture_at=newest,
maximum_age_seconds=maximum_age,
automatic_recovery_allowed=False,
)
if not client_device_id:
return SyncLaneDecision(
lane=SyncLane.BACKFILL,
trust=CaptureTimeTrust.LEGACY,
reason='unbound_capture_time',
oldest_capture_at=oldest,
newest_capture_at=newest,
maximum_age_seconds=maximum_age,
)
if maximum_age > fresh_cutoff_seconds():
return SyncLaneDecision(
lane=SyncLane.BACKFILL,
trust=trust,
reason='historical_capture',
oldest_capture_at=oldest,
newest_capture_at=newest,
maximum_age_seconds=maximum_age,
)
return SyncLaneDecision(
lane=SyncLane.FRESH,
trust=trust,
reason='recent_capture',
oldest_capture_at=oldest,
newest_capture_at=newest,
maximum_age_seconds=maximum_age,
)