diff --git a/backend/controller/log_datasource.go b/backend/controller/log_datasource.go index 601fb1a..50a80f8 100644 --- a/backend/controller/log_datasource.go +++ b/backend/controller/log_datasource.go @@ -6,6 +6,7 @@ import ( "encoding/json" "errors" "io" + "log" "net/http" "strings" "time" @@ -76,6 +77,8 @@ func (h *LogDSHandler) Create(c *gin.Context) { IsDefault: req.IsDefault, Status: 1, } if err := h.db.Create(&ds).Error; err != nil { + // 记录详细错误,便于排查字段缺失/唯一约束等数据库问题 // Logan.Li + log.Printf("[LogDataSource] 创建数据源失败: %v", err) if errors.Is(err, gorm.ErrDuplicatedKey) { c.JSON(409, model.Response{Code: 409, Message: "数据源名称已存在"}) return diff --git a/backend/pkg/database/database.go b/backend/pkg/database/database.go index 44fe261..6cfa16a 100644 --- a/backend/pkg/database/database.go +++ b/backend/pkg/database/database.go @@ -192,4 +192,16 @@ func applySchemaPatches(db *gorm.DB) { patchUserColumn("ApiAccessKeyDescription", "api_access_key_description") patchUserColumn("ApiAccessKeyDurationMonths", "api_access_key_duration_months") patchUserColumn("ApiAccessKeyLastUsedAt", "api_access_key_last_used_at") + + // 补全日志数据源表可能缺失的列(旧表仅有基础字段)// Logan.Li + patchLogDSColumn := func(field string, col string) { + if !m.HasColumn(&model.LogDataSource{}, col) { + if err := m.AddColumn(&model.LogDataSource{}, field); err != nil { + log.Printf("[DB] 补列 monitor_log_datasources.%s 失败: %v", col, err) + } + } + } + patchLogDSColumn("AuthType", "auth_type") + patchLogDSColumn("Token", "token") + patchLogDSColumn("Headers", "headers") } diff --git a/frontend/src/components/monitor/log-center/UnifiedQueryPanel.tsx b/frontend/src/components/monitor/log-center/UnifiedQueryPanel.tsx index b8d754d..ac7a650 100644 --- a/frontend/src/components/monitor/log-center/UnifiedQueryPanel.tsx +++ b/frontend/src/components/monitor/log-center/UnifiedQueryPanel.tsx @@ -176,19 +176,15 @@ export function UnifiedQueryPanel({ engine, onRuleCreated }: Props) { const [saveMenuOpen, setSaveMenuOpen] = useState(false); const [hasQueried, setHasQueried] = useState(false); - // 数据源加载后选中默认或首个(无源时保持空)// Logan.Li + // 数据源加载后选中默认或首个(无源时保持空),并同步 ES 默认索引 // Logan.Li useEffect(() => { if (sources.length > 0) { const def = sources.find(s => s.is_default) || sources[0]; setDsId(String(def.id)); + setEsIndex(def.default_index || ''); } }, [sources]); - // 当前数据源变化时,同步 ES 临时索引框为数据源的默认索引 // Logan.Li - useEffect(() => { - setEsIndex(currentSource?.default_index || ''); - }, [currentSource]); - const currentSource = sources.find(s => String(s.id) === dsId); const fields = useMemo(() => extractFields(entries), [entries]);