#2350: add num_access, num_file_deletions and num_creations to file system

This commit is contained in:
Czar Echavez
2024-03-08 11:16:27 +00:00
parent 59bbc0e733
commit e9eef2b4c0
8 changed files with 108 additions and 5 deletions

View File

@@ -38,6 +38,8 @@ class File(FileSystemItemABC):
"The Path if real is True."
sim_root: Optional[Path] = None
"Root path of the simulation."
num_access: int = 0
"Number of times the file was accessed in the current step."
def __init__(self, **kwargs):
"""
@@ -93,11 +95,23 @@ class File(FileSystemItemABC):
return os.path.getsize(self.sim_path)
return self.sim_size
def apply_timestep(self, timestep: int) -> None:
"""
Apply a timestep to the file.
:param timestep: The current timestep of the simulation.
"""
super().apply_timestep(timestep=timestep)
# reset the number of accesses to 0
self.num_access = 0
def describe_state(self) -> Dict:
"""Produce a dictionary describing the current state of this object."""
state = super().describe_state()
state["size"] = self.size
state["file_type"] = self.file_type.name
state["num_access"] = self.num_access
return state
def scan(self) -> None:
@@ -106,6 +120,7 @@ class File(FileSystemItemABC):
self.sys_log.error(f"Unable to scan deleted file {self.folder_name}/{self.name}")
return
self.num_access += 1 # file was accessed
path = self.folder.name + "/" + self.name
self.sys_log.info(f"Scanning file {self.sim_path if self.sim_path else path}")
self.visible_health_status = self.health_status
@@ -160,6 +175,7 @@ class File(FileSystemItemABC):
if self.health_status == FileSystemItemHealthStatus.CORRUPT:
self.health_status = FileSystemItemHealthStatus.GOOD
self.num_access += 1 # file was accessed
path = self.folder.name + "/" + self.name
self.sys_log.info(f"Repaired file {self.sim_path if self.sim_path else path}")
@@ -173,6 +189,7 @@ class File(FileSystemItemABC):
if self.health_status == FileSystemItemHealthStatus.GOOD:
self.health_status = FileSystemItemHealthStatus.CORRUPT
self.num_access += 1 # file was accessed
path = self.folder.name + "/" + self.name
self.sys_log.info(f"Corrupted file {self.sim_path if self.sim_path else path}")
@@ -185,6 +202,7 @@ class File(FileSystemItemABC):
if self.health_status == FileSystemItemHealthStatus.CORRUPT:
self.health_status = FileSystemItemHealthStatus.GOOD
self.num_access += 1 # file was accessed
path = self.folder.name + "/" + self.name
self.sys_log.info(f"Restored file {self.sim_path if self.sim_path else path}")
@@ -194,5 +212,6 @@ class File(FileSystemItemABC):
self.sys_log.error(f"Unable to delete an already deleted file {self.folder_name}/{self.name}")
return
self.num_access += 1 # file was accessed
self.deleted = True
self.sys_log.info(f"File deleted {self.folder_name}/{self.name}")

View File

@@ -27,6 +27,10 @@ class FileSystem(SimComponent):
"Instance of SysLog used to create system logs."
sim_root: Path
"Root path of the simulation."
num_file_creations: int = 0
"Number of file creations in the current step."
num_file_deletions: int = 0
"Number of file deletions in the current step."
def __init__(self, **kwargs):
super().__init__(**kwargs)
@@ -248,6 +252,8 @@ class FileSystem(SimComponent):
)
folder.add_file(file)
self._file_request_manager.add_request(name=file.name, request_type=RequestType(func=file._request_manager))
# increment file creation
self.num_file_creations += 1
return file
def get_file(self, folder_name: str, file_name: str, include_deleted: Optional[bool] = False) -> Optional[File]:
@@ -308,6 +314,8 @@ class FileSystem(SimComponent):
if folder:
file = folder.get_file(file_name)
if file:
# increment file creation
self.num_file_deletions += 1
folder.remove_file(file)
def delete_file_by_id(self, folder_uuid: str, file_uuid: str):
@@ -337,15 +345,14 @@ class FileSystem(SimComponent):
"""
file = self.get_file(folder_name=src_folder_name, file_name=src_file_name)
if file:
src_folder = file.folder
# remove file from src
src_folder.remove_file(file)
self.delete_file(folder_name=file.folder_name, file_name=file.name)
dst_folder = self.get_folder(folder_name=dst_folder_name)
if not dst_folder:
dst_folder = self.create_folder(dst_folder_name)
# add file to dst
dst_folder.add_file(file)
self.num_file_creations += 1
if file.real:
old_sim_path = file.sim_path
file.sim_path = file.sim_root / file.path
@@ -373,6 +380,10 @@ class FileSystem(SimComponent):
folder_name=dst_folder.name,
**file.model_dump(exclude={"uuid", "folder_id", "folder_name", "sim_path"}),
)
self.num_file_creations += 1
# increment access counter
file.num_access += 1
dst_folder.add_file(file_copy, force=True)
if file.real:
@@ -390,12 +401,20 @@ class FileSystem(SimComponent):
state = super().describe_state()
state["folders"] = {folder.name: folder.describe_state() for folder in self.folders.values()}
state["deleted_folders"] = {folder.name: folder.describe_state() for folder in self.deleted_folders.values()}
state["num_file_creations"] = self.num_file_creations
state["num_file_deletions"] = self.num_file_deletions
return state
def apply_timestep(self, timestep: int) -> None:
"""Apply time step to FileSystem and its child folders and files."""
super().apply_timestep(timestep=timestep)
# reset number of file creations
self.num_file_creations = 0
# reset number of file deletions
self.num_file_deletions = 0
# apply timestep to folders
for folder_id in self.folders:
self.folders[folder_id].apply_timestep(timestep=timestep)

View File

@@ -131,6 +131,7 @@ class Folder(FileSystemItemABC):
file.scan()
if file.visible_health_status == FileSystemItemHealthStatus.CORRUPT:
self.visible_health_status = FileSystemItemHealthStatus.CORRUPT
self.visible_health_status = self.health_status
def _reveal_to_red_timestep(self) -> None:
"""Apply reveal to red timestep."""

View File

@@ -59,6 +59,16 @@ class Application(IOSoftware):
)
return state
def apply_timestep(self, timestep: int) -> None:
"""
Apply a timestep to the application.
:param timestep: The current timestep of the simulation.
"""
super().apply_timestep(timestep=timestep)
self.num_executions = 0 # reset number of executions
def _can_perform_action(self) -> bool:
"""
Checks if the application can perform actions.

View File

@@ -76,6 +76,8 @@ class DatabaseClient(Application):
if not self._can_perform_action():
return False
self.num_executions += 1 # trying to connect counts as an execution
if not connection_id:
connection_id = str(uuid4())

View File

@@ -194,6 +194,8 @@ class DataManipulationBot(Application):
"""
if not self._can_perform_action():
return
self.num_executions += 1
if self.server_ip_address and self.payload:
self.sys_log.info(f"{self.name}: Running")
self._logon()

View File

@@ -80,6 +80,8 @@ class WebBrowser(Application):
if not self._can_perform_action():
return False
self.num_executions += 1 # trying to connect counts as an execution
# reset latest response
self.latest_response = HttpResponsePacket(status_code=HttpStatusCode.NOT_FOUND)