more filters

This commit is contained in:
Kartik Sarangmath
2025-08-10 23:47:25 -07:00
parent 6d10cea955
commit c244e5bfbc
3 changed files with 35 additions and 8 deletions

View File

@@ -61,11 +61,11 @@ def _format_instance(instance: AgentInstance) -> AgentInstanceResponse:
def get_all_agent_types_with_instances(
db: Session, user_id: UUID
) -> list[AgentTypeOverview]:
"""Get all user agents with their instances for a specific user - OPTIMIZED"""
# Get all user agents for this user with instances in a single query
"""Get all non-deleted user agents with their instances for a specific user - OPTIMIZED"""
# Get all non-deleted user agents for this user with instances in a single query
user_agents = (
db.query(UserAgent)
.filter(UserAgent.user_id == user_id)
.filter(UserAgent.user_id == user_id, UserAgent.is_deleted.is_(False))
.options(subqueryload(UserAgent.instances))
.all()
)
@@ -267,7 +267,9 @@ def get_agent_summary(db: Session, user_id: UUID) -> dict:
)
.join(AgentInstance, AgentInstance.user_agent_id == UserAgent.id)
.filter(
UserAgent.user_id == user_id, AgentInstance.status != AgentStatus.DELETED
UserAgent.user_id == user_id,
UserAgent.is_deleted.is_(False),
AgentInstance.status != AgentStatus.DELETED,
)
.group_by(UserAgent.id, UserAgent.name, AgentInstance.status)
.all()
@@ -304,7 +306,11 @@ def get_agent_type_instances(
user_agent = (
db.query(UserAgent)
.filter(UserAgent.id == agent_type_id, UserAgent.user_id == user_id)
.filter(
UserAgent.id == agent_type_id,
UserAgent.user_id == user_id,
UserAgent.is_deleted.is_(False),
)
.first()
)
if not user_agent:

View File

@@ -20,13 +20,18 @@ logger = logging.getLogger(__name__)
def create_or_get_user_agent(db: Session, name: str, user_id: str) -> UserAgent:
"""Create or get a user agent by name for a specific user"""
"""Create or get a non-deleted user agent by name for a specific user"""
# Normalize name to lowercase for consistent storage
normalized_name = name.lower()
# Only look for non-deleted user agents
user_agent = (
db.query(UserAgent)
.filter(UserAgent.name == normalized_name, UserAgent.user_id == UUID(user_id))
.filter(
UserAgent.name == normalized_name,
UserAgent.user_id == UUID(user_id),
UserAgent.is_deleted.is_(False),
)
.first()
)
if not user_agent:
@@ -34,6 +39,7 @@ def create_or_get_user_agent(db: Session, name: str, user_id: str) -> UserAgent:
name=normalized_name,
user_id=UUID(user_id),
is_active=True,
is_deleted=False, # Explicitly set to False for new agents
)
db.add(user_agent)
db.flush() # Flush to get the user_agent ID

View File

@@ -21,7 +21,22 @@ depends_on: Union[str, Sequence[str], None] = None
def upgrade() -> None:
# ### commands auto generated by Alembic - please adjust! ###
op.add_column("user_agents", sa.Column("is_deleted", sa.Boolean(), nullable=False))
# Add column as nullable first with server default
op.add_column(
"user_agents",
sa.Column(
"is_deleted", sa.Boolean(), server_default=sa.text("FALSE"), nullable=True
),
)
# Set default value for existing rows (in case any are NULL)
op.execute("UPDATE user_agents SET is_deleted = FALSE WHERE is_deleted IS NULL")
# Now make the column NOT NULL
op.alter_column(
"user_agents", "is_deleted", nullable=False, server_default=sa.text("FALSE")
)
op.drop_constraint(
op.f("uq_user_agents_user_id_name"), "user_agents", type_="unique"
)