From a49d758f5e5c23294c068fa5a2aad26dec2cc9c7 Mon Sep 17 00:00:00 2001 From: Justin Date: Wed, 6 Mar 2024 15:40:44 -0600 Subject: [PATCH] Improve logging --- .../migrations/0001_initial.py | 3 +- fractal_database_matrix/models.py | 4 ++- fractal_database_matrix/representations.py | 28 +++++++++++++------ 3 files changed, 25 insertions(+), 10 deletions(-) diff --git a/fractal_database_matrix/migrations/0001_initial.py b/fractal_database_matrix/migrations/0001_initial.py index 70d839d..a337ef9 100644 --- a/fractal_database_matrix/migrations/0001_initial.py +++ b/fractal_database_matrix/migrations/0001_initial.py @@ -1,4 +1,4 @@ -# Generated by Django 5.0.2 on 2024-02-22 21:00 +# Generated by Django 5.0.2 on 2024-03-06 19:33 import django.db.models.deletion from django.db import migrations, models @@ -50,6 +50,7 @@ class Migration(migrations.Migration): ('object_version', models.PositiveIntegerField(default=0)), ('name', models.CharField(max_length=255)), ('enabled', models.BooleanField(default=True)), + ('filter', models.CharField(blank=True, max_length=255, null=True)), ('primary', models.BooleanField(default=False)), ('metadata', models.JSONField(default=dict)), ('registration_token', models.CharField(blank=True, max_length=255, null=True)), diff --git a/fractal_database_matrix/models.py b/fractal_database_matrix/models.py index 50d67c8..3e95a9e 100644 --- a/fractal_database_matrix/models.py +++ b/fractal_database_matrix/models.py @@ -78,7 +78,9 @@ async def push_replication_log(self, fixture: List[Dict[str, Any]]) -> None: return room_id = self.metadata["room_id"] - print(f"Pushing fixture(s): {replication_event} to {room_id}") + logger.info( + "Target %s is pushing fixture(s): %s to room %s" % (self, replication_event, room_id) + ) creds = await self.aget_creds() broker = MatrixBroker().with_matrix_config( room_id=room_id, diff --git a/fractal_database_matrix/representations.py b/fractal_database_matrix/representations.py index 8f8a30e..f98f740 100644 --- a/fractal_database_matrix/representations.py +++ b/fractal_database_matrix/representations.py @@ -1,11 +1,10 @@ +import logging from copy import deepcopy from typing import TYPE_CHECKING, Any, Optional, Sequence -from uuid import UUID -from django.core.serializers import serialize from fractal.matrix import MatrixClient from fractal_database.representations import Representation -from nio import AsyncClient, RoomCreateError, RoomPutStateError, RoomVisibility +from nio import RoomCreateError, RoomPutStateError, RoomVisibility if TYPE_CHECKING: from fractal_database.models import ( @@ -15,6 +14,8 @@ ) from fractal_database_matrix.models import MatrixReplicationTarget +logger = logging.getLogger(__name__) + class MatrixRepresentation(Representation): module = __name__ @@ -101,7 +102,10 @@ async def create_room( for account in invite: await client.invite(account, room_id, admin=True) - print(f"Successfully created representation of {name} in Matrix: {room_id}") + logger.info( + "Successfully created %s for %s in Matrix: %s" + % ("Room" if not space else "Space", name, room_id) + ) return res.room_id @@ -127,8 +131,9 @@ async def add_subspace( if isinstance(res, RoomPutStateError): raise Exception(res.message) - print( - f"Successfully added child space {child_room_id} to parent space {parent_room_id}" + logger.info( + "Successfully added child space %s to parent space %s" + % (child_room_id, parent_room_id) ) @@ -165,7 +170,7 @@ async def create_representation( for account in target.matrixcredentials_set.all(): await account.accept_invite(room_id, target) - print("Created Matrix room for", name) + logger.info("Successfully created Matrix Room representation for %s" % name) return {"room_id": room_id} @@ -196,6 +201,10 @@ async def create_representation( .aget(pk=repr_log.target_id) ) # type: ignore + logger.info( + "Creating Matrix space for %s on target %s" % (repr_log.metadata["name"], target) + ) + matrix_ids_to_invite = [cred.matrix_id for cred in target.matrixcredentials_set.all()] initial_state = deepcopy(self.initial_state) room_id = await self.create_room( @@ -231,7 +240,10 @@ async def create_representation( await self.put_state(room_id, target, "f.database", initial_state[0]["content"]) await self.put_state(room_id, target, "f.database.target", initial_state[1]["content"]) - print("Created Matrix space for", name) + logger.info( + "Successfully created Matrix Space representation for %s on target %s" + % (name, target) + ) return {"room_id": room_id, "devices_room_id": devices_room_id}