models.py
9.85 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
"""数据问答子系统的表结构(otlg_ 前缀)。
刻意不塞进 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__ = "otlg_datasource"
id: Mapped[int] = mapped_column(Integer, primary_key=True, autoincrement=True)
name: Mapped[str] = mapped_column(String(128))
# 归属的本体空间;空串 = 所有空间共用(老数据)
workspace: Mapped[str] = mapped_column(String(64), default="", index=True)
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__ = "otlg_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="")
db_comment: 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__ = "otlg_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="")
# 数据库里的原始字段注释:每次内省照抄,只读,不被人工编辑覆盖(description 才是给人改的业务说明)
db_comment: 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 里存的是中文名而不是英文缩写,才知道该怎么写过滤条件。
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__ = "otlg_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 SqlPair(Base):
"""沉淀的问答对。只有 status='verified' 的会进 few-shot —— 一条错 SQL 被反复复用的代价太大。"""
__tablename__ = "otlg_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__ = "otlg_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 SqlThread(Base):
"""SQL 调试台的会话:一串连续追问。每一轮就是一条同 thread_id 的 SqlRun。"""
__tablename__ = "otlg_sql_thread"
id: Mapped[str] = mapped_column(String(64), primary_key=True)
ds_id: Mapped[int] = mapped_column(Integer, index=True, default=0)
title: Mapped[str] = mapped_column(String(255), default="")
created_by: Mapped[str] = mapped_column(String(64), default="", index=True)
created_at: Mapped[dt.datetime] = mapped_column(DateTime, server_default=_NOW)
updated_at: Mapped[dt.datetime] = mapped_column(DateTime, server_default=_NOW)