add console api for clear all annotations

pull/22878/head
leslie2046 12 months ago
parent ef51678c73
commit 5bc3293ef4

@ -260,6 +260,18 @@ class AnnotationHitHistoryListApi(Resource):
}
return response
class AnnotationClearAllApi(Resource):
@setup_required
@login_required
@account_initialization_required
def post(self, app_id):
if not current_user.is_editor:
raise Forbidden()
app_id = str(app_id)
AppAnnotationService.clear_all_annotations(app_id)
return {"result": "success"}, 200
api.add_resource(AnnotationReplyActionApi, "/apps/<uuid:app_id>/annotation-reply/<string:action>")
api.add_resource(
@ -273,3 +285,4 @@ api.add_resource(AnnotationBatchImportStatusApi, "/apps/<uuid:app_id>/annotation
api.add_resource(AnnotationHitHistoryListApi, "/apps/<uuid:app_id>/annotations/<uuid:annotation_id>/hit-histories")
api.add_resource(AppAnnotationSettingDetailApi, "/apps/<uuid:app_id>/annotation-setting")
api.add_resource(AppAnnotationSettingUpdateApi, "/apps/<uuid:app_id>/annotation-settings/<uuid:annotation_setting_id>")
api.add_resource(AnnotationClearAllApi, "/apps/<uuid:app_id>/annotations/clear-all")

@ -440,3 +440,52 @@ class AppAnnotationService:
"embedding_model_name": collection_binding_detail.model_name,
},
}
@classmethod
def clear_all_annotations(cls, app_id: str) -> dict:
"""
清空指定应用的所有标注及其相关的命中历史记录
:param app_id: 应用的ID
:return: 返回操作结果
"""
# 获取应用信息
app = (
db.session.query(App)
.filter(App.id == app_id, App.tenant_id == current_user.current_tenant_id, App.status == "normal")
.first()
)
if not app:
raise NotFound("App not found")
# 删除所有标注
annotations = db.session.query(MessageAnnotation).filter(MessageAnnotation.app_id == app_id).all()
for annotation in annotations:
# 删除标注的命中历史记录
annotation_hit_histories = (
db.session.query(AppAnnotationHitHistory)
.filter(AppAnnotationHitHistory.annotation_id == annotation.id)
.all()
)
for annotation_hit_history in annotation_hit_histories:
db.session.delete(annotation_hit_history)
# 删除标注本身
db.session.delete(annotation)
# 提交事务
db.session.commit()
# 如果标注回复功能已启用,清理相关的索引任务
annotation_setting = (
db.session.query(AppAnnotationSetting).filter(AppAnnotationSetting.app_id == app_id).first()
)
if annotation_setting:
for annotation in annotations:
delete_annotation_index_task.delay(
annotation.id, app_id, current_user.current_tenant_id, annotation_setting.collection_binding_id
)
return {"result": "success"}

@ -77,6 +77,18 @@ const HeaderOptions: FC<Props> = ({
const [showBulkImportModal, setShowBulkImportModal] = useState(false)
const handleClearAll = async () => {
await confirm({
title: t('appAnnotation.table.header.clearAllConfirm'),
type: 'danger',
})
try {
await clearAllAnnotations(appId)
onAdded()
} catch (e) {
console.error(e)
}
}
const Operations = () => {
return (
<div className="w-full py-1">
@ -125,6 +137,15 @@ const HeaderOptions: FC<Props> = ({
</MenuItems>
</Transition>
</Menu>
<button
onClick={handleClearAll}
className='h-9 py-2 px-3 mx-1 flex items-center space-x-2 hover:bg-red-50 rounded-lg cursor-pointer disabled:opacity-50 w-[calc(100%_-_8px)] text-red-600'
>
<Trash03 className='w-4 h-4'/>
<span className='grow system-sm-regular text-left'>
{t('appAnnotation.table.header.clearAll')}
</span>
</button>
</div>
)
}

@ -17,6 +17,7 @@ const translation = {
bulkImport: 'Bulk Import',
bulkExport: 'Bulk Export',
clearAll: 'Clear All Annotation',
clearAllConfirm: 'Are you sure you want to delete all annotations? This action cannot be undone.'
},
},
editModal: {

@ -19,6 +19,7 @@ const translation = {
bulkImport: '批量导入',
bulkExport: '批量导出',
clearAll: '删除所有标注',
clearAllConfirm: '您是否确定要删除所有标注?这个动作不可撤回!',
},
},
editModal: {

Loading…
Cancel
Save