models.py
11.9 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
"""数据问答子系统的表结构(otlgt3_ 前缀)。
刻意不塞进 core/db.py —— 那里已经 380 行,而且这套表是自成一体的子系统。
只要本模块在 `Base.metadata.create_all` 之前被 import,表就会被建出来;
`core.db.init_db()` 里做了这件事(函数体内 import,避免循环依赖)。
两个必须遵守的约定,理由见 core/db.py 文件头:
- datetime 一律用 `_NOW`(原生 CURRENT_TIMESTAMP),**不要**用 `func.now()`;
- 这些都是新表,create_all 一次建全,不需要进 `_ADD_COLUMNS`。
"""
from __future__ import annotations
import datetime as dt
from typing import Optional
from sqlalchemy import String, Integer, Text, DateTime, BigInteger, Float, func
from sqlalchemy.orm import Mapped, mapped_column
from ..core.db import Base, _LONGTEXT, _NOW
class DataSource(Base):
"""业务数据源。密码只存密文,主密钥在 .env 的 SECRET_KEY。"""
__tablename__ = "otlgt3_ds"
id: Mapped[int] = mapped_column(Integer, primary_key=True, autoincrement=True)
name: Mapped[str] = mapped_column(String(128))
ds_type: Mapped[str] = mapped_column(String(32), default="mysql")
host: Mapped[str] = mapped_column(String(255), default="")
port: Mapped[int] = mapped_column(Integer, default=3306)
db_name: Mapped[str] = mapped_column(String(128), default="")
username: Mapped[str] = mapped_column(String(128), default="")
password_enc: Mapped[str] = mapped_column(Text, default="")
extra_json: Mapped[str] = mapped_column(Text, default="{}")
read_only: Mapped[int] = mapped_column(Integer, default=1)
status: Mapped[str] = mapped_column(String(32), default="new") # new/ok/error
last_error: Mapped[str] = mapped_column(Text, default="")
last_introspect_at: Mapped[Optional[dt.datetime]] = mapped_column(DateTime, nullable=True)
created_at: Mapped[dt.datetime] = mapped_column(DateTime, server_default=_NOW)
updated_at: Mapped[dt.datetime] = mapped_column(DateTime, server_default=_NOW, onupdate=func.now())
class SmModel(Base):
"""逻辑模型 = 一张物理表/视图。"""
__tablename__ = "otlgt3_sm_model"
id: Mapped[int] = mapped_column(Integer, primary_key=True, autoincrement=True)
ds_id: Mapped[int] = mapped_column(Integer, index=True, default=0)
name: Mapped[str] = mapped_column(String(128)) # 逻辑名(给模型看的)
physical_table: Mapped[str] = mapped_column(String(255), default="")
kind: Mapped[str] = mapped_column(String(16), default="table") # table/view/saved_view
description: Mapped[str] = mapped_column(Text, default="")
primary_key: Mapped[str] = mapped_column(String(128), default="")
time_column: Mapped[str] = mapped_column(String(128), default="")
row_count_est: Mapped[int] = mapped_column(BigInteger, default=0)
statement: Mapped[str] = mapped_column(Text, default="") # kind=saved_view 时的 SQL
enabled: Mapped[int] = mapped_column(Integer, default=1)
properties_json: Mapped[str] = mapped_column(Text, default="{}")
created_at: Mapped[dt.datetime] = mapped_column(DateTime, server_default=_NOW)
updated_at: Mapped[dt.datetime] = mapped_column(DateTime, server_default=_NOW, onupdate=func.now())
class SmColumn(Base):
__tablename__ = "otlgt3_sm_column"
id: Mapped[int] = mapped_column(Integer, primary_key=True, autoincrement=True)
model_id: Mapped[int] = mapped_column(Integer, index=True, default=0)
name: Mapped[str] = mapped_column(String(128)) # 逻辑列名
physical_column: Mapped[str] = mapped_column(String(128), default="")
data_type: Mapped[str] = mapped_column(String(64), default="")
description: Mapped[str] = mapped_column(Text, default="")
synonyms_json: Mapped[str] = mapped_column(Text, default="[]")
is_dimension: Mapped[int] = mapped_column(Integer, default=1)
is_measure: Mapped[int] = mapped_column(Integer, default=0)
is_calculated: Mapped[int] = mapped_column(Integer, default=0)
expression: Mapped[str] = mapped_column(Text, default="")
# 低基数列的真实取值。补 WrenAI 那个「数据盲」的窟窿:
# 模型得看见 brand_name 里存的是「问界」而不是「AITO」,才知道该怎么写过滤条件。
sample_values_json: Mapped[str] = mapped_column(Text, default="[]")
distinct_count: Mapped[int] = mapped_column(Integer, default=0)
# 实测空值比例(抽样)。=1 表示整列都是 NULL —— 这种列必须停用,
# 否则模型会拿它写过滤条件然后查出 0 行,还以为是「没有数据」。
null_ratio: Mapped[float] = mapped_column(Float, default=0.0)
nullable: Mapped[int] = mapped_column(Integer, default=1)
enabled: Mapped[int] = mapped_column(Integer, default=1)
sort_order: Mapped[int] = mapped_column(Integer, default=0)
ontology_node_id: Mapped[str] = mapped_column(String(255), default="") # P3 本体绑定,先留空
created_at: Mapped[dt.datetime] = mapped_column(DateTime, server_default=_NOW)
class SmRelation(Base):
"""声明式 join 路径。condition_sql 既进提示词,也是 guard 的 JOIN 白名单。"""
__tablename__ = "otlgt3_sm_relation"
id: Mapped[int] = mapped_column(Integer, primary_key=True, autoincrement=True)
ds_id: Mapped[int] = mapped_column(Integer, index=True, default=0)
name: Mapped[str] = mapped_column(String(128), default="")
from_model_id: Mapped[int] = mapped_column(Integer, default=0)
from_column: Mapped[str] = mapped_column(String(128), default="")
to_model_id: Mapped[int] = mapped_column(Integer, default=0)
to_column: Mapped[str] = mapped_column(String(128), default="")
join_type: Mapped[str] = mapped_column(String(16), default="inner")
cardinality: Mapped[str] = mapped_column(String(16), default="N:1")
condition_sql: Mapped[str] = mapped_column(Text, default="")
description: Mapped[str] = mapped_column(Text, default="")
enabled: Mapped[int] = mapped_column(Integer, default=1)
created_at: Mapped[dt.datetime] = mapped_column(DateTime, server_default=_NOW)
class SmMetric(Base):
"""指标。calibre_note 承载业务口径(生态月/自然月、分母怎么取)。"""
__tablename__ = "otlgt3_sm_metric"
id: Mapped[int] = mapped_column(Integer, primary_key=True, autoincrement=True)
ds_id: Mapped[int] = mapped_column(Integer, index=True, default=0)
name: Mapped[str] = mapped_column(String(128))
display_name: Mapped[str] = mapped_column(String(128), default="")
# 业务分类(规模、效率、质量…)。纯展示用:十几个指标平铺成一列,
# 第一次用的人根本分不清哪些是一类的,也就不知道该问什么。
category: Mapped[str] = mapped_column(String(64), default="")
description: Mapped[str] = mapped_column(Text, default="")
base_model_id: Mapped[int] = mapped_column(Integer, default=0)
expression: Mapped[str] = mapped_column(Text, default="")
filters_json: Mapped[str] = mapped_column(Text, default="[]")
allowed_dimensions_json: Mapped[str] = mapped_column(Text, default="[]")
time_column: Mapped[str] = mapped_column(String(128), default="")
calibre_note: Mapped[str] = mapped_column(Text, default="")
synonyms_json: Mapped[str] = mapped_column(Text, default="[]")
status: Mapped[str] = mapped_column(String(16), default="draft")
enabled: Mapped[int] = mapped_column(Integer, default=1)
ontology_node_id: Mapped[str] = mapped_column(String(255), default="")
created_at: Mapped[dt.datetime] = mapped_column(DateTime, server_default=_NOW)
updated_at: Mapped[dt.datetime] = mapped_column(DateTime, server_default=_NOW, onupdate=func.now())
class SmInstruction(Base):
"""业务规则提示。is_default=1 无条件注入,否则按 condition_text 语义匹配。"""
__tablename__ = "otlgt3_sm_instruction"
id: Mapped[int] = mapped_column(Integer, primary_key=True, autoincrement=True)
ds_id: Mapped[int] = mapped_column(Integer, index=True, default=0) # 0 = 全局
scope: Mapped[str] = mapped_column(String(16), default="sql") # sql/answer/chart
category: Mapped[str] = mapped_column(String(64), default="") # 口径/写法/关联/安全…,分组展示用
is_default: Mapped[int] = mapped_column(Integer, default=0)
condition_text: Mapped[str] = mapped_column(Text, default="")
content: Mapped[str] = mapped_column(Text, default="")
priority: Mapped[int] = mapped_column(Integer, default=0)
enabled: Mapped[int] = mapped_column(Integer, default=1)
source: Mapped[str] = mapped_column(String(16), default="user") # user/distilled
created_at: Mapped[dt.datetime] = mapped_column(DateTime, server_default=_NOW)
class SqlPair(Base):
"""沉淀的问答对。只有 status='verified' 的会进 few-shot —— 一条错 SQL 被反复复用的代价太大。"""
__tablename__ = "otlgt3_sql_pair"
id: Mapped[int] = mapped_column(Integer, primary_key=True, autoincrement=True)
ds_id: Mapped[int] = mapped_column(Integer, index=True, default=0)
question: Mapped[str] = mapped_column(Text)
question_norm: Mapped[str] = mapped_column(String(512), default="")
sql_text: Mapped[str] = mapped_column(_LONGTEXT, default="")
dialect: Mapped[str] = mapped_column(String(24), default="mysql")
models_used_json: Mapped[str] = mapped_column(Text, default="[]")
status: Mapped[str] = mapped_column(String(16), default="draft") # draft/verified/deprecated
source: Mapped[str] = mapped_column(String(16), default="auto") # auto/user_saved/user_edited
origin_run_id: Mapped[int] = mapped_column(Integer, default=0)
sql_before: Mapped[str] = mapped_column(_LONGTEXT, default="") # 用户改过就留原 SQL,做 diff 学习
note: Mapped[str] = mapped_column(Text, default="")
hit_count: Mapped[int] = mapped_column(Integer, default=0)
last_hit_at: Mapped[Optional[dt.datetime]] = mapped_column(DateTime, nullable=True)
verified_at: Mapped[Optional[dt.datetime]] = mapped_column(DateTime, nullable=True)
created_at: Mapped[dt.datetime] = mapped_column(DateTime, server_default=_NOW)
updated_at: Mapped[dt.datetime] = mapped_column(DateTime, server_default=_NOW, onupdate=func.now())
class SqlRun(Base):
"""每次生成/执行的审计与留痕。对齐 QueryLog 的思路。"""
__tablename__ = "otlgt3_sql_run"
id: Mapped[int] = mapped_column(Integer, primary_key=True, autoincrement=True)
ds_id: Mapped[int] = mapped_column(Integer, index=True, default=0)
thread_id: Mapped[str] = mapped_column(String(64), default="")
question: Mapped[str] = mapped_column(Text, default="")
plan_json: Mapped[str] = mapped_column(Text, default="{}")
sql_text: Mapped[str] = mapped_column(_LONGTEXT, default="") # 模型生成的
sql_final: Mapped[str] = mapped_column(_LONGTEXT, default="") # 实际执行的(可能被用户改过)
status: Mapped[str] = mapped_column(String(16), default="ok") # ok/invalid/error/rejected
error: Mapped[str] = mapped_column(Text, default="")
row_count: Mapped[int] = mapped_column(Integer, default=0)
duration_ms: Mapped[int] = mapped_column(Integer, default=0)
retry_count: Mapped[int] = mapped_column(Integer, default=0)
trace_json: Mapped[str] = mapped_column(_LONGTEXT, default="[]")
rating: Mapped[int] = mapped_column(Integer, default=0) # 1 赞 / -1 踩
created_at: Mapped[dt.datetime] = mapped_column(DateTime, server_default=_NOW)
class SmSnapshot(Base):
"""语义模型版本快照,支持回滚与 MDL 导入导出。"""
__tablename__ = "otlgt3_sm_snapshot"
id: Mapped[int] = mapped_column(Integer, primary_key=True, autoincrement=True)
ds_id: Mapped[int] = mapped_column(Integer, index=True, default=0)
revision: Mapped[int] = mapped_column(Integer, default=1)
mdl_json: Mapped[str] = mapped_column(_LONGTEXT, default="{}")
note: Mapped[str] = mapped_column(Text, default="")
created_at: Mapped[dt.datetime] = mapped_column(DateTime, server_default=_NOW)