Skip to content

Commit

Permalink
fix delete_unused_subtemplate and improve perf
Browse files Browse the repository at this point in the history
  • Loading branch information
Regamey Benoît committed Feb 26, 2024
1 parent a047983 commit 2632e7f
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 13 deletions.
6 changes: 6 additions & 0 deletions docs/cli_tools/Delete_unused_subtemplates.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@ run this script, you should have admin rights and be able to connect to the Post

> The admin boundaries extent subtemplate are not deleted by this tool even if not used in any metadata. Admin boundaries extent are managed by the geocat team and should be available for everyone
When running the script, after analysing all subtemplates, it will show the number of unused subtemplates by contact, extent and format. For each subtemplate type, it will prompt if you want to delete them by ressing y (yes) or not by pressing n (no). Thus it's possible to have control over what type of subtemplate to delete. For example :

```console
3 contact found. Are you sure to delete them ? (y/n)
```

## Database connection
You can specify the username and password for connecting to the database in environment variables or in CLI parameters (see below).
If not, the script will prompt for credentials.
Expand Down
1 change: 0 additions & 1 deletion geopycat/subtemplate/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +0,0 @@
from geopycat.subtemplate.subtemplate import ManageSubtemplate
23 changes: 12 additions & 11 deletions geopycat/subtemplate/delete_unused_subtpl.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,21 +64,22 @@ def __get_unused_subtemplates(self) -> dict:
f"and changedate < '{self.date_limit.strftime('%Y-%m-%d')}'"
)

ro_uuids = cursor.fetchall()
count = 0

for row in cursor:
with connection.cursor() as cur2:
cur2.execute(f"SELECT uuid FROM public.metadata WHERE data ~ '.*{row[0]}[^0-9].*'")
if cur2.rowcount == 0:
if row[1].startswith("<che:CHE_CI_ResponsibleParty"):
uuids_contact.append(row[0])
elif row[1].startswith("<gmd:EX_Extent"):
uuids_extent.append(row[0])
elif row[1].startswith("<gmd:MD_Format"):
uuids_format.append(row[0])
for row in ro_uuids:

cursor.execute(f"SELECT uuid FROM public.metadata WHERE data like '%{row[0]}%'")
if cursor.rowcount == 0:
if row[1].startswith("<che:CHE_CI_ResponsibleParty"):
uuids_contact.append(row[0])
elif row[1].startswith("<gmd:EX_Extent"):
uuids_extent.append(row[0])
elif row[1].startswith("<gmd:MD_Format"):
uuids_format.append(row[0])

count += 1
print(f"Analysing RO usage: {round((count / cursor.rowcount) * 100, 1)}%", end="\r")
print(f"Analysing RO usage: {round((count / len(ro_uuids)) * 100, 1)}%", end="\r")
print(f"Analysing RO usage : {utils.okgreen('Done')}")

except (Exception, psycopg2.Error) as error:
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

setuptools.setup(
name="geopycat",
version="0.3.7",
version="0.3.8",
author="Benoit G. Regamey",
author_email="[email protected]",
description="Manage metadata and data of geocat.ch - a geonetwork instance for Switzerland",
Expand Down

0 comments on commit 2632e7f

Please sign in to comment.