-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathdatabase.py
More file actions
314 lines (281 loc) · 11.8 KB
/
Copy pathdatabase.py
File metadata and controls
314 lines (281 loc) · 11.8 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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
"""Utilities for managing mock Vuforia databases."""
import uuid
from collections.abc import Iterable
from dataclasses import dataclass, field
from typing import NotRequired, Self, TypedDict
from beartype import beartype
from mock_vws._constants import TargetStatuses
from mock_vws.database_type import DatabaseType
from mock_vws.request_rate_limits import (
RequestRateLimits,
RequestRateLimitsDict,
)
from mock_vws.states import States
from mock_vws.target import (
ImageTarget,
ImageTargetDict,
VuMarkTarget,
VuMarkTargetDict,
)
@beartype
class CloudDatabaseDict(TypedDict):
"""A dictionary type which represents a cloud database."""
database_id: str
database_name: str
server_access_key: str
server_secret_key: str
client_access_key: str
client_secret_key: str
state_name: str
database_type_name: str
targets: Iterable[ImageTargetDict]
request_quota: NotRequired[int]
reco_threshold: NotRequired[int]
current_month_recos: NotRequired[int]
previous_month_recos: NotRequired[int]
total_recos: NotRequired[int]
target_quota: NotRequired[int]
requests_per_second_limit: NotRequired[int | None]
request_rate_limits: NotRequired[RequestRateLimitsDict | None]
@beartype
class VuMarkDatabaseDict(TypedDict):
"""A dictionary type which represents a VuMark database."""
database_name: str
server_access_key: str
server_secret_key: str
vumark_targets: Iterable[VuMarkTargetDict]
state_name: str
@beartype
def _random_hex() -> str:
"""Return a random hex value."""
return uuid.uuid4().hex
@beartype
@dataclass(eq=True, frozen=True, kw_only=True)
class CloudDatabase:
"""Credentials for VWS APIs.
Args:
database_id: The identifier of a VWS target manager database. Defaults
to a random string. Endpoints which name a database in their path,
such as the reco counts report endpoint, accept only the identifier
of the database which the request's server keys belong to.
database_name: The name of a VWS target manager database name. Defaults
to a random string.
server_access_key: A VWS server access key. Defaults to a random
string.
server_secret_key: A VWS server secret key. Defaults to a random
string.
client_access_key: A VWS client access key. Defaults to a random
string.
client_secret_key: A VWS client secret key. Defaults to a random
string.
state: The state of the database.
request_quota: The request quota. Set this to ``0`` to make VWS
endpoints return ``RequestQuotaReached``.
target_quota: The target quota. When the database contains this many
targets, adding another returns ``TargetQuotaReached``.
requests_per_second_limit: The maximum number of VWS requests accepted
in a rolling one-second window, across all VWS endpoints. Set this
to ``0`` to make VWS endpoints return ``TooManyRequests``. By
default, the mock does not apply this limit.
request_rate_limits: Request rate limits which apply to individual
groups of VWS endpoints, tracked separately from each other and
from ``requests_per_second_limit``. Set this to
:data:`mock_vws.request_rate_limits.DOCUMENTED_REQUEST_RATE_LIMITS`
to apply the limits which Vuforia documents. By default, the mock
does not apply per-endpoint request limits.
"""
# We hide a few things in the ``repr`` with ``repr=False`` so that they do
# not show up in CI logs.
database_id: str = field(default_factory=_random_hex, repr=False)
database_name: str = field(default_factory=_random_hex, repr=False)
server_access_key: str = field(default_factory=_random_hex, repr=False)
server_secret_key: str = field(default_factory=_random_hex, repr=False)
client_access_key: str = field(default_factory=_random_hex, repr=False)
client_secret_key: str = field(default_factory=_random_hex, repr=False)
# We have ``targets`` as ``hash=False`` so that we can have the class as
# ``frozen=True`` while still being able to keep the interface we want.
# In particular, we might want to inspect the ``database`` object's targets
# as they change via API requests.
targets: set[ImageTarget] = field(
default_factory=set[ImageTarget],
hash=False,
)
state: States = States.WORKING
database_type: DatabaseType = DatabaseType.CLOUD_RECO
request_quota: int = 100000
reco_threshold: int = 1000
current_month_recos: int = 0
previous_month_recos: int = 0
total_recos: int = 0
target_quota: int = 1000
requests_per_second_limit: int | None = None
request_rate_limits: RequestRateLimits | None = None
def to_dict(self) -> CloudDatabaseDict:
"""Dump a target to a dictionary which can be loaded as JSON."""
targets: list[ImageTargetDict] = [
target.to_dict() for target in self.targets
]
request_rate_limits: RequestRateLimitsDict | None = (
None
if self.request_rate_limits is None
else self.request_rate_limits.to_dict()
)
return {
"database_id": self.database_id,
"database_name": self.database_name,
"server_access_key": self.server_access_key,
"server_secret_key": self.server_secret_key,
"client_access_key": self.client_access_key,
"client_secret_key": self.client_secret_key,
"state_name": self.state.name,
"database_type_name": self.database_type.name,
"targets": targets,
"request_quota": self.request_quota,
"reco_threshold": self.reco_threshold,
"current_month_recos": self.current_month_recos,
"previous_month_recos": self.previous_month_recos,
"total_recos": self.total_recos,
"target_quota": self.target_quota,
"requests_per_second_limit": self.requests_per_second_limit,
"request_rate_limits": request_rate_limits,
}
def get_target(self, target_id: str) -> ImageTarget:
"""Return a target from the database with the given ID."""
(target,) = (
target for target in self.targets if target.target_id == target_id
)
return target
@classmethod
def from_dict(cls, database_dict: CloudDatabaseDict) -> Self:
"""Load a database from a dictionary."""
targets: set[ImageTarget] = {
ImageTarget.from_dict(target_dict=target_dict)
for target_dict in database_dict["targets"]
}
request_rate_limits_dict = database_dict.get("request_rate_limits")
request_rate_limits = (
None
if request_rate_limits_dict is None
else RequestRateLimits.from_dict(
limits_dict=request_rate_limits_dict
)
)
return cls(
database_id=database_dict["database_id"],
database_name=database_dict["database_name"],
server_access_key=database_dict["server_access_key"],
server_secret_key=database_dict["server_secret_key"],
client_access_key=database_dict["client_access_key"],
client_secret_key=database_dict["client_secret_key"],
state=States[database_dict["state_name"]],
database_type=DatabaseType[database_dict["database_type_name"]],
targets=targets,
request_quota=database_dict.get("request_quota", 100000),
reco_threshold=database_dict.get("reco_threshold", 1000),
current_month_recos=database_dict.get("current_month_recos", 0),
previous_month_recos=database_dict.get("previous_month_recos", 0),
total_recos=database_dict.get("total_recos", 0),
target_quota=database_dict.get("target_quota", 1000),
requests_per_second_limit=database_dict.get(
"requests_per_second_limit"
),
request_rate_limits=request_rate_limits,
)
@property
def not_deleted_targets(self) -> set[ImageTarget]:
"""All targets which have not been deleted."""
return {target for target in self.targets if not target.delete_date}
@property
def active_targets(self) -> set[ImageTarget]:
"""All active targets."""
return {
target
for target in self.not_deleted_targets
if target.status == TargetStatuses.SUCCESS.value
and target.active_flag
}
@property
def inactive_targets(self) -> set[ImageTarget]:
"""All inactive targets."""
return {
target
for target in self.not_deleted_targets
if target.status == TargetStatuses.SUCCESS.value
and not target.active_flag
}
@property
def failed_targets(self) -> set[ImageTarget]:
"""All failed targets."""
return {
target
for target in self.not_deleted_targets
if target.status == TargetStatuses.FAILED.value
}
@property
def processing_targets(self) -> set[ImageTarget]:
"""All processing targets."""
return {
target
for target in self.not_deleted_targets
if target.status == TargetStatuses.PROCESSING.value
}
@beartype
@dataclass(eq=True, frozen=True, kw_only=True)
class VuMarkDatabase:
"""Credentials for the VuMark generation API.
Args:
database_name: The name of a VWS target manager database name. Defaults
to a random string.
server_access_key: A VWS server access key. Defaults to a random
string.
server_secret_key: A VWS server secret key. Defaults to a random
string.
"""
database_name: str = field(default_factory=_random_hex, repr=False)
server_access_key: str = field(default_factory=_random_hex, repr=False)
server_secret_key: str = field(default_factory=_random_hex, repr=False)
# We have ``vumark_targets`` as ``hash=False`` so that we can have the
# class as ``frozen=True`` while still being able to keep the interface
# we want.
vumark_targets: set[VuMarkTarget] = field(
default_factory=set[VuMarkTarget],
hash=False,
)
state: States = States.WORKING
def get_vumark_target(self, target_id: str) -> VuMarkTarget:
"""Return a VuMark target from the database with the given ID."""
(target,) = (
target
for target in self.vumark_targets
if target.target_id == target_id
)
return target
def to_dict(self) -> VuMarkDatabaseDict:
"""Dump a VuMark database to a dictionary which can be loaded as
JSON.
"""
vumark_targets = [target.to_dict() for target in self.vumark_targets]
return {
"database_name": self.database_name,
"server_access_key": self.server_access_key,
"server_secret_key": self.server_secret_key,
"vumark_targets": vumark_targets,
"state_name": self.state.name,
}
@classmethod
def from_dict(cls, database_dict: VuMarkDatabaseDict) -> Self:
"""Load a VuMark database from a dictionary."""
return cls(
database_name=database_dict["database_name"],
server_access_key=database_dict["server_access_key"],
server_secret_key=database_dict["server_secret_key"],
vumark_targets={
VuMarkTarget.from_dict(target_dict=target_dict)
for target_dict in database_dict["vumark_targets"]
},
state=States[database_dict["state_name"]],
)
@property
def not_deleted_targets(self) -> set[VuMarkTarget]:
"""All VuMark targets."""
return set(self.vumark_targets)