Implement live analysis achievements and admin console
这个提交包含在:
653
server/db.ts
653
server/db.ts
@@ -1,4 +1,4 @@
|
||||
import { eq, desc, and, asc, lte, sql } from "drizzle-orm";
|
||||
import { eq, desc, and, asc, lte, gte, sql } from "drizzle-orm";
|
||||
import { drizzle } from "drizzle-orm/mysql2";
|
||||
import {
|
||||
InsertUser, users,
|
||||
@@ -7,14 +7,22 @@ import {
|
||||
trainingVideos, InsertTrainingVideo,
|
||||
poseAnalyses, InsertPoseAnalysis,
|
||||
trainingRecords, InsertTrainingRecord,
|
||||
liveAnalysisSessions, InsertLiveAnalysisSession,
|
||||
liveActionSegments, InsertLiveActionSegment,
|
||||
dailyTrainingAggregates, InsertDailyTrainingAggregate,
|
||||
ratingHistory, InsertRatingHistory,
|
||||
ntrpSnapshots, InsertNtrpSnapshot,
|
||||
dailyCheckins, InsertDailyCheckin,
|
||||
userBadges, InsertUserBadge,
|
||||
achievementDefinitions, InsertAchievementDefinition,
|
||||
userAchievements, InsertUserAchievement,
|
||||
tutorialVideos, InsertTutorialVideo,
|
||||
tutorialProgress, InsertTutorialProgress,
|
||||
trainingReminders, InsertTrainingReminder,
|
||||
notificationLog, InsertNotificationLog,
|
||||
backgroundTasks, InsertBackgroundTask,
|
||||
adminAuditLogs, InsertAdminAuditLog,
|
||||
appSettings, InsertAppSetting,
|
||||
visionReferenceImages, InsertVisionReferenceImage,
|
||||
visionTestRuns, InsertVisionTestRun,
|
||||
} from "../drizzle/schema";
|
||||
@@ -22,6 +30,72 @@ import { ENV } from './_core/env';
|
||||
|
||||
let _db: ReturnType<typeof drizzle> | null = null;
|
||||
|
||||
const APP_TIMEZONE = process.env.TZ || "Asia/Shanghai";
|
||||
|
||||
function getDateFormatter() {
|
||||
return new Intl.DateTimeFormat("en-CA", {
|
||||
timeZone: APP_TIMEZONE,
|
||||
year: "numeric",
|
||||
month: "2-digit",
|
||||
day: "2-digit",
|
||||
});
|
||||
}
|
||||
|
||||
export function getDateKey(date = new Date()) {
|
||||
return getDateFormatter().format(date);
|
||||
}
|
||||
|
||||
function toDayKey(userId: number, trainingDate: string) {
|
||||
return `${userId}:${trainingDate}`;
|
||||
}
|
||||
|
||||
export const DEFAULT_APP_SETTINGS: Omit<InsertAppSetting, "id" | "createdAt" | "updatedAt">[] = [
|
||||
{
|
||||
settingKey: "action_unknown_confidence_threshold",
|
||||
label: "未知动作阈值",
|
||||
description: "当动作识别置信度低于此值时归类为未知动作。",
|
||||
value: { value: 0.45, type: "number" },
|
||||
},
|
||||
{
|
||||
settingKey: "action_merge_gap_ms",
|
||||
label: "动作合并间隔",
|
||||
description: "相邻同类动作小于该间隔时会合并为同一片段。",
|
||||
value: { value: 500, type: "number" },
|
||||
},
|
||||
{
|
||||
settingKey: "action_segment_max_ms",
|
||||
label: "动作片段最长时长",
|
||||
description: "单个动作片段最长持续时间。",
|
||||
value: { value: 10000, type: "number" },
|
||||
},
|
||||
{
|
||||
settingKey: "ntrp_daily_refresh_hour",
|
||||
label: "NTRP 每日刷新小时",
|
||||
description: "每天异步刷新 NTRP 的小时数。",
|
||||
value: { value: 0, type: "number" },
|
||||
},
|
||||
];
|
||||
|
||||
export const ACHIEVEMENT_DEFINITION_SEED_DATA: Omit<InsertAchievementDefinition, "id" | "createdAt" | "updatedAt">[] = [
|
||||
{ key: "training_day_1", name: "开练", description: "完成首个训练日", category: "consistency", rarity: "common", icon: "🎾", metricKey: "training_days", targetValue: 1, tier: 1, sortOrder: 1, isHidden: 0, isActive: 1 },
|
||||
{ key: "training_day_3", name: "三日连练", description: "连续训练 3 天", category: "consistency", rarity: "common", icon: "🔥", metricKey: "current_streak", targetValue: 3, tier: 2, sortOrder: 2, isHidden: 0, isActive: 1 },
|
||||
{ key: "training_day_7", name: "一周稳定", description: "连续训练 7 天", category: "consistency", rarity: "rare", icon: "⭐", metricKey: "current_streak", targetValue: 7, tier: 3, sortOrder: 3, isHidden: 0, isActive: 1 },
|
||||
{ key: "training_minutes_60", name: "首个小时", description: "累计训练 60 分钟", category: "volume", rarity: "common", icon: "⏱️", metricKey: "total_minutes", targetValue: 60, tier: 1, sortOrder: 10, isHidden: 0, isActive: 1 },
|
||||
{ key: "training_minutes_300", name: "五小时达标", description: "累计训练 300 分钟", category: "volume", rarity: "rare", icon: "🕐", metricKey: "total_minutes", targetValue: 300, tier: 2, sortOrder: 11, isHidden: 0, isActive: 1 },
|
||||
{ key: "training_minutes_1000", name: "千分钟训练者", description: "累计训练 1000 分钟", category: "volume", rarity: "epic", icon: "⏰", metricKey: "total_minutes", targetValue: 1000, tier: 3, sortOrder: 12, isHidden: 0, isActive: 1 },
|
||||
{ key: "effective_actions_50", name: "动作起步", description: "累计完成 50 个有效动作", category: "technique", rarity: "common", icon: "🏓", metricKey: "effective_actions", targetValue: 50, tier: 1, sortOrder: 20, isHidden: 0, isActive: 1 },
|
||||
{ key: "effective_actions_200", name: "动作累积", description: "累计完成 200 个有效动作", category: "technique", rarity: "rare", icon: "💥", metricKey: "effective_actions", targetValue: 200, tier: 2, sortOrder: 21, isHidden: 0, isActive: 1 },
|
||||
{ key: "recordings_1", name: "录像开启", description: "完成首个录制归档", category: "recording", rarity: "common", icon: "🎥", metricKey: "recording_count", targetValue: 1, tier: 1, sortOrder: 30, isHidden: 0, isActive: 1 },
|
||||
{ key: "analyses_1", name: "分析首秀", description: "完成首个分析会话", category: "analysis", rarity: "common", icon: "🧠", metricKey: "analysis_count", targetValue: 1, tier: 1, sortOrder: 31, isHidden: 0, isActive: 1 },
|
||||
{ key: "live_analysis_5", name: "实时观察者", description: "完成 5 次实时分析", category: "analysis", rarity: "rare", icon: "📹", metricKey: "live_analysis_count", targetValue: 5, tier: 2, sortOrder: 32, isHidden: 0, isActive: 1 },
|
||||
{ key: "score_80", name: "高分动作", description: "任意训练得分达到 80", category: "quality", rarity: "rare", icon: "🏅", metricKey: "best_score", targetValue: 80, tier: 1, sortOrder: 40, isHidden: 0, isActive: 1 },
|
||||
{ key: "score_90", name: "精确击球", description: "任意训练得分达到 90", category: "quality", rarity: "epic", icon: "🥇", metricKey: "best_score", targetValue: 90, tier: 2, sortOrder: 41, isHidden: 0, isActive: 1 },
|
||||
{ key: "ntrp_2_5", name: "NTRP 2.5", description: "综合评分达到 2.5", category: "rating", rarity: "rare", icon: "📈", metricKey: "ntrp_rating", targetValue: 2.5, tier: 1, sortOrder: 50, isHidden: 0, isActive: 1 },
|
||||
{ key: "ntrp_3_0", name: "NTRP 3.0", description: "综合评分达到 3.0", category: "rating", rarity: "epic", icon: "🚀", metricKey: "ntrp_rating", targetValue: 3.0, tier: 2, sortOrder: 51, isHidden: 0, isActive: 1 },
|
||||
{ key: "pk_session_1", name: "训练 PK", description: "完成首个 PK 会话", category: "pk", rarity: "rare", icon: "⚔️", metricKey: "pk_count", targetValue: 1, tier: 1, sortOrder: 60, isHidden: 0, isActive: 1 },
|
||||
{ key: "plan_link_5", name: "按计划训练", description: "累计 5 次训练匹配训练计划", category: "plan", rarity: "rare", icon: "🗂️", metricKey: "plan_matches", targetValue: 5, tier: 1, sortOrder: 70, isHidden: 0, isActive: 1 },
|
||||
];
|
||||
|
||||
export async function getDb() {
|
||||
if (!_db && process.env.DATABASE_URL) {
|
||||
try {
|
||||
@@ -34,6 +108,150 @@ export async function getDb() {
|
||||
return _db;
|
||||
}
|
||||
|
||||
export async function seedAppSettings() {
|
||||
const db = await getDb();
|
||||
if (!db) return;
|
||||
|
||||
for (const setting of DEFAULT_APP_SETTINGS) {
|
||||
const existing = await db.select().from(appSettings).where(eq(appSettings.settingKey, setting.settingKey)).limit(1);
|
||||
if (existing.length === 0) {
|
||||
await db.insert(appSettings).values(setting);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export async function listAppSettings() {
|
||||
const db = await getDb();
|
||||
if (!db) return [];
|
||||
return db.select().from(appSettings).orderBy(asc(appSettings.settingKey));
|
||||
}
|
||||
|
||||
export async function updateAppSetting(settingKey: string, value: unknown) {
|
||||
const db = await getDb();
|
||||
if (!db) return;
|
||||
await db.update(appSettings).set({ value }).where(eq(appSettings.settingKey, settingKey));
|
||||
}
|
||||
|
||||
export async function seedAchievementDefinitions() {
|
||||
const db = await getDb();
|
||||
if (!db) return;
|
||||
|
||||
for (const definition of ACHIEVEMENT_DEFINITION_SEED_DATA) {
|
||||
const existing = await db.select().from(achievementDefinitions).where(eq(achievementDefinitions.key, definition.key)).limit(1);
|
||||
if (existing.length === 0) {
|
||||
await db.insert(achievementDefinitions).values(definition);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export async function listAchievementDefinitions() {
|
||||
const db = await getDb();
|
||||
if (!db) return [];
|
||||
return db.select().from(achievementDefinitions)
|
||||
.where(eq(achievementDefinitions.isActive, 1))
|
||||
.orderBy(asc(achievementDefinitions.sortOrder), asc(achievementDefinitions.id));
|
||||
}
|
||||
|
||||
export async function listAllAchievementDefinitions() {
|
||||
const db = await getDb();
|
||||
if (!db) return [];
|
||||
return db.select().from(achievementDefinitions)
|
||||
.orderBy(asc(achievementDefinitions.sortOrder), asc(achievementDefinitions.id));
|
||||
}
|
||||
|
||||
export async function createAdminAuditLog(entry: InsertAdminAuditLog) {
|
||||
const db = await getDb();
|
||||
if (!db) return;
|
||||
await db.insert(adminAuditLogs).values(entry);
|
||||
}
|
||||
|
||||
export async function listAdminAuditLogs(limit = 100) {
|
||||
const db = await getDb();
|
||||
if (!db) return [];
|
||||
return db.select({
|
||||
id: adminAuditLogs.id,
|
||||
adminUserId: adminAuditLogs.adminUserId,
|
||||
adminName: users.name,
|
||||
actionType: adminAuditLogs.actionType,
|
||||
entityType: adminAuditLogs.entityType,
|
||||
entityId: adminAuditLogs.entityId,
|
||||
targetUserId: adminAuditLogs.targetUserId,
|
||||
payload: adminAuditLogs.payload,
|
||||
createdAt: adminAuditLogs.createdAt,
|
||||
}).from(adminAuditLogs)
|
||||
.leftJoin(users, eq(users.id, adminAuditLogs.adminUserId))
|
||||
.orderBy(desc(adminAuditLogs.createdAt))
|
||||
.limit(limit);
|
||||
}
|
||||
|
||||
export async function listUsersForAdmin(limit = 100) {
|
||||
const db = await getDb();
|
||||
if (!db) return [];
|
||||
return db.select({
|
||||
id: users.id,
|
||||
name: users.name,
|
||||
role: users.role,
|
||||
ntrpRating: users.ntrpRating,
|
||||
totalSessions: users.totalSessions,
|
||||
totalMinutes: users.totalMinutes,
|
||||
totalShots: users.totalShots,
|
||||
currentStreak: users.currentStreak,
|
||||
longestStreak: users.longestStreak,
|
||||
createdAt: users.createdAt,
|
||||
lastSignedIn: users.lastSignedIn,
|
||||
}).from(users).orderBy(desc(users.lastSignedIn)).limit(limit);
|
||||
}
|
||||
|
||||
export async function getAdminUserId() {
|
||||
const db = await getDb();
|
||||
if (!db) return null;
|
||||
const [admin] = await db.select().from(users).where(eq(users.role, "admin")).orderBy(desc(users.lastSignedIn)).limit(1);
|
||||
return admin?.id ?? null;
|
||||
}
|
||||
|
||||
export async function listAllBackgroundTasks(limit = 100) {
|
||||
const db = await getDb();
|
||||
if (!db) return [];
|
||||
return db.select({
|
||||
id: backgroundTasks.id,
|
||||
userId: backgroundTasks.userId,
|
||||
userName: users.name,
|
||||
type: backgroundTasks.type,
|
||||
status: backgroundTasks.status,
|
||||
title: backgroundTasks.title,
|
||||
message: backgroundTasks.message,
|
||||
progress: backgroundTasks.progress,
|
||||
payload: backgroundTasks.payload,
|
||||
result: backgroundTasks.result,
|
||||
error: backgroundTasks.error,
|
||||
attempts: backgroundTasks.attempts,
|
||||
maxAttempts: backgroundTasks.maxAttempts,
|
||||
createdAt: backgroundTasks.createdAt,
|
||||
updatedAt: backgroundTasks.updatedAt,
|
||||
completedAt: backgroundTasks.completedAt,
|
||||
}).from(backgroundTasks)
|
||||
.leftJoin(users, eq(users.id, backgroundTasks.userId))
|
||||
.orderBy(desc(backgroundTasks.createdAt))
|
||||
.limit(limit);
|
||||
}
|
||||
|
||||
export async function hasRecentBackgroundTaskOfType(
|
||||
type: "ntrp_refresh_user" | "ntrp_refresh_all",
|
||||
since: Date,
|
||||
) {
|
||||
const db = await getDb();
|
||||
if (!db) return false;
|
||||
const result = await db.select({ count: sql<number>`count(*)` }).from(backgroundTasks)
|
||||
.where(and(eq(backgroundTasks.type, type), gte(backgroundTasks.createdAt, since)));
|
||||
return (result[0]?.count || 0) > 0;
|
||||
}
|
||||
|
||||
export async function listUserIds() {
|
||||
const db = await getDb();
|
||||
if (!db) return [];
|
||||
return db.select({ id: users.id }).from(users).orderBy(asc(users.id));
|
||||
}
|
||||
|
||||
// ===== USER OPERATIONS =====
|
||||
|
||||
export async function upsertUser(user: InsertUser): Promise<void> {
|
||||
@@ -175,6 +393,41 @@ export async function updateTrainingPlan(planId: number, data: Partial<InsertTra
|
||||
await db.update(trainingPlans).set(data).where(eq(trainingPlans.id, planId));
|
||||
}
|
||||
|
||||
const PLAN_KEYWORDS: Record<string, string[]> = {
|
||||
forehand: ["正手", "forehand"],
|
||||
backhand: ["反手", "backhand"],
|
||||
serve: ["发球", "serve"],
|
||||
volley: ["截击", "volley"],
|
||||
overhead: ["高压", "overhead"],
|
||||
slice: ["切削", "slice"],
|
||||
lob: ["挑高", "lob"],
|
||||
unknown: ["综合", "基础", "训练"],
|
||||
};
|
||||
|
||||
export async function matchActivePlanForExercise(userId: number, exerciseType?: string | null) {
|
||||
const activePlan = await getActivePlan(userId);
|
||||
if (!activePlan || !exerciseType) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const keywords = PLAN_KEYWORDS[exerciseType] ?? [exerciseType];
|
||||
const exercises = Array.isArray(activePlan.exercises) ? activePlan.exercises as Array<Record<string, unknown>> : [];
|
||||
const matched = exercises.find((exercise) => {
|
||||
const haystack = JSON.stringify(exercise).toLowerCase();
|
||||
return keywords.some(keyword => haystack.includes(keyword.toLowerCase()));
|
||||
});
|
||||
|
||||
if (!matched) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return {
|
||||
planId: activePlan.id,
|
||||
confidence: 0.72,
|
||||
matchedExercise: matched,
|
||||
};
|
||||
}
|
||||
|
||||
// ===== VIDEO OPERATIONS =====
|
||||
|
||||
export async function createVideo(video: InsertTrainingVideo) {
|
||||
@@ -255,6 +508,173 @@ export async function markRecordCompleted(recordId: number, poseScore?: number)
|
||||
await db.update(trainingRecords).set({ completed: 1, poseScore: poseScore ?? null }).where(eq(trainingRecords.id, recordId));
|
||||
}
|
||||
|
||||
export async function upsertTrainingRecordBySource(
|
||||
record: InsertTrainingRecord & { sourceType: string; sourceId: string; userId: number }
|
||||
) {
|
||||
const db = await getDb();
|
||||
if (!db) throw new Error("Database not available");
|
||||
|
||||
const existing = await db.select().from(trainingRecords)
|
||||
.where(and(
|
||||
eq(trainingRecords.userId, record.userId),
|
||||
eq(trainingRecords.sourceType, record.sourceType),
|
||||
eq(trainingRecords.sourceId, record.sourceId),
|
||||
))
|
||||
.limit(1);
|
||||
|
||||
if (existing.length > 0) {
|
||||
await db.update(trainingRecords).set(record).where(eq(trainingRecords.id, existing[0].id));
|
||||
return { recordId: existing[0].id, isNew: false };
|
||||
}
|
||||
|
||||
const result = await db.insert(trainingRecords).values(record);
|
||||
return { recordId: result[0].insertId, isNew: true };
|
||||
}
|
||||
|
||||
export async function upsertDailyTrainingAggregate(input: {
|
||||
userId: number;
|
||||
trainingDate: string;
|
||||
deltaMinutes?: number;
|
||||
deltaSessions?: number;
|
||||
deltaAnalysisCount?: number;
|
||||
deltaLiveAnalysisCount?: number;
|
||||
deltaRecordingCount?: number;
|
||||
deltaPkCount?: number;
|
||||
deltaTotalActions?: number;
|
||||
deltaEffectiveActions?: number;
|
||||
deltaUnknownActions?: number;
|
||||
score?: number | null;
|
||||
metadata?: Record<string, unknown>;
|
||||
}) {
|
||||
const db = await getDb();
|
||||
if (!db) return;
|
||||
|
||||
const dayKey = toDayKey(input.userId, input.trainingDate);
|
||||
const [existing] = await db.select().from(dailyTrainingAggregates)
|
||||
.where(eq(dailyTrainingAggregates.dayKey, dayKey))
|
||||
.limit(1);
|
||||
|
||||
if (!existing) {
|
||||
const totalScore = input.score ?? 0;
|
||||
await db.insert(dailyTrainingAggregates).values({
|
||||
dayKey,
|
||||
userId: input.userId,
|
||||
trainingDate: input.trainingDate,
|
||||
totalMinutes: input.deltaMinutes ?? 0,
|
||||
sessionCount: input.deltaSessions ?? 0,
|
||||
analysisCount: input.deltaAnalysisCount ?? 0,
|
||||
liveAnalysisCount: input.deltaLiveAnalysisCount ?? 0,
|
||||
recordingCount: input.deltaRecordingCount ?? 0,
|
||||
pkCount: input.deltaPkCount ?? 0,
|
||||
totalActions: input.deltaTotalActions ?? 0,
|
||||
effectiveActions: input.deltaEffectiveActions ?? 0,
|
||||
unknownActions: input.deltaUnknownActions ?? 0,
|
||||
totalScore,
|
||||
averageScore: totalScore > 0 ? totalScore / Math.max(1, input.deltaSessions ?? 1) : 0,
|
||||
metadata: input.metadata ?? null,
|
||||
});
|
||||
} else {
|
||||
const nextSessionCount = (existing.sessionCount || 0) + (input.deltaSessions ?? 0);
|
||||
const nextTotalScore = (existing.totalScore || 0) + (input.score ?? 0);
|
||||
await db.update(dailyTrainingAggregates).set({
|
||||
totalMinutes: (existing.totalMinutes || 0) + (input.deltaMinutes ?? 0),
|
||||
sessionCount: nextSessionCount,
|
||||
analysisCount: (existing.analysisCount || 0) + (input.deltaAnalysisCount ?? 0),
|
||||
liveAnalysisCount: (existing.liveAnalysisCount || 0) + (input.deltaLiveAnalysisCount ?? 0),
|
||||
recordingCount: (existing.recordingCount || 0) + (input.deltaRecordingCount ?? 0),
|
||||
pkCount: (existing.pkCount || 0) + (input.deltaPkCount ?? 0),
|
||||
totalActions: (existing.totalActions || 0) + (input.deltaTotalActions ?? 0),
|
||||
effectiveActions: (existing.effectiveActions || 0) + (input.deltaEffectiveActions ?? 0),
|
||||
unknownActions: (existing.unknownActions || 0) + (input.deltaUnknownActions ?? 0),
|
||||
totalScore: nextTotalScore,
|
||||
averageScore: nextSessionCount > 0 ? nextTotalScore / nextSessionCount : 0,
|
||||
metadata: input.metadata ? { ...(existing.metadata as Record<string, unknown> | null ?? {}), ...input.metadata } : existing.metadata,
|
||||
}).where(eq(dailyTrainingAggregates.id, existing.id));
|
||||
}
|
||||
|
||||
await refreshUserTrainingSummary(input.userId);
|
||||
}
|
||||
|
||||
export async function listDailyTrainingAggregates(userId: number, limit = 30) {
|
||||
const db = await getDb();
|
||||
if (!db) return [];
|
||||
return db.select().from(dailyTrainingAggregates)
|
||||
.where(eq(dailyTrainingAggregates.userId, userId))
|
||||
.orderBy(desc(dailyTrainingAggregates.trainingDate))
|
||||
.limit(limit);
|
||||
}
|
||||
|
||||
export async function refreshUserTrainingSummary(userId: number) {
|
||||
const db = await getDb();
|
||||
if (!db) return;
|
||||
|
||||
const records = await db.select().from(trainingRecords)
|
||||
.where(and(eq(trainingRecords.userId, userId), eq(trainingRecords.completed, 1)));
|
||||
const aggregates = await db.select().from(dailyTrainingAggregates)
|
||||
.where(eq(dailyTrainingAggregates.userId, userId))
|
||||
.orderBy(desc(dailyTrainingAggregates.trainingDate));
|
||||
|
||||
const totalSessions = records.length;
|
||||
const totalMinutes = records.reduce((sum, item) => sum + (item.durationMinutes || 0), 0);
|
||||
const totalShots = aggregates.reduce((sum, item) => sum + (item.effectiveActions || 0), 0);
|
||||
|
||||
let currentStreak = 0;
|
||||
const sortedDays = aggregates
|
||||
.filter(item => (item.sessionCount || 0) > 0)
|
||||
.map(item => item.trainingDate)
|
||||
.sort((a, b) => a < b ? 1 : -1);
|
||||
let cursor = new Date(`${getDateKey()}T00:00:00`);
|
||||
for (const day of sortedDays) {
|
||||
const normalized = new Date(`${day}T00:00:00`);
|
||||
const diffDays = Math.round((cursor.getTime() - normalized.getTime()) / 86400000);
|
||||
if (diffDays === 0 || diffDays === 1) {
|
||||
currentStreak += 1;
|
||||
cursor = normalized;
|
||||
continue;
|
||||
}
|
||||
if (currentStreak > 0) {
|
||||
break;
|
||||
}
|
||||
cursor = normalized;
|
||||
currentStreak = 1;
|
||||
}
|
||||
|
||||
const longestStreak = Math.max(currentStreak, records.length > 0 ? (await getLongestTrainingStreak(userId)) : 0);
|
||||
|
||||
await db.update(users).set({
|
||||
totalSessions,
|
||||
totalMinutes,
|
||||
totalShots,
|
||||
currentStreak,
|
||||
longestStreak,
|
||||
}).where(eq(users.id, userId));
|
||||
}
|
||||
|
||||
async function getLongestTrainingStreak(userId: number) {
|
||||
const db = await getDb();
|
||||
if (!db) return 0;
|
||||
const aggregates = await db.select().from(dailyTrainingAggregates)
|
||||
.where(eq(dailyTrainingAggregates.userId, userId))
|
||||
.orderBy(asc(dailyTrainingAggregates.trainingDate));
|
||||
|
||||
let longest = 0;
|
||||
let current = 0;
|
||||
let prev: Date | null = null;
|
||||
for (const item of aggregates) {
|
||||
if ((item.sessionCount || 0) <= 0) continue;
|
||||
const currentDate = new Date(`${item.trainingDate}T00:00:00`);
|
||||
if (!prev) {
|
||||
current = 1;
|
||||
} else {
|
||||
const diff = Math.round((currentDate.getTime() - prev.getTime()) / 86400000);
|
||||
current = diff === 1 ? current + 1 : 1;
|
||||
}
|
||||
longest = Math.max(longest, current);
|
||||
prev = currentDate;
|
||||
}
|
||||
return longest;
|
||||
}
|
||||
|
||||
// ===== RATING HISTORY OPERATIONS =====
|
||||
|
||||
export async function createRatingEntry(entry: InsertRatingHistory) {
|
||||
@@ -270,6 +690,109 @@ export async function getUserRatingHistory(userId: number, limit = 30) {
|
||||
return db.select().from(ratingHistory).where(eq(ratingHistory.userId, userId)).orderBy(desc(ratingHistory.createdAt)).limit(limit);
|
||||
}
|
||||
|
||||
export async function createNtrpSnapshot(snapshot: InsertNtrpSnapshot) {
|
||||
const db = await getDb();
|
||||
if (!db) throw new Error("Database not available");
|
||||
const existing = await db.select().from(ntrpSnapshots)
|
||||
.where(eq(ntrpSnapshots.snapshotKey, snapshot.snapshotKey))
|
||||
.limit(1);
|
||||
if (existing.length > 0) {
|
||||
await db.update(ntrpSnapshots).set(snapshot).where(eq(ntrpSnapshots.id, existing[0].id));
|
||||
return existing[0].id;
|
||||
}
|
||||
const result = await db.insert(ntrpSnapshots).values(snapshot);
|
||||
return result[0].insertId;
|
||||
}
|
||||
|
||||
export async function getLatestNtrpSnapshot(userId: number) {
|
||||
const db = await getDb();
|
||||
if (!db) return undefined;
|
||||
const result = await db.select().from(ntrpSnapshots)
|
||||
.where(eq(ntrpSnapshots.userId, userId))
|
||||
.orderBy(desc(ntrpSnapshots.createdAt))
|
||||
.limit(1);
|
||||
return result[0];
|
||||
}
|
||||
|
||||
export async function listNtrpSnapshots(userId: number, limit = 30) {
|
||||
const db = await getDb();
|
||||
if (!db) return [];
|
||||
return db.select().from(ntrpSnapshots)
|
||||
.where(eq(ntrpSnapshots.userId, userId))
|
||||
.orderBy(desc(ntrpSnapshots.createdAt))
|
||||
.limit(limit);
|
||||
}
|
||||
|
||||
export async function createLiveAnalysisSession(session: InsertLiveAnalysisSession) {
|
||||
const db = await getDb();
|
||||
if (!db) throw new Error("Database not available");
|
||||
const result = await db.insert(liveAnalysisSessions).values(session);
|
||||
return result[0].insertId;
|
||||
}
|
||||
|
||||
export async function createLiveActionSegments(segments: InsertLiveActionSegment[]) {
|
||||
const db = await getDb();
|
||||
if (!db || segments.length === 0) return;
|
||||
await db.insert(liveActionSegments).values(segments);
|
||||
}
|
||||
|
||||
export async function listLiveAnalysisSessions(userId: number, limit = 20) {
|
||||
const db = await getDb();
|
||||
if (!db) return [];
|
||||
return db.select().from(liveAnalysisSessions)
|
||||
.where(eq(liveAnalysisSessions.userId, userId))
|
||||
.orderBy(desc(liveAnalysisSessions.createdAt))
|
||||
.limit(limit);
|
||||
}
|
||||
|
||||
export async function listAdminLiveAnalysisSessions(limit = 50) {
|
||||
const db = await getDb();
|
||||
if (!db) return [];
|
||||
return db.select({
|
||||
id: liveAnalysisSessions.id,
|
||||
userId: liveAnalysisSessions.userId,
|
||||
userName: users.name,
|
||||
title: liveAnalysisSessions.title,
|
||||
sessionMode: liveAnalysisSessions.sessionMode,
|
||||
status: liveAnalysisSessions.status,
|
||||
dominantAction: liveAnalysisSessions.dominantAction,
|
||||
overallScore: liveAnalysisSessions.overallScore,
|
||||
durationMs: liveAnalysisSessions.durationMs,
|
||||
effectiveSegments: liveAnalysisSessions.effectiveSegments,
|
||||
totalSegments: liveAnalysisSessions.totalSegments,
|
||||
videoUrl: liveAnalysisSessions.videoUrl,
|
||||
createdAt: liveAnalysisSessions.createdAt,
|
||||
}).from(liveAnalysisSessions)
|
||||
.leftJoin(users, eq(users.id, liveAnalysisSessions.userId))
|
||||
.orderBy(desc(liveAnalysisSessions.createdAt))
|
||||
.limit(limit);
|
||||
}
|
||||
|
||||
export async function getLiveAnalysisSessionById(sessionId: number) {
|
||||
const db = await getDb();
|
||||
if (!db) return undefined;
|
||||
const result = await db.select().from(liveAnalysisSessions)
|
||||
.where(eq(liveAnalysisSessions.id, sessionId))
|
||||
.limit(1);
|
||||
return result[0];
|
||||
}
|
||||
|
||||
export async function getLiveActionSegmentsBySessionId(sessionId: number) {
|
||||
const db = await getDb();
|
||||
if (!db) return [];
|
||||
return db.select().from(liveActionSegments)
|
||||
.where(eq(liveActionSegments.sessionId, sessionId))
|
||||
.orderBy(asc(liveActionSegments.startMs));
|
||||
}
|
||||
|
||||
export async function getAchievementProgress(userId: number) {
|
||||
const db = await getDb();
|
||||
if (!db) return [];
|
||||
return db.select().from(userAchievements)
|
||||
.where(eq(userAchievements.userId, userId))
|
||||
.orderBy(desc(userAchievements.unlockedAt), asc(userAchievements.achievementKey));
|
||||
}
|
||||
|
||||
// ===== DAILY CHECK-IN OPERATIONS =====
|
||||
|
||||
export async function checkinToday(userId: number, notes?: string, minutesTrained?: number) {
|
||||
@@ -329,6 +852,118 @@ export async function getTodayCheckin(userId: number) {
|
||||
return result.length > 0 ? result[0] : null;
|
||||
}
|
||||
|
||||
function metricValueFromContext(metricKey: string, context: {
|
||||
trainingDays: number;
|
||||
currentStreak: number;
|
||||
totalMinutes: number;
|
||||
effectiveActions: number;
|
||||
recordingCount: number;
|
||||
analysisCount: number;
|
||||
liveAnalysisCount: number;
|
||||
bestScore: number;
|
||||
ntrpRating: number;
|
||||
pkCount: number;
|
||||
planMatches: number;
|
||||
}) {
|
||||
const metricMap: Record<string, number> = {
|
||||
training_days: context.trainingDays,
|
||||
current_streak: context.currentStreak,
|
||||
total_minutes: context.totalMinutes,
|
||||
effective_actions: context.effectiveActions,
|
||||
recording_count: context.recordingCount,
|
||||
analysis_count: context.analysisCount,
|
||||
live_analysis_count: context.liveAnalysisCount,
|
||||
best_score: context.bestScore,
|
||||
ntrp_rating: context.ntrpRating,
|
||||
pk_count: context.pkCount,
|
||||
plan_matches: context.planMatches,
|
||||
};
|
||||
return metricMap[metricKey] ?? 0;
|
||||
}
|
||||
|
||||
export async function refreshAchievementsForUser(userId: number) {
|
||||
const db = await getDb();
|
||||
if (!db) return [];
|
||||
|
||||
const definitions = await listAchievementDefinitions();
|
||||
const progressRows = await getAchievementProgress(userId);
|
||||
const records = await db.select().from(trainingRecords).where(and(eq(trainingRecords.userId, userId), eq(trainingRecords.completed, 1)));
|
||||
const aggregates = await db.select().from(dailyTrainingAggregates).where(eq(dailyTrainingAggregates.userId, userId));
|
||||
const liveSessions = await db.select().from(liveAnalysisSessions).where(eq(liveAnalysisSessions.userId, userId));
|
||||
const [userRow] = await db.select().from(users).where(eq(users.id, userId)).limit(1);
|
||||
|
||||
const bestScore = Math.max(
|
||||
0,
|
||||
...records.map((record) => record.poseScore || 0),
|
||||
...liveSessions.map((session) => session.overallScore || 0),
|
||||
);
|
||||
const planMatches = records.filter((record) => record.linkedPlanId != null).length;
|
||||
const context = {
|
||||
trainingDays: aggregates.filter(item => (item.sessionCount || 0) > 0).length,
|
||||
currentStreak: userRow?.currentStreak || 0,
|
||||
totalMinutes: userRow?.totalMinutes || 0,
|
||||
effectiveActions: userRow?.totalShots || 0,
|
||||
recordingCount: records.filter(record => record.sourceType === "recording").length,
|
||||
analysisCount: records.filter(record => record.sourceType === "analysis_upload").length,
|
||||
liveAnalysisCount: records.filter(record => record.sourceType === "live_analysis").length,
|
||||
bestScore,
|
||||
ntrpRating: userRow?.ntrpRating || 1.5,
|
||||
pkCount: records.filter(record => ((record.metadata as Record<string, unknown> | null)?.sessionMode) === "pk").length,
|
||||
planMatches,
|
||||
};
|
||||
|
||||
const unlockedKeys: string[] = [];
|
||||
for (const definition of definitions) {
|
||||
const currentValue = metricValueFromContext(definition.metricKey, context);
|
||||
const progressPct = definition.targetValue > 0 ? Math.min(100, (currentValue / definition.targetValue) * 100) : 0;
|
||||
const progressKey = `${userId}:${definition.key}`;
|
||||
const existing = progressRows.find((row) => row.achievementKey === definition.key);
|
||||
const unlockedAt = currentValue >= definition.targetValue ? (existing?.unlockedAt ?? new Date()) : null;
|
||||
|
||||
if (!existing) {
|
||||
await db.insert(userAchievements).values({
|
||||
progressKey,
|
||||
userId,
|
||||
achievementKey: definition.key,
|
||||
currentValue,
|
||||
progressPct,
|
||||
unlockedAt,
|
||||
});
|
||||
if (unlockedAt) unlockedKeys.push(definition.key);
|
||||
} else {
|
||||
await db.update(userAchievements).set({
|
||||
currentValue,
|
||||
progressPct,
|
||||
unlockedAt: existing.unlockedAt ?? unlockedAt,
|
||||
lastEvaluatedAt: new Date(),
|
||||
}).where(eq(userAchievements.id, existing.id));
|
||||
if (!existing.unlockedAt && unlockedAt) unlockedKeys.push(definition.key);
|
||||
}
|
||||
}
|
||||
|
||||
return unlockedKeys;
|
||||
}
|
||||
|
||||
export async function listUserAchievements(userId: number) {
|
||||
const db = await getDb();
|
||||
if (!db) return [];
|
||||
|
||||
const definitions = await listAllAchievementDefinitions();
|
||||
const progress = await getAchievementProgress(userId);
|
||||
const progressMap = new Map(progress.map(item => [item.achievementKey, item]));
|
||||
|
||||
return definitions.map((definition) => {
|
||||
const row = progressMap.get(definition.key);
|
||||
return {
|
||||
...definition,
|
||||
currentValue: row?.currentValue ?? 0,
|
||||
progressPct: row?.progressPct ?? 0,
|
||||
unlockedAt: row?.unlockedAt ?? null,
|
||||
unlocked: Boolean(row?.unlockedAt),
|
||||
};
|
||||
});
|
||||
}
|
||||
|
||||
// ===== BADGE OPERATIONS =====
|
||||
|
||||
// Badge definitions
|
||||
@@ -1073,13 +1708,21 @@ export async function getUserStats(userId: number) {
|
||||
const records = await db.select().from(trainingRecords).where(eq(trainingRecords.userId, userId));
|
||||
const videos = await db.select().from(trainingVideos).where(eq(trainingVideos.userId, userId));
|
||||
const ratings = await db.select().from(ratingHistory).where(eq(ratingHistory.userId, userId)).orderBy(desc(ratingHistory.createdAt)).limit(30);
|
||||
const daily = await db.select().from(dailyTrainingAggregates).where(eq(dailyTrainingAggregates.userId, userId)).orderBy(desc(dailyTrainingAggregates.trainingDate)).limit(30);
|
||||
const liveSessions = await db.select().from(liveAnalysisSessions).where(eq(liveAnalysisSessions.userId, userId)).orderBy(desc(liveAnalysisSessions.createdAt)).limit(10);
|
||||
const latestSnapshot = await getLatestNtrpSnapshot(userId);
|
||||
const achievements = await listUserAchievements(userId);
|
||||
|
||||
const completedRecords = records.filter(r => r.completed === 1);
|
||||
const totalShots = analyses.reduce((sum, a) => sum + (a.shotCount || 0), 0);
|
||||
const totalShots = Math.max(
|
||||
analyses.reduce((sum, a) => sum + (a.shotCount || 0), 0),
|
||||
daily.reduce((sum, item) => sum + (item.effectiveActions || 0), 0),
|
||||
userRow.totalShots || 0,
|
||||
);
|
||||
const avgScore = analyses.length > 0 ? analyses.reduce((sum, a) => sum + (a.overallScore || 0), 0) / analyses.length : 0;
|
||||
|
||||
return {
|
||||
ntrpRating: userRow.ntrpRating || 1.5,
|
||||
ntrpRating: userRow.ntrpRating || latestSnapshot?.rating || 1.5,
|
||||
totalSessions: completedRecords.length,
|
||||
totalMinutes: records.reduce((sum, r) => sum + (r.durationMinutes || 0), 0),
|
||||
totalVideos: videos.length,
|
||||
@@ -1088,5 +1731,9 @@ export async function getUserStats(userId: number) {
|
||||
averageScore: Math.round(avgScore * 10) / 10,
|
||||
ratingHistory: ratings.reverse(),
|
||||
recentAnalyses: analyses.slice(0, 10),
|
||||
recentLiveSessions: liveSessions,
|
||||
dailyTraining: daily.reverse(),
|
||||
achievements,
|
||||
latestNtrpSnapshot: latestSnapshot ?? null,
|
||||
};
|
||||
}
|
||||
|
||||
在新工单中引用
屏蔽一个用户