feat: 添加FAQ导入结果持久化展示功能
This commit is contained in:
@@ -2897,6 +2897,57 @@ func csvEscape(s string) string {
|
||||
return s
|
||||
}
|
||||
|
||||
// saveFAQImportResultToDatabase 保存FAQ导入结果统计到数据库
|
||||
func (s *knowledgeService) saveFAQImportResultToDatabase(ctx context.Context,
|
||||
payload *types.FAQImportPayload, progress *types.FAQImportProgress, originalTotalEntries int) error {
|
||||
|
||||
// 获取FAQ知识库实例
|
||||
tenantID := ctx.Value(types.TenantIDContextKey).(uint64)
|
||||
knowledge, err := s.repo.GetKnowledgeByID(ctx, tenantID, payload.KnowledgeID)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to get FAQ knowledge: %w", err)
|
||||
}
|
||||
|
||||
// 计算跳过的条目数(总数 - 成功 - 失败)
|
||||
skippedCount := originalTotalEntries - progress.SuccessCount - progress.FailedCount
|
||||
if skippedCount < 0 {
|
||||
skippedCount = 0
|
||||
}
|
||||
|
||||
// 创建导入结果统计
|
||||
importResult := &types.FAQImportResult{
|
||||
TotalEntries: originalTotalEntries,
|
||||
SuccessCount: progress.SuccessCount,
|
||||
FailedCount: progress.FailedCount,
|
||||
SkippedCount: skippedCount,
|
||||
ImportMode: payload.Mode,
|
||||
ImportedAt: time.Now(),
|
||||
TaskID: payload.TaskID,
|
||||
ProcessingTime: time.Now().Unix() - progress.CreatedAt, // 处理耗时(秒)
|
||||
DisplayStatus: "open", // 新导入的结果默认显示
|
||||
}
|
||||
|
||||
// 如果有失败条目且提供了下载URL,设置失败URL
|
||||
if progress.FailedCount > 0 && progress.FailedEntriesURL != "" {
|
||||
importResult.FailedEntriesURL = progress.FailedEntriesURL
|
||||
}
|
||||
|
||||
// 设置导入结果到Knowledge的metadata中
|
||||
if err := knowledge.SetLastFAQImportResult(importResult); err != nil {
|
||||
return fmt.Errorf("failed to set FAQ import result: %w", err)
|
||||
}
|
||||
|
||||
// 更新数据库
|
||||
if err := s.repo.UpdateKnowledge(ctx, knowledge); err != nil {
|
||||
return fmt.Errorf("failed to update knowledge with import result: %w", err)
|
||||
}
|
||||
|
||||
logger.Infof(ctx, "Saved FAQ import result to database: knowledge_id=%s, task_id=%s, total=%d, success=%d, failed=%d, skipped=%d",
|
||||
payload.KnowledgeID, payload.TaskID, originalTotalEntries, progress.SuccessCount, progress.FailedCount, skippedCount)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// buildFAQFailedEntry 构建 FAQFailedEntry
|
||||
func buildFAQFailedEntry(idx int, reason string, entry *types.FAQEntryPayload) types.FAQFailedEntry {
|
||||
answerAll := false
|
||||
@@ -5967,6 +6018,13 @@ func (s *knowledgeService) finalizeFAQValidation(ctx context.Context, payload *t
|
||||
}
|
||||
}
|
||||
|
||||
// 如果不是 dry run 模式,保存导入结果统计到数据库
|
||||
if !payload.DryRun {
|
||||
if err := s.saveFAQImportResultToDatabase(ctx, payload, progress, originalTotalEntries); err != nil {
|
||||
logger.Warnf(ctx, "Failed to save FAQ import result to database: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
// 使用 updateFAQImportProgressStatus 来确保正确清理 running key
|
||||
// 但是需要先保存其他字段,因为 updateFAQImportProgressStatus 不会保存所有字段
|
||||
if err := s.saveFAQImportProgress(ctx, progress); err != nil {
|
||||
@@ -6040,6 +6098,94 @@ func (s *knowledgeService) GetFAQImportProgress(ctx context.Context, taskID stri
|
||||
return &progress, nil
|
||||
}
|
||||
|
||||
// GetLastFAQImportResult retrieves the latest FAQ import result for a knowledge base
|
||||
func (s *knowledgeService) GetLastFAQImportResult(ctx context.Context, kbID string) (*types.FAQImportResult, error) {
|
||||
// 获取当前租户ID
|
||||
tenantID := ctx.Value(types.TenantIDContextKey).(uint64)
|
||||
|
||||
// 查找FAQ类型的knowledge
|
||||
knowledgeList, err := s.repo.ListKnowledgeByKnowledgeBaseID(ctx, tenantID, kbID)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to list knowledge: %w", err)
|
||||
}
|
||||
|
||||
// 查找FAQ类型的knowledge
|
||||
var faqKnowledge *types.Knowledge
|
||||
for _, k := range knowledgeList {
|
||||
if k.Type == types.KnowledgeTypeFAQ {
|
||||
faqKnowledge = k
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
if faqKnowledge == nil {
|
||||
return nil, werrors.NewNotFoundError("FAQ knowledge not found in this knowledge base")
|
||||
}
|
||||
|
||||
// 解析导入结果
|
||||
result, err := faqKnowledge.GetLastFAQImportResult()
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to parse FAQ import result: %w", err)
|
||||
}
|
||||
|
||||
return result, nil
|
||||
}
|
||||
|
||||
// UpdateLastFAQImportResultDisplayStatus updates the display status of FAQ import result
|
||||
func (s *knowledgeService) UpdateLastFAQImportResultDisplayStatus(ctx context.Context, kbID string, displayStatus string) error {
|
||||
// 验证displayStatus参数
|
||||
if displayStatus != "open" && displayStatus != "close" {
|
||||
return werrors.NewBadRequestError("invalid display status, must be 'open' or 'close'")
|
||||
}
|
||||
|
||||
// 获取当前租户ID
|
||||
tenantID := ctx.Value(types.TenantIDContextKey).(uint64)
|
||||
|
||||
// 查找FAQ类型的knowledge
|
||||
knowledgeList, err := s.repo.ListKnowledgeByKnowledgeBaseID(ctx, tenantID, kbID)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to list knowledge: %w", err)
|
||||
}
|
||||
|
||||
// 查找FAQ类型的knowledge
|
||||
var faqKnowledge *types.Knowledge
|
||||
for _, k := range knowledgeList {
|
||||
if k.Type == types.KnowledgeTypeFAQ {
|
||||
faqKnowledge = k
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
if faqKnowledge == nil {
|
||||
return werrors.NewNotFoundError("FAQ knowledge not found in this knowledge base")
|
||||
}
|
||||
|
||||
// 解析当前的导入结果
|
||||
result, err := faqKnowledge.GetLastFAQImportResult()
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to parse FAQ import result: %w", err)
|
||||
}
|
||||
|
||||
if result == nil {
|
||||
return werrors.NewNotFoundError("no FAQ import result found")
|
||||
}
|
||||
|
||||
// 更新显示状态
|
||||
result.DisplayStatus = displayStatus
|
||||
|
||||
// 保存更新后的结果
|
||||
if err := faqKnowledge.SetLastFAQImportResult(result); err != nil {
|
||||
return fmt.Errorf("failed to set FAQ import result: %w", err)
|
||||
}
|
||||
|
||||
// 更新数据库
|
||||
if err := s.repo.UpdateKnowledge(ctx, faqKnowledge); err != nil {
|
||||
return fmt.Errorf("failed to update knowledge: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// ProcessKBClone handles Asynq knowledge base clone tasks
|
||||
func (s *knowledgeService) ProcessKBClone(ctx context.Context, t *asynq.Task) error {
|
||||
var payload types.KBClonePayload
|
||||
|
||||
@@ -422,3 +422,82 @@ func (h *FAQHandler) GetImportProgress(c *gin.Context) {
|
||||
"data": progress,
|
||||
})
|
||||
}
|
||||
|
||||
// GetLastImportResult godoc
|
||||
// @Summary 获取FAQ最后一次导入结果统计
|
||||
// @Description 获取FAQ知识库的最后一次导入结果统计信息
|
||||
// @Tags FAQ管理
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Param id path string true "知识库ID"
|
||||
// @Success 200 {object} map[string]interface{} "导入结果统计"
|
||||
// @Failure 404 {object} errors.AppError "知识库不存在或无导入记录"
|
||||
// @Security Bearer
|
||||
// @Security ApiKeyAuth
|
||||
// @Router /knowledge-bases/{id}/faq/import/last-result [get]
|
||||
func (h *FAQHandler) GetLastImportResult(c *gin.Context) {
|
||||
ctx := c.Request.Context()
|
||||
kbID := secutils.SanitizeForLog(c.Param("id"))
|
||||
|
||||
result, err := h.knowledgeService.GetLastFAQImportResult(ctx, kbID)
|
||||
if err != nil {
|
||||
logger.ErrorWithFields(ctx, err, nil)
|
||||
c.Error(err)
|
||||
return
|
||||
}
|
||||
|
||||
if result == nil {
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"success": true,
|
||||
"data": nil,
|
||||
"message": "暂无导入记录",
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"success": true,
|
||||
"data": result,
|
||||
})
|
||||
}
|
||||
|
||||
// updateLastFAQImportResultDisplayStatusRequest is the request payload for UpdateLastImportResultDisplayStatus
|
||||
type updateLastFAQImportResultDisplayStatusRequest struct {
|
||||
DisplayStatus string `json:"display_status" binding:"required,oneof=open close"`
|
||||
}
|
||||
|
||||
// UpdateLastImportResultDisplayStatus godoc
|
||||
// @Summary 更新FAQ最后一次导入结果显示状态
|
||||
// @Description 更新FAQ知识库导入结果统计卡片的显示或隐藏状态
|
||||
// @Tags FAQ管理
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Param id path string true "知识库ID"
|
||||
// @Param request body updateLastFAQImportResultDisplayStatusRequest true "状态更新请求"
|
||||
// @Success 200 {object} map[string]interface{} "更新成功"
|
||||
// @Failure 400 {object} errors.AppError "请求参数错误"
|
||||
// @Failure 404 {object} errors.AppError "知识库不存在或无导入记录"
|
||||
// @Security Bearer
|
||||
// @Security ApiKeyAuth
|
||||
// @Router /knowledge-bases/{id}/faq/import/last-result/display [put]
|
||||
func (h *FAQHandler) UpdateLastImportResultDisplayStatus(c *gin.Context) {
|
||||
ctx := c.Request.Context()
|
||||
kbID := secutils.SanitizeForLog(c.Param("id"))
|
||||
|
||||
var req updateLastFAQImportResultDisplayStatusRequest
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
logger.Error(ctx, "Failed to bind display status update payload", err)
|
||||
c.Error(errors.NewBadRequestError("请求参数不合法").WithDetails(err.Error()))
|
||||
return
|
||||
}
|
||||
|
||||
if err := h.knowledgeService.UpdateLastFAQImportResultDisplayStatus(ctx, kbID, req.DisplayStatus); err != nil {
|
||||
logger.ErrorWithFields(ctx, err, nil)
|
||||
c.Error(err)
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"success": true,
|
||||
})
|
||||
}
|
||||
|
||||
@@ -194,6 +194,9 @@ func RegisterFAQRoutes(r *gin.RouterGroup, handler *handler.FAQHandler) {
|
||||
faq.PUT("/entries/tags", handler.UpdateEntryTagBatch)
|
||||
faq.DELETE("/entries", handler.DeleteEntries)
|
||||
faq.POST("/search", handler.SearchFAQ)
|
||||
// FAQ import result API
|
||||
faq.GET("/import/last-result", handler.GetLastImportResult)
|
||||
faq.PUT("/import/last-result/display", handler.UpdateLastImportResultDisplayStatus)
|
||||
}
|
||||
// FAQ import progress route (outside of knowledge-base scope)
|
||||
faqImport := r.Group("/faq/import")
|
||||
|
||||
@@ -320,6 +320,30 @@ type FAQImportMetadata struct {
|
||||
ImportProcessed int `json:"import_processed"`
|
||||
}
|
||||
|
||||
// FAQImportResult 存储FAQ导入完成后的统计结果
|
||||
// 这个信息是持久化的,不跟随进度状态,直到下次导入时被替换
|
||||
type FAQImportResult struct {
|
||||
// 导入统计信息
|
||||
TotalEntries int `json:"total_entries"` // 总条目数
|
||||
SuccessCount int `json:"success_count"` // 成功导入的条目数
|
||||
FailedCount int `json:"failed_count"` // 失败的条目数
|
||||
SkippedCount int `json:"skipped_count"` // 跳过的条目数(如重复等)
|
||||
|
||||
// 导入模式和时间信息
|
||||
ImportMode string `json:"import_mode"` // 导入模式:append 或 replace
|
||||
ImportedAt time.Time `json:"imported_at"` // 导入完成时间
|
||||
TaskID string `json:"task_id"` // 导入任务ID
|
||||
|
||||
// 失败详情URL(失败条目较多时提供下载链接)
|
||||
FailedEntriesURL string `json:"failed_entries_url,omitempty"` // 失败条目CSV下载URL
|
||||
|
||||
// 显示控制
|
||||
DisplayStatus string `json:"display_status"` // 显示状态:open 或 close
|
||||
|
||||
// 额外统计信息
|
||||
ProcessingTime int64 `json:"processing_time"` // 处理耗时(毫秒)
|
||||
}
|
||||
|
||||
// ToJSON converts the metadata to JSON type.
|
||||
func (m *FAQImportMetadata) ToJSON() (JSON, error) {
|
||||
if m == nil {
|
||||
@@ -332,6 +356,18 @@ func (m *FAQImportMetadata) ToJSON() (JSON, error) {
|
||||
return JSON(bytes), nil
|
||||
}
|
||||
|
||||
// ToJSON converts the import result to JSON type.
|
||||
func (r *FAQImportResult) ToJSON() (JSON, error) {
|
||||
if r == nil {
|
||||
return nil, nil
|
||||
}
|
||||
bytes, err := json.Marshal(r)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return JSON(bytes), nil
|
||||
}
|
||||
|
||||
// ParseFAQImportMetadata parses FAQ import metadata from Knowledge.
|
||||
func ParseFAQImportMetadata(k *Knowledge) (*FAQImportMetadata, error) {
|
||||
if k == nil || len(k.Metadata) == 0 {
|
||||
|
||||
@@ -126,6 +126,10 @@ type KnowledgeService interface {
|
||||
SaveKBCloneProgress(ctx context.Context, progress *types.KBCloneProgress) error
|
||||
// GetFAQImportProgress retrieves the progress of an FAQ import task
|
||||
GetFAQImportProgress(ctx context.Context, taskID string) (*types.FAQImportProgress, error)
|
||||
// GetLastFAQImportResult retrieves the latest FAQ import result for a knowledge base
|
||||
GetLastFAQImportResult(ctx context.Context, kbID string) (*types.FAQImportResult, error)
|
||||
// UpdateLastFAQImportResultDisplayStatus updates the display status of FAQ import result
|
||||
UpdateLastFAQImportResultDisplayStatus(ctx context.Context, kbID string, displayStatus string) error
|
||||
// SearchKnowledge searches knowledge items by keyword across the tenant.
|
||||
// fileTypes: optional list of file extensions to filter by (e.g., ["csv", "xlsx"])
|
||||
SearchKnowledge(ctx context.Context, keyword string, offset, limit int, fileTypes []string) ([]*types.Knowledge, bool, error)
|
||||
|
||||
@@ -93,6 +93,8 @@ type Knowledge struct {
|
||||
StorageSize int64 `json:"storage_size"`
|
||||
// Metadata of the knowledge
|
||||
Metadata JSON `json:"metadata" gorm:"type:json"`
|
||||
// Last FAQ import result (for FAQ type knowledge only)
|
||||
LastFAQImportResult JSON `json:"last_faq_import_result" gorm:"type:json"`
|
||||
// Creation time of the knowledge
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
// Last updated time of the knowledge
|
||||
@@ -214,6 +216,32 @@ func (k *Knowledge) SetManualMetadata(meta *ManualKnowledgeMetadata) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// SetLastFAQImportResult sets FAQ import result to the dedicated field.
|
||||
func (k *Knowledge) SetLastFAQImportResult(result *FAQImportResult) error {
|
||||
if result == nil {
|
||||
k.LastFAQImportResult = nil
|
||||
return nil
|
||||
}
|
||||
jsonValue, err := result.ToJSON()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
k.LastFAQImportResult = jsonValue
|
||||
return nil
|
||||
}
|
||||
|
||||
// GetLastFAQImportResult parses and returns FAQ import result from the dedicated field.
|
||||
func (k *Knowledge) GetLastFAQImportResult() (*FAQImportResult, error) {
|
||||
if len(k.LastFAQImportResult) == 0 {
|
||||
return nil, nil
|
||||
}
|
||||
var result FAQImportResult
|
||||
if err := json.Unmarshal(k.LastFAQImportResult, &result); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &result, nil
|
||||
}
|
||||
|
||||
// IsManual returns true if the knowledge item is manual Markdown knowledge.
|
||||
func (k *Knowledge) IsManual() bool {
|
||||
return k != nil && k.Type == KnowledgeTypeManual
|
||||
|
||||
Reference in New Issue
Block a user