Spaces:
Running

File size: 7,564 Bytes
a1867e5
0d24759
a1867e5
0d24759
a1867e5
935e530
a1867e5
 
 
 
 
 
 
 
 
935e530
a1867e5
935e530
a1867e5
 
 
 
 
 
935e530
 
a1867e5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
935e530
 
 
a1867e5
 
 
 
 
 
 
 
 
 
935e530
a1867e5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
935e530
 
a1867e5
 
935e530
 
 
a1867e5
0d24759
a1867e5
0d24759
a1867e5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0d24759
 
 
a1867e5
0d24759
a1867e5
 
 
 
 
 
 
 
 
 
 
0d24759
 
 
a1867e5
 
 
0d24759
a1867e5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0d24759
a1867e5
0d24759
a1867e5
0d24759
a1867e5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0d24759
a1867e5
 
 
0d24759
 
a1867e5
 
 
 
 
0d24759
 
 
a1867e5
0d24759
a1867e5
0d24759
a1867e5
 
0d24759
a1867e5
 
 
0d24759
 
a1867e5
 
 
0d24759
 
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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
# MeloonChatBI RESTful API 接口规范文档

---

## 1. 全局约定与服务基础

- **基础路径**:`/api/v1`
- **协议格式**:`application/json; charset=utf-8`
- **本地服务地址**:
  - 前端开发代理:`http://localhost:3002`(请求自动代理到后端)
  - 后端网关:`http://127.0.0.1:8000`
- **认证方式**:
  - 请求头携带 `Authorization: Bearer <access_token>`。
- **敏感安全原则**:
  - 所有返回的数据源对象中,数据库密码字段一律脱敏,不输出明文。

---

## 2. 用户认证与工作区偏好 (`/api/v1/auth`, `/api/v1/profile`)

### 2.1 注册账号
- **端点**:`POST /api/v1/auth/register`
- **说明**:在统一 `users` 表创建账号,同时在 `chatbi_profile` 记录默认工作区名称。
- **请求体**:
```json
{
  "username": "analyst_david",
  "email": "david@company.com",
  "password": "SecurePassword123!",
  "default_workspace_name": "集团经营分析工作区"
}
```
- **成功响应 (200 OK)**:
```json
{
  "success": true,
  "user": {
    "id": "18f2d591-62bc-448e-9764-77a3d13264ec",
    "username": "analyst_david",
    "email": "david@company.com",
    "role": "student"
  },
  "access_token": "eyJhbGciOiJIUzI1NiIsIn...",
  "token_type": "bearer"
}
```

### 2.2 用户登录
- **端点**:`POST /api/v1/auth/login`
- **请求体**:
```json
{
  "identifier": "david@company.com",
  "password": "SecurePassword123!"
}
```
- **成功响应 (200 OK)**:返回用户基本资料与 JWT Token。

### 2.3 获取与修改工作区偏好
- **获取偏好**:`GET /api/v1/profile`
- **响应**:
```json
{
  "user_id": "18f2d591-62bc-448e-9764-77a3d13264ec",
  "default_workspace_name": "集团经营分析工作区",
  "default_data_source_id": 1,
  "settings": {
    "default_chart_type": "bar",
    "theme": "dark"
  }
}
```
- **修改偏好**:`PUT /api/v1/profile`
```json
{
  "default_workspace_name": "战略运营数据中心",
  "default_data_source_id": 2
}
```

---

## 3. 数据源管理接口 (`/api/v1/datasources`)

### 3.1 获取所有配置的数据源
- **端点**:`GET /api/v1/datasources/`
- **成功响应 (200 OK)**:
```json
[
  {
    "id": 1,
    "name": "生产订单库 (PostgreSQL)",
    "type": "postgres",
    "host": "pg-prod.internal.net",
    "port": 5432,
    "database_name": "order_db",
    "username": "readonly_user",
    "status": "active",
    "created_at": "2026-09-20T10:00:00Z"
  }
]
```

### 3.2 测试数据库连接可用性
- **端点**:`POST /api/v1/datasources/test-connection`
- **说明**:在未保存前探测数据库网络可达性与认证正确性。
- **请求体**:
```json
{
  "type": "postgres",
  "host": "pg-prod.internal.net",
  "port": 5432,
  "database_name": "order_db",
  "username": "readonly_user",
  "password": "StrongPassword123!"
}
```
- **成功响应 (200 OK)**:
```json
{
  "success": true,
  "message": "数据库连接测试成功,连接延迟 12ms",
  "server_version": "PostgreSQL 16.2"
}
```

### 3.3 新增数据源连接
- **端点**:`POST /api/v1/datasources/`
- **请求体**:包含连接凭据字段,保存后返回包含 ID 的数据源摘要。

### 3.4 上传本地表格文件 (Excel / CSV)
- **端点**:`POST /api/v1/datasources/upload`
- **请求格式**:`multipart/form-data`
  - `file`: 选中的 `.xlsx` 或 `.csv` 文件
  - `name`: 数据源名称(如“2026Q1市场部投放报表”)
