Skip to content

Commit 61788cd

Browse files
author
Johannes Hötter
authored
Merge pull request #15 from code-kern-ai/monitoring-upload
Monitoring upload
2 parents 016008d + 48ba5d1 commit 61788cd

File tree

2 files changed

+60
-5
lines changed

2 files changed

+60
-5
lines changed

refinery/__init__.py

Lines changed: 56 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# -*- coding: utf-8 -*-
22

3+
from black import Any
34
from wasabi import msg
45
import pandas as pd
56
from refinery import authentication, api_calls, settings, exceptions, util
@@ -8,6 +9,8 @@
89
import os.path
910
from tqdm import tqdm
1011
import spacy
12+
import time
13+
from refinery import settings
1114

1215

1316
class Client:
@@ -148,7 +151,9 @@ def get_record_export(
148151
)
149152

150153
else:
151-
msg.warn("There are no attributes that can be tokenized in this project.")
154+
msg.warn(
155+
"There are no attributes that can be tokenized in this project."
156+
)
152157

153158
if download_to is not None:
154159
df.to_json(download_to, orient="records")
@@ -214,8 +219,54 @@ def post_file_import(
214219
file_name,
215220
)
216221
if success:
217-
msg.good(f"Uploaded {path} to your project.")
218-
return True
222+
msg.good(f"Uploaded {path} to object storage.")
223+
upload_task_id = (
224+
upload_task_id.split("/")[-1]
225+
if "/" in upload_task_id
226+
else upload_task_id
227+
)
228+
self.__monitor_task(upload_task_id)
229+
230+
else:
231+
msg_text = f"Could not upload {path} to your project."
232+
msg.fail(msg_text)
233+
raise exceptions.FileImportError(msg_text)
234+
235+
def __monitor_task(self, upload_task_id: str) -> None:
236+
do_monitoring = True
237+
idx = 0
238+
last_progress = 0.0
239+
print_success_message = False
240+
with tqdm(
241+
total=100.00,
242+
colour="green",
243+
bar_format="{desc}: {percentage:.2f}%|{bar:10}| {n:.2f}/{total_fmt}",
244+
) as pbar:
245+
pbar.set_description_str(desc="PENDING", refresh=True)
246+
while do_monitoring:
247+
idx += 1
248+
task = self.__get_task(upload_task_id)
249+
task_progress = task.get("progress") if task.get("progress") else 0.0
250+
task_state = task.get("state") if task.get("state") else "FAILED"
251+
progress = task_progress - last_progress
252+
last_progress = task_progress
253+
pbar.update(progress)
254+
pbar.set_description_str(desc=task_state, refresh=True)
255+
if task_state == "DONE" or task_state == "FAILED":
256+
print_success_message = task_state == "DONE"
257+
do_monitoring = False
258+
if idx >= 100:
259+
raise exceptions.FileImportError(
260+
"Timeout while upload, please check the upload progress in the UI."
261+
)
262+
time.sleep(0.5)
263+
if print_success_message:
264+
msg.good("File upload successful.")
219265
else:
220-
msg.fail(f"Could not upload {path} to your project.")
221-
return False
266+
msg.fail("Upload failed. Please look into the UI notification center for more details.")
267+
268+
def __get_task(self, upload_task_id: str) -> Dict[str, Any]:
269+
api_response = api_calls.get_request(
270+
settings.get_task(self.project_id, upload_task_id), self.session_token
271+
)
272+
return api_response

refinery/settings.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,3 +46,7 @@ def get_import_url(project_id: str) -> str:
4646

4747
def get_base_config(project_id: str) -> str:
4848
return f"{get_project_url(project_id)}/import/base_config"
49+
50+
51+
def get_task(project_id: str, task_id: str) -> str:
52+
return f"{get_project_url(project_id)}/import/task/{task_id}"

0 commit comments

Comments
 (0)