refactor: opencode
This commit is contained in:
parent
09c5b30f15
commit
91d67b2e12
12 changed files with 1843 additions and 77 deletions
|
|
@ -92,17 +92,17 @@ def write_event(
|
|||
data_plain = json.dumps(data)
|
||||
ad = str(event_id).encode("ascii")
|
||||
data_ct = encrypt_field(data_plain, store._key(), associated_data=ad)
|
||||
row = {
|
||||
"id": str(event_id),
|
||||
"kind": kind,
|
||||
"severity": severity or "",
|
||||
"domain": domain or "",
|
||||
"ts": datetime.now(timezone.utc),
|
||||
"data_json": data_ct,
|
||||
"session_id": session_id,
|
||||
"source_ids_json": json.dumps([str(x) for x in (source_ids or [])]),
|
||||
}
|
||||
store.db.open_table(EVENTS_TABLE).add([row])
|
||||
ts = datetime.now(timezone.utc)
|
||||
store.events_add(
|
||||
event_id=event_id,
|
||||
kind=kind,
|
||||
severity=severity or "",
|
||||
domain=domain or "",
|
||||
ts=ts,
|
||||
data_json=data_ct,
|
||||
session_id=session_id,
|
||||
source_ids_json=json.dumps([str(x) for x in (source_ids or [])]),
|
||||
)
|
||||
return event_id
|
||||
|
||||
|
||||
|
|
@ -132,27 +132,12 @@ def query_events(
|
|||
Returns a list of dicts with keys: id, kind, severity, domain, ts, data,
|
||||
session_id, source_ids. data and source_ids are decoded from JSON.
|
||||
"""
|
||||
tbl = store.db.open_table(EVENTS_TABLE)
|
||||
df = tbl.to_pandas()
|
||||
if df.empty:
|
||||
return []
|
||||
if kind is not None:
|
||||
df = df[df["kind"] == kind]
|
||||
if severity is not None:
|
||||
df = df[df["severity"] == severity]
|
||||
if since is not None:
|
||||
# Ensure tz-aware comparison
|
||||
since_cmp = since if since.tzinfo is not None else since.replace(tzinfo=timezone.utc)
|
||||
# Pandas Timestamp compares naturally with tz-aware datetimes
|
||||
df = df[df["ts"] >= since_cmp]
|
||||
if df.empty:
|
||||
return []
|
||||
df = df.sort_values("ts", ascending=False).head(limit)
|
||||
rows = store.events_query(kind=kind, since=since, severity=severity, limit=limit)
|
||||
out: list[dict] = []
|
||||
for _, row in df.iterrows():
|
||||
for row in rows:
|
||||
# decrypt data_json when it carries the iai:enc:v1: prefix.
|
||||
# Pre-02-08 rows stay plaintext; migration rewrites them lazily.
|
||||
raw_data = row["data_json"] or "{}"
|
||||
raw_data = row["data"] if isinstance(row.get("data"), str) else json.dumps(row.get("data", {}))
|
||||
if is_encrypted(raw_data):
|
||||
ad = str(row["id"]).encode("ascii")
|
||||
try:
|
||||
|
|
@ -165,20 +150,14 @@ def query_events(
|
|||
data = json.loads(raw_data)
|
||||
except (TypeError, json.JSONDecodeError):
|
||||
data = {}
|
||||
try:
|
||||
source_ids = json.loads(row["source_ids_json"] or "[]")
|
||||
except (TypeError, json.JSONDecodeError):
|
||||
source_ids = []
|
||||
out.append(
|
||||
{
|
||||
"id": row["id"],
|
||||
"kind": row["kind"],
|
||||
"severity": row["severity"] or None,
|
||||
"domain": row["domain"] or None,
|
||||
"ts": row["ts"],
|
||||
"data": data,
|
||||
"session_id": row["session_id"],
|
||||
"source_ids": source_ids,
|
||||
}
|
||||
)
|
||||
out.append({
|
||||
"id": row["id"],
|
||||
"kind": row["kind"],
|
||||
"severity": row["severity"] or None,
|
||||
"domain": row["domain"] or None,
|
||||
"ts": row["ts"],
|
||||
"data": data,
|
||||
"session_id": row["session_id"],
|
||||
"source_ids": row["source_ids"],
|
||||
})
|
||||
return out
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue