Skip to content

Commit

Permalink
Improve logging
Browse files Browse the repository at this point in the history
  • Loading branch information
justin-russell committed Mar 6, 2024
1 parent 5247a40 commit a49d758
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 10 deletions.
3 changes: 2 additions & 1 deletion fractal_database_matrix/migrations/0001_initial.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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)),
Expand Down
4 changes: 3 additions & 1 deletion fractal_database_matrix/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
28 changes: 20 additions & 8 deletions fractal_database_matrix/representations.py
Original file line number Diff line number Diff line change
@@ -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 (
Expand All @@ -15,6 +14,8 @@
)
from fractal_database_matrix.models import MatrixReplicationTarget

logger = logging.getLogger(__name__)


class MatrixRepresentation(Representation):
module = __name__
Expand Down Expand Up @@ -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

Expand All @@ -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)
)


Expand Down Expand Up @@ -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}


Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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}


Expand Down

0 comments on commit a49d758

Please sign in to comment.