feat(admin): 增加图片清理队列查询

This commit is contained in:
dongjf 2026-11-30 13:22:43 +08:00
parent ee9aa707a7
commit a110c7a562
7 changed files with 87 additions and 6 deletions

View File

@ -15,3 +15,5 @@ docker build -t item-atlas-admin:dev .
列表使用 `GET /api/admin/households?q=&limit=25&offset=0`,按名称及 ID 排序。名称按字面包含匹配,查询长度最多 100,页大小为 1 至 100。响应中的 `hasMore` 决定是否提供下一页。查询、详情和删除沿用 `atlas-admin` 客户端与 `atlas-space-admin` 角色鉴权。部署后台镜像前先部署包含列表接口的物品服务,然后在内部入口用管理员与普通账号分别检查权限。
「删除记录」显示管理员删除空间的时间、空间 ID 和操作人 ID。列表从 `GET /api/admin/space-deletions?limit=25` 读取,可以按 `householdId` 精确查。下一页传入响应中的 `nextBefore`,排序以删除时间和记录 ID 降序为准。读取同样需要管理员权限,响应禁止缓存。操作人与空间 ID 来自删除事务落库的审计记录。记录的查询不需要关联已删除的空间。
「图片清理」读取 `GET /api/admin/media-cleanup`。它显示已到执行时间的任务、延后的任务、曾重试的任务及最早应执行时间。曾重试可同时属于前两组之一。上传结束前暂存的清理预留也计入队列,成功登记图片后会移除。服务端用一条 PostgreSQL 汇总查询读取现场状态,仅返回计数和时间。接口沿用管理员鉴权与 `Cache-Control: no-store`。如果环境没有配置图片存储,页面会提示清理任务保留在队列中。 [常用排错入口](../../docs/deployment/identity-and-media.md)

View File

