fix(billing): surface quota saturation events for admin auditing

Thread int32 saturation clamps from tiered settlement and video task
recompute into the consume/task logs under admin_info, so oversized or
malformed billing inputs stay auditable. Clamp negative audio duration
before token conversion and gate the saturation UI markers on admin.
This commit is contained in:
CaIon
2026-07-07 12:20:07 +08:00
parent c9943d37ad
commit bae799ccb1
32 changed files with 710 additions and 104 deletions
@@ -104,6 +104,23 @@ function splitQuotaDisplay(value: string): { prefix: string; amount: string } {
}
function buildDetailSegments(
log: UsageLog,
other: LogOtherData | null,
t: (key: string, opts?: Record<string, unknown>) => string,
isAdmin: boolean
): DetailSegment[] {
const segments = buildTypeDetailSegments(log, other, t)
// Quota saturation is a rare, admin-only anomaly marker; surface it first
// and in danger styling so it stands out on the related billing log. The
// backend already strips admin_info for non-admins; gate on isAdmin too as
// defense in depth so the marker never leaks if that changes.
if (isAdmin && other?.admin_info?.quota_saturation) {
return [{ text: t('Quota clamped'), danger: true }, ...segments]
}
return segments
}
function buildTypeDetailSegments(
log: UsageLog,
other: LogOtherData | null,
t: (key: string, opts?: Record<string, unknown>) => string
@@ -817,7 +834,7 @@ export function useCommonLogsColumns(isAdmin: boolean): ColumnDef<UsageLog>[] {
const log = row.original
const other = parseLogOther(log.other)
const segments = buildDetailSegments(log, other, t)
const segments = buildDetailSegments(log, other, t, isAdmin)
const primary = segments[0]
const hasMore = segments.length > 1
@@ -143,6 +143,15 @@ function formatRatio(ratio: number | undefined): string {
return ratio.toFixed(4)
}
function quotaSaturationKindLabel(
kind: 'overflow' | 'underflow' | 'nan',
t: (key: string) => string
): string {
if (kind === 'overflow') return t('Overflow')
if (kind === 'underflow') return t('Underflow')
return t('Invalid (NaN)')
}
function BillingBreakdown(props: {
log: UsageLog
other: LogOtherData
@@ -704,6 +713,41 @@ export function DetailsDialog(props: DetailsDialogProps) {
</DetailSection>
)}
{/* Quota saturation marker (admin only) */}
{props.isAdmin && other?.admin_info?.quota_saturation && (
<DetailSection
icon={<AlertTriangle className='size-3.5' aria-hidden='true' />}
label={t('Quota clamped')}
variant='danger'
>
<p className='mb-1 text-xs wrap-break-word'>
{t('Quota saturation protection triggered')}
</p>
<DetailRow
label={t('Kind')}
value={quotaSaturationKindLabel(
other.admin_info.quota_saturation.kind,
t
)}
/>
<DetailRow
label={t('Original value')}
value={String(other.admin_info.quota_saturation.original)}
mono
/>
<DetailRow
label={t('Clamped to')}
value={String(other.admin_info.quota_saturation.clamped)}
mono
/>
<DetailRow
label={t('Operation')}
value={other.admin_info.quota_saturation.op}
mono
/>
</DetailSection>
)}
{/* Reject reason (admin only) */}
{props.isAdmin && other?.reject_reason && (
<DetailSection
@@ -38,6 +38,7 @@ import {
LOG_TYPE_ENUM,
} from '../constants'
import { useColumnsByCategory } from '../lib/columns'
import { parseLogOther } from '../lib/format'
import { fetchLogsByCategory } from '../lib/utils'
import type { LogCategory } from '../types'
import { CommonLogsFilterBar } from './common-logs-filter-bar'
@@ -51,6 +52,10 @@ const logTypeRowTint: Record<number, string> = {
[LOG_TYPE_ENUM.REFUND]: 'bg-blue-50/30 dark:bg-blue-950/15',
}
// Warning tint for logs where a quota conversion saturated (admin-only marker).
// Takes precedence over the per-type tint since it flags a billing anomaly.
const quotaSaturationRowTint = 'bg-amber-50/60 dark:bg-amber-950/25'
function getColumnVisibilityStorageKey(
logCategory: LogCategory,
isAdmin: boolean
@@ -204,8 +209,16 @@ export function UsageLogsTable({ logCategory }: UsageLogsTableProps) {
const logType = (row.original as Record<string, unknown>).type as
| number
| undefined
const tintClass =
let tintClass =
isCommon && logType != null ? (logTypeRowTint[logType] ?? '') : ''
if (isCommon && isAdmin) {
const other = parseLogOther(
((row.original as Record<string, unknown>).other as string) ?? ''
)
if (other?.admin_info?.quota_saturation) {
tintClass = quotaSaturationRowTint
}
}
return (
<DataTableRow