Skip to content

Commit 7a0b883

Browse files
feat(node): Add more search parameters for nodes
1 parent dce49d2 commit 7a0b883

File tree

3 files changed

+50
-7
lines changed

3 files changed

+50
-7
lines changed

app/db/crud/node.py

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from datetime import datetime, timezone
22
from typing import Optional, Union
33

4-
from sqlalchemy import and_, case, delete, func, select, update, bindparam
4+
from sqlalchemy import and_, case, delete, func, select, update, bindparam, or_
55
from sqlalchemy.ext.asyncio import AsyncSession
66
from sqlalchemy.sql.functions import coalesce
77

@@ -69,14 +69,21 @@ async def get_nodes(
6969
core_id: int | None = None,
7070
offset: int | None = None,
7171
limit: int | None = None,
72+
ids: list[int] | None = None,
73+
search: str | None = None,
7274
) -> list[Node]:
7375
"""
74-
Retrieves nodes based on optional status and enabled filters.
76+
Retrieves nodes based on optional status, enabled, id, and search filters.
7577
7678
Args:
7779
db (AsyncSession): The database session.
7880
status (Optional[Union[app.db.models.NodeStatus, list]]): The status or list of statuses to filter by.
7981
enabled (bool): If True, excludes disabled nodes.
82+
core_id (int | None): Optional core/backend ID filter.
83+
offset (int | None): Optional pagination offset.
84+
limit (int | None): Optional pagination limit.
85+
ids (list[int] | None): Optional list of node IDs to filter by.
86+
search (str | None): Optional search term to match node names.
8087
8188
Returns:
8289
List[Node]: A list of Node objects matching the criteria.
@@ -95,6 +102,15 @@ async def get_nodes(
95102
if core_id:
96103
query = query.where(Node.core_config_id == core_id)
97104

105+
if ids:
106+
query = query.where(Node.id.in_(ids))
107+
108+
if search:
109+
search_value = search.strip()
110+
if search_value:
111+
like_expression = f"%{search_value}%"
112+
query = query.where(or_(Node.name.ilike(like_expression), Node.api_key.ilike(like_expression)))
113+
98114
if offset:
99115
query = query.offset(offset)
100116
if limit:

app/operation/node.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,20 @@ async def get_db_nodes(
5151
offset: int | None = None,
5252
limit: int | None = None,
5353
enabled: bool = False,
54+
status: NodeStatus | list[NodeStatus] | None = None,
55+
ids: list[int] | None = None,
56+
search: str | None = None,
5457
) -> list[Node]:
55-
return await get_nodes(db=db, core_id=core_id, offset=offset, limit=limit, enabled=enabled)
58+
return await get_nodes(
59+
db=db,
60+
core_id=core_id,
61+
offset=offset,
62+
limit=limit,
63+
enabled=enabled,
64+
status=status,
65+
ids=ids,
66+
search=search,
67+
)
5668

5769
@staticmethod
5870
async def _update_single_node_status(

app/routers/node.py

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
from PasarGuardNodeBridge import NodeAPIError
88

99
from app.db import AsyncSession, get_db
10+
from app.db.models import NodeStatus
1011
from app.models.admin import AdminDetails
1112
from app.models.node import NodeCreate, NodeModify, NodeResponse, NodeSettings, UsageTable, UserIPList, UserIPListAll
1213
from app.models.stats import NodeRealtimeStats, NodeStatsList, NodeUsageStatsList, Period
@@ -44,14 +45,28 @@ async def get_usage(
4445

4546
@router.get("s", response_model=list[NodeResponse])
4647
async def get_nodes(
47-
backend_id: int | None = None,
48-
offset: int = None,
49-
limit: int = None,
48+
core_id: int | None = None,
49+
offset: int | None = None,
50+
limit: int | None = None,
51+
status: list[NodeStatus] | None = Query(None),
52+
enabled: bool = False,
53+
ids: list[int] | None = Query(None),
54+
search: str | None = None,
5055
db: AsyncSession = Depends(get_db),
5156
_: AdminDetails = Depends(check_sudo_admin),
5257
):
5358
"""Retrieve a list of all nodes. Accessible only to sudo admins."""
54-
return await node_operator.get_db_nodes(db=db, core_id=backend_id, offset=offset, limit=limit)
59+
60+
return await node_operator.get_db_nodes(
61+
db=db,
62+
core_id=core_id,
63+
offset=offset,
64+
limit=limit,
65+
status=status,
66+
enabled=enabled,
67+
ids=ids,
68+
search=search,
69+
)
5570

5671

5772
@router.post("s/reconnect")

0 commit comments

Comments
 (0)