File size: 7,045 Bytes
02f0242 9730fbc 02f0242 9730fbc 02f0242 9730fbc 02f0242 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 |
"""
Cursor-based pagination models for efficient data retrieval.
This module implements cursor pagination which is more efficient
than offset pagination for large datasets and real-time data.
"""
from typing import Generic, List, Optional, TypeVar, Dict, Any
from datetime import datetime
from pydantic import BaseModel, Field
import base64
from src.core import json_utils
from src.core import get_logger
logger = get_logger(__name__)
T = TypeVar('T')
class CursorInfo(BaseModel):
"""Information encoded in a cursor."""
timestamp: datetime
id: str
direction: str = "next"
def encode(self) -> str:
"""Encode cursor info to base64 string."""
data = {
"t": self.timestamp.isoformat(),
"i": self.id,
"d": self.direction
}
json_str = json_utils.dumps(data, separators=(',', ':'))
return base64.urlsafe_b64encode(json_str.encode()).decode()
@classmethod
def decode(cls, cursor: str) -> "CursorInfo":
"""Decode cursor from base64 string."""
try:
json_str = base64.urlsafe_b64decode(cursor).decode()
data = json_utils.loads(json_str)
return cls(
timestamp=datetime.fromisoformat(data["t"]),
id=data["i"],
direction=data.get("d", "next")
)
except Exception as e:
logger.error(f"Invalid cursor: {e}")
raise ValueError("Invalid cursor format")
class CursorPaginationRequest(BaseModel):
"""Request parameters for cursor pagination."""
cursor: Optional[str] = Field(None, description="Cursor for next/previous page")
limit: int = Field(20, ge=1, le=100, description="Number of items per page")
direction: str = Field("next", pattern="^(next|prev)$", description="Pagination direction")
class CursorPaginationResponse(BaseModel, Generic[T]):
"""Response with cursor pagination metadata."""
items: List[T]
next_cursor: Optional[str] = None
prev_cursor: Optional[str] = None
has_more: bool = False
total_items: Optional[int] = None
metadata: Dict[str, Any] = Field(default_factory=dict)
class PaginationHelper:
"""Helper class for cursor-based pagination."""
@staticmethod
def create_cursor(item: Dict[str, Any], direction: str = "next") -> str:
"""Create cursor from an item."""
cursor_info = CursorInfo(
timestamp=item.get("timestamp", datetime.utcnow()),
id=str(item.get("id", "")),
direction=direction
)
return cursor_info.encode()
@staticmethod
def parse_cursor(cursor: Optional[str]) -> Optional[CursorInfo]:
"""Parse cursor string to CursorInfo."""
if not cursor:
return None
return CursorInfo.decode(cursor)
@staticmethod
def paginate_list(
items: List[Dict[str, Any]],
request: CursorPaginationRequest,
key_field: str = "timestamp",
id_field: str = "id"
) -> CursorPaginationResponse[Dict[str, Any]]:
"""
Paginate a list of items using cursor pagination.
Args:
items: List of items to paginate (should be sorted)
request: Pagination request parameters
key_field: Field to use for cursor comparison
id_field: Unique identifier field
Returns:
Paginated response with cursors
"""
# Parse cursor if provided
cursor_info = PaginationHelper.parse_cursor(request.cursor)
# Filter items based on cursor
if cursor_info:
if request.direction == "next":
# Get items after cursor
filtered_items = [
item for item in items
if item.get(key_field) > cursor_info.timestamp
or (item.get(key_field) == cursor_info.timestamp
and str(item.get(id_field)) > cursor_info.id)
]
else: # prev
# Get items before cursor (reverse order)
filtered_items = [
item for item in reversed(items)
if item.get(key_field) < cursor_info.timestamp
or (item.get(key_field) == cursor_info.timestamp
and str(item.get(id_field)) < cursor_info.id)
]
filtered_items = list(reversed(filtered_items))
else:
filtered_items = items
# Apply limit
page_items = filtered_items[:request.limit]
has_more = len(filtered_items) > request.limit
# Generate cursors
next_cursor = None
prev_cursor = None
if page_items:
# Next cursor from last item
if has_more or cursor_info:
next_cursor = PaginationHelper.create_cursor(
page_items[-1], "next"
)
# Previous cursor from first item
if cursor_info or (not cursor_info and request.direction == "prev"):
prev_cursor = PaginationHelper.create_cursor(
page_items[0], "prev"
)
return CursorPaginationResponse(
items=page_items,
next_cursor=next_cursor,
prev_cursor=prev_cursor,
has_more=has_more,
total_items=len(items),
metadata={
"page_size": len(page_items),
"direction": request.direction
}
)
class ChatMessagePagination:
"""Specialized pagination for chat messages."""
@staticmethod
def paginate_messages(
messages: List[Dict[str, Any]],
cursor: Optional[str] = None,
limit: int = 50,
direction: str = "prev" # Default to loading older messages
) -> CursorPaginationResponse[Dict[str, Any]]:
"""
Paginate chat messages with cursor.
Chat typically loads older messages, so default direction is "prev".
"""
request = CursorPaginationRequest(
cursor=cursor,
limit=limit,
direction=direction
)
# Sort messages by timestamp
sorted_messages = sorted(
messages,
key=lambda m: m.get("timestamp", datetime.min)
)
response = PaginationHelper.paginate_list(
sorted_messages,
request,
key_field="timestamp",
id_field="id"
)
# Add chat-specific metadata
response.metadata.update({
"oldest_message": sorted_messages[0].get("timestamp") if sorted_messages else None,
"newest_message": sorted_messages[-1].get("timestamp") if sorted_messages else None,
"unread_count": sum(1 for m in messages if not m.get("read", True))
})
return response |