Files
aiops/docs/database-schema.md
T

211 lines
4.9 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# 数据库设计
## 概述
AIOps 使用 GORM 作为 ORM 框架,支持 SQLite / MySQL / PostgreSQL。
- **默认**: SQLite(零配置,数据文件 `backend/data/aiops.db`)
- **生产**: 推荐使用 MySQL 或 PostgreSQL
首次启动时,`AutoMigrate` 会自动创建所有表。
## 数据模型
### 用户体系
#### users - 用户表
| 字段 | 类型 | 说明 |
|------|------|------|
| id | uint | 主键 |
| username | string | 用户名(唯一) |
| password | string | 密码(bcrypt 加密) |
| email | string | 邮箱 |
| phone | string | 手机号 |
| real_name | string | 真实姓名 |
| avatar | string | 头像 URL |
| status | int | 状态(1=正常 0=禁用) |
| last_login | time | 最后登录时间 |
| created_at | time | 创建时间 |
| updated_at | time | 更新时间 |
#### roles - 角色表
| 字段 | 类型 | 说明 |
|------|------|------|
| id | uint | 主键 |
| name | string | 角色名称 |
| code | string | 角色代码 |
| description | string | 角色描述 |
| sort | int | 排序 |
#### permissions - 权限表
| 字段 | 类型 | 说明 |
|------|------|------|
| id | uint | 主键 |
| name | string | 权限名称 |
| code | string | 权限代码 |
| type | string | 权限类型(menu/btn/api) |
| path | string | 路由路径 |
| method | string | HTTP 方法 |
### 服务器管理
#### servers - 服务器表
| 字段 | 类型 | 说明 |
|------|------|------|
| id | uint | 主键 |
| name | string | 服务器名称 |
| hostname | string | 主机名 |
| ip | string | IP 地址 |
| port | int | SSH 端口 |
| os | string | 操作系统 |
| cpu | int | CPU 核数 |
| memory | int64 | 内存(MB) |
| disk | int64 | 磁盘(GB) |
| status | string | 状态(online/offline/maintenance) |
| tags | string | 标签(JSON) |
| cloud_id | uint | 关联云账号 |
| region | string | 地域 |
#### clouds - 云平台表
#### cloud_accounts - 云账号表
#### regions - 地域表
#### domains - 域名表
### K8s 管理
#### k8s_clusters - K8s 集群表
| 字段 | 类型 | 说明 |
|------|------|------|
| id | uint | 主键 |
| name | string | 集群名称 |
| version | string | K8s 版本 |
| api_server | string | API Server 地址 |
| kubeconfig | string | Kubeconfig 内容 |
| status | string | 集群状态 |
#### k8s_nodes, k8s_pods, k8s_deployments, k8s_services, k8s_ingresses
### 监控告警
#### alert_rules - 告警规则表
| 字段 | 类型 | 说明 |
|------|------|------|
| id | uint | 主键 |
| name | string | 规则名称 |
| description | string | 规则描述 |
| type | string | 告警类型 |
| source | string | 数据源 |
| expression | string | PromQL 表达式 |
| severity | string | 严重级别(critical/warning/info) |
| duration | string | 持续时间 |
| notify_channels | string | 通知渠道(JSON) |
| enabled | bool | 是否启用 |
#### alert_events - 告警事件表
#### notify_channels - 通知渠道表
#### metric_data - 指标数据表
### CI/CD
#### pipelines - 流水线表
#### pipeline_runs - 流水线执行记录表
#### app_releases - 应用发布表
### 自动化运维
#### cron_jobs - 定时任务表
#### scripts - 脚本表
#### task_executions - 任务执行记录表
#### playbooks - Ansible Playbook 表
### 审批流程
#### approval_templates - 审批模板表
#### approval_processes - 审批流程表
#### approval_steps - 审批步骤表
### 其他
#### navigation_groups - 导航分组表
#### navigation_links - 导航链接表
#### audit_logs - 审计日志表
| 字段 | 类型 | 说明 |
|------|------|------|
| id | uint | 主键 |
| user_id | *uint | 操作用户 ID |
| module | string | 模块 |
| action | string | 操作类型 |
| resource | string | 资源标识 |
| request | string | 请求内容 |
| response | string | 响应内容 |
| error | string | 错误信息 |
| ip | string | 操作 IP |
| created_at | time | 操作时间 |
## 表关系
```
users ──< user_roles >── roles ──< role_permissions >── permissions
servers ──> cloud_accounts ──> clouds
servers ──> regions
alert_rules ──< alert_events
alert_rules ──> notify_channels
pipelines ──< pipeline_runs
app_releases ──> pipelines
approval_templates ──< approval_processes ──< approval_steps
navigation_groups ──< navigation_links
```
## AutoMigrate
表结构在 `backend/pkg/database/database.go` 中定义,服务启动时自动执行:
```go
func AutoMigrate(db *gorm.DB) error {
return db.AutoMigrate(
&models.User{}, &models.Role{}, &models.Permission{},
&models.Server{}, &models.Cloud{}, ...
)
}
```
## 切换数据库
### MySQL
编辑 `backend/config.yaml`:
```yaml
database:
driver: mysql
host: localhost
port: 3306
user: root
password: your_password
dbname: aiops
charset: utf8mb4
```
### PostgreSQL
```yaml
database:
driver: postgres
host: localhost
port: 5432
user: postgres
password: your_password
dbname: aiops
```