Collection management (#520)

* Tech spec

* Refactored Cassanda knowledge graph for single table

* Collection management, librarian services to manage metadata and collection deletion
This commit is contained in:
cybermaggedon 2025-09-18 15:57:52 +01:00 committed by GitHub
parent 48016d8fb2
commit 13ff7d765d
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
48 changed files with 2941 additions and 425 deletions

View file

@ -0,0 +1,72 @@
"""
Delete a collection and all its data
"""
import argparse
import os
from trustgraph.api import Api
default_url = os.getenv("TRUSTGRAPH_URL", 'http://localhost:8088/')
default_user = "trustgraph"
def delete_collection(url, user, collection, confirm):
if not confirm:
response = input(f"Are you sure you want to delete collection '{collection}' and all its data? (y/N): ")
if response.lower() not in ['y', 'yes']:
print("Operation cancelled.")
return
api = Api(url).collection()
api.delete_collection(user=user, collection=collection)
print(f"Collection '{collection}' deleted successfully.")
def main():
parser = argparse.ArgumentParser(
prog='tg-delete-collection',
description=__doc__,
)
parser.add_argument(
'collection',
help='Collection ID to delete'
)
parser.add_argument(
'-u', '--api-url',
default=default_url,
help=f'API URL (default: {default_url})',
)
parser.add_argument(
'-U', '--user',
default=default_user,
help=f'User ID (default: {default_user})'
)
parser.add_argument(
'-y', '--yes',
action='store_true',
help='Skip confirmation prompt'
)
args = parser.parse_args()
try:
delete_collection(
url = args.api_url,
user = args.user,
collection = args.collection,
confirm = args.yes
)
except Exception as e:
print("Exception:", e, flush=True)
if __name__ == "__main__":
main()

View file

@ -0,0 +1,85 @@
"""
List collections for a user
"""
import argparse
import os
import tabulate
from trustgraph.api import Api
import json
default_url = os.getenv("TRUSTGRAPH_URL", 'http://localhost:8088/')
default_user = "trustgraph"
def list_collections(url, user, tag_filter):
api = Api(url).collection()
collections = api.list_collections(user=user, tag_filter=tag_filter)
if len(collections) == 0:
print("No collections.")
return
table = []
for collection in collections:
table.append([
collection.collection,
collection.name,
collection.description,
", ".join(collection.tags),
collection.created_at,
collection.updated_at
])
headers = ["Collection", "Name", "Description", "Tags", "Created", "Updated"]
print(tabulate.tabulate(
table,
headers=headers,
tablefmt="pretty",
stralign="left",
maxcolwidths=[20, 30, 50, 30, 19, 19],
))
def main():
parser = argparse.ArgumentParser(
prog='tg-list-collections',
description=__doc__,
)
parser.add_argument(
'-u', '--api-url',
default=default_url,
help=f'API URL (default: {default_url})',
)
parser.add_argument(
'-U', '--user',
default=default_user,
help=f'User ID (default: {default_user})'
)
parser.add_argument(
'-t', '--tag-filter',
action='append',
help='Filter by tags (can be specified multiple times)'
)
args = parser.parse_args()
try:
list_collections(
url = args.api_url,
user = args.user,
tag_filter = args.tag_filter
)
except Exception as e:
print("Exception:", e, flush=True)
if __name__ == "__main__":
main()

View file

@ -0,0 +1,103 @@
"""
Update collection metadata
"""
import argparse
import os
import tabulate
from trustgraph.api import Api
default_url = os.getenv("TRUSTGRAPH_URL", 'http://localhost:8088/')
default_user = "trustgraph"
def update_collection(url, user, collection, name, description, tags):
api = Api(url).collection()
result = api.update_collection(
user=user,
collection=collection,
name=name,
description=description,
tags=tags
)
if result:
print(f"Collection '{collection}' updated successfully.")
table = []
table.append(("Collection", result.collection))
table.append(("Name", result.name))
table.append(("Description", result.description))
table.append(("Tags", ", ".join(result.tags)))
table.append(("Updated", result.updated_at))
print(tabulate.tabulate(
table,
tablefmt="pretty",
stralign="left",
maxcolwidths=[None, 67],
))
else:
print(f"Failed to update collection '{collection}'.")
def main():
parser = argparse.ArgumentParser(
prog='tg-update-collection',
description=__doc__,
)
parser.add_argument(
'collection',
help='Collection ID to update'
)
parser.add_argument(
'-u', '--api-url',
default=default_url,
help=f'API URL (default: {default_url})',
)
parser.add_argument(
'-U', '--user',
default=default_user,
help=f'User ID (default: {default_user})'
)
parser.add_argument(
'-n', '--name',
help='Collection name'
)
parser.add_argument(
'-d', '--description',
help='Collection description'
)
parser.add_argument(
'-t', '--tag',
action='append',
dest='tags',
help='Collection tags (can be specified multiple times)'
)
args = parser.parse_args()
try:
update_collection(
url = args.api_url,
user = args.user,
collection = args.collection,
name = args.name,
description = args.description,
tags = args.tags
)
except Exception as e:
print("Exception:", e, flush=True)
if __name__ == "__main__":
main()