Skip to content

Commit 9fa7e2d

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 8bcc551 commit 9fa7e2d

File tree

1 file changed

+23
-3
lines changed

1 file changed

+23
-3
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
]

0 commit comments

Comments
 (0)