|
1 | 1 | <template> |
2 | | - <div class="card p-4"> |
3 | | - <!-- Header --> |
4 | | - <div class="mb-3 flex flex-wrap items-center gap-3"> |
5 | | - <h3 class="text-sm font-semibold text-gray-800 dark:text-gray-100"> |
6 | | - {{ t('admin.usage.tokenRanking.title') }} |
7 | | - </h3> |
8 | | - <span class="text-xs text-gray-400">{{ t('admin.usage.tokenRanking.subtitle') }}</span> |
9 | | - <div class="ml-auto flex flex-wrap items-center gap-2"> |
10 | | - <input |
11 | | - v-model="search" |
12 | | - type="text" |
13 | | - class="input h-8 w-44 text-sm" |
14 | | - :placeholder="t('admin.usage.tokenRanking.searchPlaceholder')" |
15 | | - /> |
| 2 | + <!-- 用量页"用户排行"tab 内容:无卡片外观,依赖父级统一卡片;筛选/时间范围复用页面级筛选栏 --> |
| 3 | + <div> |
| 4 | + <!-- Toolbar --> |
| 5 | + <div class="flex flex-wrap items-center justify-between gap-3 border-b border-gray-100 px-4 py-3 dark:border-dark-700/50 sm:px-6"> |
| 6 | + <p class="text-xs text-gray-400 dark:text-gray-500">{{ t('admin.usage.tokenRanking.subtitle') }}</p> |
| 7 | + <div class="flex items-center gap-3"> |
| 8 | + <span v-if="!loading && items.length > 0" class="text-xs text-gray-400 dark:text-gray-500"> |
| 9 | + {{ t('admin.usage.tokenRanking.userCount', { count: items.length }) }} |
| 10 | + </span> |
16 | 11 | <div class="w-28"> |
17 | 12 | <Select v-model="limit" :options="limitOptions" @change="load" /> |
18 | 13 | </div> |
19 | | - <button |
20 | | - type="button" |
21 | | - class="btn btn-secondary h-8 px-2" |
22 | | - :title="t('common.refresh')" |
23 | | - @click="load" |
24 | | - > |
25 | | - <Icon name="refresh" size="sm" :class="{ 'animate-spin': loading }" /> |
26 | | - </button> |
27 | 14 | </div> |
28 | 15 | </div> |
29 | 16 |
|
30 | 17 | <!-- Table --> |
31 | 18 | <div class="overflow-x-auto"> |
32 | | - <table class="w-full text-sm"> |
33 | | - <thead> |
34 | | - <tr class="border-b border-gray-100 text-xs text-gray-500 dark:border-gray-700 dark:text-gray-400"> |
35 | | - <th class="w-10 py-2 pr-2 text-left font-medium">#</th> |
36 | | - <th class="py-2 pr-2 text-left font-medium">{{ t('admin.usage.tokenRanking.columns.user') }}</th> |
| 19 | + <table class="w-full min-w-max divide-y divide-gray-200 dark:divide-dark-700"> |
| 20 | + <thead class="bg-gray-50 dark:bg-dark-800"> |
| 21 | + <tr> |
| 22 | + <th class="w-16 px-4 py-3 text-left text-xs font-medium uppercase tracking-wider text-gray-500 dark:text-dark-400 sm:px-6">#</th> |
| 23 | + <th class="px-4 py-3 text-left text-xs font-medium uppercase tracking-wider text-gray-500 dark:text-dark-400"> |
| 24 | + {{ t('admin.usage.tokenRanking.columns.user') }} |
| 25 | + </th> |
37 | 26 | <th |
38 | 27 | v-for="col in sortableColumns" |
39 | 28 | :key="col.key" |
40 | | - class="cursor-pointer select-none py-2 pl-2 text-right font-medium hover:text-primary-500" |
41 | | - :class="{ 'text-primary-600 dark:text-primary-400': sortBy === col.key }" |
| 29 | + class="cursor-pointer select-none whitespace-nowrap px-4 py-3 text-right text-xs font-medium uppercase tracking-wider transition-colors hover:bg-gray-100 dark:hover:bg-dark-700" |
| 30 | + :class="sortBy === col.key ? 'text-primary-600 dark:text-primary-400' : 'text-gray-500 dark:text-dark-400'" |
42 | 31 | @click="setSort(col.key)" |
43 | 32 | > |
44 | 33 | {{ t(col.label) }} |
45 | 34 | <span v-if="sortBy === col.key" aria-hidden="true">↓</span> |
46 | 35 | </th> |
47 | 36 | </tr> |
48 | 37 | </thead> |
49 | | - <tbody> |
| 38 | + <tbody class="divide-y divide-gray-200 bg-white dark:divide-dark-700 dark:bg-dark-900"> |
50 | 39 | <tr v-if="loading"> |
51 | | - <td :colspan="sortableColumns.length + 2" class="py-8 text-center"> |
| 40 | + <td :colspan="sortableColumns.length + 2" class="py-12 text-center"> |
52 | 41 | <LoadingSpinner /> |
53 | 42 | </td> |
54 | 43 | </tr> |
55 | | - <tr v-else-if="displayItems.length === 0"> |
56 | | - <td :colspan="sortableColumns.length + 2" class="py-8 text-center text-sm text-gray-400"> |
| 44 | + <tr v-else-if="items.length === 0"> |
| 45 | + <td :colspan="sortableColumns.length + 2" class="py-12 text-center text-sm text-gray-400"> |
57 | 46 | {{ t('admin.dashboard.noDataAvailable') }} |
58 | 47 | </td> |
59 | 48 | </tr> |
60 | 49 | <tr |
61 | | - v-for="(item, index) in displayItems" |
| 50 | + v-for="(item, index) in items" |
62 | 51 | v-else |
63 | 52 | :key="item.user_id" |
64 | | - class="cursor-pointer border-b border-gray-50 transition-colors hover:bg-gray-50 dark:border-gray-800 dark:hover:bg-dark-700/40" |
| 53 | + class="cursor-pointer transition-colors hover:bg-gray-50 dark:hover:bg-dark-700/40" |
| 54 | + :title="t('admin.usage.tokenRanking.rowHint')" |
65 | 55 | @click="$emit('select-user', item.user_id, item.email)" |
66 | 56 | > |
67 | | - <td class="py-2 pr-2 text-gray-400">{{ index + 1 }}</td> |
68 | | - <td class="max-w-[220px] truncate py-2 pr-2 text-gray-700 dark:text-gray-200" :title="item.email"> |
| 57 | + <td class="px-4 py-3 sm:px-6"> |
| 58 | + <span |
| 59 | + v-if="index < 3" |
| 60 | + class="inline-flex h-6 w-6 items-center justify-center rounded-full text-xs font-semibold" |
| 61 | + :class="RANK_BADGE_CLASSES[index]" |
| 62 | + >{{ index + 1 }}</span> |
| 63 | + <span v-else class="inline-block w-6 text-center text-sm tabular-nums text-gray-400">{{ index + 1 }}</span> |
| 64 | + </td> |
| 65 | + <td class="max-w-[260px] truncate px-4 py-3 text-sm font-medium text-gray-700 dark:text-gray-200" :title="item.email"> |
69 | 66 | {{ item.email || `User #${item.user_id}` }} |
| 67 | + <span class="ml-1 font-normal text-gray-400 dark:text-gray-500">#{{ item.user_id }}</span> |
70 | 68 | </td> |
71 | | - <td class="py-2 pl-2 text-right text-gray-500 dark:text-gray-400">{{ item.requests.toLocaleString() }}</td> |
72 | | - <td class="py-2 pl-2 text-right text-gray-500 dark:text-gray-400">{{ fmtTokens(item.input_tokens) }}</td> |
73 | | - <td class="py-2 pl-2 text-right text-gray-500 dark:text-gray-400">{{ fmtTokens(item.output_tokens) }}</td> |
74 | | - <td class="py-2 pl-2 text-right text-gray-500 dark:text-gray-400">{{ fmtTokens(item.cache_tokens) }}</td> |
75 | | - <td class="py-2 pl-2 text-right font-medium text-gray-800 dark:text-gray-100">{{ fmtTokens(item.total_tokens) }}</td> |
76 | | - <td class="py-2 pl-2 text-right text-green-600 dark:text-green-400">${{ fmtCost(item.actual_cost) }}</td> |
| 69 | + <td class="whitespace-nowrap px-4 py-3 text-right text-sm tabular-nums text-gray-500 dark:text-gray-400">{{ item.requests.toLocaleString() }}</td> |
| 70 | + <td class="whitespace-nowrap px-4 py-3 text-right text-sm tabular-nums text-gray-500 dark:text-gray-400">{{ fmtTokens(item.input_tokens) }}</td> |
| 71 | + <td class="whitespace-nowrap px-4 py-3 text-right text-sm tabular-nums text-gray-500 dark:text-gray-400">{{ fmtTokens(item.output_tokens) }}</td> |
| 72 | + <td class="whitespace-nowrap px-4 py-3 text-right text-sm tabular-nums text-gray-500 dark:text-gray-400">{{ fmtTokens(item.cache_tokens) }}</td> |
| 73 | + <td class="whitespace-nowrap px-4 py-3 text-right text-sm font-medium tabular-nums text-gray-900 dark:text-gray-100">{{ fmtTokens(item.total_tokens) }}</td> |
| 74 | + <td class="whitespace-nowrap px-4 py-3 text-right text-sm font-medium tabular-nums text-green-600 dark:text-green-400">${{ fmtCost(item.actual_cost) }}</td> |
77 | 75 | </tr> |
78 | 76 | </tbody> |
79 | 77 | </table> |
80 | 78 | </div> |
81 | | - |
82 | | - <!-- Footer --> |
83 | | - <div v-if="!loading && items.length > 0" class="mt-2 text-right text-xs text-gray-400"> |
84 | | - {{ t('admin.usage.tokenRanking.userCount', { count: displayItems.length }) }} |
85 | | - </div> |
86 | 79 | </div> |
87 | 80 | </template> |
88 | 81 |
|
89 | 82 | <script setup lang="ts"> |
90 | | -import { ref, computed, watch } from 'vue' |
| 83 | +import { ref, watch } from 'vue' |
91 | 84 | import { useI18n } from 'vue-i18n' |
92 | 85 | import { getUserBreakdown, type UserBreakdownParams } from '@/api/admin/dashboard' |
93 | 86 | import { formatCompactNumber, formatCostFixed } from '@/utils/format' |
94 | 87 | import type { UserBreakdownItem } from '@/types' |
95 | 88 | import Select from '@/components/common/Select.vue' |
96 | | -import Icon from '@/components/icons/Icon.vue' |
97 | 89 | import LoadingSpinner from '@/components/common/LoadingSpinner.vue' |
98 | 90 |
|
99 | 91 | const props = defineProps<{ |
@@ -124,19 +116,19 @@ const limitOptions = [ |
124 | 116 | { value: 200, label: 'Top 200' }, |
125 | 117 | ] |
126 | 118 |
|
| 119 | +// 前三名金/银/铜徽章 |
| 120 | +const RANK_BADGE_CLASSES = [ |
| 121 | + 'bg-amber-100 text-amber-700 dark:bg-amber-500/20 dark:text-amber-400', |
| 122 | + 'bg-gray-200 text-gray-600 dark:bg-gray-500/20 dark:text-gray-300', |
| 123 | + 'bg-orange-100 text-orange-700 dark:bg-orange-500/20 dark:text-orange-400', |
| 124 | +] |
| 125 | +
|
127 | 126 | const items = ref<UserBreakdownItem[]>([]) |
128 | 127 | const loading = ref(false) |
129 | 128 | const sortBy = ref<SortKey>('total_tokens') |
130 | 129 | const limit = ref(50) |
131 | | -const search = ref('') |
132 | 130 | let reqSeq = 0 |
133 | 131 |
|
134 | | -const displayItems = computed(() => { |
135 | | - const kw = search.value.trim().toLowerCase() |
136 | | - if (!kw) return items.value |
137 | | - return items.value.filter((u) => (u.email || `user #${u.user_id}`).toLowerCase().includes(kw)) |
138 | | -}) |
139 | | -
|
140 | 132 | const fmtTokens = (v: number) => formatCompactNumber(v) |
141 | 133 | const fmtCost = (v: number) => formatCostFixed(v, 4) |
142 | 134 |
|
|
0 commit comments