- **成功响应 (200 OK)**:
```json
{
  "id": 5,
  "name": "2026Q1市场部投放报表",
  "type": "excel",
  "status": "active",
  "tables_extracted": ["Sheet1"],
  "total_rows": 12500
}
```

---

## 4. 智能对话与问数核心接口 (`/api/v1/conversations`)

### 4.1 创建问数会话
- **端点**:`POST /api/v1/conversations/`
- **请求体**:
```json
{
  "title": "2026年Q1各品类营收分析",
  "data_source_id": 1
}
```
- **成功响应 (201 Created)**:
```json
{
  "id": 101,
  "title": "2026年Q1各品类营收分析",
  "data_source_id": 1,
  "created_at": "2026-10-02T16:00:00Z"
}
```

### 4.2 向数据源发送自然语言提问 (Ask Question)
- **端点**:`POST /api/v1/conversations/{id}/messages`
- **说明**:核心问数接口,触发 LLM Text-to-SQL、执行查询、图表匹配与洞察总结。
- **请求体**:
```json
{
  "content": "查询上个月各个产品类别的总销售额,并找出前 5 名",
  "data_source_id": 1
}
```
- **成功响应 (200 OK)**:
```json
{
  "id": 2001,
  "conversation_id": 101,
  "role": "assistant",
  "content": "已为您统计上月销售额前 5 名的产品类别。其中【消费电子】以 428.5 万元居首位,其次是【智能家居】与【个护健康】。",
  "generated_sql": "SELECT category_name, SUM(amount) AS total_sales FROM orders WHERE order_date >= '2026-09-01' AND order_date < '2026-10-01' GROUP BY category_name ORDER BY total_sales DESC LIMIT 5;",
  "execution_result": [
    { "category_name": "消费电子", "total_sales": 4285000 },
    { "category_name": "智能家居", "total_sales": 2890000 },
    { "category_name": "个护健康", "total_sales": 1750000 },
    { "category_name": "户外运动", "total_sales": 1320000 },
    { "category_name": "办公文具", "total_sales": 890000 }
  ],
  "chart_type": "bar",
  "chart_config": {
    "x_axis": "category_name",
    "y_axis": "total_sales",
    "title": "上月产品品类销售额 TOP 5"
  },
  "created_at": "2026-10-02T16:00:05Z"
}
```

### 4.3 获取指定会话的历史消息
- **端点**:`GET /api/v1/conversations/{id}/messages`

---

## 5. 实体知识图谱接口 (`/api/v1/kg`)

### 5.1 获取数据源实体拓扑图谱
- **端点**:`GET /api/v1/kg/schema/{data_source_id}`
- **说明**:返回适合前端力导向网络渲染的节点与连接边。
- **成功响应 (200 OK)**:
```json
{
  "nodes": [
    { "id": "orders", "name": "订单主表", "group": "fact", "columns_count": 12 },
    { "id": "customers", "name": "客户资料表", "group": "dimension", "columns_count": 8 },
    { "id": "products", "name": "商品目录表", "group": "dimension", "columns_count": 15 }
  ],
  "links": [
    { "source": "orders", "target": "customers", "relation": "placed_by", "foreign_key": "customer_id" },
    { "source": "orders", "target": "products", "relation": "contains_product", "foreign_key": "product_id" }
  ]
}
```

### 5.2 触发知识图谱增量同步
- **端点**:`POST /api/v1/kg/sync/{data_source_id}`

---

## 6. 自定义仪表盘接口 (`/api/v1/dashboards`)

### 6.1 获取用户仪表盘列表
- **端点**:`GET /api/v1/dashboards/`
- **成功响应 (200 OK)**:
```json
[
  {
    "id": 1,
    "title": "公司核心经营大盘",
    "description": "监控关键营收指标与各事业部开销",
    "cards_count": 6,
    "updated_at": "2026-10-02T12:00:00Z"
  }
]
```

### 6.2 保存图表卡片至仪表盘
- **端点**:`POST /api/v1/dashboards/{id}/cards`
- **请求体**:
```json
{
  "title": "品类销售额 TOP 5",
  "chart_type": "bar",
  "generated_sql": "SELECT ...",
  "data_source_id": 1,
  "chart_config": { ... }
}
```

---

## 7. 元数据管理接口 (`/api/v1/metadata`)

### 7.1 读取数据源完整元数据
- **端点**:`GET /api/v1/metadata/{data_source_id}`

### 7.2 更新字段业务批注与同义词
- **端点**:`PUT /api/v1/metadata/columns/{column_id}`
- **请求体**:
```json
{
  "business_name": "实付金额 (元)",
  "description": "扣减各类优惠券与积分抵扣后的实付总额",
  "synonyms": ["实收金额", "客付金额", "净销售额"]
}
```