-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathanalytics.js
83 lines (73 loc) · 2.72 KB
/
analytics.js
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
let firebase = require('./functions/db-config');
const db = firebase.admin.firestore();
const classesRef = db.collection('classTimes');
const linksRef = db.collection('zoomLinks');
const loopStudents = async () => {
const snapshot = await classesRef.get();
const studentData = [];
snapshot.forEach((doc) => {
studentData.push(doc.ref.collection('students').get());
})
return Promise.all(studentData);
}
const loopLinks = async () => {
const snapshot = await linksRef.get();
const linksData = [];
snapshot.forEach((doc) => {
linksData.push({course: doc.id, links: doc.data()});
})
return linksData;
}
const getUniqueStudents = () => {
const studentsMap = {};
loopStudents().then(res => {
res.forEach(studentSnapshot => {
studentSnapshot.forEach(student => {
studentsMap[student.id] = true;
})
})
const uniqueStudentsList = Object.keys(studentsMap);
// Log all students
console.log(uniqueStudentsList);
// Log the number of unique
console.log(`Number of unique students: ${uniqueStudentsList.length}`);
})
}
const getUniqueCourses = () => {
const coursesMap = {};
loopStudents().then(res => {
res.forEach(studentSnapshot => {
studentSnapshot.forEach(student => {
const { course, section } = student.data();
if (coursesMap[`${course} ${section}`]) {
coursesMap[`${course} ${section}`].count += 1;
} else {
coursesMap[`${course} ${section}`] = {count: 1, hasLink: false}
}
})
})
// Loop through links to see they exist for a course
loopLinks().then(res => {
res.forEach(courseObj => {
const { course, links } = courseObj;
Object.keys(links).forEach((section) => {
if (coursesMap[`${course} ${section}`]) coursesMap[`${course} ${section}`].hasLink = true;
})
})
// Build array to visualize most popular courses
const classesArray = [];
Object.entries(coursesMap).forEach(([className, info]) => {
classesArray.push({className: className, count: info.count, hasLink: info.hasLink})
})
// Sort list
classesArray.sort((a, b) => (a.count > b.count) ? 1 : -1);
// Log courses array
console.log(classesArray);
// Log number of unique courses
console.log(`Number of unique classes: ${classesArray.length}`);
})
})
}
// Run functions to console log analytics
getUniqueStudents();
// getUniqueCourses();