Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Filter and sort objects (messages, posts, etc) by confirmation block ID #445 #446

Merged
merged 2 commits into from
Dec 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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:
MHHukiewitz marked this conversation as resolved.
Show resolved Hide resolved
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) | 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 @@ -109,6 +109,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 @@ -154,6 +158,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
Loading