forked from JerryIdoko/starked-education
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdataAggregation.ts
More file actions
55 lines (51 loc) · 1.42 KB
/
Copy pathdataAggregation.ts
File metadata and controls
55 lines (51 loc) · 1.42 KB
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
import { Pool } from 'pg';
// @ts-ignore
import pool from '../utils/db';
export class DataAggregationService {
/**
* Aggregates course completion statistics
*/
static async getCourseCompletionStats(courseId: string) {
const query = `
SELECT
COUNT(DISTINCT user_id) as total_enrolled,
COUNT(DISTINCT CASE WHEN completed = true THEN user_id END) as completed_count,
AVG(progress) as average_progress
FROM user_progress
WHERE course_id = $1
`;
try {
const result = await pool.query(query, [courseId]);
return {
courseId,
...result.rows[0]
};
} catch (error) {
console.error('Error aggregating course stats:', error);
throw error;
}
}
/**
* Aggregates user learning activity over time
*/
static async getUserDailyActivity(userId: string, days: number = 30) {
const query = `
SELECT
DATE(last_updated) as activity_date,
COUNT(*) as lessons_completed,
AVG(progress) as daily_avg_progress
FROM user_progress
WHERE user_id = $1
AND last_updated >= NOW() - INTERVAL '${days} days'
GROUP BY DATE(last_updated)
ORDER BY activity_date ASC
`;
try {
const result = await pool.query(query, [userId]);
return result.rows;
} catch (error) {
console.error('Error aggregating user activity:', error);
throw error;
}
}
}