Skip to content

Commit

Permalink
Delete old versions before posting new
Browse files Browse the repository at this point in the history
  • Loading branch information
tukiains committed Nov 7, 2023
1 parent 64f4783 commit 41d9f47
Showing 1 changed file with 22 additions and 1 deletion.
23 changes: 22 additions & 1 deletion src/data_processing/dvas.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)

Expand All @@ -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:
Expand Down

0 comments on commit 41d9f47

Please sign in to comment.