Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 23 additions & 3 deletions kernelci/api/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
from datetime import datetime, timedelta
from typing import Any, Optional, Dict, List, ClassVar, Literal
import enum
import os
from operator import attrgetter
import json
from typing_extensions import Annotated
Expand Down Expand Up @@ -439,7 +440,7 @@
default='checkout',
description='Type of the object',
)
data: CheckoutData = Field(

Check failure on line 443 in kernelci/api/models.py

View workflow job for this annotation

GitHub Actions / Lint

Incompatible types in assignment (expression has type "CheckoutData", base class "Node" defined the type as "dict[str, Any] | None") [assignment]
description="Checkout details",
default=None
)
Expand Down Expand Up @@ -514,7 +515,7 @@
default='kbuild',
description='Type of the object',
)
data: KbuildData = Field(

Check failure on line 518 in kernelci/api/models.py

View workflow job for this annotation

GitHub Actions / Lint

Incompatible types in assignment (expression has type "KbuildData", base class "Node" defined the type as "dict[str, Any] | None") [assignment]
description="Kbuild details",
default=None
)
Expand Down Expand Up @@ -609,7 +610,7 @@
default='test',
description='Type of the object',
)
data: TestData = Field(

Check failure on line 613 in kernelci/api/models.py

View workflow job for this annotation

GitHub Actions / Lint

Incompatible types in assignment (expression has type "TestData", base class "Node" defined the type as "dict[str, Any] | None") [assignment]
description="Test details",
default=None
)
Expand All @@ -626,7 +627,7 @@
default='job',
description='Type of the object',
)
data: TestData = Field(

Check failure on line 630 in kernelci/api/models.py

View workflow job for this annotation

GitHub Actions / Lint

Incompatible types in assignment (expression has type "TestData", base class "Node" defined the type as "dict[str, Any] | None") [assignment]
description="Test suite details",
default=None
)
Expand Down Expand Up @@ -721,7 +722,7 @@
"FAIL if the regression is still 'active', ie. the test "
"is still failing"),
)
data: RegressionData = Field(

Check failure on line 725 in kernelci/api/models.py

View workflow job for this annotation

GitHub Actions / Lint

Incompatible types in assignment (expression has type "RegressionData", base class "Node" defined the type as "dict[str, Any] | None") [assignment]
description="Regression details",
default=None
)
Expand Down Expand Up @@ -855,12 +856,29 @@


# eventhistory model
# Environment variable to configure TTL (default 7 days = 604800 seconds)
# Set EVENT_HISTORY_TTL_SECONDS to override
EVENT_HISTORY_TTL_SECONDS = int(os.getenv('EVENT_HISTORY_TTL_SECONDS', '604800'))


class EventHistory(DatabaseModel):
"""Event history object model"""
timestamp: datetime = Field(
description='Timestamp of event creation',
default_factory=datetime.now
)
sequence_id: Optional[int] = Field(
default=None,
description='Sequential ID for pub/sub ordering (auto-generated)'
)
channel: Optional[str] = Field(
default='node',
description='Pub/Sub channel name'
)
owner: Optional[str] = Field(
default=None,
description='Username of event publisher'
)
data: Dict[str, Any] = Field(
description='Event data',
default={}
Expand All @@ -871,9 +889,11 @@
"""
Create the index with the expiresAfterSeconds option for the
timestamp field to automatically remove documents after a certain
time.
Default is 86400 seconds (24 hours).
time. Default is 604800 seconds (7 days), configurable via
EVENT_HISTORY_TTL_SECONDS environment variable.
Also creates compound index for efficient pub/sub catch-up queries.
"""
return [
cls.Index('timestamp', {'expireAfterSeconds': 86400}),
cls.Index('timestamp', {'expireAfterSeconds': EVENT_HISTORY_TTL_SECONDS}),
cls.Index([('channel', 1), ('sequence_id', 1)], {}),
]
12 changes: 9 additions & 3 deletions kernelci/api/models_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

"""Common KernelCI API model definitions"""

from typing import Optional, Any, Dict
from typing import Optional, Any, Dict, List, Tuple, Union
from bson import ObjectId
from pydantic import (
BaseModel,
Expand Down Expand Up @@ -74,8 +74,14 @@
"""Database model"""
@dataclass
class Index():
"""Index class"""
field: str
"""Index class

field: Either a single field name (str) for simple indexes,
or a list of (field, direction) tuples for compound indexes.
Direction is typically 1 (ascending) or -1 (descending).
attributes: MongoDB index options (e.g., {'expireAfterSeconds': 3600})
"""
field: Union[str, List[Tuple[str, int]]]
attributes: dict[str, Any]

def update(self):
Expand All @@ -99,7 +105,7 @@
# https://github.com/pydantic/pydantic/issues/6575
if info.exclude:
for field in info.exclude:
values.pop(field, None)

Check failure on line 108 in kernelci/api/models_base.py

View workflow job for this annotation

GitHub Actions / Lint

Argument 1 to "pop" of "dict" has incompatible type "int | str"; expected "str" [arg-type]

for field_name, value in values.items():
if isinstance(value, ObjectId):
Expand Down
Loading