Prmbr/docs/enable-stats-after-migration.md
2025-08-03 19:14:06 +08:00

84 lines
1.9 KiB
Markdown
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# 启用Stats功能说明
在运行了 `create-prompt-stats-table.sql` 创建了 `prompt_stats` 表之后,请按以下步骤启用完整的统计功能:
## 1. 更新Plaza API
`src/app/api/plaza/route.ts`更新include部分恢复stats查询
```typescript
include: {
user: { /* ... */ },
tags: { /* ... */ },
versions: { /* ... */ },
stats: { // 添加这个部分
select: {
viewCount: true,
likeCount: true,
rating: true,
ratingCount: true,
}
},
_count: { /* ... */ }
}
```
## 2. 启用浏览计数API
`src/app/api/plaza/[id]/view/route.ts` 中,取消注释并启用以下代码:
```typescript
// 将这些注释的代码恢复:
await prisma.promptStats.upsert({
where: { promptId: promptId },
update: { viewCount: { increment: 1 } },
create: { promptId: promptId, viewCount: 1 }
})
```
## 3. 可选:添加按浏览量排序
如果需要按浏览量排序可以在Plaza API和前端组件中添加相关逻辑
### 后端 (plaza/route.ts):
```typescript
// 排序条件
const orderBy: Record<string, any> = {}
if (sortBy === 'viewCount') {
orderBy.stats = { viewCount: sortOrder }
} else if (sortBy === 'name') {
orderBy.name = sortOrder
} else {
orderBy.createdAt = sortOrder
}
```
### 前端 (PlazaFilters.tsx):
在SelectContent中添加
```typescript
<SelectItem value="viewCount">{t('sortByMostViewed')}</SelectItem>
```
## 4. 重新生成Prisma客户端
```bash
npm run db:generate
```
## 5. 重启开发服务器
```bash
npm run dev
```
## 当前状态
现在的代码已经修复了所有错误即使没有stats表也能正常运行
- ✅ Plaza页面可以显示提示词
- ✅ Studio页面正常工作
- ✅ 搜索和过滤功能正常
- ✅ 复制和分享功能正常
- ⏳ 浏览计数功能暂时禁用(等待数据库表创建)
一旦运行了SQL脚本创建表就可以按上述步骤启用完整的统计功能。