|
| 1 | +import type { ModelPerformanceStats } from '$lib/db/schema'; |
| 2 | +import { |
| 3 | + calculateAllModelPerformanceStats, |
| 4 | + getModelPerformanceStatsByUser, |
| 5 | +} from '$lib/db/queries/model-performance'; |
| 6 | + |
| 7 | +export type ModelAnalyticsInsights = { |
| 8 | + totalMessages: number; |
| 9 | + totalCost: number; |
| 10 | + avgRating: number | null; |
| 11 | + mostUsedModel: ModelPerformanceStats | null; |
| 12 | + bestRatedModel: ModelPerformanceStats | null; |
| 13 | + mostCostEffective: ModelPerformanceStats | null; |
| 14 | + fastestModel: ModelPerformanceStats | null; |
| 15 | +}; |
| 16 | + |
| 17 | +export async function getModelAnalytics( |
| 18 | + userId: string, |
| 19 | + options?: { recalculate?: boolean } |
| 20 | +): Promise<{ stats: ModelPerformanceStats[]; insights: ModelAnalyticsInsights }> { |
| 21 | + const recalculate = options?.recalculate ?? true; |
| 22 | + |
| 23 | + const stats = recalculate |
| 24 | + ? await calculateAllModelPerformanceStats(userId) |
| 25 | + : await getModelPerformanceStatsByUser(userId); |
| 26 | + |
| 27 | + const insights = buildModelAnalyticsInsights(stats); |
| 28 | + return { stats, insights }; |
| 29 | +} |
| 30 | + |
| 31 | +export function buildModelAnalyticsInsights( |
| 32 | + stats: ModelPerformanceStats[] |
| 33 | +): ModelAnalyticsInsights { |
| 34 | + const totalMessages = stats.reduce((sum, s) => sum + s.totalMessages, 0); |
| 35 | + const totalCost = stats.reduce((sum, s) => { |
| 36 | + const cost = s.totalCost; |
| 37 | + if (cost === null || cost === undefined || Number.isNaN(cost)) { |
| 38 | + return sum; |
| 39 | + } |
| 40 | + return sum + cost; |
| 41 | + }, 0); |
| 42 | + |
| 43 | + const ratedStats = stats.filter((s) => s.avgRating !== null && s.avgRating !== undefined); |
| 44 | + const avgRating = |
| 45 | + ratedStats.length > 0 |
| 46 | + ? ratedStats.reduce((sum, s) => sum + (s.avgRating ?? 0), 0) / ratedStats.length |
| 47 | + : null; |
| 48 | + |
| 49 | + const mostUsedModel = |
| 50 | + stats.length > 0 |
| 51 | + ? stats.reduce( |
| 52 | + (prev, current) => (current.totalMessages > prev.totalMessages ? current : prev), |
| 53 | + stats[0]! |
| 54 | + ) |
| 55 | + : null; |
| 56 | + |
| 57 | + const qualifiedModels = stats.filter((s) => s.totalMessages >= 5 && s.avgRating !== null); |
| 58 | + const bestRatedModel = |
| 59 | + qualifiedModels.length > 0 |
| 60 | + ? qualifiedModels.reduce((prev, current) => |
| 61 | + (current.avgRating ?? 0) > (prev.avgRating ?? 0) ? current : prev |
| 62 | + ) |
| 63 | + : null; |
| 64 | + |
| 65 | + const modelsWithCost = stats.filter( |
| 66 | + (s) => s.totalMessages >= 5 && s.totalCost > 0 && !Number.isNaN(s.totalCost) |
| 67 | + ); |
| 68 | + const mostCostEffective = |
| 69 | + modelsWithCost.length > 0 |
| 70 | + ? modelsWithCost.reduce((prev, current) => { |
| 71 | + const prevCostPerMsg = prev.totalCost / prev.totalMessages; |
| 72 | + const currentCostPerMsg = current.totalCost / current.totalMessages; |
| 73 | + return currentCostPerMsg < prevCostPerMsg ? current : prev; |
| 74 | + }) |
| 75 | + : null; |
| 76 | + |
| 77 | + const modelsWithSpeed = stats.filter( |
| 78 | + (s) => |
| 79 | + s.totalMessages >= 5 && |
| 80 | + s.avgTokens !== null && |
| 81 | + s.avgTokens !== undefined && |
| 82 | + s.avgResponseTime !== null && |
| 83 | + s.avgResponseTime !== undefined && |
| 84 | + s.avgResponseTime > 0 |
| 85 | + ); |
| 86 | + |
| 87 | + const fastestModel = |
| 88 | + modelsWithSpeed.length > 0 |
| 89 | + ? modelsWithSpeed.reduce((prev, current) => { |
| 90 | + const prevSpeed = (prev.avgTokens ?? 0) / ((prev.avgResponseTime ?? 0) / 1000 || 1); |
| 91 | + const currentSpeed = |
| 92 | + (current.avgTokens ?? 0) / ((current.avgResponseTime ?? 0) / 1000 || 1); |
| 93 | + return currentSpeed > prevSpeed ? current : prev; |
| 94 | + }) |
| 95 | + : null; |
| 96 | + |
| 97 | + return { |
| 98 | + totalMessages, |
| 99 | + totalCost, |
| 100 | + avgRating, |
| 101 | + mostUsedModel, |
| 102 | + bestRatedModel, |
| 103 | + mostCostEffective, |
| 104 | + fastestModel, |
| 105 | + }; |
| 106 | +} |
0 commit comments