Skip to content

Commit

Permalink
Changes suggested on the PR
Browse files Browse the repository at this point in the history
  • Loading branch information
Nithin Bodanapu committed Dec 6, 2024
1 parent 353f99c commit 865d97a
Showing 1 changed file with 12 additions and 8 deletions.
20 changes: 12 additions & 8 deletions cognite/extractorutils/uploader/upload_failure_handler.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from datetime import datetime
from typing import Any, Iterator, List
from typing import Iterator, List

import jsonlines

Expand All @@ -19,7 +19,7 @@ class FileFailureManager:
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, Any] = {}
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())
Expand All @@ -31,11 +31,10 @@ def _pre_process_file_extension(self, path_to_file: str | None) -> str:
return str(path_to_file)

def _initialize_failure_logs(self) -> None:
self.failure_logs[FileFailureManager.START_TIME_KEY] = self.start_time
self.failure_logs[FileFailureManager.FILE_REASON_MAP_KEY] = {}
self.failure_logs = {}

def __len__(self) -> int:
return len(self.failure_logs[FileFailureManager.FILE_REASON_MAP_KEY])
return len(self.failure_logs)

def clear(self) -> None:
self.failure_logs.clear()
Expand All @@ -44,16 +43,21 @@ def clear(self) -> None:
def add(self, file_name: str, error_reason: str) -> None:
error_file_object = FileErrorMapping(file_name=file_name, error_reason=error_reason)

self.failure_logs[FileFailureManager.FILE_REASON_MAP_KEY].update(error_file_object)
self.failure_logs.update(dict(error_file_object))

if len(self) >= self.MAX_QUEUE_SIZE:
self.write_to_file()

def write_to_file(self) -> None:
if len(self.failure_logs[self.FILE_REASON_MAP_KEY]) == 0:
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(self.failure_logs)
writer.write(dict_to_write)

self.clear()

0 comments on commit 865d97a

Please sign in to comment.