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:
parent
1a272ce1ff
commit
1e7e670a17
|
|
@ -1181,7 +1181,11 @@ def ops_get_health(db: DbSession = Depends(get_db)):
|
||||||
now_iso = datetime.now(timezone.utc).isoformat()
|
now_iso = datetime.now(timezone.utc).isoformat()
|
||||||
db_ok = False
|
db_ok = False
|
||||||
try:
|
try:
|
||||||
db_ok = check_database()
|
if db is not None:
|
||||||
|
db.execute(text("SELECT 1"))
|
||||||
|
db_ok = True
|
||||||
|
else:
|
||||||
|
db_ok = check_database()
|
||||||
except Exception:
|
except Exception:
|
||||||
db_ok = False
|
db_ok = False
|
||||||
|
|
||||||
|
|
@ -1233,7 +1237,11 @@ def ops_get_infrastructure(
|
||||||
):
|
):
|
||||||
db_ok = False
|
db_ok = False
|
||||||
try:
|
try:
|
||||||
db_ok = check_database()
|
if db is not None:
|
||||||
|
db.execute(text("SELECT 1"))
|
||||||
|
db_ok = True
|
||||||
|
else:
|
||||||
|
db_ok = check_database()
|
||||||
except Exception:
|
except Exception:
|
||||||
db_ok = False
|
db_ok = False
|
||||||
|
|
||||||
|
|
@ -1369,8 +1377,8 @@ def ops_get_overview(
|
||||||
AuditEvent.event_type.in_(["auth.login", "auth.login_success"]),
|
AuditEvent.event_type.in_(["auth.login", "auth.login_success"]),
|
||||||
AuditEvent.created_at >= t_since,
|
AuditEvent.created_at >= t_since,
|
||||||
)
|
)
|
||||||
if staff_emails:
|
if staff_user_ids:
|
||||||
q = q.where(~AuditEvent.actor_identity.in_(staff_emails))
|
q = q.where(~AuditEvent.actor_user_id.in_(staff_user_ids))
|
||||||
return db.scalar(q) or 0
|
return db.scalar(q) or 0
|
||||||
|
|
||||||
web_logins_24h = cust_logins_count(t_24h)
|
web_logins_24h = cust_logins_count(t_24h)
|
||||||
|
|
|
||||||
|
|
@ -21,7 +21,7 @@ from starlette.testclient import TestClient
|
||||||
|
|
||||||
import main
|
import main
|
||||||
from main import app, get_db
|
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:
|
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")
|
pub_resp3 = self.client.get("/api/v1/cms/documents/homepage_hero")
|
||||||
self.assertEqual(pub_resp3.json()["published"]["headline"], "Next-Gen Motorcycle Telemetry")
|
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__":
|
if __name__ == "__main__":
|
||||||
unittest.main()
|
unittest.main()
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue