From 41d9f47e0f8aa13a4be6021a222dd7650777ee41 Mon Sep 17 00:00:00 2001 From: Simo Tukiainen Date: Tue, 7 Nov 2023 13:41:08 +0200 Subject: [PATCH] Delete old versions before posting new --- src/data_processing/dvas.py | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/src/data_processing/dvas.py b/src/data_processing/dvas.py index 30a8847c..d7f0ff4b 100644 --- a/src/data_processing/dvas.py +++ b/src/data_processing/dvas.py @@ -41,6 +41,7 @@ def upload(self, md_api: MetadataApi, file: dict): if len(dvas_json["md_content_information"]["attribute_descriptions"]) == 0: logging.error("Skipping - no ACTRIS variables") return + self._delete_old_versions(md_api, file) dvas_id = self._post(dvas_json) md_api.update_dvas_info( file["uuid"], dvas_json["md_metadata"]["datestamp"], dvas_id @@ -51,7 +52,9 @@ def upload(self, md_api: MetadataApi, file: dict): def delete(self, file: dict): """Delete Cloudnet file metadata from DVAS API""" - logging.warning(f"Deleting Cloudnet file {file['pid']} from DVAS") + logging.warning( + f"Deleting Cloudnet file {file['pid']} with dvasId {file['dvasId']} from DVAS" + ) url = f"{self.DVAS_URL}/delete/{file['dvasId']}" self._delete(url) @@ -71,6 +74,24 @@ def _delete(self, url: str): raise DvasError(res) logging.debug(f"DELETE successful: {res.status_code} {res.text}") + def _delete_old_versions(self, md_api: MetadataApi, file: dict): + # get all versions and delete before posting new version + versions = md_api.get( + "api/files", + {"filename": file["filename"], "allVersions": True, "showLegacy": True}, + ) + for version in versions: + if version["dvasId"] is None: + continue + logging.debug( + f"Deleting version {version['version']} of {file['filename']}" + ) + try: + self.delete(version) + except DvasError as err: + logging.error(f"Failed to delete {version['filename']} from DVAS") + logging.debug(err) + def _post(self, metadata: dict) -> int: res = self.session.post(f"{self.DVAS_URL}/add", json=metadata) if not res.ok: