mirror of
https://github.com/shumaiOne/shumai.git
synced 2026-11-26 10:55:27 +00:00
feat(quota): move usage monitoring to team dashboard (#327)
This commit is contained in:
@@ -1,274 +0,0 @@
|
||||
import React, { useState } from 'react'
|
||||
import { useQuery } from '@tanstack/react-query'
|
||||
import { client } from '@/ui/api/client'
|
||||
import {
|
||||
Dialog,
|
||||
DialogContent,
|
||||
DialogDescription,
|
||||
DialogHeader,
|
||||
DialogTitle,
|
||||
} from '@/ui/components/ui/dialog'
|
||||
import { Input } from '@/ui/components/ui/input'
|
||||
import { Badge } from '@/ui/components/ui/badge'
|
||||
import { Progress } from '@/ui/components/ui/progress'
|
||||
import { ScrollArea } from '@/ui/components/ui/scroll-area'
|
||||
import { Avatar, AvatarFallback, AvatarImage } from '@/ui/components/ui/avatar'
|
||||
import {
|
||||
Cpu,
|
||||
DollarSign,
|
||||
Server,
|
||||
Terminal,
|
||||
Wrench,
|
||||
Loader2,
|
||||
Users,
|
||||
Search,
|
||||
CheckCircle2,
|
||||
Clock,
|
||||
Activity,
|
||||
} from 'lucide-react'
|
||||
import { m } from '@/ui/paraglide/messages.js'
|
||||
import { formatRemainingTime } from '@/ui/lib/time'
|
||||
import type { QuotaRuleResponse, QuotaResourceTypeEnum, QuotaRecordResponse } from '@shumai/dtos'
|
||||
import { formatQuotaPeriod } from '@shumai/dtos'
|
||||
|
||||
interface QuotaUsageRecordsDialogProps {
|
||||
open: boolean
|
||||
onOpenChange: (open: boolean) => void
|
||||
teamId: string
|
||||
rule: QuotaRuleResponse
|
||||
}
|
||||
|
||||
/* eslint-disable @typescript-eslint/naming-convention */
|
||||
const RESOURCE_META: Record<
|
||||
QuotaResourceTypeEnum,
|
||||
{ label: () => string; icon: React.ElementType; unit: string }
|
||||
> = {
|
||||
agent_total_tokens: {
|
||||
label: () => m.quota_resource_agent_total_tokens(),
|
||||
icon: Cpu,
|
||||
unit: 'tokens',
|
||||
},
|
||||
agent_cost: {
|
||||
label: () => m.quota_resource_agent_cost(),
|
||||
icon: DollarSign,
|
||||
unit: '$',
|
||||
},
|
||||
agent_mcp_call_count: {
|
||||
label: () => m.quota_resource_agent_mcp_call_count(),
|
||||
icon: Server,
|
||||
unit: 'calls',
|
||||
},
|
||||
agent_bash_call_count: {
|
||||
label: () => m.quota_resource_agent_bash_call_count(),
|
||||
icon: Terminal,
|
||||
unit: 'calls',
|
||||
},
|
||||
agent_tool_call_count: {
|
||||
label: () => m.quota_resource_agent_tool_call_count(),
|
||||
icon: Wrench,
|
||||
unit: 'calls',
|
||||
},
|
||||
}
|
||||
/* eslint-enable @typescript-eslint/naming-convention */
|
||||
|
||||
function formatResourceValue(resource: QuotaResourceTypeEnum, val: number): string {
|
||||
if (resource === 'agent_cost') {
|
||||
return `$${val.toFixed(2)}`
|
||||
}
|
||||
return val.toLocaleString()
|
||||
}
|
||||
|
||||
function getPeriodLabel(period: string): string {
|
||||
const norm = formatQuotaPeriod(period)
|
||||
switch (norm) {
|
||||
case '1hour':
|
||||
return m.quota_period_1hour()
|
||||
case '5hour':
|
||||
return m.quota_period_5hour()
|
||||
case '1day':
|
||||
return m.quota_period_1day()
|
||||
case '7day':
|
||||
return m.quota_period_7day()
|
||||
default:
|
||||
return norm
|
||||
}
|
||||
}
|
||||
|
||||
export const QuotaUsageRecordsDialog: React.FC<QuotaUsageRecordsDialogProps> = ({
|
||||
open,
|
||||
onOpenChange,
|
||||
teamId,
|
||||
rule,
|
||||
}) => {
|
||||
const [search, setSearch] = useState('')
|
||||
|
||||
const { data, isLoading } = useQuery({
|
||||
queryKey: ['teams', teamId, 'quotas', rule.id, 'records'],
|
||||
queryFn: async () => {
|
||||
const res = await client.api.teams[':teamId'].quotas[':id'].records.$get({
|
||||
param: { teamId, id: rule.id },
|
||||
})
|
||||
if (!res.ok) return { records: [], total: 0 }
|
||||
return await res.json()
|
||||
},
|
||||
enabled: open,
|
||||
refetchInterval: 10000,
|
||||
})
|
||||
|
||||
const records: QuotaRecordResponse[] = data?.records || []
|
||||
const meta = RESOURCE_META[rule.resource]
|
||||
const Icon = meta?.icon || Activity
|
||||
|
||||
const filteredRecords = records.filter((r) => {
|
||||
if (!search.trim()) return true
|
||||
const q = search.toLowerCase()
|
||||
if (r.user?.name.toLowerCase().includes(q)) return true
|
||||
if (r.user?.email.toLowerCase().includes(q)) return true
|
||||
return false
|
||||
})
|
||||
|
||||
return (
|
||||
<Dialog open={open} onOpenChange={onOpenChange}>
|
||||
<DialogContent className="sm:max-w-[620px] p-0 gap-0 overflow-hidden">
|
||||
<DialogHeader className="p-6 pb-4 border-b border-border/60">
|
||||
<div className="flex items-center gap-3">
|
||||
<div className="p-2 rounded-lg bg-primary/10 text-primary shrink-0">
|
||||
<Icon className="w-5 h-5" />
|
||||
</div>
|
||||
<div>
|
||||
<DialogTitle className="text-lg font-bold flex items-center gap-2">
|
||||
<span>{m.quota_usage_records()}</span>
|
||||
<Badge variant="outline" className="text-xs font-normal">
|
||||
{meta?.label ? meta.label() : rule.resource} -{' '}
|
||||
{formatResourceValue(rule.resource, rule.limit)} {meta?.unit} /{' '}
|
||||
{getPeriodLabel(rule.period)}
|
||||
</Badge>
|
||||
</DialogTitle>
|
||||
<DialogDescription className="text-xs text-muted-foreground mt-0.5">
|
||||
{m.quota_usage_records_description()}
|
||||
</DialogDescription>
|
||||
</div>
|
||||
</div>
|
||||
</DialogHeader>
|
||||
|
||||
{rule.scopeMode !== 'all_members' && records.length > 3 && (
|
||||
<div className="px-6 pt-4 pb-2">
|
||||
<div className="relative">
|
||||
<Search className="w-4 h-4 absolute left-3 top-1/2 -translate-y-1/2 text-muted-foreground" />
|
||||
<Input
|
||||
value={search}
|
||||
onChange={(e) => setSearch(e.target.value)}
|
||||
placeholder={m.quota_search_members()}
|
||||
className="pl-9 h-9 text-sm"
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
|
||||
<ScrollArea className="max-h-[60vh] p-6 pt-3">
|
||||
{isLoading ? (
|
||||
<div className="flex justify-center py-12">
|
||||
<Loader2 className="w-8 h-8 animate-spin text-primary" />
|
||||
</div>
|
||||
) : filteredRecords.length === 0 ? (
|
||||
<div className="text-center py-10 text-sm text-muted-foreground">
|
||||
{records.length === 0 ? m.no_quotas_title() : 'No matching records found'}
|
||||
</div>
|
||||
) : (
|
||||
<div className="space-y-3">
|
||||
{filteredRecords.map((record, index) => {
|
||||
const percent = record.percent
|
||||
const isOverLimit = percent >= 100
|
||||
const isSharedPool = rule.scopeMode === 'all_members'
|
||||
|
||||
return (
|
||||
<div
|
||||
key={record.id || record.userId || `record-${index}`}
|
||||
className="p-4 rounded-xl border border-border/70 bg-card text-card-foreground shadow-2xs space-y-3"
|
||||
>
|
||||
{/* Top Row: User / Pool Info & Status Badge */}
|
||||
<div className="flex items-center justify-between gap-2">
|
||||
<div className="flex items-center gap-3 min-w-0">
|
||||
{isSharedPool ? (
|
||||
<div className="w-9 h-9 rounded-full bg-primary/10 text-primary flex items-center justify-center shrink-0">
|
||||
<Users className="w-5 h-5" />
|
||||
</div>
|
||||
) : (
|
||||
<Avatar className="w-9 h-9 border border-border/50 shrink-0">
|
||||
<AvatarImage src={record.user?.image || undefined} />
|
||||
<AvatarFallback className="text-xs font-semibold">
|
||||
{record.user?.name?.[0]?.toUpperCase() || 'U'}
|
||||
</AvatarFallback>
|
||||
</Avatar>
|
||||
)}
|
||||
<div className="min-w-0">
|
||||
<div className="font-semibold text-sm text-foreground truncate">
|
||||
{isSharedPool
|
||||
? rule.role
|
||||
? `${m.quota_role_shared_pool()}: ${rule.role}`
|
||||
: m.quota_team_shared_pool()
|
||||
: record.user?.name || record.userId}
|
||||
</div>
|
||||
{!isSharedPool && record.user?.email && (
|
||||
<div className="text-xs text-muted-foreground truncate">
|
||||
{record.user.email}
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{/* Status Badge */}
|
||||
<div className="shrink-0">
|
||||
{record.isWindowActive && record.periodEnd ? (
|
||||
<Badge
|
||||
variant="outline"
|
||||
className="gap-1 text-xs text-primary border-primary/30 bg-primary/5"
|
||||
>
|
||||
<Clock className="w-3 h-3" />
|
||||
<span>
|
||||
{m.quota_refreshes_in({
|
||||
time: formatRemainingTime(record.periodEnd),
|
||||
})}
|
||||
</span>
|
||||
</Badge>
|
||||
) : (
|
||||
<Badge
|
||||
variant="secondary"
|
||||
className="gap-1 text-xs text-muted-foreground bg-muted"
|
||||
>
|
||||
<CheckCircle2 className="w-3 h-3 text-emerald-500" />
|
||||
<span>{m.quota_available()}</span>
|
||||
</Badge>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{/* Progress Bar & Details */}
|
||||
<div className="space-y-1.5 pt-1">
|
||||
<Progress value={Math.min(100, Math.max(0, percent))} className="h-2" />
|
||||
<div className="flex items-center justify-between text-xs font-medium">
|
||||
<span
|
||||
className={isOverLimit ? 'text-destructive font-bold' : 'text-foreground'}
|
||||
>
|
||||
{formatResourceValue(rule.resource, record.consumed)} /{' '}
|
||||
{formatResourceValue(rule.resource, rule.limit)} {meta?.unit}
|
||||
</span>
|
||||
<span
|
||||
className={
|
||||
isOverLimit ? 'text-destructive font-bold' : 'text-muted-foreground'
|
||||
}
|
||||
>
|
||||
{percent}%
|
||||
</span>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
)
|
||||
})}
|
||||
</div>
|
||||
)}
|
||||
</ScrollArea>
|
||||
</DialogContent>
|
||||
</Dialog>
|
||||
)
|
||||
}
|
||||
@@ -28,89 +28,29 @@ import {
|
||||
Loader2,
|
||||
MoreVertical,
|
||||
Trash2,
|
||||
Cpu,
|
||||
DollarSign,
|
||||
Server,
|
||||
Terminal,
|
||||
Wrench,
|
||||
Users,
|
||||
UserCheck,
|
||||
User,
|
||||
Clock,
|
||||
Eye,
|
||||
} from 'lucide-react'
|
||||
import { toast } from 'sonner'
|
||||
import { m } from '@/ui/paraglide/messages.js'
|
||||
import type { QuotaRuleResponse, QuotaResourceTypeEnum } from '@shumai/dtos'
|
||||
import { formatQuotaPeriod } from '@shumai/dtos'
|
||||
import type { QuotaRuleResponse } from '@shumai/dtos'
|
||||
import {
|
||||
QUOTA_RESOURCE_META,
|
||||
formatQuotaResourceValue,
|
||||
getQuotaPeriodLabel,
|
||||
} from '../quota/quota-display'
|
||||
import { QuotaRuleDialog } from './QuotaRuleDialog'
|
||||
import { QuotaUsageRecordsDialog } from './QuotaUsageRecordsDialog'
|
||||
|
||||
interface QuotasSettingsProps {
|
||||
teamId: string
|
||||
}
|
||||
|
||||
/* eslint-disable @typescript-eslint/naming-convention */
|
||||
const RESOURCE_META: Record<
|
||||
QuotaResourceTypeEnum,
|
||||
{ label: () => string; icon: React.ElementType; unit: string }
|
||||
> = {
|
||||
agent_total_tokens: {
|
||||
label: () => m.quota_resource_agent_total_tokens(),
|
||||
icon: Cpu,
|
||||
unit: 'tokens',
|
||||
},
|
||||
agent_cost: {
|
||||
label: () => m.quota_resource_agent_cost(),
|
||||
icon: DollarSign,
|
||||
unit: '$',
|
||||
},
|
||||
agent_mcp_call_count: {
|
||||
label: () => m.quota_resource_agent_mcp_call_count(),
|
||||
icon: Server,
|
||||
unit: 'calls',
|
||||
},
|
||||
agent_bash_call_count: {
|
||||
label: () => m.quota_resource_agent_bash_call_count(),
|
||||
icon: Terminal,
|
||||
unit: 'calls',
|
||||
},
|
||||
agent_tool_call_count: {
|
||||
label: () => m.quota_resource_agent_tool_call_count(),
|
||||
icon: Wrench,
|
||||
unit: 'calls',
|
||||
},
|
||||
}
|
||||
/* eslint-enable @typescript-eslint/naming-convention */
|
||||
|
||||
function getPeriodLabel(period: string): string {
|
||||
const norm = formatQuotaPeriod(period)
|
||||
switch (norm) {
|
||||
case '1hour':
|
||||
return m.quota_period_1hour()
|
||||
case '5hour':
|
||||
return m.quota_period_5hour()
|
||||
case '1day':
|
||||
return m.quota_period_1day()
|
||||
case '7day':
|
||||
return m.quota_period_7day()
|
||||
default:
|
||||
return norm
|
||||
}
|
||||
}
|
||||
|
||||
function formatResourceValue(resource: QuotaResourceTypeEnum, val: number): string {
|
||||
if (resource === 'agent_cost') {
|
||||
return `$${val.toFixed(2)}`
|
||||
}
|
||||
return val.toLocaleString()
|
||||
}
|
||||
|
||||
export const QuotasSettings: React.FC<QuotasSettingsProps> = ({ teamId }) => {
|
||||
const queryClient = useQueryClient()
|
||||
const [isCreateOpen, setIsCreateOpen] = useState(false)
|
||||
const [selectedRule, setSelectedRule] = useState<QuotaRuleResponse | null>(null)
|
||||
const [viewingUsageRule, setViewingUsageRule] = useState<QuotaRuleResponse | null>(null)
|
||||
const [deletingRule, setDeletingRule] = useState<QuotaRuleResponse | null>(null)
|
||||
|
||||
// Fetch quota rules
|
||||
@@ -204,7 +144,7 @@ export const QuotasSettings: React.FC<QuotasSettingsProps> = ({ teamId }) => {
|
||||
) : (
|
||||
<div className="flex flex-col gap-4">
|
||||
{rules.map((rule) => {
|
||||
const meta = RESOURCE_META[rule.resource]
|
||||
const meta = QUOTA_RESOURCE_META[rule.resource]
|
||||
const resData = (rule.resourceData as Record<string, unknown> | null) || {}
|
||||
|
||||
return (
|
||||
@@ -272,8 +212,8 @@ export const QuotasSettings: React.FC<QuotasSettingsProps> = ({ teamId }) => {
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{/* Badges Row & View Usage Action */}
|
||||
<div className="flex flex-wrap items-center justify-between gap-2 pt-2 border-t border-border/50">
|
||||
{/* Badges Row */}
|
||||
<div className="flex flex-wrap items-center gap-2 pt-2 border-t border-border/50">
|
||||
<div className="flex flex-wrap items-center gap-1.5">
|
||||
{/* Scope Mode & Role badge */}
|
||||
{rule.scopeMode === 'all_members' && (
|
||||
@@ -308,25 +248,11 @@ export const QuotasSettings: React.FC<QuotasSettingsProps> = ({ teamId }) => {
|
||||
<Badge variant="outline" className="gap-1 text-xs">
|
||||
<Clock className="w-3 h-3" />
|
||||
<span>
|
||||
{formatResourceValue(rule.resource, rule.limit)} {meta?.unit} /{' '}
|
||||
{getPeriodLabel(rule.period)}
|
||||
{formatQuotaResourceValue(rule.resource, rule.limit)} {meta?.unit} /{' '}
|
||||
{getQuotaPeriodLabel(rule.period)}
|
||||
</span>
|
||||
</Badge>
|
||||
</div>
|
||||
|
||||
{/* View Usage Button */}
|
||||
<Button
|
||||
variant="outline"
|
||||
size="sm"
|
||||
onClick={(e) => {
|
||||
e.stopPropagation()
|
||||
setViewingUsageRule(rule)
|
||||
}}
|
||||
className="gap-1.5 text-xs h-7 px-2.5"
|
||||
>
|
||||
<Eye className="w-3.5 h-3.5 text-primary" />
|
||||
<span>{m.quota_view_usage()}</span>
|
||||
</Button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
@@ -353,18 +279,6 @@ export const QuotasSettings: React.FC<QuotasSettingsProps> = ({ teamId }) => {
|
||||
/>
|
||||
)}
|
||||
|
||||
{/* View Usage Records Dialog */}
|
||||
{!!viewingUsageRule && (
|
||||
<QuotaUsageRecordsDialog
|
||||
open={!!viewingUsageRule}
|
||||
onOpenChange={(open) => {
|
||||
if (!open) setViewingUsageRule(null)
|
||||
}}
|
||||
teamId={teamId}
|
||||
rule={viewingUsageRule}
|
||||
/>
|
||||
)}
|
||||
|
||||
{/* Delete Confirmation Alert Dialog */}
|
||||
<AlertDialog
|
||||
open={!!deletingRule}
|
||||
|
||||
Reference in New Issue
Block a user