Skip to content

Commit

Permalink
simplify logs
Browse files Browse the repository at this point in the history
  • Loading branch information
FynnBe committed Jun 13, 2024
1 parent e0a4db2 commit eea7ca6
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 111 deletions.
11 changes: 2 additions & 9 deletions bioimageio_collection_backoffice/_backoffice.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from ._settings import settings
from .backup import backup
from .db_structure.chat import Chat, Message
from .db_structure.log import Log, LogContent, LogEntry
from .db_structure.log import Log, LogEntry
from .gh_utils import set_gh_actions_outputs
from .mailroom.send_email import notify_uploader
from .remote_collection import (
Expand Down Expand Up @@ -46,14 +46,7 @@ def log(self, message: str, concept_id: str, version: str):
raise ValueError("'RUN_URL' not set")

rv = get_remote_resource_version(self.client, concept_id, version)

rv.extend_log(
Log(
collection=[
LogEntry(log=LogContent(message=message, run_url=settings.run_url))
]
)
)
rv.extend_log(Log(entries=[LogEntry(message=message)]))

def wipe(self, subfolder: str = ""):
"""DANGER ZONE: wipes `subfolder` completely, only use for test folders!"""
Expand Down
38 changes: 17 additions & 21 deletions bioimageio_collection_backoffice/db_structure/log.py
Original file line number Diff line number Diff line change
@@ -1,45 +1,41 @@
from __future__ import annotations

import collections.abc
from datetime import datetime
from typing import Any, ClassVar, Dict, Optional, Sequence, Union
from typing import Any, ClassVar, Optional, Sequence

from pydantic import Field

from .._settings import settings
from ..common import Node


class LogContent(Node, frozen=True, extra="ignore"):
class LogEntry(Node, frozen=True, extra="ignore"):
message: str = ""
details: Any = None
run_url: Optional[str] = settings.run_url
"""log message"""

details: Any = None
"""log details"""

class LogEntry(Node, frozen=True, extra="ignore"):
timestamp: datetime = Field(default_factory=datetime.now)
"""creation of log entry"""

log: LogContent
"""log content"""
run_url: Optional[str] = settings.run_url
"""gh action run url"""


class Log(Node, frozen=True, extra="allow"):
class Log(Node, frozen=True, extra="ignore"):
"""`<concept_id>/<version>/log.json` contains a version specific log"""

file_name: ClassVar[str] = "log.json"

bioimageio_spec: Sequence[LogEntry] = Field(default_factory=list)
bioimageio_core: Sequence[LogEntry] = Field(default_factory=list)
collection: Sequence[LogEntry] = Field(default_factory=list)
log_version: str = "0.1.0"
entries: Sequence[LogEntry] = Field(default_factory=list)

def get_updated(self, update: Log) -> Log:
v: Union[Any, Sequence[Any]]
data: Dict[str, Sequence[Any]] = {}
for k, v in update:
assert isinstance(v, collections.abc.Sequence)
old = getattr(self, k, ())
assert isinstance(old, collections.abc.Sequence)
data[k] = list(old) + list(v)

return Log.model_validate(data)
if update.log_version != self.log_version:
return update

return Log(
log_version=update.log_version,
entries=list(self.entries) + list(update.entries),
)
18 changes: 3 additions & 15 deletions bioimageio_collection_backoffice/remote_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from loguru import logger

from .db_structure.chat import Chat
from .db_structure.log import CollectionLog, Log, LogContent
from .db_structure.log import Log, LogEntry
from .db_structure.reserved import Reserved
from .db_structure.version_info import DraftInfo, RecordInfo
from .s3_client import Client
Expand Down Expand Up @@ -48,13 +48,7 @@ def log(self) -> Log:
return self._get_json(Log)

def log_message(self, message: str, details: Optional[Any] = None):
self._update_json(
Log(
collection=[
CollectionLog(log=LogContent(message=message, details=details))
]
)
)
self._update_json(Log(entries=[LogEntry(message=message, details=details)]))

def log_error(self, error: Union[Exception, str], details: Optional[Any] = None):
if isinstance(error, Exception):
Expand All @@ -66,10 +60,4 @@ def log_error(self, error: Union[Exception, str], details: Optional[Any] = None)
if isinstance(details, dict) and "traceback" not in details:
details["traceback"] = traceback.format_stack()

self._update_json(
Log(
collection=[
CollectionLog(log=LogContent(message=error, details=details))
]
)
)
self._update_json(Log(entries=[LogEntry(message=error, details=details)]))
56 changes: 16 additions & 40 deletions bioimageio_collection_backoffice/remote_collection.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
TestSummary,
TestSummaryEntry,
)
from .db_structure.log import Log, LogContent, LogEntry
from .db_structure.log import Log, LogEntry
from .db_structure.reserved import Reserved
from .db_structure.version_info import (
AcceptedStatus,
Expand Down Expand Up @@ -560,6 +560,10 @@ def rdf_url(self) -> str:
def chat(self) -> Chat:
return self._get_json(Chat)

def add_log_entry(self, log_entry: LogEntry):
"""add a log entry"""
self.extend_log(Log(entries=[log_entry]))

def extend_log(
self,
extension: Log,
Expand Down Expand Up @@ -641,16 +645,10 @@ def unpack(self, package_url: str):
# ensure we have a chat.json
self.extend_chat(Chat())

self.extend_log(
Log(
collection=[
LogEntry(
log=LogContent(
message="new status: unpacking",
details={"package_url": package_url},
)
)
]
self.add_log_entry(
LogEntry(
message="new status: unpacking",
details={"package_url": package_url},
)
)

Expand Down Expand Up @@ -697,32 +695,18 @@ def unpack(self, package_url: str):
for e in collection["collection"]:
if e["name"] == rdf["name"]:
if e["id"] != rdf["id"]:
self.extend_log(
Log(
collection=[
LogEntry(
log=LogContent(
message=f"error: Another resource with name='{rdf['name']}' already exists ({e['id']})"
)
)
]
self.add_log_entry(
LogEntry(
message=f"error: Another resource with name='{rdf['name']}' already exists ({e['id']})"
)
)
break

# set matching id_emoji
rdf["id_emoji"] = self.collection.config.id_parts.get_icon(self.id)
if rdf["id_emoji"] is None:
self.extend_log(
Log(
collection=[
LogEntry(
log=LogContent(
message=f"error: Failed to get icon for {self.id}"
)
)
]
)
self.add_log_entry(
LogEntry(message=f"error: Failed to get icon for {self.id}")
)

if "id" not in rdf:
Expand Down Expand Up @@ -880,16 +864,8 @@ def publish(self, reviewer: str) -> Record:

def _set_status(self, value: DraftStatus):
current_status = self.info.status
self.extend_log(
Log(
collection=[
LogEntry(
log=LogContent(
message=f"set new status: {value.name}", details=value
)
)
]
)
self.add_log_entry(
LogEntry(message=f"new status: {value.description}", details=value)
)
if value.name == "testing" or current_status is None:
pass
Expand Down
18 changes: 3 additions & 15 deletions bioimageio_collection_backoffice/run_dynamic_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
CompatiblityReport,
)

from .db_structure.log import Log, LogContent, LogEntry
from .db_structure.log import LogEntry
from .remote_collection import Record, RecordDraft

try:
Expand Down Expand Up @@ -53,13 +53,7 @@ def run_dynamic_tests(
)
summary = _run_dynamic_tests_impl(staged.rdf_url, weight_format, create_env_outcome)
if summary is not None:
staged.extend_log(
Log(
bioimageio_core=[
LogEntry(log=LogContent(message=summary.name, details=summary))
]
)
)
staged.add_log_entry(LogEntry(message=summary.name, details=summary))


def rerun_dynamic_tests(
Expand All @@ -71,13 +65,7 @@ def rerun_dynamic_tests(
published.rdf_url, weight_format, create_env_outcome
)
if summary is not None:
published.extend_log(
Log(
bioimageio_core=[
LogEntry(log=LogContent(message=summary.name, details=summary))
]
)
)
published.add_log_entry(LogEntry(message=summary.name, details=summary))
report = CompatiblityReport(
tool="bioimageio.core",
status=summary.status,
Expand Down
16 changes: 5 additions & 11 deletions bioimageio_collection_backoffice/validate_format.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from ruyaml import YAML
from typing_extensions import assert_never

from .db_structure.log import Log, LogContent, LogEntry
from .db_structure.log import LogEntry
from .remote_collection import Record, RecordDraft

yaml = YAML(typ="safe")
Expand Down Expand Up @@ -319,16 +319,10 @@ def validate_format(rv: Union[RecordDraft, Record]):
)
)

rv.extend_log(
Log(
bioimageio_spec=[
LogEntry(
log=LogContent(
message=rd.validation_summary.name,
details=rd.validation_summary,
)
)
]
rv.add_log_entry(
LogEntry(
message=rd.validation_summary.name,
details=rd.validation_summary,
)
)
return dynamic_test_cases, conda_envs
Expand Down

0 comments on commit eea7ca6

Please sign in to comment.