From 9e98848d696407ffdc2a857e775c9692625cbca3 Mon Sep 17 00:00:00 2001 From: wizardchen Date: Fri, 21 Nov 2025 01:39:43 +0800 Subject: [PATCH] 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. --- frontend/src/i18n/locales/en-US.ts | 5 ++++ frontend/src/i18n/locales/ru-RU.ts | 5 ++++ frontend/src/i18n/locales/zh-CN.ts | 5 ++++ .../src/views/knowledge/KnowledgeBase.vue | 2 +- .../src/views/knowledge/KnowledgeBaseList.vue | 7 +++++- .../knowledge/components/FAQEntryManager.vue | 3 +-- internal/application/repository/chunk.go | 9 ++++++++ internal/application/repository/knowledge.go | 9 ++++++++ internal/application/service/knowledgebase.go | 23 +++++++++++++++++++ internal/types/interfaces/chunk.go | 2 ++ internal/types/interfaces/knowledge.go | 2 ++ internal/types/knowledgebase.go | 4 ++++ 12 files changed, 72 insertions(+), 4 deletions(-) diff --git a/frontend/src/i18n/locales/en-US.ts b/frontend/src/i18n/locales/en-US.ts index 4e751492..531b99df 100644 --- a/frontend/src/i18n/locales/en-US.ts +++ b/frontend/src/i18n/locales/en-US.ts @@ -750,6 +750,11 @@ export default { features: { knowledgeGraph: 'Knowledge Graph Enabled', multimodal: 'Multimodal Enabled' + }, + stats: { + documents: 'Document Count', + faqEntries: 'FAQ Entries', + chunks: 'Chunk Count' } }, knowledgeEditor: { diff --git a/frontend/src/i18n/locales/ru-RU.ts b/frontend/src/i18n/locales/ru-RU.ts index 7fc166a6..0fe3567a 100644 --- a/frontend/src/i18n/locales/ru-RU.ts +++ b/frontend/src/i18n/locales/ru-RU.ts @@ -839,6 +839,11 @@ export default { features: { knowledgeGraph: 'Граф знаний включен', multimodal: 'Мультимодальность включена' + }, + stats: { + documents: 'Количество документов', + faqEntries: 'FAQ записи', + chunks: 'Количество фрагментов' } }, knowledgeEditor: { diff --git a/frontend/src/i18n/locales/zh-CN.ts b/frontend/src/i18n/locales/zh-CN.ts index 039a1987..36576c6a 100644 --- a/frontend/src/i18n/locales/zh-CN.ts +++ b/frontend/src/i18n/locales/zh-CN.ts @@ -1075,6 +1075,11 @@ export default { knowledgeGraph: "已启用知识图谱", multimodal: "已启用多模态", }, + stats: { + documents: "文档数量", + faqEntries: "问答条目", + chunks: "分块数量", + }, }, knowledgeEditor: { titleCreate: "新建知识库", diff --git a/frontend/src/views/knowledge/KnowledgeBase.vue b/frontend/src/views/knowledge/KnowledgeBase.vue index 10100239..0cdc11e1 100644 --- a/frontend/src/views/knowledge/KnowledgeBase.vue +++ b/frontend/src/views/knowledge/KnowledgeBase.vue @@ -678,7 +678,7 @@ const knowledgeDropdownOptions = computed(() => knowledgeList.value.map((item) => ({ content: item.name, value: item.id, - prefixIcon: () => h(TIcon, { name: item.type === 'faq' ? 'help-circle' : 'folder', size: '16px' }), + prefixIcon: () => h(TIcon, { name: item.type === 'faq' ? 'chat-bubble-help' : 'folder', size: '16px' }), })) ); diff --git a/frontend/src/views/knowledge/KnowledgeBaseList.vue b/frontend/src/views/knowledge/KnowledgeBaseList.vue index 179c49be..ef3eb041 100644 --- a/frontend/src/views/knowledge/KnowledgeBaseList.vue +++ b/frontend/src/views/knowledge/KnowledgeBaseList.vue @@ -74,7 +74,10 @@
- {{ kb.type === 'faq' ? $t('knowledgeEditor.basic.typeFAQ') : $t('knowledgeEditor.basic.typeDocument') }} + + {{ kb.type === 'faq' ? $t('knowledgeEditor.basic.typeFAQ') : $t('knowledgeEditor.basic.typeDocument') }} + ({{ kb.type === 'faq' ? (kb.chunk_count || 0) : (kb.knowledge_count || 0) }}) +
@@ -167,6 +170,8 @@ interface KB { vlm_config?: { enabled?: boolean; model_id?: string }; extract_config?: { enabled?: boolean }; cos_config?: { provider?: string; bucket_name?: string }; + knowledge_count?: number; + chunk_count?: number; } const kbs = ref([]) diff --git a/frontend/src/views/knowledge/components/FAQEntryManager.vue b/frontend/src/views/knowledge/components/FAQEntryManager.vue index c42e1723..d6281d19 100644 --- a/frontend/src/views/knowledge/components/FAQEntryManager.vue +++ b/frontend/src/views/knowledge/components/FAQEntryManager.vue @@ -1097,11 +1097,10 @@ const kbInfo = ref(null) const knowledgeList = ref>([]) const knowledgeDropdownOptions = computed(() => knowledgeList.value - .filter((item) => !item.type || item.type === 'document') .map((item) => ({ content: item.name, value: item.id, - prefixIcon: () => h(TIcon, { name: 'file', size: '16px' }), + prefixIcon: () => h(TIcon, { name: item.type === 'document' ? 'folder' : 'chat-bubble-help', size: '16px' }), })), ) diff --git a/internal/application/repository/chunk.go b/internal/application/repository/chunk.go index 72487b65..c5e43ece 100644 --- a/internal/application/repository/chunk.go +++ b/internal/application/repository/chunk.go @@ -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 +} diff --git a/internal/application/repository/knowledge.go b/internal/application/repository/knowledge.go index 60cba698..ec07d6d9 100644 --- a/internal/application/repository/knowledge.go +++ b/internal/application/repository/knowledge.go @@ -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 +} diff --git a/internal/application/service/knowledgebase.go b/internal/application/service/knowledgebase.go index 9eac0197..9637579d 100644 --- a/internal/application/service/knowledgebase.go +++ b/internal/application/service/knowledgebase.go @@ -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", diff --git a/internal/types/interfaces/chunk.go b/internal/types/interfaces/chunk.go index 2849c296..d8f0c057 100644 --- a/internal/types/interfaces/chunk.go +++ b/internal/types/interfaces/chunk.go @@ -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 diff --git a/internal/types/interfaces/knowledge.go b/internal/types/interfaces/knowledge.go index b5231da9..bedc514e 100644 --- a/internal/types/interfaces/knowledge.go +++ b/internal/types/interfaces/knowledge.go @@ -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) } diff --git a/internal/types/knowledgebase.go b/internal/types/knowledgebase.go index 7d076849..5788b50d 100644 --- a/internal/types/knowledgebase.go +++ b/internal/types/knowledgebase.go @@ -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