AP0.7: Tenant Hardening, Actor Directory und Data Layer Minimum
Deploy Development / deploy (push) Successful in 47s
Test Suite / pytest-backend (push) Successful in 42s
Test Suite / lint-backend (push) Successful in 2s
Test Suite / compose-smoke (push) Has been skipped
Test Suite / k6 /api/health Baseline (push) Successful in 18s
Test Suite / playwright-smoke (push) Successful in 12s
Deploy Development / deploy (push) Successful in 47s
Test Suite / pytest-backend (push) Successful in 42s
Test Suite / lint-backend (push) Successful in 2s
Test Suite / compose-smoke (push) Has been skipped
Test Suite / k6 /api/health Baseline (push) Successful in 18s
Test Suite / playwright-smoke (push) Successful in 12s
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
1 parent
36487d2869
commit
db06af0489
32 files changed
+1424
-43
No files matched your search
@@ -0,0 +1 @@
|
||||
"""Kairo Data Layer — read-oriented aggregation for workspace and dashboards."""
|
||||
@@ -0,0 +1,85 @@
|
||||
"""Action read-models for workspace — tenant-scoped, actor-assignment based."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from typing import Any
|
||||
|
||||
from psycopg2.extras import RealDictCursor
|
||||
|
||||
from db import get_connection
|
||||
from services.actions import OPEN_ACTION_STATUSES, _attach_assignments, _serialize_row
|
||||
from tenant_context import TenantContext
|
||||
|
||||
|
||||
def _require_actor(ctx: TenantContext) -> str:
|
||||
if not ctx.actor_id:
|
||||
raise ValueError("Kein Actor im TenantContext")
|
||||
return ctx.actor_id
|
||||
|
||||
|
||||
def _list_assigned_actions(
|
||||
*,
|
||||
tenant_id: str,
|
||||
actor_id: str,
|
||||
statuses: list[str],
|
||||
) -> list[dict[str, Any]]:
|
||||
conn = get_connection()
|
||||
try:
|
||||
with conn.cursor(cursor_factory=RealDictCursor) as cur:
|
||||
cur.execute(
|
||||
"""
|
||||
SELECT a.id, a.tenant_id, a.initiative_id, a.title, a.description,
|
||||
a.status, a.priority, a.created_at, a.updated_at,
|
||||
i.title AS initiative_title
|
||||
FROM actions a
|
||||
JOIN action_assignments aa
|
||||
ON aa.action_id = a.id AND aa.tenant_id = a.tenant_id
|
||||
JOIN initiatives i
|
||||
ON i.id = a.initiative_id AND i.tenant_id = a.tenant_id
|
||||
WHERE a.tenant_id = %s
|
||||
AND aa.actor_id = %s
|
||||
AND a.status = ANY(%s)
|
||||
ORDER BY
|
||||
CASE a.priority
|
||||
WHEN 'high' THEN 0
|
||||
WHEN 'normal' THEN 1
|
||||
WHEN 'low' THEN 2
|
||||
END,
|
||||
a.updated_at DESC
|
||||
""",
|
||||
(tenant_id, actor_id, statuses),
|
||||
)
|
||||
actions = [_serialize_row(dict(row)) for row in cur.fetchall()]
|
||||
finally:
|
||||
conn.close()
|
||||
return _attach_assignments(actions, tenant_id=tenant_id)
|
||||
|
||||
|
||||
def get_my_open_actions(ctx: TenantContext) -> list[dict[str, Any]]:
|
||||
"""Open + in_progress actions assigned to current actor (excludes blocked)."""
|
||||
actor_id = _require_actor(ctx)
|
||||
return _list_assigned_actions(
|
||||
tenant_id=ctx.tenant_id,
|
||||
actor_id=actor_id,
|
||||
statuses=["open", "in_progress"],
|
||||
)
|
||||
|
||||
|
||||
def get_my_blocked_actions(ctx: TenantContext) -> list[dict[str, Any]]:
|
||||
"""Blocked actions assigned to current actor."""
|
||||
actor_id = _require_actor(ctx)
|
||||
return _list_assigned_actions(
|
||||
tenant_id=ctx.tenant_id,
|
||||
actor_id=actor_id,
|
||||
statuses=["blocked"],
|
||||
)
|
||||
|
||||
|
||||
def get_all_my_open_actions(ctx: TenantContext) -> list[dict[str, Any]]:
|
||||
"""All non-terminal open actions (open, in_progress, blocked) — legacy compat."""
|
||||
actor_id = _require_actor(ctx)
|
||||
return _list_assigned_actions(
|
||||
tenant_id=ctx.tenant_id,
|
||||
actor_id=actor_id,
|
||||
statuses=list(OPEN_ACTION_STATUSES),
|
||||
)
|
||||
@@ -0,0 +1,61 @@
|
||||
"""Actor workload read-models — tenant-scoped aggregation."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from typing import Any
|
||||
|
||||
from psycopg2.extras import RealDictCursor
|
||||
|
||||
from db import get_connection
|
||||
from tenant_context import TenantContext
|
||||
|
||||
|
||||
def get_actor_workload(ctx: TenantContext) -> list[dict[str, Any]]:
|
||||
"""Open/blocked/in_progress action counts per active actor in tenant."""
|
||||
conn = get_connection()
|
||||
try:
|
||||
with conn.cursor(cursor_factory=RealDictCursor) as cur:
|
||||
cur.execute(
|
||||
"""
|
||||
SELECT
|
||||
a.id AS actor_id,
|
||||
a.name AS display_name,
|
||||
a.actor_type,
|
||||
COUNT(*) FILTER (WHERE act.status = 'open') AS open_actions,
|
||||
COUNT(*) FILTER (WHERE act.status = 'blocked') AS blocked_actions,
|
||||
COUNT(*) FILTER (WHERE act.status = 'in_progress') AS in_progress_actions
|
||||
FROM actors a
|
||||
LEFT JOIN action_assignments aa
|
||||
ON aa.actor_id = a.id AND aa.tenant_id = a.tenant_id
|
||||
LEFT JOIN actions act
|
||||
ON act.id = aa.action_id
|
||||
AND act.tenant_id = aa.tenant_id
|
||||
AND act.status IN ('open', 'in_progress', 'blocked')
|
||||
WHERE a.tenant_id = %s AND a.is_active = TRUE
|
||||
GROUP BY a.id, a.name, a.actor_type
|
||||
ORDER BY
|
||||
CASE a.actor_type
|
||||
WHEN 'human' THEN 0
|
||||
WHEN 'working_group' THEN 1
|
||||
WHEN 'agent' THEN 2
|
||||
ELSE 3
|
||||
END,
|
||||
a.name
|
||||
""",
|
||||
(ctx.tenant_id,),
|
||||
)
|
||||
result = []
|
||||
for row in cur.fetchall():
|
||||
result.append(
|
||||
{
|
||||
"actor_id": str(row["actor_id"]),
|
||||
"display_name": row["display_name"],
|
||||
"actor_type": row["actor_type"],
|
||||
"open_actions": int(row["open_actions"] or 0),
|
||||
"blocked_actions": int(row["blocked_actions"] or 0),
|
||||
"in_progress_actions": int(row["in_progress_actions"] or 0),
|
||||
}
|
||||
)
|
||||
return result
|
||||
finally:
|
||||
conn.close()
|
||||
@@ -0,0 +1,37 @@
|
||||
"""Initiative read-models for workspace."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from typing import Any, Optional
|
||||
|
||||
from psycopg2.extras import RealDictCursor
|
||||
|
||||
from db import get_connection
|
||||
from services.initiatives import _serialize_row
|
||||
from tenant_context import TenantContext
|
||||
|
||||
|
||||
def get_active_initiatives(
|
||||
ctx: TenantContext,
|
||||
*,
|
||||
limit: Optional[int] = None,
|
||||
) -> list[dict[str, Any]]:
|
||||
"""Active and paused initiatives in the current tenant."""
|
||||
conn = get_connection()
|
||||
try:
|
||||
with conn.cursor(cursor_factory=RealDictCursor) as cur:
|
||||
sql = """
|
||||
SELECT id, tenant_id, title, goal, status, priority,
|
||||
owner_actor_id, created_at, updated_at
|
||||
FROM initiatives
|
||||
WHERE tenant_id = %s AND status IN ('active', 'paused')
|
||||
ORDER BY updated_at DESC, title
|
||||
"""
|
||||
params: list[Any] = [ctx.tenant_id]
|
||||
if limit is not None:
|
||||
sql += " LIMIT %s"
|
||||
params.append(limit)
|
||||
cur.execute(sql, params)
|
||||
return [_serialize_row(dict(row)) for row in cur.fetchall()]
|
||||
finally:
|
||||
conn.close()
|
||||
@@ -0,0 +1,70 @@
|
||||
"""Workspace summary read-model — tenant-scoped KPIs for current actor."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from typing import Any
|
||||
|
||||
from psycopg2.extras import RealDictCursor
|
||||
|
||||
from db import get_connection
|
||||
from tenant_context import TenantContext
|
||||
|
||||
|
||||
def get_workspace_summary(ctx: TenantContext) -> dict[str, Any]:
|
||||
"""
|
||||
Summary for workspace dashboard.
|
||||
|
||||
Personal counts (current actor): open, blocked, recently done.
|
||||
Tenant-wide: active initiatives count.
|
||||
"""
|
||||
if not ctx.actor_id:
|
||||
return {
|
||||
"open_actions_count": 0,
|
||||
"blocked_actions_count": 0,
|
||||
"active_initiatives_count": 0,
|
||||
"done_actions_recent_count": 0,
|
||||
}
|
||||
|
||||
conn = get_connection()
|
||||
try:
|
||||
with conn.cursor(cursor_factory=RealDictCursor) as cur:
|
||||
cur.execute(
|
||||
"""
|
||||
SELECT
|
||||
COUNT(*) FILTER (
|
||||
WHERE a.status IN ('open', 'in_progress')
|
||||
) AS open_actions_count,
|
||||
COUNT(*) FILTER (
|
||||
WHERE a.status = 'blocked'
|
||||
) AS blocked_actions_count,
|
||||
COUNT(*) FILTER (
|
||||
WHERE a.status = 'done'
|
||||
AND a.updated_at >= NOW() - INTERVAL '7 days'
|
||||
) AS done_actions_recent_count
|
||||
FROM actions a
|
||||
JOIN action_assignments aa
|
||||
ON aa.action_id = a.id AND aa.tenant_id = a.tenant_id
|
||||
WHERE a.tenant_id = %s AND aa.actor_id = %s
|
||||
""",
|
||||
(ctx.tenant_id, ctx.actor_id),
|
||||
)
|
||||
row = dict(cur.fetchone())
|
||||
|
||||
cur.execute(
|
||||
"""
|
||||
SELECT COUNT(*) AS active_initiatives_count
|
||||
FROM initiatives
|
||||
WHERE tenant_id = %s AND status IN ('active', 'paused')
|
||||
""",
|
||||
(ctx.tenant_id,),
|
||||
)
|
||||
initiatives_row = dict(cur.fetchone())
|
||||
finally:
|
||||
conn.close()
|
||||
|
||||
return {
|
||||
"open_actions_count": int(row["open_actions_count"] or 0),
|
||||
"blocked_actions_count": int(row["blocked_actions_count"] or 0),
|
||||
"active_initiatives_count": int(initiatives_row["active_initiatives_count"] or 0),
|
||||
"done_actions_recent_count": int(row["done_actions_recent_count"] or 0),
|
||||
}
|
||||
Reference in new issue
Block a user