Spaces:
Runtime error
Runtime error
File size: 5,097 Bytes
41af422 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 |
<script setup lang='ts'>
import { nextTick, onMounted, reactive, ref } from 'vue'
import { NCol, NDatePicker, NIcon, NNumberAnimation, NRow, NSpin, NStatistic } from 'naive-ui'
import type { ChartData, ChartOptions } from 'chart.js'
import { BarElement, CategoryScale, Chart as ChartJS, Legend, LinearScale, Title, Tooltip } from 'chart.js'
import { Bar } from 'vue-chartjs'
import dayjs from 'dayjs'
import { t } from '@/locales'
import { fetchUserStatistics } from '@/api'
import { SvgIcon } from '@/components/common'
ChartJS.register(Title, Tooltip, Legend, BarElement, CategoryScale, LinearScale)
const chartData: ChartData<'bar'> = reactive({
labels: [],
datasets: [
{
label: t('setting.statisticsPrompt'),
data: [],
borderColor: '#a1dc95',
backgroundColor: '#a1dc95',
stack: 'Usage',
},
{
label: t('setting.statisticsCompletion'),
data: [],
borderColor: '#6d6e7e',
backgroundColor: '#6d6e7e',
stack: 'Usage',
},
],
})
const chartOptions: ChartOptions<'bar'> = {
responsive: true,
}
const summary = ref({
promptTokens: 0,
completionTokens: 0,
totalTokens: 0,
})
const loading = ref(false)
const range: any = ref([
dayjs().subtract(30, 'day').startOf('day').valueOf(),
dayjs().endOf('day').valueOf(),
])
const rangeShortcuts: { [index: string]: [number, number] } = {}
// last month
rangeShortcuts[t('setting.statisticsPeriodLastMonth')] = [
dayjs().subtract(1, 'month').startOf('month').valueOf(),
dayjs().subtract(1, 'month').endOf('month').valueOf(),
]
// current month
rangeShortcuts[t('setting.statisticsPeriodCurrentMonth')] = [
dayjs().startOf('month').valueOf(),
dayjs().endOf('month').valueOf(),
]
// last 30 days
rangeShortcuts[t('setting.statisticsPeriodLast30Days')] = [
dayjs().subtract(30, 'day').startOf('day').valueOf(),
dayjs().endOf('day').valueOf(),
]
const showChart = ref(true)
async function fetchStatistics() {
try {
loading.value = true
const { data } = await fetchUserStatistics(
dayjs(range.value[0]).startOf('day').valueOf(),
dayjs(range.value[1]).endOf('day').valueOf(),
)
if (Object.keys(data.chartData).length) {
summary.value.promptTokens = data.promptTokens
summary.value.completionTokens = data.completionTokens
summary.value.totalTokens = data.totalTokens
chartData.labels = data.chartData.map((item: any) => item._id)
chartData.datasets[0].data = data.chartData.map((item: any) => item.promptTokens)
chartData.datasets[1].data = data.chartData.map((item: any) => item.completionTokens)
// todo: don't know why data change won't trigger chart re-render, dirty hack
showChart.value = false
nextTick(() => {
showChart.value = true
})
}
}
finally {
loading.value = false
}
}
onMounted(() => {
fetchStatistics()
})
</script>
<template>
<NSpin :show="loading">
<div class="p-4 space-y-5 min-h-[200px]">
<div class="space-y-6">
<div class="flex items-center space-x-4">
<span class="flex-shrink-0 w-[100px]">{{ $t('setting.statisticsPeriod') }}</span>
<div class="flex-1">
<NDatePicker
v-model:value="range"
type="daterange"
:shortcuts="rangeShortcuts"
@update:value="fetchStatistics"
/>
</div>
</div>
<div class="flex items-center space-x-4">
<NRow>
<NCol :span="8" class="text-center">
<NStatistic :label="t('setting.statisticsPrompt')">
<template #prefix>
<NIcon>
<SvgIcon class="text-lg" icon="ri-chat-upload-line" />
</NIcon>
</template>
<NNumberAnimation :duration="1000" :to="summary.promptTokens" />
</NStatistic>
</NCol>
<NCol :span="8" class="text-center">
<NStatistic :label="t('setting.statisticsCompletion')">
<template #prefix>
<NIcon>
<SvgIcon class="text-lg" icon="ri-chat-download-line" />
</NIcon>
</template>
<NNumberAnimation :duration="1000" :to="summary.completionTokens" />
</NStatistic>
</NCol>
<NCol :span="8" class="text-center">
<NStatistic :label="t('setting.statisticsTotal')">
<template #prefix>
<NIcon>
<SvgIcon class="text-lg" icon="ri-question-answer-line" />
</NIcon>
</template>
<NNumberAnimation :duration="1000" :to="summary.totalTokens" />
</NStatistic>
</NCol>
</NRow>
</div>
<Bar
v-if="showChart && chartData.labels?.length"
ref="statisticsChart"
style="aspect-ratio: 3/2;"
:options="chartOptions"
:data="chartData"
/>
</div>
</div>
</NSpin>
</template>
<style>
</style>
|