1616from datetime import datetime , timedelta
1717from typing import Any , Optional , Dict , List , ClassVar , Literal
1818import enum
19+ import os
1920from operator import attrgetter
2021import json
2122from 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+
858864class 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