feat: Add statistics localization and enhance knowledge base functionality
- Introduced new localization strings for document count, FAQ entries, and chunk count in English, Russian, and Chinese. - Updated the KnowledgeBase and KnowledgeBaseList components to display knowledge and chunk counts. - Implemented backend functions to count knowledge items and chunks associated with each knowledge base, improving data management and user insights.
This commit is contained in:
@@ -154,3 +154,12 @@ func (r *chunkRepository) DeleteByKnowledgeList(ctx context.Context, tenantID ui
|
||||
"tenant_id = ? AND knowledge_id in ?", tenantID, knowledgeIDs,
|
||||
).Delete(&types.Chunk{}).Error
|
||||
}
|
||||
|
||||
// CountChunksByKnowledgeBaseID counts the number of chunks in a knowledge base
|
||||
func (r *chunkRepository) CountChunksByKnowledgeBaseID(ctx context.Context, tenantID uint, kbID string) (int64, error) {
|
||||
var count int64
|
||||
err := r.db.WithContext(ctx).Model(&types.Chunk{}).
|
||||
Where("tenant_id = ? AND knowledge_base_id = ?", tenantID, kbID).
|
||||
Count(&count).Error
|
||||
return count, err
|
||||
}
|
||||
|
||||
@@ -220,3 +220,12 @@ func (r *knowledgeRepository) UpdateKnowledgeColumn(ctx context.Context, id stri
|
||||
err := r.db.WithContext(ctx).Model(&types.Knowledge{}).Where("id = ?", id).Update(column, value).Error
|
||||
return err
|
||||
}
|
||||
|
||||
// CountKnowledgeByKnowledgeBaseID counts the number of knowledge items in a knowledge base
|
||||
func (r *knowledgeRepository) CountKnowledgeByKnowledgeBaseID(ctx context.Context, tenantID uint, kbID string) (int64, error) {
|
||||
var count int64
|
||||
err := r.db.WithContext(ctx).Model(&types.Knowledge{}).
|
||||
Where("tenant_id = ? AND knowledge_base_id = ?", tenantID, kbID).
|
||||
Count(&count).Error
|
||||
return count, err
|
||||
}
|
||||
|
||||
@@ -122,6 +122,29 @@ func (s *knowledgeBaseService) ListKnowledgeBases(ctx context.Context) ([]*types
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Query knowledge count and chunk count for each knowledge base
|
||||
for _, kb := range kbs {
|
||||
kb.EnsureDefaults()
|
||||
|
||||
// Get knowledge count
|
||||
if kb.Type == types.KnowledgeBaseTypeDocument {
|
||||
knowledgeCount, err := s.kgRepo.CountKnowledgeByKnowledgeBaseID(ctx, tenantID, kb.ID)
|
||||
if err != nil {
|
||||
logger.Warnf(ctx, "Failed to get knowledge count for knowledge base %s: %v", kb.ID, err)
|
||||
} else {
|
||||
kb.KnowledgeCount = knowledgeCount
|
||||
}
|
||||
} else if kb.Type == types.KnowledgeBaseTypeFAQ {
|
||||
// Get chunk count
|
||||
chunkCount, err := s.chunkRepo.CountChunksByKnowledgeBaseID(ctx, tenantID, kb.ID)
|
||||
if err != nil {
|
||||
logger.Warnf(ctx, "Failed to get chunk count for knowledge base %s: %v", kb.ID, err)
|
||||
} else {
|
||||
kb.ChunkCount = chunkCount
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
logger.Infof(
|
||||
ctx,
|
||||
"Knowledge base list retrieved successfully, tenant ID: %d, knowledge base count: %d",
|
||||
|
||||
@@ -37,6 +37,8 @@ type ChunkRepository interface {
|
||||
DeleteChunksByKnowledgeID(ctx context.Context, tenantID uint, knowledgeID string) error
|
||||
// DeleteByKnowledgeList deletes all chunks for a knowledge list
|
||||
DeleteByKnowledgeList(ctx context.Context, tenantID uint, knowledgeIDs []string) error
|
||||
// CountChunksByKnowledgeBaseID counts the number of chunks in a knowledge base.
|
||||
CountChunksByKnowledgeBaseID(ctx context.Context, tenantID uint, kbID string) (int64, error)
|
||||
}
|
||||
|
||||
// ChunkService defines the interface for chunk service operations
|
||||
|
||||
@@ -102,4 +102,6 @@ type KnowledgeRepository interface {
|
||||
// AminusB returns the difference set of A and B.
|
||||
AminusB(ctx context.Context, Atenant uint, A string, Btenant uint, B string) ([]string, error)
|
||||
UpdateKnowledgeColumn(ctx context.Context, id string, column string, value interface{}) error
|
||||
// CountKnowledgeByKnowledgeBaseID counts the number of knowledge items in a knowledge base.
|
||||
CountKnowledgeByKnowledgeBaseID(ctx context.Context, tenantID uint, kbID string) (int64, error)
|
||||
}
|
||||
|
||||
@@ -69,6 +69,10 @@ type KnowledgeBase struct {
|
||||
UpdatedAt time.Time `yaml:"updated_at" json:"updated_at"`
|
||||
// Deletion time of the knowledge base
|
||||
DeletedAt gorm.DeletedAt `yaml:"deleted_at" json:"deleted_at" gorm:"index"`
|
||||
// Knowledge count (not stored in database, calculated on query)
|
||||
KnowledgeCount int64 `yaml:"knowledge_count" json:"knowledge_count" gorm:"-"`
|
||||
// Chunk count (not stored in database, calculated on query)
|
||||
ChunkCount int64 `yaml:"chunk_count" json:"chunk_count" gorm:"-"`
|
||||
}
|
||||
|
||||
// KnowledgeBaseConfig represents the knowledge base configuration
|
||||
|
||||
Reference in New Issue
Block a user