97 lines
3.3 KiB
Python
97 lines
3.3 KiB
Python
from datetime import datetime, timezone
|
|
|
|
from sqlalchemy import insert, select, desc
|
|
from sqlalchemy.ext.asyncio import AsyncConnection
|
|
|
|
from api.schemas.process.node_link import NodeLink
|
|
|
|
from orm.tables.process import ps_node_table, node_link_table
|
|
from orm.tables.process import NodeLinkStatus
|
|
|
|
|
|
async def get_last_link_name_by_node_id(connection: AsyncConnection, ps_id: int) -> str | None:
|
|
"""
|
|
Получает link_name из последней записи node_link по ps_id.
|
|
Находит все node_id в ps_node по ps_id, затем ищет связи в node_link
|
|
и возвращает link_name из самой последней записи.
|
|
"""
|
|
query = (
|
|
select(node_link_table.c.link_name)
|
|
.where(node_link_table.c.node_id.in_(select(ps_node_table.c.id).where(ps_node_table.c.ps_id == ps_id)))
|
|
.order_by(desc(node_link_table.c.created_at))
|
|
.limit(1)
|
|
)
|
|
|
|
result = await connection.execute(query)
|
|
link_name = result.scalar_one_or_none()
|
|
|
|
return link_name
|
|
|
|
|
|
async def get_last_node_link_by_creator_and_ps_id(
|
|
connection: AsyncConnection, creator_id: int, node_link_id: int
|
|
) -> NodeLink | None:
|
|
"""
|
|
Получает последнюю созданную node_link для данного создателя и процесса.
|
|
"""
|
|
query = (
|
|
select(node_link_table)
|
|
.where(
|
|
node_link_table.c.creator_id == creator_id,
|
|
node_link_table.c.node_id.in_(select(ps_node_table.c.id).where(ps_node_table.c.id == node_link_id)),
|
|
)
|
|
.order_by(desc(node_link_table.c.created_at))
|
|
.limit(1)
|
|
)
|
|
|
|
node_link_db_cursor = await connection.execute(query)
|
|
node_link_data = node_link_db_cursor.mappings().one_or_none()
|
|
|
|
if not node_link_data:
|
|
return None
|
|
|
|
return NodeLink.model_validate(node_link_data)
|
|
|
|
|
|
async def create_node_link_schema(
|
|
connection: AsyncConnection,
|
|
validated_link_schema,
|
|
creator_id: int,
|
|
) -> NodeLink | None:
|
|
"""
|
|
Создает нове поле в таблице process_schema_table.
|
|
"""
|
|
query = insert(node_link_table).values(
|
|
link_name=validated_link_schema.link_name,
|
|
node_id=validated_link_schema.from_id,
|
|
link_point_id=validated_link_schema.parent_port_number,
|
|
next_node_id=validated_link_schema.to_id,
|
|
settings={},
|
|
creator_id=creator_id,
|
|
created_at=datetime.now(timezone.utc),
|
|
status=NodeLinkStatus.ACTIVE.value,
|
|
)
|
|
|
|
await connection.execute(query)
|
|
|
|
await connection.commit()
|
|
|
|
return await get_last_node_link_by_creator_and_ps_id(connection, creator_id, validated_link_schema.from_id)
|
|
|
|
|
|
async def get_all_node_links_by_next_node_ids(connection: AsyncConnection, next_node_ids: list[int]) -> list[NodeLink]:
|
|
"""
|
|
Получает все активные node_link для списка next_node_id одним запросом.
|
|
"""
|
|
if not next_node_ids:
|
|
return []
|
|
|
|
query = select(node_link_table).where(
|
|
node_link_table.c.next_node_id.in_(next_node_ids), node_link_table.c.status == NodeLinkStatus.ACTIVE.value
|
|
)
|
|
|
|
node_link_db_cursor = await connection.execute(query)
|
|
node_links_data = node_link_db_cursor.mappings().all()
|
|
|
|
return [NodeLink.model_validate(link_data) for link_data in node_links_data]
|