01

Sample Code

activity-trail.py
from psp.platform.audit import (
    ActivityRecord, EntityRef, InMemoryActivityLogRepo
)
from uuid import uuid4

log = InMemoryActivityLogRepo()

# Two users working on tasks
alice = uuid4()
bob = uuid4()
task_id = uuid4()

# Alice creates a task
log.append(ActivityRecord.create(
    actor_id=alice,
    owner_id=alice,
    action="create_task",
    entity_ref=EntityRef("Task", task_id),
    metadata={"title": "Write docs"},
))

# Bob updates the task
log.append(ActivityRecord.create(
    actor_id=bob,
    owner_id=alice,  # Alice still owns it
    action="update_task",
    entity_ref=EntityRef("Task", task_id),
))

# Alice completes the task
log.append(ActivityRecord.create(
    actor_id=alice,
    owner_id=alice,
    action="complete_task",
    entity_ref=EntityRef("Task", task_id),
))

# Query by entity - full history
history = log.get_by_entity(EntityRef("Task", task_id))
# Returns: complete_task, update_task, create_task (most recent first)

# Query by actor - what did Bob do?
bob_actions = log.get_by_actor(bob)
# Returns: update_task

# Query by owner - all activity on Alice's data
alice_feed = log.get_by_owner(alice)
# Returns: all 3 records