2024-11-25 14:01:09 +03:00
|
|
|
import os
|
|
|
|
|
from loguru import logger
|
|
|
|
|
|
2024-12-05 13:18:53 +03:00
|
|
|
from utils.base.base import BaseDirectoryMonitor
|
2024-11-25 14:01:09 +03:00
|
|
|
|
|
|
|
|
|
|
|
|
|
class DirectoryMonitor(BaseDirectoryMonitor):
|
|
|
|
|
|
|
|
|
|
def _init_state(self):
|
2024-12-23 14:18:54 +03:00
|
|
|
|
|
|
|
|
self._files = [
|
|
|
|
|
os.path.join(self._directory_path, file)
|
|
|
|
|
for file in os.listdir(self._directory_path)
|
|
|
|
|
if file.lower().endswith('.csv')
|
|
|
|
|
]
|
2024-11-25 14:01:09 +03:00
|
|
|
|
|
|
|
|
self.update_timer.timeout.connect(self._monitor)
|
|
|
|
|
logger.info("Monitor initiated!")
|
|
|
|
|
|
|
|
|
|
def _monitor(self):
|
2024-12-23 14:18:54 +03:00
|
|
|
current_files = [
|
|
|
|
|
os.path.join(self._directory_path, file)
|
|
|
|
|
for file in os.listdir(self._directory_path)
|
|
|
|
|
if file.lower().endswith('.csv')
|
|
|
|
|
]
|
|
|
|
|
new_files = sorted(list(filter(lambda x: x not in self._files, current_files)))
|
2024-11-25 14:01:09 +03:00
|
|
|
if new_files:
|
|
|
|
|
logger.info(f"New files detected: {new_files}")
|
|
|
|
|
self._mediator.notify(self, new_files)
|
2024-12-23 14:18:54 +03:00
|
|
|
self._files = current_files
|
|
|
|
|
if not current_files:
|
2024-11-25 14:01:09 +03:00
|
|
|
self._files = []
|
|
|
|
|
|
2024-11-25 17:20:00 +03:00
|
|
|
def update_settings(self, data: list[dict]) -> None:
|
2024-12-18 19:43:46 +03:00
|
|
|
if self.isActive:
|
|
|
|
|
self.stop()
|
|
|
|
|
_, system_params = data
|
|
|
|
|
self._directory_path = system_params['trace_storage_path'][0]
|
|
|
|
|
self._update_time = system_params['monitor_update_period'][0]
|
2024-12-23 14:18:54 +03:00
|
|
|
|
|
|
|
|
if not os.path.exists(self._directory_path):
|
|
|
|
|
logger.error(f"Путь {self._directory_path} не существует.")
|
|
|
|
|
raise FileNotFoundError(f"Путь {self._directory_path} не существует.")
|
|
|
|
|
else:
|
|
|
|
|
self._init_state()
|
|
|
|
|
self.start()
|
2024-12-18 19:43:46 +03:00
|
|
|
else:
|
|
|
|
|
_, system_params = data
|
|
|
|
|
self._directory_path = system_params['trace_storage_path'][0]
|
|
|
|
|
self._update_time = system_params['monitor_update_period'][0]
|
|
|
|
|
|
|
|
|
|
def update_plots(self):
|
|
|
|
|
if self._files is not None:
|
|
|
|
|
self._mediator.notify(self, self._files)
|
2024-11-25 17:20:00 +03:00
|
|
|
|
2024-12-18 19:43:46 +03:00
|
|
|
def custom_csv_extract_only(self, path: str):
|
2024-12-19 14:25:11 +03:00
|
|
|
self._files.append(path)
|
2024-12-18 19:43:46 +03:00
|
|
|
if path is not None:
|
|
|
|
|
self._mediator.notify(self, [path])
|
2024-12-19 13:23:27 +03:00
|
|
|
else:
|
2024-12-25 16:09:17 +03:00
|
|
|
self._mediator.notify(self, [''])
|
2024-12-24 10:37:34 +03:00
|
|
|
|
|
|
|
|
def start_raport(self) -> None:
|
|
|
|
|
self.stop()
|
2024-12-24 16:25:50 +03:00
|
|
|
self._files = ['']
|
2024-12-25 16:09:17 +03:00
|
|
|
self._mediator.notify(self, [''])
|
2024-12-19 13:23:27 +03:00
|
|
|
|
2024-12-18 19:43:46 +03:00
|
|
|
|
|
|
|
|
def start_seeking(self) -> None:
|
|
|
|
|
self._init_state()
|
|
|
|
|
self.start()
|
2024-11-25 17:20:00 +03:00
|
|
|
|