-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Changes for reporting files that have not been uploaded (#394)
* Changes for reporting files that have not been uploaded * formatting changes * Changes requested on PR * Changes suggested on the PR * Changes suggested on the PR * removed datetime as a dependency * Added test case for file upload failure * change the method being called in the test case * Undo a minor change * parameter fix * validation changes * minor change to error message * path fix for file * Added file with no permission * Changes to path for failure log * Flush failure logger * Changes to check in-memory * Added sleep for the error logs to get stored * Added print statements * stringify the path on joinpath * add assert * raise manual exception * Added random printing stuff * changes to assert statements * Handle the exception in upload queue and check for value in failure logger * read jsonlines file and validate it * remove incorrect validation with len * Delete file with no permission * remove print statements --------- Co-authored-by: Nithin Bodanapu <[email protected]>
- Loading branch information
Showing
4 changed files
with
224 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
from datetime import datetime | ||
from typing import Iterator, List | ||
|
||
import jsonlines | ||
|
||
|
||
class FileErrorMapping: | ||
def __init__(self, file_name: str, error_reason: str) -> None: | ||
self.file_name = file_name | ||
self.error_reason = error_reason | ||
|
||
def __iter__(self) -> Iterator[List[str]]: | ||
return iter([[self.file_name, self.error_reason]]) | ||
|
||
|
||
class FileFailureManager: | ||
MAX_QUEUE_SIZE = 500 | ||
START_TIME_KEY = "start_time" | ||
FILE_REASON_MAP_KEY = "file_error_reason_map" | ||
|
||
def __init__(self, start_time: str | None = None, path_to_file: str | None = None) -> None: | ||
self.failure_logs: dict[str, str] = {} | ||
|
||
self.path_to_failure_log: str = self._pre_process_file_extension(path_to_file) | ||
self.start_time = start_time or str(datetime.now()) | ||
self._initialize_failure_logs() | ||
|
||
def _pre_process_file_extension(self, path_to_file: str | None) -> str: | ||
if path_to_file and not path_to_file.endswith(".jsonl"): | ||
return path_to_file + ".jsonl" | ||
return str(path_to_file) | ||
|
||
def _initialize_failure_logs(self) -> None: | ||
self.failure_logs = {} | ||
|
||
def __len__(self) -> int: | ||
return len(self.failure_logs) | ||
|
||
def clear(self) -> None: | ||
self.failure_logs.clear() | ||
self._initialize_failure_logs() | ||
|
||
def add(self, file_name: str, error_reason: str) -> None: | ||
error_file_object = FileErrorMapping(file_name=file_name, error_reason=error_reason) | ||
error_file_dict = dict(error_file_object) | ||
|
||
self.failure_logs.update(error_file_dict) | ||
|
||
if len(self) >= self.MAX_QUEUE_SIZE: | ||
self.write_to_file() | ||
|
||
def write_to_file(self) -> None: | ||
if len(self) == 0: | ||
return | ||
|
||
dict_to_write = { | ||
self.START_TIME_KEY: self.start_time, | ||
self.FILE_REASON_MAP_KEY: self.failure_logs, | ||
} | ||
|
||
with jsonlines.open(self.path_to_failure_log, mode="a") as writer: | ||
writer.write(dict_to_write) | ||
|
||
self.clear() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.