mirror of
https://github.com/Rain-kl/OpenFlare.git
synced 2026-10-11 09:46:37 +08:00
[优化] 添加清理历史快照功能
This commit is contained in:
@@ -189,3 +189,42 @@ func ActivateConfigVersion(c *gin.Context) {
|
|||||||
"data": version,
|
"data": version,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type CleanupConfigVersionRequest struct {
|
||||||
|
KeepCount int `json:"keep_count" binding:"required,min=3"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// CleanupConfigVersions godoc
|
||||||
|
// @Summary Cleanup old config versions
|
||||||
|
// @Tags ConfigVersions
|
||||||
|
// @Produce json
|
||||||
|
// @Security BearerAuth
|
||||||
|
// @Param request body CleanupConfigVersionRequest true "Cleanup request"
|
||||||
|
// @Success 200 {object} map[string]interface{}
|
||||||
|
// @Failure 400 {object} map[string]interface{}
|
||||||
|
// @Router /api/config-versions/cleanup [post]
|
||||||
|
func CleanupConfigVersions(c *gin.Context) {
|
||||||
|
var req CleanupConfigVersionRequest
|
||||||
|
if err := c.ShouldBindJSON(&req); err != nil {
|
||||||
|
c.JSON(http.StatusOK, gin.H{
|
||||||
|
"success": false,
|
||||||
|
"message": "参数错误",
|
||||||
|
})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
deletedCount, err := service.CleanupConfigVersions(req.KeepCount)
|
||||||
|
if err != nil {
|
||||||
|
c.JSON(http.StatusOK, gin.H{
|
||||||
|
"success": false,
|
||||||
|
"message": err.Error(),
|
||||||
|
})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
c.JSON(http.StatusOK, gin.H{
|
||||||
|
"success": true,
|
||||||
|
"message": "清理成功",
|
||||||
|
"data": map[string]interface{}{"deleted_count": deletedCount},
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|||||||
@@ -149,6 +149,7 @@ func SetApiRouter(router *gin.Engine) {
|
|||||||
configVersionRoute.GET("/:id", controller.GetConfigVersion)
|
configVersionRoute.GET("/:id", controller.GetConfigVersion)
|
||||||
configVersionRoute.POST("/publish", controller.PublishConfigVersion)
|
configVersionRoute.POST("/publish", controller.PublishConfigVersion)
|
||||||
configVersionRoute.POST("/:id/activate", controller.ActivateConfigVersion)
|
configVersionRoute.POST("/:id/activate", controller.ActivateConfigVersion)
|
||||||
|
configVersionRoute.POST("/cleanup", controller.CleanupConfigVersions)
|
||||||
}
|
}
|
||||||
dashboardRoute := apiRouter.Group("/dashboard")
|
dashboardRoute := apiRouter.Group("/dashboard")
|
||||||
dashboardRoute.Use(middleware.AdminAuth())
|
dashboardRoute.Use(middleware.AdminAuth())
|
||||||
|
|||||||
@@ -406,6 +406,34 @@ func ActivateConfigVersion(id uint) (*model.ConfigVersion, error) {
|
|||||||
return version, nil
|
return version, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func CleanupConfigVersions(keepCount int) (int64, error) {
|
||||||
|
if keepCount < 3 {
|
||||||
|
keepCount = 3
|
||||||
|
}
|
||||||
|
var versions []model.ConfigVersion
|
||||||
|
if err := model.DB.Select("id", "is_active").Order("id desc").Find(&versions).Error; err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
if len(versions) <= keepCount {
|
||||||
|
return 0, nil
|
||||||
|
}
|
||||||
|
var deleteIDs []uint
|
||||||
|
for i, v := range versions {
|
||||||
|
if i < keepCount {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if v.IsActive {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
deleteIDs = append(deleteIDs, v.ID)
|
||||||
|
}
|
||||||
|
if len(deleteIDs) == 0 {
|
||||||
|
return 0, nil
|
||||||
|
}
|
||||||
|
result := model.DB.Where("id IN ?", deleteIDs).Delete(&model.ConfigVersion{})
|
||||||
|
return result.RowsAffected, result.Error
|
||||||
|
}
|
||||||
|
|
||||||
func buildCurrentConfigBundle(requireRoutes bool) (*configBundle, error) {
|
func buildCurrentConfigBundle(requireRoutes bool) (*configBundle, error) {
|
||||||
routes, err := model.GetEnabledProxyRoutes()
|
routes, err := model.GetEnabledProxyRoutes()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
@@ -38,3 +38,10 @@ export function activateConfigVersion(id: number) {
|
|||||||
method: 'POST',
|
method: 'POST',
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function cleanupConfigVersions(payload: { keep_count: number }) {
|
||||||
|
return apiRequest<{ deleted_count: number }>('/config-versions/cleanup', {
|
||||||
|
method: 'POST',
|
||||||
|
body: JSON.stringify(payload),
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|||||||
+79
@@ -0,0 +1,79 @@
|
|||||||
|
'use client';
|
||||||
|
|
||||||
|
import { useState } from 'react';
|
||||||
|
|
||||||
|
import { InlineMessage } from '@/components/feedback/inline-message';
|
||||||
|
import { AppModal } from '@/components/ui/app-modal';
|
||||||
|
import {
|
||||||
|
PrimaryButton,
|
||||||
|
SecondaryButton,
|
||||||
|
} from '@/features/shared/components/resource-primitives';
|
||||||
|
|
||||||
|
export function CleanupConfigVersionsModal({
|
||||||
|
isOpen,
|
||||||
|
onClose,
|
||||||
|
onConfirm,
|
||||||
|
isPending,
|
||||||
|
}: {
|
||||||
|
isOpen: boolean;
|
||||||
|
onClose: () => void;
|
||||||
|
onConfirm: (keepCount: number) => void;
|
||||||
|
isPending: boolean;
|
||||||
|
}) {
|
||||||
|
const [keepCount, setKeepCount] = useState<number>(10);
|
||||||
|
const [error, setError] = useState<string | null>(null);
|
||||||
|
|
||||||
|
const handleSubmit = (e: React.FormEvent) => {
|
||||||
|
e.preventDefault();
|
||||||
|
if (keepCount < 3) {
|
||||||
|
setError('最少保留 3 个历史快照');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
setError(null);
|
||||||
|
onConfirm(keepCount);
|
||||||
|
};
|
||||||
|
|
||||||
|
return (
|
||||||
|
<AppModal
|
||||||
|
isOpen={isOpen}
|
||||||
|
onClose={onClose}
|
||||||
|
title="清理历史快照"
|
||||||
|
description="删除旧的历史快照配置,系统将始终自动保护当前已激活的版本不被删除。"
|
||||||
|
size="md"
|
||||||
|
footer={
|
||||||
|
<div className="flex justify-end gap-3">
|
||||||
|
<SecondaryButton type="button" onClick={onClose} disabled={isPending}>
|
||||||
|
取消
|
||||||
|
</SecondaryButton>
|
||||||
|
<PrimaryButton type="submit" form="cleanup-form" disabled={isPending} className="bg-[var(--status-danger-foreground)] hover:bg-[var(--status-danger-foreground)] hover:opacity-90 border-[var(--status-danger-foreground)]">
|
||||||
|
{isPending ? '清理中...' : '确认清理'}
|
||||||
|
</PrimaryButton>
|
||||||
|
</div>
|
||||||
|
}
|
||||||
|
>
|
||||||
|
<form id="cleanup-form" onSubmit={handleSubmit} className="space-y-4">
|
||||||
|
{error && <InlineMessage tone="danger" message={error} />}
|
||||||
|
<div>
|
||||||
|
<label
|
||||||
|
htmlFor="keepCount"
|
||||||
|
className="mb-1 block text-sm font-medium text-[var(--foreground-primary)]"
|
||||||
|
>
|
||||||
|
保留最近快照个数
|
||||||
|
</label>
|
||||||
|
<input
|
||||||
|
id="keepCount"
|
||||||
|
type="number"
|
||||||
|
min={3}
|
||||||
|
value={keepCount}
|
||||||
|
onChange={(e) => setKeepCount(parseInt(e.target.value, 10) || 3)}
|
||||||
|
className="w-full rounded-lg border border-[var(--border-default)] bg-[var(--surface-default)] px-3 py-2 text-sm text-[var(--foreground-primary)] focus:border-[var(--brand-primary)] focus:ring-1 focus:ring-[var(--brand-primary)] focus:outline-none"
|
||||||
|
disabled={isPending}
|
||||||
|
/>
|
||||||
|
<p className="mt-1 text-xs text-[var(--foreground-secondary)]">
|
||||||
|
默认为 10 个,最少需保留 3 个。
|
||||||
|
</p>
|
||||||
|
</div>
|
||||||
|
</form>
|
||||||
|
</AppModal>
|
||||||
|
);
|
||||||
|
}
|
||||||
@@ -17,8 +17,10 @@ import {
|
|||||||
getConfigVersionPreview,
|
getConfigVersionPreview,
|
||||||
getConfigVersions,
|
getConfigVersions,
|
||||||
publishConfigVersion,
|
publishConfigVersion,
|
||||||
|
cleanupConfigVersions,
|
||||||
} from '@/features/config-versions/api/config-versions';
|
} from '@/features/config-versions/api/config-versions';
|
||||||
import { ConfigVersionSnapshotModal } from '@/features/config-versions/components/config-version-snapshot-modal';
|
import { ConfigVersionSnapshotModal } from '@/features/config-versions/components/config-version-snapshot-modal';
|
||||||
|
import { CleanupConfigVersionsModal } from '@/features/config-versions/components/cleanup-config-versions-modal';
|
||||||
import type {
|
import type {
|
||||||
ConfigOptionDiffItem,
|
ConfigOptionDiffItem,
|
||||||
ConfigDiffResult,
|
ConfigDiffResult,
|
||||||
@@ -404,6 +406,7 @@ export function ConfigVersionsPage() {
|
|||||||
diff: ConfigDiffResult;
|
diff: ConfigDiffResult;
|
||||||
} | null>(null);
|
} | null>(null);
|
||||||
const [isPreviewLoading, setIsPreviewLoading] = useState(false);
|
const [isPreviewLoading, setIsPreviewLoading] = useState(false);
|
||||||
|
const [isCleanupModalOpen, setIsCleanupModalOpen] = useState(false);
|
||||||
|
|
||||||
const versionsQuery = useQuery({
|
const versionsQuery = useQuery({
|
||||||
queryKey: versionsQueryKey,
|
queryKey: versionsQueryKey,
|
||||||
@@ -462,6 +465,21 @@ export function ConfigVersionsPage() {
|
|||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
|
const cleanupMutation = useMutation({
|
||||||
|
mutationFn: cleanupConfigVersions,
|
||||||
|
onSuccess: async (data) => {
|
||||||
|
setFeedback({
|
||||||
|
tone: 'success',
|
||||||
|
message: `成功清理 ${data.deleted_count} 个历史快照`,
|
||||||
|
});
|
||||||
|
setIsCleanupModalOpen(false);
|
||||||
|
await queryClient.invalidateQueries({ queryKey: versionsQueryKey });
|
||||||
|
},
|
||||||
|
onError: (error) => {
|
||||||
|
setFeedback({ tone: 'danger', message: getErrorMessage(error) });
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
const handleOpenPublishPreview = async () => {
|
const handleOpenPublishPreview = async () => {
|
||||||
setFeedback(null);
|
setFeedback(null);
|
||||||
setIsPreviewLoading(true);
|
setIsPreviewLoading(true);
|
||||||
@@ -535,16 +553,24 @@ export function ConfigVersionsPage() {
|
|||||||
title="历史版本"
|
title="历史版本"
|
||||||
description="发布成功后会立即刷新列表,不再需要手动刷新页面。"
|
description="发布成功后会立即刷新列表,不再需要手动刷新页面。"
|
||||||
action={
|
action={
|
||||||
<SecondaryButton
|
<div className="flex gap-2">
|
||||||
type="button"
|
<SecondaryButton
|
||||||
onClick={() =>
|
type="button"
|
||||||
void queryClient.invalidateQueries({
|
onClick={() => setIsCleanupModalOpen(true)}
|
||||||
queryKey: versionsQueryKey,
|
>
|
||||||
})
|
清理旧版本
|
||||||
}
|
</SecondaryButton>
|
||||||
>
|
<SecondaryButton
|
||||||
刷新列表
|
type="button"
|
||||||
</SecondaryButton>
|
onClick={() =>
|
||||||
|
void queryClient.invalidateQueries({
|
||||||
|
queryKey: versionsQueryKey,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
>
|
||||||
|
刷新列表
|
||||||
|
</SecondaryButton>
|
||||||
|
</div>
|
||||||
}
|
}
|
||||||
>
|
>
|
||||||
{versionsQuery.isLoading ? (
|
{versionsQuery.isLoading ? (
|
||||||
@@ -630,6 +656,13 @@ export function ConfigVersionsPage() {
|
|||||||
version={selectedVersion}
|
version={selectedVersion}
|
||||||
onClose={() => setSelectedVersionId(null)}
|
onClose={() => setSelectedVersionId(null)}
|
||||||
/>
|
/>
|
||||||
|
|
||||||
|
<CleanupConfigVersionsModal
|
||||||
|
isOpen={isCleanupModalOpen}
|
||||||
|
onClose={() => setIsCleanupModalOpen(false)}
|
||||||
|
onConfirm={(keepCount) => cleanupMutation.mutate({ keep_count: keepCount })}
|
||||||
|
isPending={cleanupMutation.isPending}
|
||||||
|
/>
|
||||||
</>
|
</>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user