-
Notifications
You must be signed in to change notification settings - Fork 11.8k
feat(usage): add per-user external channel billing statistics #7075
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,59 @@ | ||
| /* External billing API: per-user external (third-party) channel usage. */ | ||
| import { api } from '@/lib/api' | ||
|
|
||
| export interface ExternalBillingRow { | ||
| username: string | ||
| prompt_tokens: number | ||
| completion_tokens: number | ||
| total_tokens: number | ||
| quota: number | ||
| model_count: number | ||
| } | ||
|
|
||
| export interface ExternalBillingResponse { | ||
| success: boolean | ||
| message: string | ||
| data: ExternalBillingRow[] | ||
| } | ||
|
|
||
| function buildParams(startTimestamp: number, endTimestamp: number, username?: string) { | ||
| const p: Record<string, number | string> = {} | ||
| if (startTimestamp) p.start_timestamp = startTimestamp | ||
| if (endTimestamp) p.end_timestamp = endTimestamp | ||
| if (username) p.username = username | ||
| return p | ||
| } | ||
|
|
||
| // The backend reports failures as HTTP 200 + success:false; surface them as | ||
| // query errors instead of letting the page render them as empty usage. | ||
| function assertSuccess(payload: ExternalBillingResponse): ExternalBillingResponse { | ||
| if (!payload.success) { | ||
| throw new Error(payload.message || 'Failed to load external usage') | ||
| } | ||
| return payload | ||
| } | ||
|
|
||
| /** Admin: all users' external usage. */ | ||
| export async function fetchExternalBilling( | ||
| startTimestamp: number, | ||
| endTimestamp: number, | ||
| username?: string | ||
| ): Promise<ExternalBillingResponse> { | ||
| const res = await api.get('/api/log/stat/external', { | ||
| params: buildParams(startTimestamp, endTimestamp, username), | ||
| }) | ||
| return assertSuccess(res.data) | ||
| } | ||
|
|
||
| /** Member: own external usage. */ | ||
| export async function fetchExternalBillingSelf( | ||
| startTimestamp: number, | ||
| endTimestamp: number | ||
| ): Promise<ExternalBillingResponse> { | ||
| const res = await api.get('/api/log/self/stat/external', { | ||
| params: buildParams(startTimestamp, endTimestamp), | ||
| }) | ||
| return assertSuccess(res.data) | ||
| } | ||
|
|
||
| export const QUOTA_PER_USD = 500000 |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -29,6 +29,7 @@ import { | |
| MessageSquare, | ||
| PlugZap, | ||
| Radio, | ||
| Receipt, | ||
| ServerCog, | ||
| Settings, | ||
| ShieldCheck, | ||
|
|
@@ -116,6 +117,11 @@ export function useSidebarData(): SidebarData { | |
| url: '/wallet', | ||
| icon: Wallet, | ||
| }, | ||
| { | ||
| title: t('My External Usage'), | ||
| url: '/external-billing?view=self', | ||
| icon: Receipt, | ||
|
Comment on lines
+120
to
+123
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win 🔎 Supported by static analysis🏁 Script executed: #!/bin/bash
# Inspect how the shared route selects its billing view.
ast-grep outline web/src/routes/_authenticated/external-billing/index.tsx --view expanded
rg -n -C 5 'isAdmin|ROLE|fetchExternal|self|username|search|useSearch|useLocation' web/src/routes/_authenticated/external-billing/index.tsxRepository: QuantumNous/new-api Length of output: 3441 🏁 Script executed: #!/bin/bash
set -e
printf '%s\n' '--- sidebar ---'
sed -n '90,135p' web/src/hooks/use-sidebar-data.ts
printf '%s\n' '--- route ---'
sed -n '54,86p' web/src/routes/_authenticated/external-billing/index.tsx
printf '%s\n' '--- API definitions ---'
rg -n -C 8 'fetchExternalBilling(Self)?' web/src/features/external-billing/api.tsRepository: QuantumNous/new-api Length of output: 3449 🏁 Script executed: rg -n -C 6 "External Billing|external-billing" web/src/hooks/use-sidebar-data.tsRepository: QuantumNous/new-api Length of output: 948 Add an explicit personal-view selector. Both sidebar entries use 🤖 Prompt for AI Agents |
||
| }, | ||
| { | ||
| title: t('Profile'), | ||
| url: '/profile', | ||
|
|
@@ -137,6 +143,11 @@ export function useSidebarData(): SidebarData { | |
| url: '/channels', | ||
| icon: Radio, | ||
| }, | ||
| { | ||
| title: t('External Billing'), | ||
| url: '/external-billing', | ||
| icon: Receipt, | ||
| }, | ||
| { | ||
| title: t('Models'), | ||
| url: '/models/metadata', | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.