Skip to content

Commit

Permalink
Fix dvas delete
Browse files Browse the repository at this point in the history
  • Loading branch information
tukiains committed Nov 7, 2023
1 parent 480efc7 commit 1cc0a6b
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 13 deletions.
35 changes: 24 additions & 11 deletions 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 @@ -49,18 +50,12 @@ def upload(self, md_api: MetadataApi, file: dict):
logging.error(f"Failed to upload {file['filename']} to DVAS")
logging.debug(err)

def get(self, pid: str) -> dict:
"""Fetch metadata for a single Cloudnet file from DVAS API"""
url = f"{self.DVAS_URL}/pid/{pid}/type/Handle"
res = self.session.get(url)
if not res.ok:
raise DvasError(f"GET failed: {res.status_code} {res.text}")
return res.json()

def delete(self, identifier: int):
def delete(self, file: dict):
"""Delete Cloudnet file metadata from DVAS API"""
logging.warning(f"Deleting Cloudnet file {identifier} from DVAS")
url = f"{self.DVAS_URL}/delete/{identifier}"
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)

def delete_all(self):
Expand All @@ -79,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
3 changes: 1 addition & 2 deletions src/data_processing/subcmds/upload_to_dvas.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,7 @@ def _delete_clu_data(md_api: MetadataApi, files: list[dict]):
dvas = Dvas()
for file in files:
try:
dvas_metadata = dvas.get(file["pid"])
dvas.delete(dvas_metadata["id"])
dvas.delete(file)
except DvasError as err:
logging.error(f"Failed to process {file['uuid']}: {err}")
finally:
Expand Down

0 comments on commit 1cc0a6b

Please sign in to comment.