|
26 | 26 | KnowledgeSyncStatus, |
27 | 27 | KnowledgeSyncType, |
28 | 28 | KnowledgeType, |
29 | | - Paragraph, |
30 | 29 | ) |
31 | 30 | from knowledge.models.knowledge_action import KnowledgeAction, State |
| 31 | +from knowledge.services.workflow_sync import merge_workflow_incremental_snapshot |
32 | 32 | from rest_framework import serializers |
33 | 33 | from rest_framework.exceptions import ErrorDetail, ValidationError |
34 | 34 | from tools.models import ToolRecord |
@@ -156,77 +156,42 @@ def handler(self, workflow): |
156 | 156 | if self.sync_log_id is not None: |
157 | 157 | sync_log = QuerySet(KnowledgeSyncLog).filter(id=self.sync_log_id).first() |
158 | 158 | if sync_log is not None: |
159 | | - new_documents = list( |
160 | | - QuerySet(Document).filter( |
161 | | - knowledge_id=sync_log.knowledge_id, |
162 | | - type=KnowledgeType.WORKFLOW, |
163 | | - resource_type=DocumentResourceType.DOCUMENT, |
164 | | - create_time__gte=sync_log.create_time, |
165 | | - ) |
166 | | - ) |
167 | | - synced_count = len(new_documents) |
168 | | - skipped_count = 0 |
169 | 159 | if ( |
170 | 160 | state == State.SUCCESS |
171 | 161 | and sync_log.sync_type == KnowledgeSyncType.INCREMENTAL |
172 | 162 | and self.document_cleanup is not None |
173 | 163 | ): |
174 | | - old_documents = list( |
175 | | - QuerySet(Document).filter( |
| 164 | + stats = merge_workflow_incremental_snapshot(sync_log) |
| 165 | + else: |
| 166 | + stats = { |
| 167 | + "total_count": QuerySet(Document) |
| 168 | + .filter( |
176 | 169 | knowledge_id=sync_log.knowledge_id, |
177 | | - type=KnowledgeType.WORKFLOW, |
178 | 170 | resource_type=DocumentResourceType.DOCUMENT, |
179 | | - create_time__lt=sync_log.create_time, |
180 | 171 | ) |
181 | | - ) |
182 | | - old_by_name = {} |
183 | | - for document in old_documents: |
184 | | - old_by_name.setdefault(document.name, []).append(document) |
185 | | - for new_document in new_documents: |
186 | | - matched = old_by_name.get(new_document.name, []) |
187 | | - if not matched: |
188 | | - continue |
189 | | - new_content = list( |
190 | | - QuerySet(Paragraph) |
191 | | - .filter(document_id=new_document.id) |
192 | | - .order_by("position") |
193 | | - .values_list("title", "content") |
194 | | - ) |
195 | | - unchanged = next( |
196 | | - ( |
197 | | - old_document |
198 | | - for old_document in matched |
199 | | - if list( |
200 | | - QuerySet(Paragraph) |
201 | | - .filter(document_id=old_document.id) |
202 | | - .order_by("position") |
203 | | - .values_list("title", "content") |
204 | | - ) |
205 | | - == new_content |
206 | | - ), |
207 | | - None, |
| 172 | + .count(), |
| 173 | + "synced_count": QuerySet(Document) |
| 174 | + .filter( |
| 175 | + knowledge_id=sync_log.knowledge_id, |
| 176 | + type=KnowledgeType.WORKFLOW, |
| 177 | + resource_type=DocumentResourceType.DOCUMENT, |
| 178 | + create_time__gte=sync_log.create_time, |
208 | 179 | ) |
209 | | - if unchanged is not None: |
210 | | - self.document_cleanup([new_document.id]) |
211 | | - synced_count -= 1 |
212 | | - skipped_count += 1 |
213 | | - else: |
214 | | - self.document_cleanup([document.id for document in matched]) |
215 | | - total_count = ( |
216 | | - QuerySet(Document) |
217 | | - .filter( |
218 | | - knowledge_id=sync_log.knowledge_id, |
219 | | - resource_type=DocumentResourceType.DOCUMENT, |
220 | | - ) |
221 | | - .count() |
222 | | - ) |
| 180 | + .count(), |
| 181 | + "skipped_count": 0, |
| 182 | + "deleted_count": sync_log.deleted_count, |
| 183 | + "failed_count": 0 if state == State.SUCCESS else 1, |
| 184 | + } |
223 | 185 | is_success = state == State.SUCCESS |
224 | 186 | QuerySet(KnowledgeSyncLog).filter(id=sync_log.id).update( |
225 | | - status=KnowledgeSyncStatus.SUCCESS if is_success else KnowledgeSyncStatus.FAILURE, |
226 | | - total_count=total_count, |
227 | | - synced_count=synced_count, |
228 | | - skipped_count=skipped_count, |
229 | | - failed_count=0 if is_success else 1, |
| 187 | + status=KnowledgeSyncStatus.SUCCESS |
| 188 | + if is_success and not stats["failed_count"] |
| 189 | + else KnowledgeSyncStatus.FAILURE, |
| 190 | + total_count=stats["total_count"], |
| 191 | + synced_count=stats["synced_count"], |
| 192 | + skipped_count=stats["skipped_count"], |
| 193 | + deleted_count=stats["deleted_count"], |
| 194 | + failed_count=stats["failed_count"], |
230 | 195 | duration_ms=max(0, round(run_time * 1000)), |
231 | 196 | message=f"Workflow action {self.knowledge_action_id}: {state}", |
232 | 197 | ) |
|
0 commit comments