Skip to content

Open source file-system and knowledge based agent template. Build AI agents that stay up to date with your knowledge base

improve stats

Hugo Richardd4d8815922ca
diff --git a/apps/chat/app/composables/useModels.ts b/apps/chat/app/composables/useModels.ts
index b208ca9..072f0a2 100644
--- a/apps/chat/app/composables/useModels.ts
+++ b/apps/chat/app/composables/useModels.ts
@@ -15,7 +15,7 @@ export function formatModelName(modelId: string): string {
export function useModels() {
const models = [
- 'moonshotai/kimi-k2-turbo',
+ 'moonshotai/kimi-k2.5',
'anthropic/claude-opus-4.5',
'openai/gpt-5.1-instant',
'google/gemini-3-flash',
diff --git a/apps/chat/app/pages/admin/stats.vue b/apps/chat/app/pages/admin/stats.vue
index 4c50218..f67b1eb 100644
--- a/apps/chat/app/pages/admin/stats.vue
+++ b/apps/chat/app/pages/admin/stats.vue
@@ -1,5 +1,4 @@
<script setup lang="ts">
-const toast = useToast()
const periodOptions = [
{ label: '7 days', value: 7 },
@@ -8,16 +7,36 @@ const periodOptions = [
]
const selectedPeriod = ref(30)
+const selectedSources = ref<string[]>([])
+const selectedModels = ref<string[]>([])
+const chartMetric = ref<'tokens' | 'messages'>('tokens')
const { data: stats, refresh, status } = await useFetch<GlobalStatsResponse>('/api/stats', {
- query: computed(() => ({ days: selectedPeriod.value })),
- watch: [selectedPeriod],
+ query: computed(() => ({
+ days: selectedPeriod.value,
+ sources: selectedSources.value.length > 0 ? selectedSources.value.join(',') : undefined,
+ models: selectedModels.value.length > 0 ? selectedModels.value.join(',') : undefined,
+ })),
+ watch: [selectedPeriod, selectedSources, selectedModels],
})
// Track if we're refreshing (vs initial load) to avoid flickering
const isRefreshing = computed(() => status.value === 'pending' && stats.value !== null)
-const isComputing = ref(false)
+const hasFilters = computed(() => selectedSources.value.length > 0 || selectedModels.value.length > 0)
+
+function clearFilters() {
+ selectedSources.value = []
+ selectedModels.value = []
+}
+
+const sourceOptions = computed(() =>
+ (stats.value?.availableSources ?? []).map(s => ({ label: s.replace('-', ' '), value: s })),
+)
+
+const modelOptions = computed(() =>
+ (stats.value?.availableModels ?? []).map(m => ({ label: formatModelName(m), value: m })),
+)
function formatNumber(num: number): string {
return num.toLocaleString()
@@ -40,6 +59,13 @@ function formatModelName(modelId: string): string {
return name.split('-').map(w => w.charAt(0).toUpperCase() + w.slice(1)).join(' ')
}
+function formatCost(amount: number): string {
+ if (amount >= 1) return `$${amount.toFixed(2)}`
+ if (amount >= 0.01) return `$${amount.toFixed(3)}`
+ if (amount === 0) return '$0.00'
+ return `$${amount.toFixed(4)}`
+}
+
// Feedback score color
function getScoreColor(score: number | null): string {
if (score === null) return 'text-muted'
@@ -76,65 +102,94 @@ function getTrendIcon(trend: number | null): string {
return 'i-lucide-minus'
}
-const chartData = computed(() => {
- if (!stats.value?.daily) return []
+// Source color map
+const sourceColorMap: Record<string, string> = {
+ 'web': 'bg-primary',
+ 'github-bot': 'bg-emerald-500',
+ 'discord-bot': 'bg-violet-500',
+ 'sdk': 'bg-amber-500',
+}
- const byDate = new Map<string, { input: number, output: number }>()
+function getSourceColor(source: string): string {
+ return sourceColorMap[source] ?? 'bg-gray-400'
+}
+
+// Stacked-by-source chart data
+const chartDataBySource = computed(() => {
+ if (!stats.value?.dailyBySource) return []
+
+ const byDate = new Map<string, Map<string, { tokens: number, messages: number }>>()
- for (const day of stats.value.daily) {
- const existing = byDate.get(day.date) ?? { input: 0, output: 0 }
- existing.input += day.inputTokens
- existing.output += day.outputTokens
- byDate.set(day.date, existing)
+ for (const entry of stats.value.dailyBySource) {
+ if (!byDate.has(entry.date)) byDate.set(entry.date, new Map())
+ const sourceMap = byDate.get(entry.date)!
+ const existing = sourceMap.get(entry.source) ?? { tokens: 0, messages: 0 }
+ existing.tokens += entry.inputTokens + entry.outputTokens
+ existing.messages += entry.messageCount
+ sourceMap.set(entry.source, existing)
}
return Array.from(byDate.entries())
- .map(([date, tokens]) => ({
- date: date.slice(5), // MM-DD format
- fullDate: date, // Keep full date for tooltip
- input: tokens.input,
- output: tokens.output,
+ .map(([date, sourceMap]) => ({
+ date: date.slice(5),
+ fullDate: date,
+ sources: Object.fromEntries(sourceMap),
}))
.sort((a, b) => a.fullDate.localeCompare(b.fullDate))
})
+const chartSources = computed(() => {
+ const sources = new Set<string>()
+ for (const day of chartDataBySource.value) {
+ for (const src of Object.keys(day.sources)) sources.add(src)
+ }
+ return Array.from(sources).sort()
+})
+
+const chartMax = computed(() => {
+ let max = 0
+ for (const day of chartDataBySource.value) {
+ let total = 0
+ for (const s of Object.values(day.sources)) {
+ total += chartMetric.value === 'tokens' ? s.tokens : s.messages
+ }
+ if (total > max) max = total
+ }
+ return max || 1
+})
+
// Get evenly spaced date labels for the x-axis (5 labels max)
const chartDateLabels = computed(() => {
- if (chartData.value.length === 0) return []
- if (chartData.value.length <= 5) {
- return chartData.value.map((d, i) => ({ index: i, label: d.date }))
+ if (chartDataBySource.value.length === 0) return []
+ if (chartDataBySource.value.length <= 5) {
+ return chartDataBySource.value.map((d, i) => ({ index: i, label: d.date }))
}
- const count = chartData.value.length
- const step = (count - 1) / 4 // 5 labels: start, 3 middle, end
+ const count = chartDataBySource.value.length
+ const step = (count - 1) / 4
return [0, 1, 2, 3, 4].map(i => {
const index = Math.round(i * step)
- return { index, label: chartData.value[index]?.date ?? '' }
+ return { index, label: chartDataBySource.value[index]?.date ?? '' }
})
})
-async function triggerCompute() {
- try {
- isComputing.value = true
- await $fetch('/api/stats/compute', { method: 'POST' })
- toast.add({
- title: 'Stats computation started',
- description: 'The workflow has been triggered.',
- icon: 'i-lucide-check',
- })
- setTimeout(() => refresh(), 2000)
- } catch (error) {
- toast.add({
- title: 'Error',
- description: error instanceof Error ? error.message : 'Failed to compute stats',
- color: 'error',
- icon: 'i-lucide-alert-circle',
- })
- } finally {
- isComputing.value = false
+// Peak hours
+const peakHoursData = computed(() => stats.value?.hourlyDistribution ?? [])
+const peakHoursMax = computed(() => {
+ let max = 0
+ for (const h of peakHoursData.value) {
+ if (h.messageCount > max) max = h.messageCount
}
+ return max || 1
+})
+
+// Model cost lookup from estimatedCost
+function getModelCost(modelId: string): number | null {
+ const entry = stats.value?.estimatedCost?.byModel?.find(m => m.model === modelId)
+ return entry ? entry.totalCost : null
}
+
</script>
<template>
@@ -166,29 +221,16 @@ async function triggerCompute() {
{{ option.label }}
</UButton>
</div>
- <UDropdownMenu
- :items="[
- {
- label: 'Refresh data',
- icon: 'i-lucide-refresh-cw',
- onClick: () => refresh(),
- },
- {
- label: 'Recompute stats',
- icon: 'i-lucide-calculator',
- description: 'Run workflow (may take a few seconds)',
- onClick: triggerCompute,
- },
- ]"
- >
+ <UTooltip text="Refresh data">
<UButton
- icon="i-lucide-more-vertical"
+ icon="i-lucide-refresh-cw"
color="neutral"
variant="ghost"
size="xs"
- :loading="status === 'pending' || isComputing"
+ :loading="isRefreshing"
+ @click="refresh()"
/>
- </UDropdownMenu>
+ </UTooltip>
</div>
</div>
</header>
@@ -202,6 +244,40 @@ async function triggerCompute() {
class="transition-opacity duration-200"
:class="isRefreshing ? 'opacity-50 pointer-events-none' : 'opacity-100'"
>
+ <!-- Filter Bar -->
+ <div v-if="sourceOptions.length > 0 || modelOptions.length > 0" class="flex items-center gap-3 mb-6 flex-wrap">
+ <USelectMenu
+ v-if="sourceOptions.length > 0"
+ v-model="selectedSources"
+ :items="sourceOptions"
+ multiple
+ placeholder="All sources"
+ class="w-44"
+ size="xs"
+ value-key="value"
+ />
+ <USelectMenu
+ v-if="modelOptions.length > 0"
+ v-model="selectedModels"
+ :items="modelOptions"
+ multiple
+ placeholder="All models"
+ class="w-52"
+ size="xs"
+ value-key="value"
+ />
+ <UButton
+ v-if="hasFilters"
+ size="xs"
+ color="neutral"
+ variant="ghost"
+ icon="i-lucide-x"
+ @click="clearFilters"
+ >
+ Clear
+ </UButton>
+ </div>
+
<!-- KPI Cards with Trends -->
<div class="grid grid-cols-2 sm:grid-cols-3 lg:grid-cols-6 gap-4 mb-8">
<div class="rounded-lg border border-default bg-elevated/50 p-4">
@@ -250,10 +326,13 @@ async function triggerCompute() {
</div>
<div class="rounded-lg border border-default bg-elevated/50 p-4">
<p class="text-xs text-muted mb-1">
- Models
+ Est. Cost
</p>
<p class="text-2xl font-semibold text-highlighted tabular-nums">
- {{ stats.byModel.length }}
+ {{ formatCost(stats.estimatedCost?.total ?? 0) }}
+ </p>
+ <p class="text-[11px] text-muted mt-1">
+ {{ stats.byModel.length }} model{{ stats.byModel.length !== 1 ? 's' : '' }}
</p>
</div>
<div class="rounded-lg border border-default bg-elevated/50 p-4">
@@ -274,31 +353,51 @@ async function triggerCompute() {
</div>
</div>
- <!-- Token Usage Chart -->
+ <!-- Usage Chart (Stacked by Source) -->
<section class="mb-10">
- <h2 class="text-sm font-medium text-highlighted mb-3">
- Token Usage Over Time
- </h2>
- <div v-if="chartData.length > 0" class="rounded-lg border border-default bg-elevated/50 p-4 overflow-hidden">
+ <div class="flex items-center justify-between mb-3">
+ <h2 class="text-sm font-medium text-highlighted">
+ Usage Over Time
+ </h2>
+ <div class="flex items-center gap-1">
+ <UButton
+ size="xs"
+ :color="chartMetric === 'tokens' ? 'primary' : 'neutral'"
+ :variant="chartMetric === 'tokens' ? 'solid' : 'ghost'"
+ @click="chartMetric = 'tokens'"
+ >
+ Tokens
+ </UButton>
+ <UButton
+ size="xs"
+ :color="chartMetric === 'messages' ? 'primary' : 'neutral'"
+ :variant="chartMetric === 'messages' ? 'solid' : 'ghost'"
+ @click="chartMetric = 'messages'"
+ >
+ Messages
+ </UButton>
+ </div>
+ </div>
+ <div v-if="chartDataBySource.length > 0" class="rounded-lg border border-default bg-elevated/50 p-4 overflow-hidden">
<div class="h-40 flex items-end gap-0.5 px-4">
<div
- v-for="(day, index) in chartData"
+ v-for="(day, index) in chartDataBySource"
:key="index"
class="flex-1 flex flex-col justify-end group"
>
<UTooltip
- :text="`${day.fullDate} — In: ${formatNumber(day.input)} / Out: ${formatNumber(day.output)}`"
+ :text="`${day.fullDate} — ${chartMetric === 'tokens' ? formatCompactNumber(Object.values(day.sources).reduce((s, v) => s + v.tokens, 0)) + ' tokens' : Object.values(day.sources).reduce((s, v) => s + v.messages, 0) + ' msgs'}`"
:content="{ side: 'top', sideOffset: 6 }"
>
- <div class="w-full flex flex-col gap-px relative before:absolute before:inset-x-0 before:bottom-full before:h-40">
- <div
- class="w-full bg-primary rounded-t-sm transition-opacity group-hover:opacity-80"
- :style="{ height: `${Math.max(2, (day.output / Math.max(...chartData.map(d => d.input + d.output))) * 100)}px` }"
- />
- <div
- class="w-full bg-primary/40 rounded-b-sm transition-opacity group-hover:opacity-80"
- :style="{ height: `${Math.max(2, (day.input / Math.max(...chartData.map(d => d.input + d.output))) * 100)}px` }"
- />
+ <div class="w-full flex flex-col-reverse relative before:absolute before:inset-x-0 before:bottom-full before:h-40">
+ <template v-for="src in chartSources" :key="src">
+ <div
+ v-if="day.sources[src]"
+ class="w-full transition-opacity group-hover:opacity-80 first:rounded-b-sm last:rounded-t-sm"
+ :class="getSourceColor(src)"
+ :style="{ height: `${Math.max(1, ((chartMetric === 'tokens' ? day.sources[src].tokens : day.sources[src].messages) / chartMax) * 140)}px` }"
+ />
+ </template>
</div>
</UTooltip>
</div>
@@ -308,19 +407,15 @@ async function triggerCompute() {
v-for="label in chartDateLabels"
:key="label.index"
class="absolute text-[10px] text-muted transform -translate-x-1/2"
- :style="{ left: `${(label.index / (chartData.length - 1)) * 100}%` }"
+ :style="{ left: `${(label.index / Math.max(chartDataBySource.length - 1, 1)) * 100}%` }"
>
{{ label.label }}
</span>
</div>
<div class="flex items-center justify-center gap-4 mt-3 pt-3 border-t border-default text-[10px] text-muted">
- <span class="flex items-center gap-1">
- <span class="inline-block size-2 rounded bg-primary/40" />
- Input
- </span>
- <span class="flex items-center gap-1">
- <span class="inline-block size-2 rounded bg-primary" />
- Output
+ <span v-for="src in chartSources" :key="src" class="flex items-center gap-1">
+ <span class="inline-block size-2 rounded" :class="getSourceColor(src)" />
+ {{ src.replace('-', ' ') }}
</span>
</div>
</div>
@@ -331,6 +426,37 @@ async function triggerCompute() {
</div>
</section>
+ <!-- Peak Hours -->
+ <section v-if="peakHoursData.some(h => h.messageCount > 0)" class="mb-10">
+ <h2 class="text-sm font-medium text-highlighted mb-3">
+ Peak Hours (UTC)
+ </h2>
+ <div class="rounded-lg border border-default bg-elevated/50 p-4 overflow-hidden">
+ <div class="h-24 flex items-end gap-px px-2">
+ <div
+ v-for="h in peakHoursData"
+ :key="h.hour"
+ class="flex-1 group"
+ >
+ <UTooltip
+ :text="`${String(h.hour).padStart(2, '0')}:00 — ${h.messageCount} msgs, ${formatCompactNumber(h.totalTokens)} tokens`"
+ :content="{ side: 'top', sideOffset: 4 }"
+ >
+ <div
+ class="w-full bg-primary/60 rounded-t-sm transition-opacity group-hover:bg-primary relative before:absolute before:inset-x-0 before:bottom-full before:h-24"
+ :style="{ height: `${Math.max(2, (h.messageCount / peakHoursMax) * 80)}px` }"
+ />
+ </UTooltip>
+ </div>
+ </div>
+ <div class="flex justify-between px-2 mt-1.5">
+ <span v-for="label in ['00', '06', '12', '18', '23']" :key="label" class="text-[10px] text-muted">
+ {{ label }}
+ </span>
+ </div>
+ </div>
+ </section>
+
<!-- Two column layout for tables -->
<div class="grid md:grid-cols-2 gap-8 mb-8 items-start">
<!-- Top Users -->
@@ -415,7 +541,7 @@ async function triggerCompute() {
<td class="px-4 py-2.5">
<div class="flex items-center gap-2">
<UIcon
- :name="source.source === 'web' ? 'i-lucide-globe' : source.source === 'github-bot' ? 'i-simple-icons-github' : 'i-lucide-code'"
+ :name="source.source === 'web' ? 'i-lucide-globe' : source.source === 'github-bot' ? 'i-simple-icons-github' : source.source === 'discord-bot' ? 'i-simple-icons-discord' : 'i-lucide-code'"
class="size-4 text-muted"
/>
<span class="text-highlighted capitalize text-xs">{{ source.source.replace('-', ' ') }}</span>
@@ -452,6 +578,9 @@ async function triggerCompute() {
<th class="text-right font-medium px-3 py-2.5">
Tokens
</th>
+ <th class="text-right font-medium px-3 py-2.5">
+ Cost
+ </th>
<th class="text-right font-medium px-3 py-2.5">
Avg Response
</th>
@@ -472,6 +601,9 @@ async function triggerCompute() {
<td class="text-right text-muted tabular-nums px-3 py-2.5">
{{ formatCompactNumber(model.inputTokens + model.outputTokens) }}
</td>
+ <td class="text-right text-muted tabular-nums px-3 py-2.5">
+ {{ getModelCost(model.model) !== null ? formatCost(getModelCost(model.model)!) : '—' }}
+ </td>
<td class="text-right text-muted tabular-nums px-3 py-2.5">
{{ formatDuration(model.avgDurationMs) }}
</td>
diff --git a/apps/chat/server/api/stats/index.get.ts b/apps/chat/server/api/stats/index.get.ts
index 95d28f2..a7a978b 100644
--- a/apps/chat/server/api/stats/index.get.ts
+++ b/apps/chat/server/api/stats/index.get.ts
@@ -1,5 +1,5 @@
import { db, schema } from '@nuxthub/db'
-import { desc, and, gte, lt, eq, countDistinct } from 'drizzle-orm'
+import { desc, and, gte, lt, eq } from 'drizzle-orm'
/**
* GET /api/stats
@@ -7,12 +7,16 @@ import { desc, and, gte, lt, eq, countDistinct } from 'drizzle-orm'
*
* Query params:
* - days: number of days to look back (default: 30)
+ * - sources: comma-separated source filter (e.g. web,github-bot)
+ * - models: comma-separated model filter
*/
export default defineEventHandler(async (event) => {
await requireAdmin(event)
const query = getQuery(event)
const days = Math.min(Math.max(Number(query.days) || 30, 1), 365)
+ const sourcesFilter = typeof query.sources === 'string' && query.sources ? query.sources.split(',') : null
+ const modelsFilter = typeof query.models === 'string' && query.models ? query.models.split(',') : null
// Current period
const endDate = new Date()
@@ -112,7 +116,7 @@ export default defineEventHandler(async (event) => {
}
}
- const apiUsageData = await db
+ const apiUsageDataRaw = await db
.select({
source: schema.apiUsage.source,
model: schema.apiUsage.model,
@@ -125,6 +129,31 @@ export default defineEventHandler(async (event) => {
.where(gte(schema.apiUsage.createdAt, startDate))
.orderBy(desc(schema.apiUsage.createdAt))
+ // Collect available sources and models from unfiltered data (for dropdown options)
+ const allSources = new Set<string>(['web'])
+ const allModels = new Set<string>()
+ for (const msg of messagesWithStats) {
+ if (msg.model) allModels.add(msg.model)
+ }
+ for (const usage of apiUsageDataRaw) {
+ allSources.add(usage.source)
+ if (usage.model) allModels.add(usage.model)
+ }
+ const availableSources = Array.from(allSources).sort()
+ const availableModels = Array.from(allModels).sort()
+
+ // Apply filters
+ const filteredMessages = messagesWithStats.filter((msg) => {
+ if (sourcesFilter && !sourcesFilter.includes('web')) return false
+ if (modelsFilter && !modelsFilter.includes(msg.model ?? 'unknown')) return false
+ return true
+ })
+ const apiUsageData = apiUsageDataRaw.filter((usage) => {
+ if (sourcesFilter && !sourcesFilter.includes(usage.source)) return false
+ if (modelsFilter && !modelsFilter.includes(usage.model ?? 'unknown')) return false
+ return true
+ })
+
let totalMessages = 0
let totalInputTokens = 0
let totalOutputTokens = 0
@@ -164,9 +193,15 @@ export default defineEventHandler(async (event) => {
totalDurationMs: number
}>()
+ // Daily by source aggregation
+ const dailyBySourceMap = new Map<string, Map<string, { messageCount: number, inputTokens: number, outputTokens: number }>>()
+
+ // Hourly distribution (0-23)
+ const hourlyBuckets: Array<{ messageCount: number, totalTokens: number }> = Array.from({ length: 24 }, () => ({ messageCount: 0, totalTokens: 0 }))
+
bySourceMap.set('web', { requests: 0, inputTokens: 0, outputTokens: 0, totalDurationMs: 0 })
- for (const msg of messagesWithStats) {
+ for (const msg of filteredMessages) {
totalMessages++
totalInputTokens += msg.inputTokens ?? 0
totalOutputTokens += msg.outputTokens ?? 0
@@ -212,6 +247,20 @@ export default defineEventHandler(async (event) => {
webStats.outputTokens += msg.outputTokens ?? 0
webStats.totalDurationMs += msg.durationMs ?? 0
+ // Daily by source
+ if (!dailyBySourceMap.has(dateStr)) dailyBySourceMap.set(dateStr, new Map())
+ const daySourceMap = dailyBySourceMap.get(dateStr)!
+ const daySourceStats = daySourceMap.get('web') ?? { messageCount: 0, inputTokens: 0, outputTokens: 0 }
+ daySourceStats.messageCount++
+ daySourceStats.inputTokens += msg.inputTokens ?? 0
+ daySourceStats.outputTokens += msg.outputTokens ?? 0
+ daySourceMap.set('web', daySourceStats)
+
+ // Hourly distribution
+ const hour = msg.createdAt.getUTCHours()
+ hourlyBuckets[hour]!.messageCount++
+ hourlyBuckets[hour]!.totalTokens += (msg.inputTokens ?? 0) + (msg.outputTokens ?? 0)
+
// Aggregate by user
const userId = chatUserMap.get(msg.chatId)
if (userId) {
@@ -248,6 +297,38 @@ export default defineEventHandler(async (event) => {
modelStats.outputTokens += usage.outputTokens ?? 0
modelStats.totalDurationMs += usage.durationMs ?? 0
byModelMap.set(modelKey, modelStats)
+
+ // Also add to dailyMap so the chart includes API usage
+ const dateStr = usage.createdAt.toISOString().split('T')[0]!
+ if (!dailyMap.has(dateStr)) {
+ dailyMap.set(dateStr, new Map())
+ }
+ const dayModelMap = dailyMap.get(dateStr)!
+ const dayModelStats = dayModelMap.get(modelKey) ?? {
+ messageCount: 0,
+ inputTokens: 0,
+ outputTokens: 0,
+ totalDurationMs: 0,
+ }
+ dayModelStats.messageCount++
+ dayModelStats.inputTokens += usage.inputTokens ?? 0
+ dayModelStats.outputTokens += usage.outputTokens ?? 0
+ dayModelStats.totalDurationMs += usage.durationMs ?? 0
+ dayModelMap.set(modelKey, dayModelStats)
+
+ // Daily by source for API usage
+ if (!dailyBySourceMap.has(dateStr)) dailyBySourceMap.set(dateStr, new Map())
+ const daySourceMap2 = dailyBySourceMap.get(dateStr)!
+ const daySourceStats2 = daySourceMap2.get(source) ?? { messageCount: 0, inputTokens: 0, outputTokens: 0 }
+ daySourceStats2.messageCount++
+ daySourceStats2.inputTokens += usage.inputTokens ?? 0
+ daySourceStats2.outputTokens += usage.outputTokens ?? 0
+ daySourceMap2.set(source, daySourceStats2)
+
+ // Hourly distribution for API usage
+ const hour = usage.createdAt.getUTCHours()
+ hourlyBuckets[hour]!.messageCount++
+ hourlyBuckets[hour]!.totalTokens += (usage.inputTokens ?? 0) + (usage.outputTokens ?? 0)
}
// Calculate previous period totals for trends
@@ -320,18 +401,74 @@ export default defineEventHandler(async (event) => {
)
.sort((a, b) => a.date.localeCompare(b.date))
+ // Generate all dates in the period for zero-filling
+ const allDates: string[] = []
+ const cursor = new Date(startDate)
+ cursor.setHours(0, 0, 0, 0)
+ const endDay = new Date(endDate)
+ endDay.setHours(0, 0, 0, 0)
+ while (cursor <= endDay) {
+ allDates.push(cursor.toISOString().split('T')[0]!)
+ cursor.setDate(cursor.getDate() + 1)
+ }
+
+ // Zero-fill the per-model daily array
+ const dailyDates = new Set(daily.map(d => d.date))
+ for (const date of allDates) {
+ if (!dailyDates.has(date)) {
+ daily.push({ date, model: '', messageCount: 0, inputTokens: 0, outputTokens: 0, avgDurationMs: 0 })
+ }
+ }
+ daily.sort((a, b) => a.date.localeCompare(b.date))
+
// Daily totals for message trend chart
- const dailyTotals = Array.from(dailyMap.entries())
- .map(([date, modelMap]) => {
- let messages = 0
- let tokens = 0
- for (const stats of modelMap.values()) {
- messages += stats.messageCount
- tokens += stats.inputTokens + stats.outputTokens
- }
- return { date, messages, tokens }
- })
- .sort((a, b) => a.date.localeCompare(b.date))
+ const dailyTotalsMap = new Map<string, { messages: number, tokens: number }>()
+ for (const [date, modelMap] of dailyMap.entries()) {
+ let messages = 0
+ let tokens = 0
+ for (const stats of modelMap.values()) {
+ messages += stats.messageCount
+ tokens += stats.inputTokens + stats.outputTokens
+ }
+ dailyTotalsMap.set(date, { messages, tokens })
+ }
+
+ // Zero-fill dailyTotals for all dates in the period
+ const dailyTotals = allDates.map(date => {
+ const existing = dailyTotalsMap.get(date)
+ return { date, messages: existing?.messages ?? 0, tokens: existing?.tokens ?? 0 }
+ })
+
+ // Flatten dailyBySource with zero-fill
+ const dailyBySource: Array<{ date: string, source: string, messageCount: number, inputTokens: number, outputTokens: number }> = []
+ const allSourcesInData = new Set<string>()
+ for (const sourceMap of dailyBySourceMap.values()) {
+ for (const src of sourceMap.keys()) allSourcesInData.add(src)
+ }
+ for (const date of allDates) {
+ const sourceMap = dailyBySourceMap.get(date)
+ for (const src of allSourcesInData) {
+ const s = sourceMap?.get(src)
+ dailyBySource.push({
+ date,
+ source: src,
+ messageCount: s?.messageCount ?? 0,
+ inputTokens: s?.inputTokens ?? 0,
+ outputTokens: s?.outputTokens ?? 0,
+ })
+ }
+ }
+
+ // Hourly distribution
+ const hourlyDistribution = hourlyBuckets.map((bucket, hour) => ({
+ hour,
+ messageCount: bucket.messageCount,
+ totalTokens: bucket.totalTokens,
+ }))
+
+ // Estimated cost from byModel using dynamic gateway pricing
+ const pricingMap = await getModelPricingMap()
+ const estimatedCost = computeEstimatedCost(byModel, pricingMap)
const totalFeedback = positiveFeedback + negativeFeedback
const feedbackScore = totalFeedback > 0 ? Math.round((positiveFeedback / totalFeedback) * 100) : null
@@ -366,5 +503,10 @@ export default defineEventHandler(async (event) => {
topUsers,
daily,
dailyTotals,
+ dailyBySource,
+ hourlyDistribution,
+ estimatedCost,
+ availableSources,
+ availableModels,
}
})
diff --git a/apps/chat/server/utils/model-pricing.ts b/apps/chat/server/utils/model-pricing.ts
new file mode 100644
index 0000000..6be8ecd
--- /dev/null
+++ b/apps/chat/server/utils/model-pricing.ts
@@ -0,0 +1,57 @@
+import { createGateway } from '@ai-sdk/gateway'
+
+interface ModelPricing {
+ input: number // cost per token in USD
+ output: number
+}
+
+let cachedPricing: Map<string, ModelPricing> | null = null
+let cacheTimestamp = 0
+const CACHE_TTL = 1000 * 60 * 60 // 1 hour
+
+export async function getModelPricingMap(): Promise<Map<string, ModelPricing>> {
+ if (cachedPricing && Date.now() - cacheTimestamp < CACHE_TTL) {
+ return cachedPricing
+ }
+
+ try {
+ const config = useRuntimeConfig()
+ const gateway = createGateway({ apiKey: config.savoir?.apiKey })
+ const { models } = await gateway.getAvailableModels()
+
+ const pricing = new Map<string, ModelPricing>()
+ for (const model of models) {
+ if (model.pricing) {
+ pricing.set(model.id, {
+ input: parseFloat(model.pricing.input),
+ output: parseFloat(model.pricing.output),
+ })
+ }
+ }
+
+ cachedPricing = pricing
+ cacheTimestamp = Date.now()
+ return pricing
+ } catch {
+ // Return empty map on failure — cost will show as $0
+ return cachedPricing ?? new Map()
+ }
+}
+
+export function computeEstimatedCost(
+ byModel: Array<{ model: string, inputTokens: number, outputTokens: number }>,
+ pricingMap: Map<string, ModelPricing>,
+) {
+ const byModelCosts = byModel.map((m) => {
+ const pricing = pricingMap.get(m.model)
+ if (!pricing) return { model: m.model, inputCost: 0, outputCost: 0, totalCost: 0 }
+ const inputCost = m.inputTokens * pricing.input
+ const outputCost = m.outputTokens * pricing.output
+ return { model: m.model, inputCost, outputCost, totalCost: inputCost + outputCost }
+ })
+
+ return {
+ total: byModelCosts.reduce((sum, m) => sum + m.totalCost, 0),
+ byModel: byModelCosts,
+ }
+}
diff --git a/apps/chat/shared/types/stats.d.ts b/apps/chat/shared/types/stats.d.ts
index 4a3f750..c5eb3b7 100644
--- a/apps/chat/shared/types/stats.d.ts
+++ b/apps/chat/shared/types/stats.d.ts
@@ -76,6 +76,39 @@ export interface TrendStats {
activeUsers: number | null
}
+/**
+ * Daily statistics broken down by source
+ */
+export interface DailyBySource {
+ date: string
+ source: string
+ messageCount: number
+ inputTokens: number
+ outputTokens: number
+}
+
+/**
+ * Hourly activity distribution (UTC)
+ */
+export interface HourlyDistribution {
+ hour: number // 0-23
+ messageCount: number
+ totalTokens: number
+}
+
+/**
+ * Estimated cost breakdown
+ */
+export interface EstimatedCost {
+ total: number
+ byModel: Array<{
+ model: string
+ inputCost: number
+ outputCost: number
+ totalCost: number
+ }>
+}
+
/**
* Global stats response (admin)
*/
@@ -108,4 +141,9 @@ export interface GlobalStatsResponse {
topUsers: TopUserStats[]
daily: DailyStats[]
dailyTotals: DailyTotals[]
+ dailyBySource: DailyBySource[]
+ hourlyDistribution: HourlyDistribution[]
+ estimatedCost: EstimatedCost
+ availableSources: string[]
+ availableModels: string[]
}