11<script setup lang='ts'>
2+ import type { Ref } from ' vue'
23import { nextTick , onMounted , reactive , ref } from ' vue'
3- import { NCol , NDatePicker , NIcon , NNumberAnimation , NRow , NSpin , NStatistic } from ' naive-ui'
4+ import { NCol , NDatePicker , NIcon , NNumberAnimation , NRow , NSelect , NSpin , NStatistic } from ' naive-ui'
45import type { ChartData , ChartOptions } from ' chart.js'
56import { BarElement , CategoryScale , Chart as ChartJS , Legend , LinearScale , Title , Tooltip } from ' chart.js'
67import { Bar } from ' vue-chartjs'
78import dayjs from ' dayjs'
9+ import type { UserInfo } from ' ./model'
810import { t } from ' @/locales'
9- import { fetchUserStatistics } from ' @/api'
11+ import { fetchGetUsers , fetchUserStatistics } from ' @/api'
1012import { SvgIcon } from ' @/components/common'
13+ import { useUserStore } from ' @/store'
1114
1215ChartJS .register (Title , Tooltip , Legend , BarElement , CategoryScale , LinearScale )
1316
17+ const userStore = useUserStore ()
18+
1419const chartData: ChartData <' bar' > = reactive ({
1520 labels: [],
1621 datasets: [
@@ -38,6 +43,10 @@ const summary = ref({
3843 completionTokens: 0 ,
3944 totalTokens: 0 ,
4045})
46+
47+ const usersOptions: Ref <{ label: string ; filter: string ; value: string }[]> = ref ([])
48+ const user: Ref <string | null > = ref (null )
49+
4150const loading = ref (false )
4251const range: any = ref ([
4352 dayjs ().subtract (30 , ' day' ).startOf (' day' ).valueOf (),
@@ -66,6 +75,7 @@ async function fetchStatistics() {
6675 try {
6776 loading .value = true
6877 const { data } = await fetchUserStatistics (
78+ user .value as string ,
6979 dayjs (range .value [0 ]).startOf (' day' ).valueOf (),
7080 dayjs (range .value [1 ]).endOf (' day' ).valueOf (),
7181 )
@@ -91,8 +101,25 @@ async function fetchStatistics() {
91101 }
92102}
93103
104+ async function fetchUsers() {
105+ const result = await fetchGetUsers (1 , 10000 )
106+ result .data .users .forEach ((user : UserInfo ) => {
107+ usersOptions .value .push ({
108+ label: ` ${user .email } ` ,
109+ value: ` ${user ._id } ` ,
110+ filter: ` ${user .email } ${user .remark } ` ,
111+ })
112+ })
113+ }
114+
115+ function filter(pattern : string , option : object ): boolean {
116+ const a = option as { label: string ; filter: string ; value: string }
117+ return ! a .filter ? false : a .filter .includes (pattern )
118+ }
119+
94120onMounted (() => {
95121 fetchStatistics ()
122+ fetchUsers ()
96123})
97124 </script >
98125
@@ -101,7 +128,16 @@ onMounted(() => {
101128 <div class =" p-4 space-y-5 min-h-[200px]" >
102129 <div class =" space-y-6" >
103130 <div class =" flex items-center space-x-4" >
104- <span class =" flex-shrink-0 w-[100px]" >{{ $t('setting.statisticsPeriod') }}</span >
131+ <NSelect
132+ v-if =" userStore.userInfo.root"
133+ v-model:value =" user"
134+ style =" width : 250px "
135+ filterable
136+ :filter =" filter"
137+ placeholder =" Email or remark"
138+ :options =" usersOptions"
139+ @update-value =" (value) => { user = value; fetchStatistics() }"
140+ />
105141 <div class =" flex-1" >
106142 <NDatePicker
107143 v-model:value =" range"
0 commit comments