feat: delete host from UI with events and problems cleanup
DELETE /api/v1/hosts/{id} (JWT); host reappears on next agent ingest
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
from fastapi import APIRouter, Depends, Query
|
||||
from fastapi import APIRouter, Depends, HTTPException, Query
|
||||
from pydantic import BaseModel
|
||||
from sqlalchemy import func, select
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
@@ -7,6 +8,7 @@ from app.config import get_settings
|
||||
from app.database import get_db
|
||||
from app.models import Event, Host
|
||||
from app.schemas.list_models import HostListResponse, HostSummary
|
||||
from app.services.host_delete import delete_host_and_related
|
||||
from app.services.host_health import (
|
||||
HEARTBEAT_TYPE,
|
||||
agent_status,
|
||||
@@ -75,3 +77,31 @@ def list_hosts(
|
||||
)
|
||||
|
||||
return HostListResponse(items=items, total=total, page=page, page_size=page_size)
|
||||
|
||||
|
||||
class HostDeleteResponse(BaseModel):
|
||||
status: str
|
||||
host_id: int
|
||||
hostname: str
|
||||
deleted_events: int
|
||||
deleted_problems: int
|
||||
|
||||
|
||||
@router.delete("/{host_id}", response_model=HostDeleteResponse)
|
||||
def delete_host(
|
||||
host_id: int,
|
||||
db: Session = Depends(get_db),
|
||||
_user: str = Depends(get_current_user),
|
||||
) -> HostDeleteResponse:
|
||||
result = delete_host_and_related(db, host_id)
|
||||
if result is None:
|
||||
raise HTTPException(status_code=404, detail="Host not found")
|
||||
db.commit()
|
||||
return HostDeleteResponse(
|
||||
status="deleted",
|
||||
host_id=result.host_id,
|
||||
hostname=result.hostname,
|
||||
deleted_events=result.deleted_events,
|
||||
deleted_problems=result.deleted_problems,
|
||||
)
|
||||
|
||||
|
||||
@@ -0,0 +1,51 @@
|
||||
"""Удаление хоста и связанных events/problems (ручная очистка UI)."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from dataclasses import dataclass
|
||||
|
||||
from sqlalchemy import delete, or_, select
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from app.models import Event, Host, Problem, ProblemEvent
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class HostDeleteResult:
|
||||
host_id: int
|
||||
hostname: str
|
||||
deleted_events: int
|
||||
deleted_problems: int
|
||||
|
||||
|
||||
def delete_host_and_related(db: Session, host_id: int) -> HostDeleteResult | None:
|
||||
host = db.get(Host, host_id)
|
||||
if host is None:
|
||||
return None
|
||||
|
||||
hostname = host.hostname
|
||||
event_ids = list(db.scalars(select(Event.id).where(Event.host_id == host_id)).all())
|
||||
problem_ids = list(db.scalars(select(Problem.id).where(Problem.host_id == host_id)).all())
|
||||
|
||||
if event_ids or problem_ids:
|
||||
link_filter = []
|
||||
if event_ids:
|
||||
link_filter.append(ProblemEvent.event_id.in_(event_ids))
|
||||
if problem_ids:
|
||||
link_filter.append(ProblemEvent.problem_id.in_(problem_ids))
|
||||
db.execute(delete(ProblemEvent).where(or_(*link_filter)))
|
||||
|
||||
if problem_ids:
|
||||
db.execute(delete(Problem).where(Problem.id.in_(problem_ids)))
|
||||
if event_ids:
|
||||
db.execute(delete(Event).where(Event.id.in_(event_ids)))
|
||||
|
||||
db.delete(host)
|
||||
db.flush()
|
||||
|
||||
return HostDeleteResult(
|
||||
host_id=host_id,
|
||||
hostname=hostname,
|
||||
deleted_events=len(event_ids),
|
||||
deleted_problems=len(problem_ids),
|
||||
)
|
||||
Reference in New Issue
Block a user