|
8 | 8 | import os.path |
9 | 9 | from tqdm import tqdm |
10 | 10 | import spacy |
| 11 | +import time |
| 12 | +from refinery import settings |
11 | 13 |
|
12 | 14 |
|
13 | 15 | class Client: |
@@ -148,7 +150,9 @@ def get_record_export( |
148 | 150 | ) |
149 | 151 |
|
150 | 152 | else: |
151 | | - msg.warn("There are no attributes that can be tokenized in this project.") |
| 153 | + msg.warn( |
| 154 | + "There are no attributes that can be tokenized in this project." |
| 155 | + ) |
152 | 156 |
|
153 | 157 | if download_to is not None: |
154 | 158 | df.to_json(download_to, orient="records") |
@@ -214,8 +218,48 @@ def post_file_import( |
214 | 218 | file_name, |
215 | 219 | ) |
216 | 220 | if success: |
217 | | - msg.good(f"Uploaded {path} to your project.") |
218 | | - return True |
| 221 | + msg.good(f"Uploaded {path} to object storage.") |
| 222 | + upload_task_id = ( |
| 223 | + upload_task_id.split("/")[-1] |
| 224 | + if "/" in upload_task_id |
| 225 | + else upload_task_id |
| 226 | + ) |
| 227 | + self.__monitor_task(upload_task_id) |
| 228 | + |
219 | 229 | else: |
220 | | - msg.fail(f"Could not upload {path} to your project.") |
221 | | - return False |
| 230 | + msg_text = f"Could not upload {path} to your project." |
| 231 | + msg.fail(msg_text) |
| 232 | + raise exceptions.FileImportError(msg_text) |
| 233 | + |
| 234 | + def __monitor_task(self, upload_task_id): |
| 235 | + do_monitoring = True |
| 236 | + idx = 0 |
| 237 | + last_progress = 0.0 |
| 238 | + with tqdm( |
| 239 | + total=100.00, |
| 240 | + colour="green", |
| 241 | + bar_format="{desc}: {percentage:.2f}%|{bar:10}| {n:.2f}/{total_fmt}", |
| 242 | + ) as pbar: |
| 243 | + pbar.set_description_str(desc="PENDING", refresh=True) |
| 244 | + while do_monitoring: |
| 245 | + idx += 1 |
| 246 | + task = self.__get_task(upload_task_id) |
| 247 | + task_progress = task.get("progress") if task.get("progress") else 0.0 |
| 248 | + task_state = task.get("state") if task.get("state") else "FAILED" |
| 249 | + progress = task_progress - last_progress |
| 250 | + last_progress = task_progress |
| 251 | + pbar.update(progress) |
| 252 | + pbar.set_description_str(desc=task_state, refresh=True) |
| 253 | + if task_state == "DONE" or task_state == "FAILED": |
| 254 | + do_monitoring = False |
| 255 | + if idx >= 100: |
| 256 | + raise exceptions.FileImportError( |
| 257 | + "Timeout while upload, please check the upload progress in the UI." |
| 258 | + ) |
| 259 | + time.sleep(0.5) |
| 260 | + |
| 261 | + def __get_task(self, upload_task_id): |
| 262 | + api_response = api_calls.get_request( |
| 263 | + settings.get_task(self.project_id, upload_task_id), self.session_token |
| 264 | + ) |
| 265 | + return api_response |
0 commit comments