-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbase.py
More file actions
365 lines (295 loc) · 10.1 KB
/
base.py
File metadata and controls
365 lines (295 loc) · 10.1 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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
"""
Simvue Artifact
===============
Class for defining and interacting with artifact objects.
"""
import datetime
import http
import io
import typing
import pydantic
try:
from typing import Self
except ImportError:
from typing_extensions import Self # noqa: F401
from simvue.api.url import URL
from simvue.exception import ObjectNotFoundError
from simvue.models import DATETIME_FORMAT
from simvue.api.objects.base import SimvueObject, staging_check, write_only
from simvue.api.objects.run import Run
from simvue.api.request import (
put as sv_put,
get_json_from_response,
post as sv_post,
get as sv_get,
)
Category = typing.Literal["code", "input", "output"]
BASE_TIMEOUT: int = 10
UPLOAD_TIMEOUT_PER_MB: int = 1
DOWNLOAD_TIMEOUT_PER_MB: int = 1
DOWNLOAD_CHUNK_SIZE: int = 8192
class ArtifactBase(SimvueObject):
"""Connect to/create an artifact locally or on the server"""
def __init__(
self, identifier: str | None = None, _read_only: bool = True, **kwargs
) -> None:
"""Initialise an artifact connection.
Parameters
----------
identifier : str, optional
the identifier of this object on the server.
"""
self._label = "artifact"
self._endpoint = f"{self._label}s"
super().__init__(identifier=identifier, _read_only=_read_only, **kwargs)
# If the artifact is an online instance, need a place to store the response
# from the initial creation
self._init_data: dict[str, dict] = {}
@classmethod
def new(cls, *_, **__) -> Self:
raise NotImplementedError
def commit(self) -> None:
"""Not applicable, cannot commit single write artifact."""
self._logger.info("Cannot call method 'commit' on write-once type 'Artifact'")
def attach_to_run(self, run_id: str, category: Category) -> None:
"""Attach this artifact to a given run.
Parameters
----------
run_id : str
identifier of run to associate this artifact with.
category : Literal['input', 'output', 'code']
category of this artifact with respect to the run.
"""
self._init_data["runs"][run_id] = category
if self._offline:
self._staging["runs"] = self._init_data["runs"]
super().commit()
return
_run_artifacts_url = (
URL(self._user_config.server.url)
/ f"runs/{run_id}/artifacts/{self._init_data['id']}"
)
_response = sv_put(
url=f"{_run_artifacts_url}",
headers=self._headers,
json={"category": category},
)
get_json_from_response(
expected_status=[http.HTTPStatus.OK],
scenario=f"adding artifact '{self.name}' to run '{run_id}'",
response=_response,
)
def on_reconnect(self, id_mapping: dict[str, str]) -> None:
"""Operations performed when this artifact is switched from offline to online mode.
Parameters
----------
id_mapping : dict[str, str]
mapping from offline identifier to new online identifier.
"""
_offline_staging = self._init_data["runs"].copy()
for id, category in _offline_staging.items():
self.attach_to_run(run_id=id_mapping[id], category=category)
def _upload(self, file: io.BytesIO, timeout: int, file_size: int) -> None:
if self._offline:
super().commit()
return
if not (_url := self._staging.get("url")):
return
if not timeout:
timeout = BASE_TIMEOUT + UPLOAD_TIMEOUT_PER_MB * file_size / 1024 / 1024
self._logger.debug(
f"Will wait for a period of {timeout:.0f}s for upload of file for {file_size}B file to complete."
)
_name = self._staging["name"]
_response = sv_post(
url=_url,
headers={},
params={},
is_json=False,
timeout=timeout,
files={"file": file},
data=self._init_data.get("fields"),
)
self._logger.debug(
"Got status code %d when uploading artifact",
_response.status_code,
)
get_json_from_response(
expected_status=[http.HTTPStatus.OK, http.HTTPStatus.NO_CONTENT],
allow_parse_failure=True, # JSON response from S3 not parsible
scenario=f"uploading artifact '{_name}' to object storage",
response=_response,
)
# Temporarily remove read-only state
self.read_only(False)
# Update the server status to confirm file uploaded
self.uploaded = True
super().commit()
self.read_only(True)
def _get(
self, storage: str | None = None, url: str | None = None, **kwargs
) -> dict[str, typing.Any]:
return super()._get(
storage=storage or self._staging.get("server", {}).get("storage_id"),
url=url,
**kwargs,
)
@property
def checksum(self) -> str:
"""Retrieve the checksum for this artifact.
Returns
-------
str
"""
return self._get_attribute("checksum")
@property
def storage_url(self) -> URL | None:
"""Retrieve upload URL for artifact.
Returns
-------
simvue.api.url.URL | None
"""
return URL(_url) if (_url := self._init_data.get("url")) else None
@property
def original_path(self) -> str:
"""Retrieve the original path of the file associated with this artifact.
Returns
-------
str
"""
return self._get_attribute("original_path")
@property
def storage_id(self) -> str | None:
"""Retrieve the storage identifier for this artifact.
Returns
-------
str | None
"""
return self._get_attribute("storage_id")
@property
def mime_type(self) -> str:
"""Retrieve the MIME type for this artifact.
Returns
-------
str
"""
return self._get_attribute("mime_type")
@property
def size(self) -> int:
"""Retrieve the size for this artifact in bytes.
Returns
-------
int
"""
return self._get_attribute("size")
@property
def name(self) -> str | None:
"""Retrieve name for the artifact.
Returns
-------
str | None
"""
return self._get_attribute("name")
@property
def created(self) -> datetime.datetime | None:
"""Retrieve created datetime for the artifact.
Returns
-------
datetime.datetime | None
"""
_created: str | None = self._get_attribute("created")
return (
datetime.datetime.strptime(_created, DATETIME_FORMAT) if _created else None
)
@property
@staging_check
def uploaded(self) -> bool:
"""Returns whether a file was uploaded for this artifact.
Returns
-------
bool
"""
return self._get_attribute("uploaded")
@uploaded.setter
@write_only
@pydantic.validate_call
def uploaded(self, is_uploaded: bool) -> None:
"""Set if a file was successfully uploaded for this artifact."""
self._staging["uploaded"] = is_uploaded
@property
def download_url(self) -> URL | None:
"""Retrieve the URL for downloading this artifact
Returns
-------
simvue.api.url.URL | None
"""
return self._get_attribute("url")
@property
def runs(self) -> typing.Generator[str, None, None]:
"""Retrieve all runs for which this artifact is related.
Yields
------
str
run identifier for run associated with this artifact
Returns
-------
Generator[str, None, None]
"""
for _id, _ in Run.get(filters=[f"artifact.id == {self.id}"]):
yield _id
def get_category(self, run_id: str) -> Category:
"""Retrieve the category of this artifact with respect to a given run.
Returns
-------
Literal['input', 'output', 'code']
"""
_run_url = (
URL(self._user_config.server.url)
/ f"runs/{run_id}/artifacts/{self._identifier}"
)
_response = sv_get(url=_run_url, header=self._headers)
_json_response = get_json_from_response(
response=_response,
expected_status=[http.HTTPStatus.OK, http.HTTPStatus.NOT_FOUND],
scenario=f"Retrieval of category for artifact '{self._identifier}' with respect to run '{run_id}'",
)
if _response.status_code == http.HTTPStatus.NOT_FOUND:
raise ObjectNotFoundError(
self._label, self._identifier, extra=f"for run '{run_id}'"
)
return _json_response["category"]
@pydantic.validate_call
def download_content(self) -> typing.Generator[bytes, None, None]:
"""Stream artifact content.
Yields
------
bytes
artifact content from server.
Returns
-------
Generator[bytes, None, None]
"""
if not self.download_url:
raise ValueError(
f"Could not retrieve URL for artifact '{self._identifier}'"
)
_timeout = BASE_TIMEOUT + DOWNLOAD_TIMEOUT_PER_MB * self.size / 1024 / 1024
self._logger.debug(
f"Will wait {_timeout:.0f}s for download of file {self.name} of size {self.size}B"
)
_response = sv_get(
f"{self.download_url}",
timeout=_timeout,
headers=None,
)
get_json_from_response(
response=_response,
allow_parse_failure=True,
expected_status=[http.HTTPStatus.OK],
scenario=f"Retrieval of file for {self._label} '{self._identifier}'",
)
_total_length: str | None = _response.headers.get("content-length")
if _total_length is None:
yield _response.content
else:
yield from _response.iter_content(chunk_size=DOWNLOAD_CHUNK_SIZE)