|
1 | 1 | # -*- coding: utf-8 -*- |
2 | 2 |
|
| 3 | +from black import Any |
3 | 4 | from wasabi import msg |
4 | 5 | import pandas as pd |
5 | 6 | from refinery import authentication, api_calls, settings, exceptions, util |
|
8 | 9 | import os.path |
9 | 10 | from tqdm import tqdm |
10 | 11 | import spacy |
| 12 | +import time |
| 13 | +from refinery import settings |
11 | 14 |
|
12 | 15 |
|
13 | 16 | class Client: |
@@ -148,7 +151,9 @@ def get_record_export( |
148 | 151 | ) |
149 | 152 |
|
150 | 153 | 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 | + ) |
152 | 157 |
|
153 | 158 | if download_to is not None: |
154 | 159 | df.to_json(download_to, orient="records") |
@@ -214,8 +219,54 @@ def post_file_import( |
214 | 219 | file_name, |
215 | 220 | ) |
216 | 221 | 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.") |
219 | 265 | 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 |
0 commit comments