feat(calllog): 调用日志落地(SQLite / 90 天 / 管理员可查全部)
- app/calllog.py:SQLite 表 api_calls(21 列,含提示词与回复)+ 异步批量写 + 双向握手
(流式请求的访问日志条目比响应晚到也能落库)+ 早失败补记 + 失败也记 Key 前 8 位
- 新增 4 个仅管理员端点:/logs/calls、/logs/calls/stats、/logs/calls/{id}、/logs/calls/export
- 前端:日志页新增「实时 / 调用日志」页签,新组件 CallLogs.vue(筛选/分页/详情/CSV 导出)
- 保留 90 天;队列满丢弃计数,不反压请求;零新依赖
- docs:新增 docs/call-log-design.md(方案对比与三个关键设计)
- 版本 2.7.3 → 2.8.0
This commit is contained in:
1 parent
1999b5e7ac
commit
09b79c1306
10 files changed
+1101
-4
No files matched your search
@@ -0,0 +1,239 @@
|
||||
<template>
|
||||
<div class="calllogs">
|
||||
<!-- 筛选行 -->
|
||||
<div class="filters">
|
||||
<el-date-picker
|
||||
v-model="range" type="daterange" size="small" class="w-range"
|
||||
value-format="YYYY-MM-DD" :start-placeholder="t('calllogs.startDate')"
|
||||
:end-placeholder="t('calllogs.endDate')" :clearable="true"
|
||||
/>
|
||||
<el-input v-model="account" size="small" class="w140" clearable
|
||||
:placeholder="t('calllogs.account')" @keyup.enter="reload" />
|
||||
<el-input v-model="model" size="small" class="w170" clearable
|
||||
:placeholder="t('calllogs.model')" @keyup.enter="reload" />
|
||||
<el-select v-model="okSel" size="small" class="w120" clearable :placeholder="t('calllogs.result')">
|
||||
<el-option value="1" :label="t('calllogs.onlyOk')" />
|
||||
<el-option value="0" :label="t('calllogs.onlyFail')" />
|
||||
</el-select>
|
||||
<el-input v-model="kw" size="small" class="w200" clearable
|
||||
:placeholder="t('calllogs.keyword')" @keyup.enter="reload" />
|
||||
<span class="spacer" />
|
||||
<el-button size="small" :loading="loading" @click="reload">
|
||||
<Icon name="refresh" :size="13" /><span>{{ t('common.refresh') }}</span>
|
||||
</el-button>
|
||||
<el-button size="small" :disabled="!total" @click="exportCsv">
|
||||
<Icon name="dl" :size="13" /><span>{{ t('common.export') }}</span>
|
||||
</el-button>
|
||||
</div>
|
||||
|
||||
<div class="card">
|
||||
<div class="card-h">
|
||||
<b>{{ t('calllogs.title') }}</b>
|
||||
<span class="sub">{{ t('calllogs.count', { n: total }) }}</span>
|
||||
<div class="pager">
|
||||
<span class="pill" :class="failCount ? 'warn' : 'ok'">
|
||||
{{ t('calllogs.failN', { n: failCount }) }}
|
||||
</span>
|
||||
<span class="pill">{{ t('calllogs.avgMs', { n: avgMs }) }}</span>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<el-table :data="rows" size="small" v-loading="loading" max-height="580"
|
||||
class="tbl" @row-click="openDetail" :row-class-name="rowCls">
|
||||
<el-table-column prop="ts_iso" :label="t('calllogs.colTime')" width="152" />
|
||||
<el-table-column prop="account" :label="t('calllogs.colAccount')" width="112" show-overflow-tooltip />
|
||||
<el-table-column prop="model" :label="t('calllogs.colModel')" min-width="150" show-overflow-tooltip>
|
||||
<template #default="{ row }">
|
||||
<span>{{ row.model || '—' }}</span>
|
||||
<span v-if="row.served && row.served !== row.model" class="sub"> → {{ row.served }}</span>
|
||||
</template>
|
||||
</el-table-column>
|
||||
<el-table-column :label="t('calllogs.colLatency')" width="92">
|
||||
<template #default="{ row }">{{ row.latency_ms != null ? row.latency_ms + 'ms' : '—' }}</template>
|
||||
</el-table-column>
|
||||
<el-table-column :label="t('calllogs.colStatus')" width="84">
|
||||
<template #default="{ row }">
|
||||
<span :class="row.ok ? 'ok-t' : 'err-t'">{{ row.status ?? '—' }}</span>
|
||||
</template>
|
||||
</el-table-column>
|
||||
<el-table-column :label="t('calllogs.colStream')" width="66">
|
||||
<template #default="{ row }">{{ row.stream ? t('calllogs.yes') : '—' }}</template>
|
||||
</el-table-column>
|
||||
<el-table-column prop="ua" :label="t('calllogs.colUa')" min-width="130" show-overflow-tooltip />
|
||||
</el-table>
|
||||
|
||||
<div class="pager-bar">
|
||||
<el-pagination
|
||||
v-model:current-page="page" :page-size="pageSize" :total="total"
|
||||
layout="prev, pager, next, total" small background @current-change="loadPage"
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- 详情弹窗:完整提示词 + 回复 -->
|
||||
<el-dialog v-model="dlg" :title="t('calllogs.detailTitle')" width="860px" top="6vh">
|
||||
<div v-if="detail" class="det">
|
||||
<div class="kv">
|
||||
<span><i>{{ t('calllogs.colTime') }}</i>{{ detail.ts_iso }}</span>
|
||||
<span><i>rid</i><code>{{ detail.rid || '—' }}</code></span>
|
||||
<span><i>{{ t('calllogs.colAccount') }}</i>{{ detail.account || '—' }}</span>
|
||||
<span><i>Key</i>{{ detail.key_id ? detail.key_id + '…' : '—' }}</span>
|
||||
<span><i>{{ t('calllogs.colModel') }}</i>{{ detail.model || '—' }}</span>
|
||||
<span><i>{{ t('calllogs.colStatus') }}</i>{{ detail.status ?? '—' }} · {{ detail.ok ? 'OK' : 'FAIL' }}</span>
|
||||
<span><i>{{ t('calllogs.colLatency') }}</i>{{ detail.latency_ms != null ? detail.latency_ms + 'ms' : '—' }}</span>
|
||||
<span><i>{{ t('calllogs.colStream') }}</i>{{ detail.stream ? t('calllogs.yes') : t('calllogs.no') }}</span>
|
||||
<span><i>IP</i>{{ detail.ip || '—' }}</span>
|
||||
<span><i>UA</i>{{ detail.ua || '—' }}</span>
|
||||
<span><i>path</i>{{ detail.path || '—' }}</span>
|
||||
<span><i>ver</i>{{ detail.ver || '—' }}</span>
|
||||
</div>
|
||||
<div class="sec">
|
||||
<label>{{ t('calllogs.prompt') }}</label>
|
||||
<pre>{{ detail.prompt || '—' }}</pre>
|
||||
</div>
|
||||
<div class="sec">
|
||||
<label>{{ t('calllogs.completion') }}</label>
|
||||
<pre>{{ detail.completion || '—' }}</pre>
|
||||
</div>
|
||||
</div>
|
||||
<div v-else class="sub" style="padding: 20px 0">{{ t('calllogs.loading') }}</div>
|
||||
</el-dialog>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<script setup lang="ts">
|
||||
import { computed, onMounted, ref } from 'vue'
|
||||
import { useI18n } from 'vue-i18n'
|
||||
import { api, getToken } from '../api'
|
||||
import Icon from '../components/Icon.vue'
|
||||
import { filenameFromHeader } from '../lib/download'
|
||||
|
||||
const { t } = useI18n()
|
||||
|
||||
const rows = ref<any[]>([])
|
||||
const total = ref(0)
|
||||
const loading = ref(false)
|
||||
const page = ref(1)
|
||||
const pageSize = 50
|
||||
|
||||
const range = ref<[string, string] | null>(null)
|
||||
const account = ref('')
|
||||
const model = ref('')
|
||||
const okSel = ref('')
|
||||
const kw = ref('')
|
||||
|
||||
const dlg = ref(false)
|
||||
const detail = ref<any>(null)
|
||||
|
||||
const failCount = computed(() => rows.value.filter((r) => !r.ok).length)
|
||||
const avgMs = computed(() => {
|
||||
const v = rows.value.map((r) => Number(r.latency_ms)).filter((n) => Number.isFinite(n) && n > 0)
|
||||
if (!v.length) return 0
|
||||
return Math.round(v.reduce((a, b) => a + b, 0) / v.length)
|
||||
})
|
||||
|
||||
function qs(extra: Record<string, string> = {}): string {
|
||||
const p = new URLSearchParams()
|
||||
if (range.value && range.value[0]) p.set('start', `${range.value[0]} 00:00`)
|
||||
if (range.value && range.value[1]) p.set('end', `${range.value[1]} 23:59`)
|
||||
if (account.value.trim()) p.set('account', account.value.trim())
|
||||
if (model.value.trim()) p.set('model', model.value.trim())
|
||||
if (okSel.value !== '') p.set('ok', okSel.value)
|
||||
if (kw.value.trim()) p.set('q', kw.value.trim())
|
||||
Object.entries(extra).forEach(([k, v]) => p.set(k, v))
|
||||
return p.toString()
|
||||
}
|
||||
|
||||
async function loadPage() {
|
||||
loading.value = true
|
||||
try {
|
||||
const data = await api(`/logs/calls?${qs({ limit: String(pageSize), offset: String((page.value - 1) * pageSize) })}`)
|
||||
rows.value = data.items || []
|
||||
total.value = data.total || 0
|
||||
} catch (e: any) {
|
||||
rows.value = []
|
||||
total.value = 0
|
||||
} finally {
|
||||
loading.value = false
|
||||
}
|
||||
}
|
||||
|
||||
function reload() {
|
||||
page.value = 1
|
||||
loadPage()
|
||||
}
|
||||
|
||||
async function openDetail(row: any) {
|
||||
detail.value = null
|
||||
dlg.value = true
|
||||
try {
|
||||
detail.value = await api(`/logs/calls/${row.id}`)
|
||||
} catch {
|
||||
detail.value = row
|
||||
}
|
||||
}
|
||||
|
||||
async function exportCsv() {
|
||||
const q = qs()
|
||||
const resp = await fetch(`/logs/calls/export?${q}`, {
|
||||
headers: { Authorization: `Bearer ${getToken()}` },
|
||||
})
|
||||
if (!resp.ok) return
|
||||
const blob = await resp.blob()
|
||||
const name = filenameFromHeader(resp.headers.get('content-disposition') || '') ||
|
||||
`calllog-${new Date().toISOString().slice(0, 19).replace(/[:T]/g, '')}.csv`
|
||||
const href = URL.createObjectURL(blob)
|
||||
const a = document.createElement('a')
|
||||
a.href = href
|
||||
a.download = name
|
||||
a.style.display = 'none'
|
||||
document.body.appendChild(a)
|
||||
a.click()
|
||||
a.remove()
|
||||
setTimeout(() => URL.revokeObjectURL(href), 10_000)
|
||||
}
|
||||
|
||||
function rowCls({ row }: { row: any }) {
|
||||
return row.ok ? '' : 'row-fail'
|
||||
}
|
||||
|
||||
onMounted(loadPage)
|
||||
</script>
|
||||
|
||||
<style scoped>
|
||||
.calllogs { display: flex; flex-direction: column; gap: 12px; }
|
||||
.filters { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }
|
||||
.w-range { width: 250px; }
|
||||
.w140 { width: 140px; }
|
||||
.w170 { width: 170px; }
|
||||
.w120 { width: 120px; }
|
||||
.w200 { width: 200px; }
|
||||
.spacer { flex: 1; }
|
||||
.card { background: var(--surface); border: 1px solid var(--line); border-radius: 12px; padding: 12px 14px; }
|
||||
.card-h { display: flex; align-items: center; gap: 10px; margin-bottom: 10px; }
|
||||
.card-h b { font-size: 13.5px; }
|
||||
.pager { display: flex; gap: 6px; margin-left: auto; }
|
||||
.pill { font-size: 11.5px; padding: 2px 8px; border-radius: 999px; background: var(--chip); color: var(--muted-2); }
|
||||
.pill.ok { color: var(--ok); }
|
||||
.pill.warn { color: var(--err); }
|
||||
.tbl :deep(.row-fail) { background: color-mix(in srgb, var(--err) 6%, transparent); }
|
||||
.ok-t { color: var(--ok); font-variant-numeric: tabular-nums; }
|
||||
.err-t { color: var(--err); font-weight: 600; font-variant-numeric: tabular-nums; }
|
||||
.pager-bar { display: flex; justify-content: flex-end; padding-top: 10px; }
|
||||
.sub { font-size: 11.5px; color: var(--muted-2); }
|
||||
.kv { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 6px 14px; font-size: 12.5px; }
|
||||
.kv span { display: flex; gap: 6px; align-items: baseline; overflow: hidden; }
|
||||
.kv i { font-style: normal; color: var(--muted-2); min-width: 52px; }
|
||||
.kv code { font-size: 11.5px; }
|
||||
.sec { margin-top: 12px; }
|
||||
.sec label { font-size: 12px; color: var(--muted-2); }
|
||||
.sec pre {
|
||||
margin: 4px 0 0; padding: 10px 12px; max-height: 260px; overflow: auto;
|
||||
background: var(--surface-2, #f7f8fa); border: 1px solid var(--line); border-radius: 8px;
|
||||
font-size: 12px; line-height: 1.5; white-space: pre-wrap; word-break: break-word;
|
||||
}
|
||||
@media (max-width: 768px) {
|
||||
.kv { grid-template-columns: 1fr; }
|
||||
.w-range, .w140, .w170, .w120, .w200 { width: 100%; }
|
||||
}
|
||||
</style>
|
||||
@@ -1,5 +1,14 @@
|
||||
<template>
|
||||
<div class="page">
|
||||
<!-- 2026-10-23 调用日志落地:两页签 —— 实时(内存环形缓冲,重启即丢)/ 调用日志(SQLite 持久化 90 天,仅管理员) -->
|
||||
<div class="tabs">
|
||||
<span class="tab" :class="{ on: tab === 'live' }" @click="tab = 'live'">{{ t('logs.tabLive') }}</span>
|
||||
<span v-if="isAdmin" class="tab" :class="{ on: tab === 'calls' }" @click="tab = 'calls'">
|
||||
{{ t('logs.tabCalls') }}
|
||||
</span>
|
||||
</div>
|
||||
|
||||
<div v-show="tab === 'live'">
|
||||
<!-- 筛选行:类型胶囊 + 原有筛选控件 + 刷新/导出 -->
|
||||
<div class="filters">
|
||||
<!-- 第十九轮:类型胶囊与「失败」互斥(原来两个维度叠加,「失败」亮着时点类型只剩空列表,
|
||||
@@ -102,6 +111,9 @@
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<CallLogs v-if="tab === 'calls' && isAdmin" />
|
||||
</div>
|
||||
</template>
|
||||
|
||||
@@ -112,6 +124,13 @@ import { useI18n } from 'vue-i18n'
|
||||
import { api } from '../api'
|
||||
import Icon from '../components/Icon.vue'
|
||||
import { stamp } from '../lib/download'
|
||||
import { useSessionStore } from '../store/session'
|
||||
import CallLogs from './CallLogs.vue'
|
||||
|
||||
const store = useSessionStore()
|
||||
const isAdmin = computed(() => store.isAdmin)
|
||||
// 'live' = 内存实时日志(原有页面);'calls' = SQLite 持久化的调用日志(仅管理员)
|
||||
const tab = ref<'live' | 'calls'>('live')
|
||||
|
||||
const router = useRouter()
|
||||
const { t } = useI18n()
|
||||
@@ -335,6 +354,15 @@ onUnmounted(() => clearInterval(timer))
|
||||
.sub { font-size: 11.5px; color: var(--muted-2); }
|
||||
.err-text { color: var(--err); font-size: 12px; }
|
||||
|
||||
/* 2026-10-23: 顶部页签(实时 / 调用日志) */
|
||||
.tabs { display: flex; gap: 6px; margin-bottom: 10px; border-bottom: 1px solid var(--line); }
|
||||
.tabs .tab {
|
||||
padding: 7px 14px; font-size: 13px; cursor: pointer; color: var(--muted-2);
|
||||
border-bottom: 2px solid transparent; margin-bottom: -1px; user-select: none;
|
||||
}
|
||||
.tabs .tab:hover { color: var(--text); }
|
||||
.tabs .tab.on { color: var(--primary); border-bottom-color: var(--primary); font-weight: 600; }
|
||||
|
||||
/* 类型胶囊上的条数:小一号、淡一档;胶囊选中时跟着主色走(.on 已给 font-weight) */
|
||||
.chip-sel .cn {
|
||||
font-style: normal; font-size: 11px; font-variant-numeric: tabular-nums;
|
||||
|
||||
Reference in new issue
Block a user