Release 1.7.0: Grafana catalog, ingress/IRM, tests
This commit is contained in:
@ -27,6 +27,12 @@ class IncidentCreate(BaseModel):
|
||||
severity: str = Field(default="warning", max_length=32)
|
||||
|
||||
|
||||
class IncidentPatch(BaseModel):
|
||||
title: str | None = Field(default=None, min_length=1, max_length=500)
|
||||
status: str | None = Field(default=None, max_length=64)
|
||||
severity: str | None = Field(default=None, max_length=32)
|
||||
|
||||
|
||||
def register_events(bus: EventBus, pool: asyncpg.Pool | None = None) -> None:
|
||||
if pool is None:
|
||||
return
|
||||
@ -41,12 +47,17 @@ def register_events(bus: EventBus, pool: asyncpg.Pool | None = None) -> None:
|
||||
async with pool.acquire() as conn:
|
||||
await conn.execute(
|
||||
"""
|
||||
INSERT INTO incidents (title, status, severity, source, ingress_event_id)
|
||||
VALUES ($1, 'open', $2, 'grafana', $3::uuid)
|
||||
INSERT INTO incidents (
|
||||
title, status, severity, source, ingress_event_id,
|
||||
grafana_org_slug, service_name
|
||||
)
|
||||
VALUES ($1, 'open', $2, 'grafana', $3::uuid, $4, $5)
|
||||
""",
|
||||
title,
|
||||
sev,
|
||||
ev.raw_payload_ref,
|
||||
ev.grafana_org_slug,
|
||||
ev.service_name,
|
||||
)
|
||||
except Exception:
|
||||
log.exception("incidents: не удалось создать инцидент из alert.received")
|
||||
@ -70,20 +81,33 @@ async def render_home_fragment(request: Request) -> str:
|
||||
async def list_incidents_api(
|
||||
pool: asyncpg.Pool | None = Depends(get_pool),
|
||||
limit: int = 50,
|
||||
grafana_org_slug: str | None = None,
|
||||
service_name: str | None = None,
|
||||
):
|
||||
if pool is None:
|
||||
return {"items": [], "database": "disabled"}
|
||||
limit = min(max(limit, 1), 200)
|
||||
async with pool.acquire() as conn:
|
||||
rows = await conn.fetch(
|
||||
"""
|
||||
SELECT id, title, status, severity, source, ingress_event_id, created_at
|
||||
conditions: list[str] = []
|
||||
args: list = []
|
||||
if grafana_org_slug and grafana_org_slug.strip():
|
||||
args.append(grafana_org_slug.strip())
|
||||
conditions.append(f"grafana_org_slug = ${len(args)}")
|
||||
if service_name and service_name.strip():
|
||||
args.append(service_name.strip())
|
||||
conditions.append(f"service_name = ${len(args)}")
|
||||
where_sql = ("WHERE " + " AND ".join(conditions)) if conditions else ""
|
||||
args.append(limit)
|
||||
lim_ph = f"${len(args)}"
|
||||
q = f"""
|
||||
SELECT id, title, status, severity, source, ingress_event_id, created_at, updated_at,
|
||||
grafana_org_slug, service_name
|
||||
FROM incidents
|
||||
{where_sql}
|
||||
ORDER BY created_at DESC
|
||||
LIMIT $1
|
||||
""",
|
||||
limit,
|
||||
)
|
||||
LIMIT {lim_ph}
|
||||
"""
|
||||
async with pool.acquire() as conn:
|
||||
rows = await conn.fetch(q, *args)
|
||||
items = []
|
||||
for r in rows:
|
||||
items.append(
|
||||
@ -95,6 +119,9 @@ async def list_incidents_api(
|
||||
"source": r["source"],
|
||||
"ingress_event_id": str(r["ingress_event_id"]) if r["ingress_event_id"] else None,
|
||||
"created_at": r["created_at"].isoformat() if r["created_at"] else None,
|
||||
"updated_at": r["updated_at"].isoformat() if r.get("updated_at") else None,
|
||||
"grafana_org_slug": r.get("grafana_org_slug"),
|
||||
"service_name": r.get("service_name"),
|
||||
}
|
||||
)
|
||||
return {"items": items}
|
||||
@ -110,9 +137,10 @@ async def create_incident_api(
|
||||
async with pool.acquire() as conn:
|
||||
row = await conn.fetchrow(
|
||||
"""
|
||||
INSERT INTO incidents (title, status, severity, source)
|
||||
VALUES ($1, $2, $3, 'manual')
|
||||
RETURNING id, title, status, severity, source, ingress_event_id, created_at
|
||||
INSERT INTO incidents (title, status, severity, source, grafana_org_slug, service_name)
|
||||
VALUES ($1, $2, $3, 'manual', NULL, NULL)
|
||||
RETURNING id, title, status, severity, source, ingress_event_id, created_at, updated_at,
|
||||
grafana_org_slug, service_name
|
||||
""",
|
||||
body.title.strip(),
|
||||
body.status,
|
||||
@ -126,9 +154,63 @@ async def create_incident_api(
|
||||
"source": row["source"],
|
||||
"ingress_event_id": None,
|
||||
"created_at": row["created_at"].isoformat() if row["created_at"] else None,
|
||||
"updated_at": row["updated_at"].isoformat() if row.get("updated_at") else None,
|
||||
"grafana_org_slug": row.get("grafana_org_slug"),
|
||||
"service_name": row.get("service_name"),
|
||||
}
|
||||
|
||||
|
||||
def _incident_row_dict(row) -> dict:
|
||||
return {
|
||||
"id": str(row["id"]),
|
||||
"title": row["title"],
|
||||
"status": row["status"],
|
||||
"severity": row["severity"],
|
||||
"source": row["source"],
|
||||
"ingress_event_id": str(row["ingress_event_id"]) if row["ingress_event_id"] else None,
|
||||
"created_at": row["created_at"].isoformat() if row["created_at"] else None,
|
||||
"updated_at": row["updated_at"].isoformat() if row.get("updated_at") else None,
|
||||
"grafana_org_slug": row.get("grafana_org_slug"),
|
||||
"service_name": row.get("service_name"),
|
||||
}
|
||||
|
||||
|
||||
@router.get("/{incident_id}/tasks")
|
||||
async def list_incident_tasks_api(
|
||||
incident_id: UUID,
|
||||
pool: asyncpg.Pool | None = Depends(get_pool),
|
||||
limit: int = 100,
|
||||
):
|
||||
if pool is None:
|
||||
raise HTTPException(status_code=503, detail="database disabled")
|
||||
limit = min(max(limit, 1), 200)
|
||||
async with pool.acquire() as conn:
|
||||
exists = await conn.fetchval("SELECT 1 FROM incidents WHERE id = $1::uuid", incident_id)
|
||||
if not exists:
|
||||
raise HTTPException(status_code=404, detail="incident not found")
|
||||
rows = await conn.fetch(
|
||||
"""
|
||||
SELECT id, incident_id, title, status, created_at
|
||||
FROM tasks WHERE incident_id = $1::uuid
|
||||
ORDER BY created_at DESC LIMIT $2
|
||||
""",
|
||||
incident_id,
|
||||
limit,
|
||||
)
|
||||
items = []
|
||||
for r in rows:
|
||||
items.append(
|
||||
{
|
||||
"id": str(r["id"]),
|
||||
"incident_id": str(r["incident_id"]) if r["incident_id"] else None,
|
||||
"title": r["title"],
|
||||
"status": r["status"],
|
||||
"created_at": r["created_at"].isoformat() if r["created_at"] else None,
|
||||
}
|
||||
)
|
||||
return {"incident_id": str(incident_id), "items": items}
|
||||
|
||||
|
||||
@router.get("/{incident_id}")
|
||||
async def get_incident_api(incident_id: UUID, pool: asyncpg.Pool | None = Depends(get_pool)):
|
||||
if pool is None:
|
||||
@ -136,22 +218,47 @@ async def get_incident_api(incident_id: UUID, pool: asyncpg.Pool | None = Depend
|
||||
async with pool.acquire() as conn:
|
||||
row = await conn.fetchrow(
|
||||
"""
|
||||
SELECT id, title, status, severity, source, ingress_event_id, created_at
|
||||
SELECT id, title, status, severity, source, ingress_event_id, created_at, updated_at,
|
||||
grafana_org_slug, service_name
|
||||
FROM incidents WHERE id = $1::uuid
|
||||
""",
|
||||
incident_id,
|
||||
)
|
||||
if not row:
|
||||
raise HTTPException(status_code=404, detail="not found")
|
||||
return {
|
||||
"id": str(row["id"]),
|
||||
"title": row["title"],
|
||||
"status": row["status"],
|
||||
"severity": row["severity"],
|
||||
"source": row["source"],
|
||||
"ingress_event_id": str(row["ingress_event_id"]) if row["ingress_event_id"] else None,
|
||||
"created_at": row["created_at"].isoformat() if row["created_at"] else None,
|
||||
}
|
||||
return _incident_row_dict(row)
|
||||
|
||||
|
||||
@router.patch("/{incident_id}")
|
||||
async def patch_incident_api(
|
||||
incident_id: UUID,
|
||||
body: IncidentPatch,
|
||||
pool: asyncpg.Pool | None = Depends(get_pool),
|
||||
):
|
||||
if pool is None:
|
||||
raise HTTPException(status_code=503, detail="database disabled")
|
||||
if body.title is None and body.status is None and body.severity is None:
|
||||
raise HTTPException(status_code=400, detail="no fields to update")
|
||||
async with pool.acquire() as conn:
|
||||
row = await conn.fetchrow(
|
||||
"""
|
||||
UPDATE incidents SET
|
||||
title = COALESCE($2, title),
|
||||
status = COALESCE($3, status),
|
||||
severity = COALESCE($4, severity),
|
||||
updated_at = now()
|
||||
WHERE id = $1::uuid
|
||||
RETURNING id, title, status, severity, source, ingress_event_id, created_at, updated_at,
|
||||
grafana_org_slug, service_name
|
||||
""",
|
||||
incident_id,
|
||||
body.title.strip() if body.title is not None else None,
|
||||
body.status,
|
||||
body.severity,
|
||||
)
|
||||
if not row:
|
||||
raise HTTPException(status_code=404, detail="not found")
|
||||
return _incident_row_dict(row)
|
||||
|
||||
|
||||
@ui_router.get("/", response_class=HTMLResponse)
|
||||
@ -166,13 +273,15 @@ async def incidents_ui_home(request: Request):
|
||||
async with pool.acquire() as conn:
|
||||
rows = await conn.fetch(
|
||||
"""
|
||||
SELECT id, title, status, severity, source, created_at
|
||||
SELECT id, title, status, severity, source, created_at, grafana_org_slug, service_name
|
||||
FROM incidents
|
||||
ORDER BY created_at DESC
|
||||
LIMIT 100
|
||||
"""
|
||||
)
|
||||
for r in rows:
|
||||
org = html.escape(str(r["grafana_org_slug"] or "—"))
|
||||
svc = html.escape(str(r["service_name"] or "—"))
|
||||
rows_html += (
|
||||
"<tr>"
|
||||
f"<td>{html.escape(str(r['id']))[:8]}…</td>"
|
||||
@ -180,6 +289,8 @@ async def incidents_ui_home(request: Request):
|
||||
f"<td>{html.escape(r['status'])}</td>"
|
||||
f"<td>{html.escape(r['severity'])}</td>"
|
||||
f"<td>{html.escape(r['source'])}</td>"
|
||||
f"<td>{org}</td>"
|
||||
f"<td>{svc}</td>"
|
||||
"</tr>"
|
||||
)
|
||||
except Exception as e:
|
||||
@ -187,8 +298,8 @@ async def incidents_ui_home(request: Request):
|
||||
inner = f"""<h1>Инциденты</h1>
|
||||
{err}
|
||||
<table class="irm-table">
|
||||
<thead><tr><th>ID</th><th>Заголовок</th><th>Статус</th><th>Важность</th><th>Источник</th></tr></thead>
|
||||
<tbody>{rows_html or '<tr><td colspan="5">Пока нет записей</td></tr>'}</tbody>
|
||||
<thead><tr><th>ID</th><th>Заголовок</th><th>Статус</th><th>Важность</th><th>Источник</th><th>Grafana slug</th><th>Сервис</th></tr></thead>
|
||||
<tbody>{rows_html or '<tr><td colspan="7">Пока нет записей</td></tr>'}</tbody>
|
||||
</table>
|
||||
<p><small>Создание из Grafana: webhook → запись в <code>ingress_events</code> → событие → строка здесь.</small></p>"""
|
||||
return HTMLResponse(
|
||||
|
||||
Reference in New Issue
Block a user