models.py
14 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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
"""本体内核的表。
一张 `otlg_concept` 装所有概念,`kind` 决定 `spec_json` 的结构(见 schema.py)——
而不是给对象类型、指标、规则、日历各建一张表:概念之间互相引用(指标引用口径规则和日历,
动作引用对象类型),放一张表里 id 空间统一,影响分析、导入导出、发布快照都简单得多。
"""
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 Concept(Base):
__tablename__ = "otlg_concept"
id: Mapped[str] = mapped_column(String(64), primary_key=True)
workspace: Mapped[str] = mapped_column(String(64), default="default", index=True)
kind: Mapped[str] = mapped_column(String(24), index=True)
name: Mapped[str] = mapped_column(String(255), index=True)
display_name: Mapped[str] = mapped_column(String(255), default="")
description: Mapped[str] = mapped_column(Text, default="")
synonyms_json: Mapped[str] = mapped_column(Text, default="[]")
spec_json: Mapped[str] = mapped_column(_LONGTEXT, default="{}")
status: Mapped[str] = mapped_column(String(16), default="draft", index=True) # draft/published/deprecated
dirty: Mapped[int] = mapped_column(Integer, default=1) # 自上次发布以来改过
# 已发布版本(含绑定)的完整副本。运行时(问答、取数、SOP)只读这一份,
# 编辑改的是上面的工作副本 —— 改到一半的概念不会影响线上,发布才生效。
published_json: Mapped[str] = mapped_column(_LONGTEXT, default="")
version: Mapped[int] = mapped_column(Integer, default=1)
origin: Mapped[str] = mapped_column(String(24), default="user") # user/template/extract/introspect/ai
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 Binding(Base):
"""概念 ↔ 物理数据。一个概念可以在多个数据源各有一条绑定。"""
__tablename__ = "otlg_binding"
id: Mapped[str] = mapped_column(String(64), primary_key=True)
workspace: Mapped[str] = mapped_column(String(64), default="default", index=True)
concept_id: Mapped[str] = mapped_column(String(64), index=True)
ds_id: Mapped[int] = mapped_column(Integer, default=0, index=True)
target_kind: Mapped[str] = mapped_column(String(24)) # table / metric_expr / join
ref_json: Mapped[str] = mapped_column(_LONGTEXT, default="{}")
created_at: Mapped[dt.datetime] = mapped_column(DateTime, default=now)
updated_at: Mapped[dt.datetime] = mapped_column(DateTime, default=now, onupdate=now)
class Evidence(Base):
"""概念的出处:文档片段 / 图谱实体。回答口径问题时直接引用。"""
__tablename__ = "otlg_evidence"
id: Mapped[int] = mapped_column(Integer, primary_key=True, autoincrement=True)
workspace: Mapped[str] = mapped_column(String(64), default="default", index=True)
concept_id: Mapped[str] = mapped_column(String(64), index=True)
doc_id: Mapped[int] = mapped_column(Integer, default=0)
chunk_id: Mapped[str] = mapped_column(String(128), default="")
heading: Mapped[str] = mapped_column(String(512), default="")
snippet: Mapped[str] = mapped_column(Text, default="")
graph_entity_id: Mapped[str] = mapped_column(String(255), default="")
created_at: Mapped[dt.datetime] = mapped_column(DateTime, default=now)
class Revision(Base):
"""本体发布快照。发布 = 把草稿概念转正 + 冻结一份全量快照,可 diff、可回滚。"""
__tablename__ = "otlg_revision"
id: Mapped[int] = mapped_column(Integer, primary_key=True, autoincrement=True)
workspace: Mapped[str] = mapped_column(String(64), default="default", index=True)
revision: Mapped[int] = mapped_column(Integer, default=1)
note: Mapped[str] = mapped_column(Text, default="")
snapshot_json: Mapped[str] = mapped_column(_LONGTEXT, default="{}")
diff_json: Mapped[str] = mapped_column(_LONGTEXT, default="{}")
published_by: Mapped[str] = mapped_column(String(128), default="")
created_at: Mapped[dt.datetime] = mapped_column(DateTime, default=now)
class ObjectEdit(Base):
"""写回层:Action 对对象属性的修改记在这里,读取时叠加在业务库(只读)之上。"""
__tablename__ = "otlg_object_edit"
id: Mapped[int] = mapped_column(Integer, primary_key=True, autoincrement=True)
workspace: Mapped[str] = mapped_column(String(64), default="default", index=True)
object_type: Mapped[str] = mapped_column(String(64), index=True)
object_key: Mapped[str] = mapped_column(String(255), index=True)
property: Mapped[str] = mapped_column(String(128))
value_json: Mapped[str] = mapped_column(Text, default="null")
prev_json: Mapped[str] = mapped_column(Text, default="null")
action_run_id: Mapped[str] = mapped_column(String(64), default="")
active: Mapped[int] = mapped_column(Integer, default=1)
# overlay:写在写回层、读取时覆盖;db:已写进业务库(只留痕、可撤销);create:在业务库新建的行
mode: Mapped[str] = mapped_column(String(16), default="overlay")
created_by: Mapped[str] = mapped_column(String(128), default="")
created_at: Mapped[dt.datetime] = mapped_column(DateTime, default=now)
class ObjectIndex(Base):
"""对象索引(数据管道从后备数据源同步):主键、显示名、关系键列、维度属性、状态属性与记录历史的属性。
用途:问句里点名具体对象时做链接;在关系边上做多跳遍历 / 影响分析;和上一次同步对比出变化。
业务明细永远实时查业务库,这里只存索引。
"""
__tablename__ = "otlg_object_index"
id: Mapped[int] = mapped_column(Integer, primary_key=True, autoincrement=True)
workspace: Mapped[str] = mapped_column(String(64), default="default", index=True)
concept_id: Mapped[str] = mapped_column(String(64), index=True)
obj_key: Mapped[str] = mapped_column(String(255), index=True)
label: Mapped[str] = mapped_column(String(255), index=True)
# {"p": {属性: 值}, "j": {关系键列: 值}}
props_json: Mapped[str] = mapped_column(Text, default="{}")
props_hash: Mapped[str] = mapped_column(String(40), default="")
gone: Mapped[int] = mapped_column(Integer, default=0, index=True) # 业务库里已经没有这个对象了
first_seen: Mapped[dt.datetime | None] = mapped_column(DateTime, nullable=True)
last_changed: Mapped[dt.datetime | None] = mapped_column(DateTime, nullable=True)
synced_at: Mapped[dt.datetime | None] = mapped_column(DateTime, nullable=True)
class ObjectChange(Base):
"""对象变化记录:同步时和上一次索引对比出来的、动作 / 流程写入的,都记在这里。
时间层的基础:「某属性从 A 变成 B」的触发、对象在某个时点的样子(as_of)、变化时间线都靠它。
property = "*" 表示整条对象出现(created)或消失(removed)。
"""
__tablename__ = "otlg_object_change"
id: Mapped[int] = mapped_column(Integer, primary_key=True, autoincrement=True)
workspace: Mapped[str] = mapped_column(String(64), default="default", index=True)
type_id: Mapped[str] = mapped_column(String(64), index=True)
obj_key: Mapped[str] = mapped_column(String(255), index=True)
property: Mapped[str] = mapped_column(String(128), default="")
old_json: Mapped[str] = mapped_column(Text, default="null")
new_json: Mapped[str] = mapped_column(Text, default="null")
source: Mapped[str] = mapped_column(String(16), default="sync") # sync / action / revert
ref: Mapped[str] = mapped_column(String(64), default="") # 动作运行 id 等
at: Mapped[dt.datetime] = mapped_column(DateTime, default=now, index=True)
class SeriesPoint(Base):
"""时间序列的一个点。series = "prop:<对象类型id>:<属性>" 或 "metric:<指标id>";
skey = 对象主键 / 指标按维度拆开的键(不拆为空)。"""
__tablename__ = "otlg_series_point"
id: Mapped[int] = mapped_column(Integer, primary_key=True, autoincrement=True)
workspace: Mapped[str] = mapped_column(String(64), default="default", index=True)
series: Mapped[str] = mapped_column(String(160), index=True)
skey: Mapped[str] = mapped_column(String(255), default="", index=True)
at: Mapped[dt.datetime] = mapped_column(DateTime, default=now, index=True)
value: Mapped[float | None] = mapped_column(Float, nullable=True)
text: Mapped[str] = mapped_column(String(255), default="")
class LinkEdge(Base):
"""关系实例:同步时按关系的外键 / 中间表物化出来,遍历只走这张表。中间表关系的属性放在 props_json。"""
__tablename__ = "otlg_link_edge"
id: Mapped[int] = mapped_column(Integer, primary_key=True, autoincrement=True)
workspace: Mapped[str] = mapped_column(String(64), default="default", index=True)
link_id: Mapped[str] = mapped_column(String(64), index=True)
from_type: Mapped[str] = mapped_column(String(64), index=True)
from_key: Mapped[str] = mapped_column(String(255), index=True)
to_type: Mapped[str] = mapped_column(String(64), index=True)
to_key: Mapped[str] = mapped_column(String(255), index=True)
props_json: Mapped[str] = mapped_column(Text, default="")
class Workspace(Base):
"""本体空间:一个行业 / 一个业务域一套本体,元素、版本、动作、审计都按空间隔离。"""
__tablename__ = "otlg_workspace"
id: Mapped[str] = mapped_column(String(64), primary_key=True)
display_name: Mapped[str] = mapped_column(String(255), default="")
description: Mapped[str] = mapped_column(Text, default="")
industry: Mapped[str] = mapped_column(String(64), default="")
status: Mapped[str] = mapped_column(String(16), default="active") # active / archived
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 Pipeline(Base):
"""数据管道:把一个对象类型的骨架从业务库同步进来(手动 / 定时)。"""
__tablename__ = "otlg_pipeline"
id: Mapped[str] = mapped_column(String(64), primary_key=True)
workspace: Mapped[str] = mapped_column(String(64), default="default", index=True)
object_type_id: Mapped[str] = mapped_column(String(64), index=True)
schedule: Mapped[str] = mapped_column(String(16), default="manual") # manual / hourly / daily
row_limit: Mapped[int] = mapped_column(Integer, default=20000)
enabled: Mapped[int] = mapped_column(Integer, default=1)
status: Mapped[str] = mapped_column(String(16), default="idle") # idle / running / ok / failed
last_rows: Mapped[int] = mapped_column(Integer, default=0)
last_edges: Mapped[int] = mapped_column(Integer, default=0)
last_error: Mapped[str] = mapped_column(Text, default="")
last_run_at: Mapped[dt.datetime | None] = mapped_column(DateTime, nullable=True)
last_duration_ms: 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)
class ModelRun(Base):
"""一次业务建模:输入文档 → 行业路由 → LLM 转换 → 规则深化 → SOP 编排 → 四元组草案。"""
__tablename__ = "otlg_model_run"
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="")
inputs_json: Mapped[str] = mapped_column(_LONGTEXT, default="{}") # doc_ids / 临时上传文件与片段
options_json: Mapped[str] = mapped_column(Text, default="{}")
stages_json: Mapped[str] = mapped_column(_LONGTEXT, default="[]")
counts_json: Mapped[str] = mapped_column(Text, default="{}")
# draft / running / stopped(暂停,可继续)/ cancelled(终止)/ done / failed
status: Mapped[str] = mapped_column(String(16), default="draft", index=True)
job_id: Mapped[int] = mapped_column(Integer, default=0)
error: Mapped[str] = mapped_column(Text, default="")
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 ModelCall(Base):
"""建模流水线里的一次 AI 调用:哪个环节、花了多久、想了什么、答了什么。点开流水线节点看的就是这些。"""
__tablename__ = "otlg_model_call"
id: Mapped[int] = mapped_column(Integer, primary_key=True, autoincrement=True)
run_id: Mapped[str] = mapped_column(String(64), index=True)
stage: Mapped[str] = mapped_column(String(24), default="")
feature: Mapped[str] = mapped_column(String(48), default="")
label: Mapped[str] = mapped_column(String(255), default="")
model: Mapped[str] = mapped_column(String(128), default="")
status: Mapped[str] = mapped_column(String(16), default="running") # running / done / failed / cancelled
started_at: Mapped[dt.datetime] = mapped_column(DateTime, default=now)
duration_ms: Mapped[int] = mapped_column(Integer, default=0)
prompt_tokens: Mapped[int] = mapped_column(Integer, default=0)
output_tokens: Mapped[int] = mapped_column(Integer, default=0)
reasoning_tokens: Mapped[int] = mapped_column(Integer, default=0)
cached: Mapped[int] = mapped_column(Integer, default=0)
error: Mapped[str] = mapped_column(Text, default="")
prompt: Mapped[str] = mapped_column(_LONGTEXT, default="")
reasoning: Mapped[str] = mapped_column(_LONGTEXT, default="")
output: Mapped[str] = mapped_column(_LONGTEXT, default="")