models.py
6.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
"""自动化(预警规则)的表:规则定义、每次扫描的记录、预警事件。"""
from __future__ import annotations
import datetime as dt
from sqlalchemy import DateTime, Float, Integer, String, Text
from sqlalchemy.orm import Mapped, mapped_column
from ..core.db import _LONGTEXT, Base
from ..core.dbx import now
class Watch(Base):
"""一条预警规则 = 什么时候看(schedule / 变化事件)+ 看什么(source)+ 怎么算异常(detectors + condition)
+ 多严重(severity)+ 命中了做什么(strategies)+ 怎样算恢复(recovery)。"""
__tablename__ = "otlg_watch"
id: Mapped[str] = mapped_column(String(64), primary_key=True)
workspace: Mapped[str] = mapped_column(String(64), default="default", index=True)
title: Mapped[str] = mapped_column(String(255), default="")
description: Mapped[str] = mapped_column(Text, default="")
enabled: Mapped[int] = mapped_column(Integer, default=1, index=True)
schedule_json: Mapped[str] = mapped_column(Text, default="{}")
trigger_json: Mapped[str] = mapped_column(Text, default="{}")
source_json: Mapped[str] = mapped_column(Text, default="{}")
detectors_json: Mapped[str] = mapped_column(Text, default="[]")
condition: Mapped[str] = mapped_column(Text, default="")
severity_json: Mapped[str] = mapped_column(Text, default="{}")
strategies_json: Mapped[str] = mapped_column(_LONGTEXT, default="[]")
dedup_json: Mapped[str] = mapped_column(Text, default="{}")
recovery_json: Mapped[str] = mapped_column(Text, default="{}")
owner_role: Mapped[str] = mapped_column(String(128), default="")
# 调度水位:next_fire_at 提前算好落库,界面才能显示「下次执行时间」,
# 调度线程也只需要一条 WHERE 就能挑出到点的,不用每轮把所有规则解析一遍。
next_fire_at: Mapped[dt.datetime | None] = mapped_column(DateTime, nullable=True, index=True)
last_fired_at: Mapped[dt.datetime | None] = mapped_column(DateTime, nullable=True)
last_status: Mapped[str] = mapped_column(String(16), default="") # ok / error / ""
last_error: Mapped[str] = mapped_column(Text, default="")
fail_count: Mapped[int] = mapped_column(Integer, default=0)
created_by: Mapped[str] = mapped_column(String(128), default="")
created_at: Mapped[dt.datetime] = mapped_column(DateTime, default=now)
updated_at: Mapped[dt.datetime] = mapped_column(DateTime, default=now, onupdate=now)
class WatchRun(Base):
"""一次扫描。扫了几行、命中几行、新开 / 恢复了几条事件、触发出去什么,都留痕。"""
__tablename__ = "otlg_watch_run"
id: Mapped[str] = mapped_column(String(64), primary_key=True)
workspace: Mapped[str] = mapped_column(String(64), default="default", index=True)
watch_id: Mapped[str] = mapped_column(String(64), index=True)
trigger: Mapped[str] = mapped_column(String(64), default="schedule") # schedule / change / manual / dry
status: Mapped[str] = mapped_column(String(16), default="running") # running / done / failed
scanned: Mapped[int] = mapped_column(Integer, default=0)
hit_count: Mapped[int] = mapped_column(Integer, default=0)
fired_count: Mapped[int] = mapped_column(Integer, default=0)
suppressed: Mapped[int] = mapped_column(Integer, default=0)
opened: Mapped[int] = mapped_column(Integer, default=0)
recovered: Mapped[int] = mapped_column(Integer, default=0)
hits_json: Mapped[str] = mapped_column(_LONGTEXT, default="[]")
fired_json: Mapped[str] = mapped_column(_LONGTEXT, default="[]")
error: Mapped[str] = mapped_column(Text, default="")
started_at: Mapped[dt.datetime] = mapped_column(DateTime, default=now)
finished_at: Mapped[dt.datetime | None] = mapped_column(DateTime, nullable=True)
class AlertEvent(Base):
"""预警事件:一个命中键从「发现」到「恢复 / 忽略」的一整轮。它是本体里的系统对象「预警事件」。
状态:新发现 → 处置中(策略已触发、还有单据 / 流程没走完)→ 待验证(处置都结束了,等下一次扫描确认)
→ 已恢复(条件不再成立);也可以人工标「已忽略」(误报,可静默一段时间)。
恢复后 reopen 天内同一个键再命中,算复发:新开一条,recurrence_of 指向上一条。
"""
__tablename__ = "otlg_alert_event"
id: Mapped[str] = mapped_column(String(64), primary_key=True)
workspace: Mapped[str] = mapped_column(String(64), default="default", index=True)
watch_id: Mapped[str] = mapped_column(String(64), index=True)
rule_title: Mapped[str] = mapped_column(String(255), default="")
hit_key: Mapped[str] = mapped_column(String(255), default="", index=True)
title: Mapped[str] = mapped_column(String(512), default="")
object_type: Mapped[str] = mapped_column(String(64), default="", index=True)
object_type_name: Mapped[str] = mapped_column(String(255), default="")
object_key: Mapped[str] = mapped_column(String(255), default="", index=True)
object_label: Mapped[str] = mapped_column(String(255), default="")
severity: Mapped[str] = mapped_column(String(8), default="中", index=True) # 高 / 中 / 低
status: Mapped[str] = mapped_column(String(16), default="新发现", index=True)
value: Mapped[float | None] = mapped_column(Float, nullable=True)
baseline: Mapped[float | None] = mapped_column(Float, nullable=True)
deviation: Mapped[float | None] = mapped_column(Float, nullable=True)
detail_json: Mapped[str] = mapped_column(_LONGTEXT, default="{}") # 命中时的行和检测值
fired_json: Mapped[str] = mapped_column(_LONGTEXT, default="[]") # 触发了什么(单据、流程、待办)
handling: Mapped[str] = mapped_column(String(255), default="") # 处置方式(给人看的一句话)
auto_handled: Mapped[int] = mapped_column(Integer, default=0) # 处置是不是没经过人工就完成了
owner_role: Mapped[str] = mapped_column(String(128), default="")
first_seen_at: Mapped[dt.datetime] = mapped_column(DateTime, default=now, index=True)
last_seen_at: Mapped[dt.datetime] = mapped_column(DateTime, default=now)
last_fired_at: Mapped[dt.datetime | None] = mapped_column(DateTime, nullable=True)
handled_at: Mapped[dt.datetime | None] = mapped_column(DateTime, nullable=True)
recovered_at: Mapped[dt.datetime | None] = mapped_column(DateTime, nullable=True)
recover_hours: Mapped[float | None] = mapped_column(Float, nullable=True)
fire_count: Mapped[int] = mapped_column(Integer, default=0)
seen_count: Mapped[int] = mapped_column(Integer, default=1)
recurrence_of: Mapped[str] = mapped_column(String(64), default="")
is_recurrence: Mapped[int] = mapped_column(Integer, default=0)
silence_until: Mapped[dt.datetime | None] = mapped_column(DateTime, nullable=True)
note: Mapped[str] = mapped_column(Text, default="") # 标忽略时写的原因
closed_by: Mapped[str] = mapped_column(String(128), default="")