Spaces:
Configuration error
Configuration error
File size: 6,215 Bytes
bcce530 | 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 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 | import { NextRequest, NextResponse } from "next/server"
import prisma from "@/lib/prisma"
import { stackServerApp } from "@/lib/stack-server"
// GET - Get detailed analytics for a creator
export async function GET(request: NextRequest) {
try {
const user = await stackServerApp.getUser()
if (!user) {
return NextResponse.json(
{ error: "Unauthorized" },
{ status: 401 }
)
}
const { searchParams } = new URL(request.url)
const days = parseInt(searchParams.get("days") || "30")
const daysAgo = new Date()
daysAgo.setDate(daysAgo.getDate() - days)
// Get all prompts by this user
const prompts = await prisma.prompt.findMany({
where: { creatorId: user.id },
select: {
id: true,
title: true,
slug: true,
totalRuns: true,
starsCount: true,
remixesCount: true,
framework: true,
badges: true,
parentId: true,
createdAt: true,
},
orderBy: { totalRuns: "desc" },
})
// Get runs data for the period
const runs = await prisma.run.findMany({
where: {
prompt: { creatorId: user.id },
createdAt: { gte: daysAgo },
},
select: {
id: true,
model: true,
tokens: true,
cached: true,
createdAt: true,
promptId: true,
},
orderBy: { createdAt: "desc" },
})
// Get engagement metrics (errors tracked separately)
const engagementMetrics = await prisma.engagementMetric.findMany({
where: {
targetType: "prompt",
targetId: { in: prompts.map(p => p.id) },
createdAt: { gte: daysAgo },
},
select: {
action: true,
createdAt: true,
targetId: true,
}
})
// Aggregate runs by day
const runsByDay: Record<string, { runs: number; errors: number; tokens: number }> = {}
runs.forEach(run => {
const day = run.createdAt.toISOString().split("T")[0]
if (!runsByDay[day]) {
runsByDay[day] = { runs: 0, errors: 0, tokens: 0 }
}
runsByDay[day].runs += 1
runsByDay[day].tokens += run.tokens || 0
})
// Prepare daily data for chart
const dailyData = []
for (let i = days - 1; i >= 0; i--) {
const date = new Date()
date.setDate(date.getDate() - i)
const dateStr = date.toISOString().split("T")[0]
const dayData = runsByDay[dateStr] || { runs: 0, errors: 0, tokens: 0 }
dailyData.push({
date: dateStr,
runs: dayData.runs,
errors: dayData.errors,
tokens: dayData.tokens,
})
}
// Model usage stats
const modelUsage: Record<string, { runs: number; tokens: number }> = {}
runs.forEach(run => {
if (!modelUsage[run.model]) {
modelUsage[run.model] = { runs: 0, tokens: 0 }
}
modelUsage[run.model].runs += 1
modelUsage[run.model].tokens += run.tokens || 0
})
// Token usage per prompt
const tokensByPrompt: Record<string, number> = {}
runs.forEach(run => {
if (!tokensByPrompt[run.promptId]) {
tokensByPrompt[run.promptId] = 0
}
tokensByPrompt[run.promptId] += run.tokens || 0
})
// Remix traffic - find remixes of user's prompts
const remixTraffic = await prisma.prompt.findMany({
where: {
parentId: { in: prompts.map(p => p.id) },
createdAt: { gte: daysAgo },
},
select: {
id: true,
title: true,
parentId: true,
totalRuns: true,
createdAt: true,
},
orderBy: { createdAt: "desc" },
take: 20,
})
// Engagement breakdown
const engagementByAction: Record<string, number> = {}
engagementMetrics.forEach(metric => {
engagementByAction[metric.action] = (engagementByAction[metric.action] || 0) + 1
})
// Total tokens used
const totalTokens = runs.reduce((sum, run) => sum + (run.tokens || 0), 0)
const cachedRuns = runs.filter(r => r.cached).length
const cacheHitRate = runs.length > 0 ? (cachedRuns / runs.length) * 100 : 0
// Calculate totals
const totalRuns = prompts.reduce((sum, p) => sum + p.totalRuns, 0)
const totalStars = prompts.reduce((sum, p) => sum + p.starsCount, 0)
const totalRemixes = prompts.reduce((sum, p) => sum + p.remixesCount, 0)
// Error rate estimation (based on low token runs which might indicate errors)
const potentialErrors = runs.filter(r => r.tokens !== null && r.tokens < 10).length
const errorRate = runs.length > 0 ? (potentialErrors / runs.length) * 100 : 0
return NextResponse.json({
prompts: prompts.map(p => ({
...p,
tokens: tokensByPrompt[p.id] || 0,
})),
dailyData,
modelUsage,
totalTokens,
cacheHitRate,
errorRate,
remixTraffic,
engagementByAction,
totals: {
runs: totalRuns,
stars: totalStars,
remixes: totalRemixes,
prompts: prompts.length,
periodRuns: runs.length,
periodTokens: totalTokens,
},
})
} catch (error) {
console.error("Error fetching analytics:", error)
return NextResponse.json(
{ error: "Failed to fetch analytics" },
{ status: 500 }
)
}
}
|