Skip to content

Commit 02cf32a

Browse files
committed
models: Extend EventHistory model for durable pub/sub delivery
This is necessary changes to update EventHistory model, so it can be used for new durable pub/sub. Signed-off-by: Denys Fedoryshchenko <[email protected]>
1 parent 5920499 commit 02cf32a

File tree

2 files changed

+32
-6
lines changed

2 files changed

+32
-6
lines changed

kernelci/api/models.py

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
from datetime import datetime, timedelta
1717
from typing import Any, Optional, Dict, List, ClassVar, Literal
1818
import enum
19+
import os
1920
from operator import attrgetter
2021
import json
2122
from typing_extensions import Annotated
@@ -855,12 +856,29 @@ def parse_node_obj(node: Node):
855856

856857

857858
# eventhistory model
859+
# Environment variable to configure TTL (default 7 days = 604800 seconds)
860+
# Set EVENT_HISTORY_TTL_SECONDS to override
861+
EVENT_HISTORY_TTL_SECONDS = int(os.getenv('EVENT_HISTORY_TTL_SECONDS', '604800'))
862+
863+
858864
class EventHistory(DatabaseModel):
859865
"""Event history object model"""
860866
timestamp: datetime = Field(
861867
description='Timestamp of event creation',
862868
default_factory=datetime.now
863869
)
870+
sequence_id: Optional[int] = Field(
871+
default=None,
872+
description='Sequential ID for pub/sub ordering (auto-generated)'
873+
)
874+
channel: Optional[str] = Field(
875+
default='node',
876+
description='Pub/Sub channel name'
877+
)
878+
owner: Optional[str] = Field(
879+
default=None,
880+
description='Username of event publisher'
881+
)
864882
data: Dict[str, Any] = Field(
865883
description='Event data',
866884
default={}
@@ -871,9 +889,11 @@ def get_indexes(cls):
871889
"""
872890
Create the index with the expiresAfterSeconds option for the
873891
timestamp field to automatically remove documents after a certain
874-
time.
875-
Default is 86400 seconds (24 hours).
892+
time. Default is 604800 seconds (7 days), configurable via
893+
EVENT_HISTORY_TTL_SECONDS environment variable.
894+
Also creates compound index for efficient pub/sub catch-up queries.
876895
"""
877896
return [
878-
cls.Index('timestamp', {'expireAfterSeconds': 86400}),
897+
cls.Index('timestamp', {'expireAfterSeconds': EVENT_HISTORY_TTL_SECONDS}),
898+
cls.Index([('channel', 1), ('sequence_id', 1)], {}),
879899
]

kernelci/api/models_base.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
"""Common KernelCI API model definitions"""
1414

15-
from typing import Optional, Any, Dict
15+
from typing import Optional, Any, Dict, List, Tuple, Union
1616
from bson import ObjectId
1717
from pydantic import (
1818
BaseModel,
@@ -74,8 +74,14 @@ class DatabaseModel(ModelId):
7474
"""Database model"""
7575
@dataclass
7676
class Index():
77-
"""Index class"""
78-
field: str
77+
"""Index class
78+
79+
field: Either a single field name (str) for simple indexes,
80+
or a list of (field, direction) tuples for compound indexes.
81+
Direction is typically 1 (ascending) or -1 (descending).
82+
attributes: MongoDB index options (e.g., {'expireAfterSeconds': 3600})
83+
"""
84+
field: Union[str, List[Tuple[str, int]]]
7985
attributes: dict[str, Any]
8086

8187
def update(self):

0 commit comments

Comments
 (0)