过滤无价值日志并实现最近24小时倒序分页查询
Security Checks / security (push) Waiting to run

This commit is contained in:
xuans committed 2026-09-09 01:06:45 +08:00
1 parent 161bc3a345
commit bb4ac4d5f5
20 files changed
+765 -132

No files matched your search

@@ -92,29 +92,15 @@
max-width: 190px;
}
.log-filters .el-select {
flex: 1;
min-width: 0;
}
.log-filters-runtime {
flex-wrap: nowrap;
}
.log-filters :deep(.el-select__wrapper),
.log-filters :deep(.el-button) {
border-radius: 8px;
}
.log-file-option {
.log-filter-actions {
display: flex;
align-items: center;
justify-content: space-between;
gap: 12px;
flex-shrink: 0;
margin-left: auto;
}
.log-file-option small {
color: var(--el-text-color-secondary);
.log-filters :deep(.el-button) {
border-radius: 8px;
}
.log-content {
@@ -1,25 +1,28 @@
import { computed, onBeforeUnmount, onMounted, ref, watch } from 'vue'
import { http } from '@/utils/axios'
import { formatFileSize, parseRuntimeLine, summarizeRuntime, timeParts } from './RuntimeLog.js'
import { parseRuntimeLine, summarizeRuntime, timeParts } from './RuntimeLog.js'
export function useLogs() {
const category = ref('config')
const loading = ref(false)
const error = ref('')
const items = ref([])
const files = ref([])
const selectedFile = ref('')
const lines = ref([])
const offset = ref(0)
const hasMore = ref(false)
const actorId = ref('')
const dateRange = ref([])
const recentRange = () => {
const end = new Date()
return [new Date(end.getTime() - 24 * 60 * 60 * 1000), end]
}
const dateRange = ref(recentRange())
const detail = ref(null)
const runtimeDetail = ref(null)
const pageSize = 50
const pageSize = 10
let requestId = 0
let aborter
const previousOffsets = []
let appliedFilters = {}
const categories = [
{ label: '配置审计', value: 'config' },
@@ -36,7 +39,6 @@ export function useLogs() {
const changes = computed(() => details.value.changes || [])
const runtimeRows = computed(() => lines.value.map((line, index) => parseRuntimeLine(line, offset.value + index + 1)))
const runtimeSummary = computed(() => summarizeRuntime(runtimeRows.value))
const currentFile = computed(() => files.value.find(file => file.name === selectedFile.value))
const visibleCount = computed(() => isRuntime.value ? lines.value.length : items.value.length)
const pageRange = computed(() => visibleCount.value ? `第 ${offset.value + 1}–${offset.value + visibleCount.value} 条` : '暂无记录')
const currentCategory = computed(() => categories.find(item => item.value === category.value)?.label)
@@ -55,32 +57,7 @@ export function useLogs() {
loading.value = true
error.value = ''
try {
const params = { kind: category.value, offset: offset.value, limit: pageSize }
if (isRuntime.value) {
const listing = requireData(await http.get('/api/v1/logs', {
params: { kind: 'runtime' }, signal: aborter.signal
}))
if (id !== requestId) return
files.value = listing.files || []
if (!files.value.some(file => file.name === selectedFile.value)) {
selectedFile.value = files.value[0]?.name || ''
offset.value = 0
params.offset = 0
previousOffsets.length = 0
}
if (!selectedFile.value) {
lines.value = []
hasMore.value = false
return
}
params.file = selectedFile.value
} else {
if (actorId.value.trim()) params.actorId = actorId.value.trim()
if (dateRange.value?.length === 2) {
params.from = new Date(dateRange.value[0]).toISOString()
params.to = new Date(dateRange.value[1]).toISOString()
}
}
const params = { ...appliedFilters, offset: offset.value, limit: pageSize }
const data = requireData(await http.get('/api/v1/logs', { params, signal: aborter.signal }))
if (id !== requestId) return
items.value = data.items || []
@@ -99,10 +76,31 @@ export function useLogs() {
}
function search() {
// 只在查询时采纳筛选条件,翻页沿用快照,不随当前时间或未提交输入漂移。
if (!dateRange.value?.length) dateRange.value = recentRange()
const [from, to] = (dateRange.value || []).map(value => new Date(value))
if (!from || !to || !Number.isFinite(from.getTime()) || !Number.isFinite(to.getTime()) || from > to) {
++requestId
aborter?.abort()
loading.value = false
error.value = '请选择有效的起止时间'
items.value = []
lines.value = []
hasMore.value = false
return
}
appliedFilters = { kind: category.value, from: from.toISOString(), to: to.toISOString() }
if (!isRuntime.value && actorId.value.trim()) appliedFilters.actorId = actorId.value.trim()
offset.value = 0
previousOffsets.length = 0
detail.value = null
runtimeDetail.value = null
return load()
}
function recent() {
dateRange.value = recentRange()
return search()
}
function next() {
if (loading.value || !hasMore.value) return
previousOffsets.push(offset.value)
@@ -119,17 +117,17 @@ export function useLogs() {
runtimeDetail.value = null
search()
})
onMounted(load)
onMounted(search)
onBeforeUnmount(() => {
++requestId
aborter?.abort()
})
return {
category, categories, loading, error, items, files, selectedFile, lines, offset,
category, categories, loading, error, items, lines, offset,
hasMore, actorId, dateRange, detail, details, changes, isRuntime,
runtimeDetail, runtimeRows, runtimeSummary, currentFile, visibleCount, pageRange,
currentCategory, resultType, runtimeRowClass, timeParts, formatFileSize,
formatTime, resultLabel, actionLabel, displayActor, search, next, previous
runtimeDetail, runtimeRows, runtimeSummary, visibleCount, pageRange,
currentCategory, resultType, runtimeRowClass, timeParts,
formatTime, resultLabel, actionLabel, displayActor, search, recent, next, previous
}
}
@@ -2,51 +2,34 @@
<section class="log-settings">
<header class="log-heading">
<h2>日志记录</h2>
<span class="log-retention">最近 30 天</span>
<span class="log-retention">保留 30 天</span>
</header>
<el-radio-group v-model="category" class="log-categories" aria-label="日志分类">
<el-radio-button v-for="item in categories" :key="item.value" :value="item.value">
{{ item.label }}
</el-radio-button>
</el-radio-group>
<div class="log-filters" :class="{ 'log-filters-runtime': isRuntime }">
<template v-if="isRuntime">
<el-select v-model="selectedFile" placeholder="选择日志文件" aria-label="日志文件" @change="search">
<el-option
v-for="file in files"
:key="file.name"
:value="file.name"
:label="`${file.name}${file.compressed ? '(已压缩)' : ''}`"
>
<div class="log-file-option">
<span>{{ file.name }}</span>
<small>{{ file.compressed ? '已压缩 · ' : '' }}{{ formatFileSize(file.bytes) }}</small>
</div>
</el-option>
</el-select>
</template>
<template v-else>
<el-date-picker
v-model="dateRange"
type="datetimerange"
start-placeholder="开始时间"
end-placeholder="结束时间"
format="YYYY-MM-DD HH:mm"
aria-label="日志时间范围"
/>
<el-input v-model="actorId" clearable placeholder="操作者编号" aria-label="操作者编号" @keyup.enter="search" />
</template>
<el-button type="primary" :loading="loading" @click="search">{{ isRuntime ? '刷新' : '查询' }}</el-button>
<div class="log-filters">
<el-date-picker
v-model="dateRange"
type="datetimerange"
start-placeholder="开始时间"
end-placeholder="结束时间"
format="YYYY-MM-DD HH:mm"
aria-label="日志时间范围"
/>
<el-input v-if="!isRuntime" v-model="actorId" clearable placeholder="操作者编号" aria-label="操作者编号" @keyup.enter="search" />
<div class="log-filter-actions">
<el-button :disabled="loading" @click="recent">最近 24 小时</el-button>
<el-button type="primary" :loading="loading" @click="search">查询</el-button>
</div>
</div>
<el-alert v-if="error" :title="error" type="error" :closable="false" show-icon />
<div v-loading="loading" class="log-content" :aria-busy="loading">
<div class="log-table-heading">
<div class="log-table-title">
<strong>{{ currentCategory }}</strong>
<span v-if="isRuntime && currentFile" class="log-file-meta">
{{ formatFileSize(currentFile.bytes) }} · {{ currentFile.compressed ? '压缩归档' : '原始文件' }}
</span>
<span v-else class="log-file-meta">本页 {{ visibleCount }} 条</span>
<span class="log-file-meta">最新在前 · 每页 10 条</span>
</div>
<div v-if="isRuntime" class="log-summary" aria-label="本页日志统计">
<span>本页 {{ runtimeSummary.total }} 条</span>
@@ -173,11 +156,11 @@
import { useLogs } from './LogSettings.js'
const {
category, categories, loading, error, items, files, selectedFile, lines, offset,
category, categories, loading, error, items, offset,
hasMore, actorId, dateRange, detail, details, changes, isRuntime,
runtimeDetail, runtimeRows, runtimeSummary, currentFile, visibleCount, pageRange,
currentCategory, resultType, runtimeRowClass, timeParts, formatFileSize,
formatTime, resultLabel, actionLabel, displayActor, search, next, previous
runtimeDetail, runtimeRows, runtimeSummary, pageRange,
currentCategory, resultType, runtimeRowClass, timeParts,
formatTime, resultLabel, actionLabel, displayActor, search, recent, next, previous
} = useLogs()
</script>