Files
rengine/api/app/services/target_scope.py
T
Yogesh OjhaandClaude Opus 5.5 a400ade446 feat(dashboard): scope the dashboard by target, organization and tag
A scope bar under the header narrows every cell to chosen targets, an
organization or a tag, kept in the URL. The dimension endpoints, the
dashboard reads, threat intel, interest and domain posture accept
target_id, organization_id and tag_id through one resolver. A target row
in Surface against risk scopes the board on click, cell links carry
target=[...] so each count opens the same rows, and cells that cannot
narrow are marked All targets.

Dashboard copy reworded to state facts: no explanations, reassurance or
colourful labels, and failures read "<Noun> not loaded."

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-09-24 07:52:30 +05:30

59 lines
1.7 KiB
Python

from __future__ import annotations
from dataclasses import dataclass
from uuid import UUID
from sqlalchemy import select
from sqlalchemy.ext.asyncio import AsyncSession
from shared.models.tag import TargetTag
from shared.models.target import Target, TargetOrganization
@dataclass(frozen=True)
class TargetFilter:
"""Targets named directly, by organization or by tag."""
target_ids: tuple[UUID, ...] = ()
organization_id: UUID | None = None
tag_id: UUID | None = None
@property
def active(self) -> bool:
return bool(self.target_ids or self.organization_id or self.tag_id)
NO_FILTER = TargetFilter()
Targets = frozenset[UUID] | None
async def resolve_targets(
session: AsyncSession, project_id: UUID | None, spec: TargetFilter | None
) -> Targets:
"""The project's targets the filter keeps, or None when nothing is filtered."""
if project_id is None or spec is None or not spec.active:
return None
query = select(Target.id).where(Target.project_id == project_id)
if spec.target_ids:
query = query.where(Target.id.in_(spec.target_ids))
if spec.organization_id:
query = query.where(
Target.id.in_(
select(TargetOrganization.target_id).where(
TargetOrganization.organization_id == spec.organization_id
)
)
)
if spec.tag_id:
query = query.where(
Target.id.in_(
select(TargetTag.target_id).where(TargetTag.tag_id == spec.tag_id)
)
)
return frozenset((await session.execute(query)).scalars().all())
def keeps(targets: Targets, target_id: UUID | None) -> bool:
return targets is None or target_id in targets