Skip to content

Commit

Permalink
refactor: review ResultManager serialization
Browse files Browse the repository at this point in the history
  • Loading branch information
mtache committed Dec 26, 2024
1 parent 75e612c commit 49a8f8d
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 14 deletions.
4 changes: 2 additions & 2 deletions anta/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
from string import Formatter
from typing import TYPE_CHECKING, Any, Callable, ClassVar, Literal, TypeVar

from pydantic import BaseModel, ConfigDict, ValidationError, create_model
from pydantic import BaseModel, ConfigDict, Field, ValidationError, create_model

from anta import GITHUB_SUGGESTION
from anta.constants import KNOWN_EOS_ERRORS
Expand Down Expand Up @@ -385,7 +385,7 @@ class Input(BaseModel):
"""

model_config = ConfigDict(extra="forbid")
result_overwrite: ResultOverwrite | None = None
result_overwrite: ResultOverwrite | None = Field(None, exclude=True)
filters: Filters | None = None

def __hash__(self) -> int:
Expand Down
12 changes: 7 additions & 5 deletions anta/result_manager/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,14 @@

from __future__ import annotations

import json
import logging
from collections import defaultdict
from functools import cached_property
from itertools import chain
from typing import Any

from pydantic import TypeAdapter

from anta.result_manager.models import AntaTestStatus, TestResult

from .models import CategoryStats, DeviceStats, TestStats
Expand Down Expand Up @@ -107,8 +108,9 @@ def __init__(self) -> None:

def reset(self) -> None:
"""Create or reset the attributes of the ResultManager instance."""
self._result_entries: list[TestResult] = []
self.status: AntaTestStatus = AntaTestStatus.UNSET
self._result_entries = []
self._result_entries_ta = TypeAdapter(list[TestResult])
self.status = AntaTestStatus.UNSET
self.error_status = False

# Initialize the statistics attributes
Expand All @@ -135,12 +137,12 @@ def results(self, value: list[TestResult]) -> None:
@property
def dump(self) -> list[dict[str, Any]]:
"""Get a list of dictionary of the results."""
return [result.model_dump() for result in self._result_entries]
return self._result_entries_ta.dump_python(self._result_entries)

@property
def json(self) -> str:
"""Get a JSON representation of the results."""
return json.dumps(self.dump, indent=4)
return self._result_entries_ta.dump_json(self._result_entries, exclude_none=True, indent=4).decode()

@property
def device_stats(self) -> defaultdict[str, DeviceStats]:
Expand Down
9 changes: 2 additions & 7 deletions anta/result_manager/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from enum import Enum
from typing import Any

from pydantic import BaseModel, SkipValidation, field_serializer
from pydantic import BaseModel, SerializeAsAny


class AntaTestStatus(str, Enum):
Expand Down Expand Up @@ -44,12 +44,7 @@ class BaseTestResult(BaseModel, ABC):
description: str
result: AntaTestStatus = AntaTestStatus.UNSET
messages: list[str] = []
inputs: SkipValidation[BaseModel | None] = None

@field_serializer("inputs")
def serialize_inputs(self, inputs: BaseModel | None) -> dict[str, Any] | None:
"""Serialize the inputs field to a dictionary."""
return inputs.model_dump(mode="json", serialize_as_any=True, exclude_none=True, exclude={"result_overwrite"}) if inputs else None
inputs: SerializeAsAny[BaseModel | None] = None

def is_success(self, message: str | None = None) -> None:
"""Set status to success.
Expand Down

0 comments on commit 49a8f8d

Please sign in to comment.