Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@
"author": "",
"license": "ISC",
"dependencies": {
"@prisma/adapter-pg": "^7.5.0",
"@prisma/client": "^7.5.0",
"@prisma/adapter-pg": "^6.7.0",
"@prisma/client": "^6.7.0",
"@stellar/stellar-sdk": "^14.6.1",
"@types/ioredis": "^4.28.10",
"@types/json2csv": "^5.0.7",
Expand All @@ -36,7 +36,7 @@
"jsonwebtoken": "^9.0.2",
"openai": "^6.32.0",
"pg": "^8.20.0",
"prisma": "^7.5.0",
"prisma": "^6.7.0",
"socket.io": "^4.8.3",
"winston": "^3.19.0",
"ws": "^8.20.0",
Expand Down
84 changes: 84 additions & 0 deletions backend/src/routes/activity.routes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
import { Request, Response, Router } from 'express';
import { authenticate } from '../auth/auth.middleware.js';
import prisma from '../db/index.js';

const router = Router();

/**
* @route GET /api/v1/dashboard/activity
* @desc Get student engagement activity for heatmap
* @access Private
*/
router.get('/', authenticate, async (req: Request, res: Response): Promise<void> => {
try {
const studentId = req.user!.id;
const days = 365;
const startDate = new Date();
startDate.setDate(startDate.getDate() - days);

// Fetch student's individual activities
const studentActivities = await (prisma as any).studentActivity.findMany({
where: {
studentId,
timestamp: {
gte: startDate,
},
},
orderBy: {
timestamp: 'asc',
},
});

// Fetch class average data (aggregated by day)
// For performance in a real app, this would be pre-computed or cached
const allActivities = await (prisma as any).studentActivity.groupBy({
by: ['timestamp'],
where: {
timestamp: {
gte: startDate,
},
},
_count: {
_all: true,
},
});

const totalStudents = await prisma.student.count();

// Process activities into a map of date -> {count, labs}
const activityMap: Record<string, { count: number; labs: string[] }> = {};
studentActivities.forEach((act: any) => {
const dateStr = act.timestamp.toISOString().split('T')[0];
if (!activityMap[dateStr]) {
activityMap[dateStr] = { count: 0, labs: [] };
}
activityMap[dateStr].count += 1;
activityMap[dateStr].labs.push(act.lessonId);
});

// Process average data
const averageMap: Record<string, number> = {};
allActivities.forEach((act: any) => {
const dateStr = act.timestamp.toISOString().split('T')[0];
const count = act._count?._all || 0;
averageMap[dateStr] = totalStudents > 0 ? count / totalStudents : 0;
});

res.json({
activities: Object.entries(activityMap).map(([date, data]) => ({
date,
count: data.count,
labs: data.labs,
})),
classAverage: Object.entries(averageMap).map(([date, avg]) => ({
date,
count: avg,
})),
});
} catch (error) {
console.error('Failed to fetch activity data:', error);
res.status(500).json({ error: 'Internal server error' });
}
});

export default router;
2 changes: 2 additions & 0 deletions backend/src/routes/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import searchRoutes from './search/search.routes.js';
import studentsRouter from './students.js';
import webhookRouter from './webhooks.js';

import activityRouter from './activity.routes.js';
import analyticsRouter from './analytics.routes.js';
import securityRouter from './security.routes.js';

Expand All @@ -36,6 +37,7 @@ router.use('/learning', learningRoutes);
router.use('/generator', generatorRoutes);
router.use('/search', searchRoutes);
router.use('/user', userRouter);
router.use('/activity', activityRouter);
router.use('/audit', auditRouter);
router.use('/export', exportRouter);

Expand Down
11 changes: 11 additions & 0 deletions backend/src/routes/learning/learning.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,17 @@ export const updateStudentProgress = async (
const completedLessonSet = new Set(existingProgress.completedLessons);

if (input.status === 'completed') {
if (!completedLessonSet.has(input.lessonId)) {
// Log individual lesson completion activity
(prisma as any).studentActivity.create({
data: {
studentId,
courseId,
lessonId: input.lessonId,
action: 'COMPLETED_LESSON',
}
}).catch((err: any) => console.warn('Failed to log student activity:', err));
}
completedLessonSet.add(input.lessonId);
} else {
completedLessonSet.delete(input.lessonId);
Expand Down
Loading
Loading