@@ -4,6 +4,7 @@ import { NCol, NDatePicker, NIcon, NNumberAnimation, NRow, NSpin, NStatistic } f
44import type { ChartData , ChartOptions } from ' chart.js'
55import { BarElement , CategoryScale , Chart as ChartJS , Legend , LinearScale , Title , Tooltip } from ' chart.js'
66import { Bar } from ' vue-chartjs'
7+ import dayjs from ' dayjs'
78import { t } from ' @/locales'
89import { fetchUserStatistics } from ' @/api'
910import { SvgIcon } from ' @/components/common'
@@ -29,28 +30,45 @@ const chartData: ChartData<'bar'> = reactive({
2930 },
3031 ],
3132})
32-
3333const chartOptions: ChartOptions <' bar' > = {
3434 responsive: true ,
3535}
36-
3736const summary = ref ({
3837 promptTokens: 0 ,
3938 completionTokens: 0 ,
4039 totalTokens: 0 ,
4140})
4241const loading = ref (false )
43- const range = ref <[ number , number ]> ([
44- new Date ().getTime () - 30 * 86400 * 1000 ,
45- new Date ().getTime (),
42+ const range = ref ([
43+ dayjs ().subtract ( 30 , ' day ' ). startOf ( ' day ' ). valueOf () ,
44+ dayjs ().endOf ( ' day ' ). valueOf (),
4645])
46+ const rangeShortcuts: { [index : string ]: [number , number ] } = {}
47+ // last month
48+ rangeShortcuts [t (' setting.statisticsPeriodLastMonth' )] = [
49+ dayjs ().subtract (1 , ' month' ).startOf (' month' ).valueOf (),
50+ dayjs ().subtract (1 , ' month' ).endOf (' month' ).valueOf (),
51+ ]
52+ // current month
53+ rangeShortcuts [t (' setting.statisticsPeriodCurrentMonth' )] = [
54+ dayjs ().startOf (' month' ).valueOf (),
55+ dayjs ().endOf (' month' ).valueOf (),
56+ ]
57+ // last 30 days
58+ rangeShortcuts [t (' setting.statisticsPeriodLast30Days' )] = [
59+ dayjs ().subtract (30 , ' day' ).startOf (' day' ).valueOf (),
60+ dayjs ().endOf (' day' ).valueOf (),
61+ ]
4762
4863const showChart = ref (true )
4964
5065async function fetchStatistics() {
5166 try {
5267 loading .value = true
53- const { data } = await fetchUserStatistics (... range .value )
68+ const { data } = await fetchUserStatistics (
69+ dayjs (range .value [0 ]).startOf (' day' ).valueOf (),
70+ dayjs (range .value [1 ]).endOf (' day' ).valueOf (),
71+ )
5472
5573 if (Object .keys (data .chartData ).length ) {
5674 summary .value .promptTokens = data .promptTokens
@@ -87,8 +105,8 @@ onMounted(() => {
87105 <div class =" flex-1" >
88106 <NDatePicker
89107 v-model:value =" range"
90- type =" datetimerange "
91- clearable
108+ type =" daterange "
109+ :shortcuts = " rangeShortcuts "
92110 @update:value =" fetchStatistics"
93111 />
94112 </div >
@@ -103,7 +121,7 @@ onMounted(() => {
103121 <SvgIcon class =" text-lg" icon =" ri-chat-upload-line" />
104122 </NIcon >
105123 </template >
106- <NNumberAnimation :from = " 0 " :to =" summary.promptTokens" />
124+ <NNumberAnimation :duration = " 1000 " :to =" summary.promptTokens" />
107125 </NStatistic >
108126 </NCol >
109127 <NCol :span =" 8" class =" text-center" >
@@ -113,7 +131,7 @@ onMounted(() => {
113131 <SvgIcon class =" text-lg" icon =" ri-chat-download-line" />
114132 </NIcon >
115133 </template >
116- <NNumberAnimation :from = " 0 " :to =" summary.completionTokens" />
134+ <NNumberAnimation :duration = " 1000 " :to =" summary.completionTokens" />
117135 </NStatistic >
118136 </NCol >
119137 <NCol :span =" 8" class =" text-center" >
@@ -123,7 +141,7 @@ onMounted(() => {
123141 <SvgIcon class =" text-lg" icon =" ri-question-answer-line" />
124142 </NIcon >
125143 </template >
126- <NNumberAnimation :from = " 0 " :to =" summary.totalTokens" />
144+ <NNumberAnimation :duration = " 1000 " :to =" summary.totalTokens" />
127145 </NStatistic >
128146 </NCol >
129147 </NRow >
0 commit comments