fix(ops): query actor_user_id on AuditEvent and optimize session health check (#25)

Co-authored-by: Developer <dev@example.com>
This commit is contained in:
Billie Ponder 2026-09-22 02:05:50 -04:00 committed by GitHub
parent 1a272ce1ff
commit 1e7e670a17
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 36 additions and 5 deletions

View File

@ -1181,6 +1181,10 @@ def ops_get_health(db: DbSession = Depends(get_db)):
now_iso = datetime.now(timezone.utc).isoformat()
db_ok = False
try:
if db is not None:
db.execute(text("SELECT 1"))
db_ok = True
else:
db_ok = check_database()
except Exception:
db_ok = False
@ -1233,6 +1237,10 @@ def ops_get_infrastructure(
):
db_ok = False
try:
if db is not None:
db.execute(text("SELECT 1"))
db_ok = True
else:
db_ok = check_database()
except Exception:
db_ok = False
@ -1369,8 +1377,8 @@ def ops_get_overview(
AuditEvent.event_type.in_(["auth.login", "auth.login_success"]),
AuditEvent.created_at >= t_since,
)
if staff_emails:
q = q.where(~AuditEvent.actor_identity.in_(staff_emails))
if staff_user_ids:
q = q.where(~AuditEvent.actor_user_id.in_(staff_user_ids))
return db.scalar(q) or 0
web_logins_24h = cust_logins_count(t_24h)

View File

@ -21,7 +21,7 @@ from starlette.testclient import TestClient
import main
from main import app, get_db
from models import Base, Operator, OpsAuditEvent, OpsSession, User, OpsCmsDocument, OpsCmsRevision
from models import Base, Operator, OpsAuditEvent, OpsSession, User, OpsCmsDocument, OpsCmsRevision, AuditEvent
def hash_token(raw_token: str) -> str:
@ -259,6 +259,29 @@ class TestOpsUsersAndCms(unittest.TestCase):
pub_resp3 = self.client.get("/api/v1/cms/documents/homepage_hero")
self.assertEqual(pub_resp3.json()["published"]["headline"], "Next-Gen Motorcycle Telemetry")
def test_ops_overview(self):
with DbSession(self.engine) as db:
audit = AuditEvent(
id=uuid.uuid4(),
event_type="auth.login_success",
actor_user_id=None,
created_at=datetime.now(timezone.utc),
)
db.add(audit)
db.commit()
resp = self.client.get(
"/api/v1/ops/overview",
headers={"Authorization": f"Bearer {self.op_token}"},
)
self.assertEqual(resp.status_code, 200)
data = resp.json()
self.assertIn("active_24h", data["users"])
self.assertEqual(data["activity"]["web_logins_24h"], 1)
self.assertIn("subsystems", data)
self.assertIn("operations_api", data["subsystems"])
self.assertIn("operations_database", data["subsystems"])
if __name__ == "__main__":
unittest.main()