-
-
Notifications
You must be signed in to change notification settings - Fork 71
Expand file tree
/
Copy pathStudentController.java
More file actions
162 lines (131 loc) · 6.64 KB
/
StudentController.java
File metadata and controls
162 lines (131 loc) · 6.64 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
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
package ai.elimu.web.analytics.students;
import ai.elimu.dao.LetterSoundAssessmentEventDao;
import ai.elimu.dao.StoryBookLearningEventDao;
import ai.elimu.dao.StudentDao;
import ai.elimu.dao.VideoLearningEventDao;
import ai.elimu.dao.WordLearningEventDao;
import ai.elimu.entity.analytics.LetterSoundAssessmentEvent;
import ai.elimu.entity.analytics.StoryBookLearningEvent;
import ai.elimu.entity.analytics.VideoLearningEvent;
import ai.elimu.entity.analytics.WordLearningEvent;
import ai.elimu.entity.analytics.students.Student;
import ai.elimu.util.AnalyticsHelper;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
@RequestMapping("/analytics/students/{studentId}")
@RequiredArgsConstructor
@Slf4j
public class StudentController {
private final StudentDao studentDao;
private final LetterSoundAssessmentEventDao letterSoundAssessmentEventDao;
private final WordLearningEventDao wordLearningEventDao;
private final StoryBookLearningEventDao storyBookLearningEventDao;
private final VideoLearningEventDao videoLearningEventDao;
@GetMapping
public String handleRequest(@PathVariable Long studentId, Model model) {
log.info("handleRequest");
Student student = studentDao.read(studentId);
log.info("student.getAndroidId(): " + student.getAndroidId());
List<LetterSoundAssessmentEvent> letterSoundAssessmentEvents = letterSoundAssessmentEventDao.readAll();
model.addAttribute("letterSoundAssessmentEvents", letterSoundAssessmentEvents);
// Prepare chart data - WordLearningEvents
List<WordLearningEvent> wordLearningEvents = wordLearningEventDao.readAll();
List<String> wordMonthList = new ArrayList<>();
List<Integer> wordEventCountList = new ArrayList<>();
if (!wordLearningEvents.isEmpty()) {
// Group event count by month (e.g. "Aug-2024", "Sep-2024")
Map<String, Integer> eventCountByMonthMap = new HashMap<>();
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("MMM-yyyy");
for (WordLearningEvent event : wordLearningEvents) {
String eventMonth = simpleDateFormat.format(event.getTimestamp().getTime());
eventCountByMonthMap.put(eventMonth, eventCountByMonthMap.getOrDefault(eventMonth, 0) + 1);
}
// Iterate each month from 4 years ago until now
Calendar calendar4YearsAgo = Calendar.getInstance();
calendar4YearsAgo.add(Calendar.YEAR, -4);
Calendar calendarNow = Calendar.getInstance();
Calendar month = calendar4YearsAgo;
while (!month.after(calendarNow)) {
String monthAsString = simpleDateFormat.format(month.getTime());
wordMonthList.add(monthAsString);
wordEventCountList.add(eventCountByMonthMap.getOrDefault(monthAsString, 0));
// Increase the date by 1 month
month.add(Calendar.MONTH, 1);
}
}
model.addAttribute("wordMonthList", wordMonthList);
model.addAttribute("wordEventCountList", wordEventCountList);
model.addAttribute("wordLearningEvents", wordLearningEvents);
// Prepare chart data - StoryBookLearningEvents
List<StoryBookLearningEvent> storyBookLearningEvents = storyBookLearningEventDao.readAll();
List<String> storyBookMonthList = new ArrayList<>();
List<Integer> storyBookEventCountList = new ArrayList<>();
if (!storyBookLearningEvents.isEmpty()) {
// Group event count by month (e.g. "Aug-2024", "Sep-2024")
Map<String, Integer> eventCountByMonthMap = new HashMap<>();
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("MMM-yyyy");
for (StoryBookLearningEvent event : storyBookLearningEvents) {
String eventMonth = simpleDateFormat.format(event.getTimestamp().getTime());
eventCountByMonthMap.put(eventMonth, eventCountByMonthMap.getOrDefault(eventMonth, 0) + 1);
}
// Iterate each month from 4 years ago until now
Calendar calendar4YearsAgo = Calendar.getInstance();
calendar4YearsAgo.add(Calendar.YEAR, -4);
Calendar calendarNow = Calendar.getInstance();
Calendar month = calendar4YearsAgo;
while (!month.after(calendarNow)) {
String monthAsString = simpleDateFormat.format(month.getTime());
storyBookMonthList.add(monthAsString);
storyBookEventCountList.add(eventCountByMonthMap.getOrDefault(monthAsString, 0));
// Increase the date by 1 month
month.add(Calendar.MONTH, 1);
}
}
model.addAttribute("storyBookMonthList", storyBookMonthList);
model.addAttribute("storyBookEventCountList", storyBookEventCountList);
model.addAttribute("storyBookLearningEvents", storyBookLearningEvents);
// Prepare chart data - VideoLearningEvents
List<VideoLearningEvent> videoLearningEvents = videoLearningEventDao.readAll();
List<String> videoMonthList = new ArrayList<>();
List<Integer> videoEventCountList = new ArrayList<>();
if (!videoLearningEvents.isEmpty()) {
// Group event count by month (e.g. "Aug-2024", "Sep-2024")
Map<String, Integer> eventCountByMonthMap = new HashMap<>();
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("MMM-yyyy");
for (VideoLearningEvent event : videoLearningEvents) {
String eventMonth = simpleDateFormat.format(event.getTimestamp().getTime());
eventCountByMonthMap.put(eventMonth, eventCountByMonthMap.getOrDefault(eventMonth, 0) + 1);
}
// Iterate each month from 4 years ago until now
Calendar calendar4YearsAgo = Calendar.getInstance();
calendar4YearsAgo.add(Calendar.YEAR, -4);
Calendar calendarNow = Calendar.getInstance();
Calendar month = calendar4YearsAgo;
while (!month.after(calendarNow)) {
String monthAsString = simpleDateFormat.format(month.getTime());
videoMonthList.add(monthAsString);
videoEventCountList.add(eventCountByMonthMap.getOrDefault(monthAsString, 0));
// Increase the date by 1 month
month.add(Calendar.MONTH, 1);
}
}
model.addAttribute("videoMonthList", videoMonthList);
model.addAttribute("videoEventCountList", videoEventCountList);
model.addAttribute("videoLearningEvents", videoLearningEvents);
student.setAndroidId(AnalyticsHelper.redactAndroidId(student.getAndroidId()));
model.addAttribute("student", student);
return "analytics/students/id";
}
}