@ -8,11 +8,13 @@ interface Space extends SpaceSummary { items: number; photos: number }
interface SpacePage { spaces: SpaceSummary[]; hasMore: boolean }
interface SpaceDeletion { id: string; householdId: string; operatorId: string; deletedAt: string }
interface DeletionPage { records: SpaceDeletion[]; nextBefore: string | null }
type AdminView = 'spaces' | 'audit'
interface MediaCleanupStatus { workerEnabled: boolean; due: number; scheduled: number; retrying: number; oldestDueAt: string | null }
type AdminView = 'spaces' | 'audit' | 'media'
const pageSize = 25
const spacesApi = '/api/admin/households'
const auditApi = '/api/admin/space-deletions'
const cleanupApi = '/api/admin/media-cleanup'
const identityUrl = import.meta.env.VITE_OIDC_URL || 'https://api-atlas.tutudog.com/auth'
const keycloak = new Keycloak({
@ -39,6 +41,7 @@ const auditInput = ref('')
const auditHousehold = ref('')
const auditPage = ref<DeletionPage>()
const auditCursors = ref<(string | null)[]>([null])
const cleanup = ref<MediaCleanupStatus>()
const isId = (value: string) => /^[\da-f]{8}(-[\da-f]{4}){3}-[\da-f]{12}$/i.test(value)
const pageNumber = computed(() => pageOffset.value / pageSize + 1)
const auditDate = new Intl.DateTimeFormat('zh-CN', { dateStyle: 'medium', timeStyle: 'medium' })
@ -61,6 +64,7 @@ function clearManagementData() {
page.value = undefined
space.value = undefined
auditPage.value = undefined
cleanup.value = undefined
confirming.value = false
confirmation.value = success.value = ''
}
@ -95,6 +99,21 @@ function selectView(next: AdminView) {
view.value = next
success.value = error.value = ''
if (next === 'audit') void loadAudit([null], auditHousehold.value)
if (next === 'media') void loadMediaCleanup()
}
async function loadMediaCleanup() {
if (busy.value) return
busy.value = true
error.value = ''
cleanup.value = undefined
try {
cleanup.value = await (await request(cleanupApi)).json()
} catch (failure) {
error.value = failure instanceof Error ? failure.message : '图片清理状态加载失败。'
} finally {
busy.value = false
}
}
function searchAudit() {
@ -208,6 +227,7 @@ async function deleteSpace() {
<nav class="views" aria-label="后台功能">
<el-button :type="view === 'spaces' ? 'primary' : 'default'" :aria-pressed="view === 'spaces'" :disabled="busy" @click="selectView('spaces')">空间管理</el-button>
<el-button :type="view === 'audit' ? 'primary' : 'default'" :aria-pressed="view === 'audit'" :disabled="busy" @click="selectView('audit')">删除记录</el-button>
<el-button :type="view === 'media' ? 'primary' : 'default'" :aria-pressed="view === 'media'" :disabled="busy" @click="selectView('media')">图片清理</el-button>
</nav>
</template>
<template v-if="authorized && view === 'spaces'">
@ -277,6 +297,26 @@ async function deleteSpace() {
</nav>
</template>
</section>
<section v-if="authorized && view === 'media'" v-loading="busy" aria-label="图片清理">
<el-card shadow="never">
<header class="section-head">
<h2>图片清理</h2>
<el-button :disabled="busy" @click="loadMediaCleanup">刷新</el-button>
</header>
<template v-if="cleanup">
<el-alert v-if="!cleanup.workerEnabled" title="当前环境未配置图片存储,清理任务会保留在队列里。"
type="warning" show-icon :closable="false" />
<el-descriptions :column="2" border>
<el-descriptions-item label="待执行">{{ cleanup.due }}</el-descriptions-item>
<el-descriptions-item label="未到执行时间">{{ cleanup.scheduled }}</el-descriptions-item>
<el-descriptions-item label="曾重试">{{ cleanup.retrying }}</el-descriptions-item>
<el-descriptions-item label="最早待执行">
{{ cleanup.oldestDueAt ? auditDate.format(new Date(cleanup.oldestDueAt)) : '—' }}
</el-descriptions-item>
</el-descriptions>
</template>
</el-card>
</section>
</main>
</el-config-provider>
</template>
@ -285,6 +325,8 @@ async function deleteSpace() {
body { margin: 0; background: #f4f6fa; font-family: system-ui, sans-serif; color: #222c41; }
main { max-width: 920px; margin: 0 auto; padding: 26px 20px; }
header { display: flex; align-items: center; justify-content: space-between; margin-bottom: 24px; }
.section-head { margin-bottom: 16px; }
.section-head h2 { margin: 0; font-size: 18px; }
h1 { margin: 0; font-size: 26px; }
h1 small { color: #596476; font-size: 15px; font-weight: 500; margin-left: 8px; }
h2 { margin-top: 0; }

View File

@ -0,0 +1,18 @@
package com.tutudog.itematlas.inventory;
import org.springframework.http.CacheControl;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
class MediaAdminController {
private final PhotoService photos;
MediaAdminController(PhotoService photos) { this.photos = photos; }
@GetMapping("/api/admin/media-cleanup")
ResponseEntity<PhotoService.CleanupStatus> cleanupStatus() {
return ResponseEntity.ok().cacheControl(CacheControl.noStore()).body(photos.cleanupStatus());
}
}

View File

@ -4,6 +4,8 @@ import static org.springframework.http.HttpStatus.*;
import io.micrometer.core.instrument.MeterRegistry;
import java.sql.Types;
import java.time.Instant;
import java.time.OffsetDateTime;
import java.util.List;
import java.util.Optional;
import java.util.UUID;
@ -41,6 +43,7 @@ class PhotoService {
}
record Photo(UUID id) {}
record CleanupStatus(boolean workerEnabled, long due, long scheduled, long retrying, Instant oldestDueAt) {}
private record Cleanup(String prefix, int attempts) {}
enum Target {
@ -155,6 +158,20 @@ class PhotoService {
.param("house", household).param("entity", entity).param("id", id).update();
}
CleanupStatus cleanupStatus() {
return db.sql("""
SELECT count(*) FILTER (WHERE due_at <= now()) AS due,
count(*) FILTER (WHERE due_at > now()) AS scheduled,
count(*) FILTER (WHERE attempts > 0) AS retrying,
min(due_at) FILTER (WHERE due_at <= now()) AS oldest_due
FROM media_cleanup
""").query((row, index) -> {
var oldest = row.getObject("oldest_due", OffsetDateTime.class);
return new CleanupStatus(storage != null, row.getLong("due"), row.getLong("scheduled"),
row.getLong("retrying"), oldest == null ? null : oldest.toInstant());
}).single();
}
@Scheduled(fixedDelay = 2000)
void processCleanup() {
if (storage == null) { return; }

View File

@ -26,12 +26,12 @@ namespace=item-atlas-admin-preview
secret_dir=/path/on/ssd/item-atlas-admin-preview
mvn -B -ntp -f backend/pom.xml -pl inventory-service -am package
docker build -t item-atlas-inventory-service:admin-preview-0.1.2 backend/inventory-service
docker build -t item-atlas-inventory-service:admin-preview-0.1.3 backend/inventory-service
docker build --build-arg VITE_OIDC_URL=/auth \
--build-arg VITE_OIDC_REALM=item-atlas-admin-preview \
-t item-atlas-admin:admin-preview-0.1.2 apps/admin
-t item-atlas-admin:admin-preview-0.1.3 apps/admin
kind load docker-image --name "$kind_cluster" \
item-atlas-inventory-service:admin-preview-0.1.2 item-atlas-admin:admin-preview-0.1.2
item-atlas-inventory-service:admin-preview-0.1.3 item-atlas-admin:admin-preview-0.1.3
python3 deploy/admin-preview/prepare-secrets.py "$secret_dir"
kubectl --context "$context" create namespace "$namespace"
@ -54,6 +54,8 @@ kubectl --context "$context" -n "$namespace" port-forward --address 127.0.0.1 sv
打开 [管理后台](http://127.0.0.1:18482/)。管理员可以分页查空间、按名称或完整 ID 定位、查看物品和照片数量。普通账号登录后会看到权限提示。删除请使用专用测试空间,提交后打开「删除记录」,按空间 ID 核对操作人和时间。对应数据保存在 `space_admin_action`。照片记录被删除时,数据库触发器会在同一个事务登记 `media_cleanup`。本预览用于核对队列写入,真实对象清理由 [对象存储冒烟脚本](../../backend/tests/media_smoke.py) 在带存储的环境验收。
打开「图片清理」检查队列统计。本机预览会标明没有配置图片存储。真实存储环境持续出现待执行或重试时,先核对最早待执行时间,再从物品服务日志查清理重试及对象存储请求耗时。更新此模块时,先交付支持该接口的物品服务,再更新 Web 镜像。
页面打不开先查 `/healthz` 和端口转发。浏览器返回登录页时看 Keycloak 回调地址和 Nginx `/auth` 路由。管理 API 返回 `401` 时核对 issuer、JWKS 和有效期,返回 `403` 时核对客户端和管理员角色。物品服务启动时 Flyway 会等待数据库,持续失败时查看 PVC、PostgreSQL 日志及 Flyway 日志。后台 Nginx 的访问日志保留路径、状态码与响应耗时,省略包含授权码的查询参数。
更新后台 Pod 后端口转发会断开,重新运行 `kubectl port-forward` 即可。停止转发后入口关闭,预览数据库保留在 PVC 中。 [端口转发](https://kubernetes.io/docs/reference/kubectl/generated/kubectl_port-forward/) · [Keycloak hostname](https://www.keycloak.org/server/hostname) · [Spring Boot Flyway 配置](https://docs.spring.io/spring-boot/4.0/appendix/application-properties/#application-properties.data-migration)

View File

@ -23,7 +23,7 @@ spec:
automountServiceAccountToken: false
containers:
- name: inventory
image: item-atlas-inventory-service:admin-preview-0.1.2
image: item-atlas-inventory-service:admin-preview-0.1.3
imagePullPolicy: Never
envFrom:
- secretRef: {name: atlas-admin-inventory}

View File

@ -42,7 +42,7 @@ spec:
automountServiceAccountToken: false
containers:
- name: nginx
image: item-atlas-admin:admin-preview-0.1.2
image: item-atlas-admin:admin-preview-0.1.3
imagePullPolicy: Never
env:
- {name: ATLAS_GATEWAY_URL, value: 'http://atlas-admin-gateway:8080'}