Skip to content

Commit 7135e25

Browse files
committed
Add logging to state mutations
1 parent 2bb4a1d commit 7135e25

3 files changed

Lines changed: 31 additions & 0 deletions

File tree

src/routers/openml/datasets.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from typing import Annotated, Any, Literal, NamedTuple
66

77
from fastapi import APIRouter, Body, Depends
8+
from loguru import logger
89
from sqlalchemy import bindparam, text
910
from sqlalchemy.engine import Row
1011
from sqlalchemy.ext.asyncio import AsyncConnection
@@ -61,6 +62,7 @@ async def tag_dataset(
6162
raise TagAlreadyExistsError(msg)
6263

6364
await database.datasets.tag(data_id, tag, user_id=user.user_id, connection=expdb_db)
65+
logger.info("Dataset {dataset_id} tagged '{tag}'.", dataset_id=data_id, tag=tag)
6466
return {
6567
"data_tag": {"id": str(data_id), "tag": [*tags, tag]},
6668
}
@@ -375,6 +377,12 @@ async def update_dataset_status(
375377
msg = f"Unknown status transition: {current_status} -> {status}"
376378
raise InternalError(msg)
377379

380+
logger.info(
381+
"Dataset {dataset_id} changed from {previous} to {current}",
382+
dataset_id=dataset_id,
383+
previous=current_status.status if current_status else DatasetStatus.IN_PREPARATION,
384+
current=status,
385+
)
378386
return {"dataset_id": dataset_id, "status": status}
379387

380388

src/routers/openml/setups.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
from typing import Annotated
55

66
from fastapi import APIRouter, Body, Depends, Path
7+
from loguru import logger
78
from sqlalchemy.ext.asyncio import AsyncConnection
89

910
import database.setups
@@ -65,6 +66,7 @@ async def tag_setup(
6566
raise TagAlreadyExistsError(msg)
6667

6768
await database.setups.tag(setup_id, tag, user.user_id, expdb_db)
69+
logger.info("Setup {setup_id} tagged '{tag}'.", setup_id=setup_id, tag=tag)
6870
all_tags = [t.tag for t in setup_tags] + [tag]
6971
return {"setup_tag": {"id": str(setup_id), "tag": all_tags}}
7072

@@ -94,9 +96,15 @@ async def untag_setup(
9496
msg = (
9597
f"You may not remove tag {tag!r} of setup {setup_id} because it was not created by you."
9698
)
99+
logger.warning(
100+
"User attempted to remove tag '{tag}' from setup {setup_id}.",
101+
setup_id=setup_id,
102+
tag=tag,
103+
)
97104
raise TagNotOwnedError(msg)
98105

99106
await database.setups.untag(setup_id, matched_tag_row.tag, expdb_db)
107+
logger.info("Setup {setup_id} had tag '{tag}' removed.", setup_id=setup_id, tag=tag)
100108
remaining_tags = [
101109
t.tag for t in setup_tags if t.tag.casefold() != matched_tag_row.tag.casefold()
102110
]

src/routers/openml/study.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from typing import Annotated, Literal
22

33
from fastapi import APIRouter, Body, Depends
4+
from loguru import logger
45
from pydantic import BaseModel
56
from sqlalchemy.engine import Row
67
from sqlalchemy.ext.asyncio import AsyncConnection
@@ -73,6 +74,11 @@ async def attach_to_study(
7374
# PHP lets *anyone* edit *any* study. We're not going to do that.
7475
if study.creator != user.user_id and not await user.is_admin():
7576
msg = f"Study {study_id} can only be edited by its creator."
77+
logger.warning(
78+
"User attempted to attach entities to study they do not own.",
79+
study_id=study_id,
80+
entity_ids=entity_ids,
81+
)
7682
raise StudyNotEditableError(msg)
7783
if study.status != StudyStatus.IN_PREPARATION:
7884
msg = f"Study {study_id} can only be edited while in preparation."
@@ -93,6 +99,11 @@ async def attach_to_study(
9399
except ValueError as e:
94100
msg = str(e)
95101
raise StudyConflictError(msg) from e
102+
logger.info(
103+
"User {user_id} attached entities to study {study_id}.",
104+
study_id=study_id,
105+
entity_ids=entity_ids,
106+
)
96107
return AttachDetachResponse(study_id=study_id, main_entity_type=study.type_)
97108

98109

@@ -124,6 +135,10 @@ async def create_study(
124135
user=user,
125136
expdb=expdb,
126137
)
138+
logger.info(
139+
"User {user_id} created study {study_id}.",
140+
study_id=study_id,
141+
)
127142
# Make sure that invalid fields raise an error (e.g., "task_ids")
128143
return {"study_id": study_id}
129144

0 commit comments

Comments
 (0)