Skip to content

Commit

Permalink
Filter and sort objects (messages, posts, etc) by confirmation block ID
Browse files Browse the repository at this point in the history
  • Loading branch information
MHHukiewitz committed May 30, 2023
1 parent 64c97e2 commit 928bad3
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 10 deletions.
31 changes: 21 additions & 10 deletions src/aleph/db/accessors/messages.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ def make_matching_messages_query(
message_type: Optional[MessageType] = None,
start_date: Optional[Union[float, dt.datetime]] = None,
end_date: Optional[Union[float, dt.datetime]] = None,
start_block: Optional[int] = None,
end_block: Optional[int] = None,
content_hashes: Optional[Sequence[ItemHash]] = None,
content_types: Optional[Sequence[str]] = None,
tags: Optional[Sequence[str]] = None,
Expand Down Expand Up @@ -112,7 +114,7 @@ def make_matching_messages_query(

order_by_columns: Tuple # For mypy to leave us alone until SQLA2

if sort_by == SortBy.TX_TIME:
if sort_by == SortBy.TX_TIME or start_block or end_block:
select_earliest_confirmation = (
select(
message_confirmations.c.item_hash,
Expand All @@ -126,17 +128,26 @@ def make_matching_messages_query(
MessageDb.item_hash == select_earliest_confirmation.c.item_hash,
isouter=True,
)
order_by_columns = (
(
nullsfirst(select_earliest_confirmation.c.earliest_confirmation.desc()),
MessageDb.time.desc(),
if start_block:
select_stmt = select_stmt.where(
select_earliest_confirmation.c.height.is_(None) or select_earliest_confirmation.c.height >= start_block
)
if sort_order == SortOrder.DESCENDING
else (
nullslast(select_earliest_confirmation.c.earliest_confirmation.asc()),
MessageDb.time.asc(),
if end_block:
select_stmt = select_stmt.where(
select_earliest_confirmation.c.height < end_block
)
if sort_by == SortBy.TX_TIME:
order_by_columns = (
(
nullsfirst(select_earliest_confirmation.c.earliest_confirmation.desc()),
MessageDb.time.desc(),
)
if sort_order == SortOrder.DESCENDING
else (
nullslast(select_earliest_confirmation.c.earliest_confirmation.asc()),
MessageDb.time.asc(),
)
)
)
else:
order_by_columns = (
(
Expand Down
19 changes: 19 additions & 0 deletions src/aleph/web/controllers/messages.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,10 @@ def validate_field_dependencies(cls, values):
end_date = values.get("end_date")
if start_date and end_date and (end_date < start_date):
raise ValueError("end date cannot be lower than start date.")
start_block = values.get("start_block")
end_block = values.get("end_block")
if start_block and end_block and (end_block < start_block):
raise ValueError("end block cannot be lower than start block.")
return values

@validator(
Expand Down Expand Up @@ -151,6 +155,21 @@ class MessageQueryParams(BaseMessageQueryParams):
"a time field lower than this value will be returned.",
)

start_block: int = Field(
default=0,
ge=0,
alias="startBlock",
description="Start block number. If specified, only messages with "
"a block number greater or equal to this value will be returned.",
)
end_block: int = Field(
default=0,
ge=0,
alias="endBlock",
description="End block number. If specified, only messages with "
"a block number lower than this value will be returned.",
)


class WsMessageQueryParams(BaseMessageQueryParams):
history: Optional[int] = Field(
Expand Down

0 comments on commit 928bad3

Please sign in to comment.