feat(knowledge): add scoped activity audit trail

This commit is contained in:
wizardchen
2026-07-22 19:15:27 +08:00
committed by lyingbug
parent f195e0494c
commit a3a0308b62
44 changed files with 2429 additions and 81 deletions
@@ -32,6 +32,7 @@ type DataSourceService struct {
scheduler *datasource.Scheduler
tenantRepo interfaces.TenantRepository
tagService interfaces.KnowledgeTagService
audit interfaces.AuditLogService
}
// NewDataSourceService creates a new data source service
@@ -45,6 +46,7 @@ func NewDataSourceService(
scheduler *datasource.Scheduler,
tenantRepo interfaces.TenantRepository,
tagService interfaces.KnowledgeTagService,
audit interfaces.AuditLogService,
) interfaces.DataSourceService {
return &DataSourceService{
dsRepo: dsRepo,
@@ -56,6 +58,7 @@ func NewDataSourceService(
scheduler: scheduler,
tenantRepo: tenantRepo,
tagService: tagService,
audit: audit,
}
}
@@ -105,6 +108,9 @@ func (s *DataSourceService) CreateDataSource(ctx context.Context, ds *types.Data
}
logger.Infof(ctx, "data source created: id=%s type=%s kb=%s", ds.ID, ds.Type, ds.KnowledgeBaseID)
recordKBActivity(ctx, s.audit, ds.TenantID, ds.KnowledgeBaseID, types.AuditActionDataSourceCreated,
"data_source", ds.ID, types.AuditOutcomeSuccess,
map[string]any{"name": ds.Name, "type": ds.Type})
return ds, nil
}
@@ -218,6 +224,9 @@ func (s *DataSourceService) UpdateDataSource(ctx context.Context, ds *types.Data
}
logger.Infof(ctx, "data source updated: id=%s", ds.ID)
recordKBActivity(ctx, s.audit, ds.TenantID, ds.KnowledgeBaseID, types.AuditActionDataSourceUpdated,
"data_source", ds.ID, types.AuditOutcomeSuccess,
map[string]any{"name": ds.Name, "type": ds.Type, "changed_fields": []string{"settings"}})
return ds, nil
}
@@ -261,6 +270,9 @@ func (s *DataSourceService) UpdateDataSourceCredentials(
return nil, err
}
logger.Infof(ctx, "DataSource credentials updated: id=%s", secutils.SanitizeForLog(id))
recordKBActivity(ctx, s.audit, existing.TenantID, existing.KnowledgeBaseID, types.AuditActionDataSourceUpdated,
"data_source", existing.ID, types.AuditOutcomeSuccess,
map[string]any{"name": existing.Name, "type": existing.Type, "changed_fields": []string{"credentials"}})
return existing, nil
}
@@ -300,13 +312,16 @@ func (s *DataSourceService) ClearDataSourceCredentials(ctx context.Context, id s
return err
}
logger.Infof(ctx, "DataSource credentials cleared by user: id=%s", secutils.SanitizeForLog(id))
recordKBActivity(ctx, s.audit, existing.TenantID, existing.KnowledgeBaseID, types.AuditActionDataSourceUpdated,
"data_source", existing.ID, types.AuditOutcomeSuccess,
map[string]any{"name": existing.Name, "type": existing.Type, "changed_fields": []string{"credentials"}})
return nil
}
// DeleteDataSource deletes a data source (soft delete)
func (s *DataSourceService) DeleteDataSource(ctx context.Context, id string) error {
// Verify data source exists
_, err := s.dsRepo.FindByID(ctx, id)
existing, err := s.dsRepo.FindByID(ctx, id)
if err != nil {
return err
}
@@ -325,6 +340,9 @@ func (s *DataSourceService) DeleteDataSource(ctx context.Context, id string) err
}
logger.Infof(ctx, "data source deleted: id=%s", id)
recordKBActivity(ctx, s.audit, existing.TenantID, existing.KnowledgeBaseID, types.AuditActionDataSourceDeleted,
"data_source", existing.ID, types.AuditOutcomeSuccess,
map[string]any{"name": existing.Name, "type": existing.Type})
return nil
}
@@ -464,6 +482,8 @@ func (s *DataSourceService) ManualSync(ctx context.Context, dsID string) (*types
TenantID: ds.TenantID,
SyncLogID: syncLog.ID,
ForceFull: false,
Initiator: types.TaskInitiatorFromContext(ctx),
Trigger: "manual",
}
langfuse.InjectTracing(ctx, payload)
@@ -471,7 +491,7 @@ func (s *DataSourceService) ManualSync(ctx context.Context, dsID string) (*types
task := asynq.NewTask(types.TypeDataSourceSync, payloadJSON,
asynq.Queue(types.QueueSync), asynq.MaxRetry(5), asynq.Timeout(2*time.Hour))
_, err = s.taskEnqueuer.Enqueue(task)
info, err := s.taskEnqueuer.Enqueue(task)
if err != nil {
logger.Errorf(ctx, "failed to enqueue sync task: %v", err)
syncLog.Status = types.SyncLogStatusFailed
@@ -483,10 +503,17 @@ func (s *DataSourceService) ManualSync(ctx context.Context, dsID string) (*types
}
ds.ErrorMessage = fmt.Sprintf("Failed to enqueue sync: %v", err)
_ = s.dsRepo.Update(ctx, ds)
recordKBActivity(ctx, s.audit, ds.TenantID, ds.KnowledgeBaseID, types.AuditActionDataSourceSyncFailed,
"data_source", ds.ID, types.AuditOutcomeFailed,
map[string]any{"name": ds.Name, "type": ds.Type, "sync_log_id": syncLog.ID, "trigger": "manual"})
return nil, err
}
logger.Infof(ctx, "sync task enqueued: ds=%s syncLog=%s", dsID, syncLog.ID)
recordKBActivity(ctx, s.audit, ds.TenantID, ds.KnowledgeBaseID, types.AuditActionDataSourceSyncStarted,
"data_source", ds.ID, types.AuditOutcomeAccepted,
map[string]any{"name": ds.Name, "type": ds.Type, "sync_log_id": syncLog.ID,
"task_id": info.ID, "trigger": "manual", "processing_status": "pending"})
return syncLog, nil
}
@@ -507,6 +534,8 @@ func (s *DataSourceService) PauseDataSource(ctx context.Context, id string) erro
s.scheduler.Remove(id)
logger.Infof(ctx, "data source paused: id=%s", id)
recordKBActivity(ctx, s.audit, ds.TenantID, ds.KnowledgeBaseID, types.AuditActionDataSourcePaused,
"data_source", ds.ID, types.AuditOutcomeSuccess, map[string]any{"name": ds.Name, "type": ds.Type})
return nil
}
@@ -529,6 +558,8 @@ func (s *DataSourceService) ResumeDataSource(ctx context.Context, id string) err
}
logger.Infof(ctx, "data source resumed: id=%s", id)
recordKBActivity(ctx, s.audit, ds.TenantID, ds.KnowledgeBaseID, types.AuditActionDataSourceResumed,
"data_source", ds.ID, types.AuditOutcomeSuccess, map[string]any{"name": ds.Name, "type": ds.Type})
return nil
}
@@ -558,6 +589,9 @@ func (s *DataSourceService) ProcessSync(ctx context.Context, task *asynq.Task) e
logger.Errorf(ctx, "failed to unmarshal sync payload: %v", err)
return err
}
ctx = payload.Initiator.Apply(ctx)
taskID, _ := asynq.GetTaskID(ctx)
ctx = withKBActivityTask(ctx, taskID, payload.Trigger)
logger.Infof(ctx, "processing data source sync: ds=%s syncLog=%s", payload.DataSourceID, payload.SyncLogID)
@@ -708,7 +742,7 @@ func (s *DataSourceService) ProcessSync(ctx context.Context, task *asynq.Task) e
for _, item := range items {
item := item
s.applyFetchedItem(ctx, ds, &item, autoTagIDs, result)
s.applyFetchedItem(withKBActivitySuppressed(ctx), ds, &item, autoTagIDs, result)
}
resultJSON, _ := result.ToJSON()
@@ -879,7 +913,7 @@ func (h *streamSyncHandler) Emit(ctx context.Context, item types.FetchedItem) er
return err
}
h.result.Total++
h.svc.applyFetchedItem(ctx, h.ds, &item, h.tagIDs, h.result)
h.svc.applyFetchedItem(withKBActivitySuppressed(ctx), h.ds, &item, h.tagIDs, h.result)
return nil
}
@@ -1028,6 +1062,21 @@ func (s *DataSourceService) updateSyncRunResult(
if err := s.dsRepo.UpdateSyncState(ctx, ds); err != nil {
logger.Errorf(ctx, "failed to update data source: %v", err)
}
action := types.AuditActionDataSourceSyncCompleted
outcome := types.AuditOutcomeSuccess
if status == types.SyncLogStatusFailed {
action = types.AuditActionDataSourceSyncFailed
outcome = types.AuditOutcomeFailed
} else if status == types.SyncLogStatusPartial {
outcome = types.AuditOutcomePartial
}
recordKBActivity(ctx, s.audit, ds.TenantID, ds.KnowledgeBaseID, action,
"data_source", ds.ID, outcome,
map[string]any{
"name": ds.Name, "type": ds.Type, "sync_log_id": syncLog.ID,
"total": result.Total, "created": result.Created, "updated": result.Updated,
"deleted": result.Deleted, "skipped": result.Skipped, "failed": result.Failed,
})
}
func allFetchedItemsFailedError(result *types.SyncResult) error {