diff --git a/plesna/storage/metadata_repository/fs_metadata_repository.py b/plesna/storage/metadata_repository/fs_metadata_repository.py index 4d9f408..ba07431 100644 --- a/plesna/storage/metadata_repository/fs_metadata_repository.py +++ b/plesna/storage/metadata_repository/fs_metadata_repository.py @@ -21,8 +21,10 @@ class FSMetaDataRepository(MetaDataRepository): """ - EXECUTION_FILEMODEL = "{flux_id}_execution.csv" - MODIFICATION_FILEMODEL = "{table_id}_modification.csv" + FILEMODEL = { + "execution": "{flux_id}_execution.csv", + "modification": "{table_id}_modification.csv", + } def __init__(self, basepath: str): super().__init__() @@ -36,7 +38,7 @@ class FSMetaDataRepository(MetaDataRepository): def add_flux(self, flux_id: str) -> str: """Get the flux metadata""" - filepath = self._basepath / self.EXECUTION_FILEMODEL.format(flux_id=flux_id) + filepath = self._basepath / self.FILEMODEL["execution"].format(flux_id=flux_id) filepath.touch() with open(filepath, "a") as csvfile: writer = csv.DictWriter(csvfile, fieldnames=ExecutionLog.model_fields.keys()) @@ -45,7 +47,7 @@ class FSMetaDataRepository(MetaDataRepository): def register_flux_execution(self, flux_id: str, dt: datetime, output: dict) -> ExecutionLog: """Get the flux metadata""" - filepath = self._basepath / self.EXECUTION_FILEMODEL.format(flux_id=flux_id) + filepath = self._basepath / self.FILEMODEL["execution"].format(flux_id=flux_id) metadata_ = ExecutionLog(datetime=dt, output={"data": output}) if not filepath.exists: raise FSMetaDataRepositoryError(f"The flux {flux_id} hasn't been added yet.") @@ -57,12 +59,12 @@ class FSMetaDataRepository(MetaDataRepository): return metadata_ def flux(self, flux_id: str) -> ExecutionLog: - """Get the flux metadata""" - raise NotImplementedError + """Get the last flux log""" + return max(self.flux_logs(flux_id), key=lambda l: l.datetime) def flux_logs(self, flux_id: str) -> list[ExecutionLog]: - """Get the flux metadata""" - filepath = self._basepath / self.EXECUTION_FILEMODEL.format(flux_id=flux_id) + """Get all flux logs""" + filepath = self._basepath / self.FILEMODEL["execution"].format(flux_id=flux_id) if not filepath.exists: raise FSMetaDataRepositoryError(f"The flux {flux_id} hasn't been added yet.") with open(filepath, "r") as csvfile: diff --git a/tests/storage/test_fs_metadata_repository.py b/tests/storage/test_fs_metadata_repository.py index 8e16ab1..c587525 100644 --- a/tests/storage/test_fs_metadata_repository.py +++ b/tests/storage/test_fs_metadata_repository.py @@ -30,7 +30,9 @@ def test_add_flux(location, metadata_repository): flux_id = "my_flux" metadata_repository.add_flux(flux_id) - metadata_filepath = location / metadata_repository.EXECUTION_FILEMODEL.format(flux_id=flux_id) + metadata_filepath = location / metadata_repository.FILEMODEL["execution"].format( + flux_id=flux_id + ) assert metadata_filepath.exists() with open(metadata_filepath, "r") as csvfile: @@ -50,7 +52,9 @@ def test_register_flux_execution(location, metadata_repository): }, ) - metadata_filepath = location / metadata_repository.EXECUTION_FILEMODEL.format(flux_id=flux_id) + metadata_filepath = location / metadata_repository.FILEMODEL["execution"].format( + flux_id=flux_id + ) with open(metadata_filepath, "r") as csvfile: content = csvfile.read() assert ( @@ -58,7 +62,7 @@ def test_register_flux_execution(location, metadata_repository): ) -def test_register_and_get_flux_execution(location, metadata_repository): +def test_register_and_get_logs(metadata_repository): flux_id = "my_flux" metadata_repository.add_flux(flux_id) @@ -86,3 +90,27 @@ def test_register_and_get_flux_execution(location, metadata_repository): output=FluxMetaData(data={"truc": "chose"}), ), ] + + +def test_register_and_get_last_log(metadata_repository): + flux_id = "my_flux" + metadata_repository.add_flux(flux_id) + + metadata_repository.register_flux_execution( + flux_id, + datetime(2023, 3, 15, 14, 30), + output={"truc": "machin"}, + ) + metadata_repository.register_flux_execution( + flux_id, + datetime(2024, 3, 15, 14, 30), + output={ + "truc": "chose", + }, + ) + + logs = metadata_repository.flux(flux_id) + assert logs == ExecutionLog( + datetime=datetime(2024, 3, 15, 14, 30), + output=FluxMetaData(data={"truc": "chose"}), + )