Skip to content

Commit e1eed49

Browse files
committed
refactor: group event count by week - word learning events
#2199
1 parent ea2b0c7 commit e1eed49

File tree

3 files changed

+50
-63
lines changed

3 files changed

+50
-63
lines changed

src/main/java/ai/elimu/web/analytics/MainAnalyticsController.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ public class MainAnalyticsController {
2828
// private final WordAssessmentEventDao wordAssessmentEventDao;
2929
private final WordLearningEventDao wordLearningEventDao;
3030

31+
// TODO: Numbers
32+
3133
private final StoryBookLearningEventDao storyBookLearningEventDao;
3234

3335
private final VideoLearningEventDao videoLearningEventDao;

src/main/java/ai/elimu/web/analytics/students/StudentController.java

Lines changed: 45 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,21 @@ public String handleRequest(@PathVariable Long studentId, Model model) {
6060
model.addAttribute("numeracySkills", NumeracySkill.values());
6161

6262

63+
// Generate a list of weeks from 6 months ago until now
64+
List<String> weekList = new ArrayList<>();
65+
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-ww");
66+
Calendar calendar6MonthsAgo = Calendar.getInstance();
67+
calendar6MonthsAgo.add(Calendar.MONTH, -6);
68+
Calendar calendarNow = Calendar.getInstance();
69+
Calendar week = calendar6MonthsAgo;
70+
while (!week.after(calendarNow)) {
71+
String weekAsString = simpleDateFormat.format(week.getTime());
72+
weekList.add(weekAsString);
73+
week.add(Calendar.WEEK_OF_YEAR, 1);
74+
}
75+
model.addAttribute("weekList", weekList);
76+
77+
6378
List<LetterSoundAssessmentEvent> letterSoundAssessmentEvents = letterSoundAssessmentEventDao.readAll(student.getAndroidId());
6479
model.addAttribute("letterSoundAssessmentEvents", letterSoundAssessmentEvents);
6580

@@ -69,99 +84,69 @@ public String handleRequest(@PathVariable Long studentId, Model model) {
6984

7085
// Prepare chart data - WordLearningEvents
7186
List<WordLearningEvent> wordLearningEvents = wordLearningEventDao.readAll(student.getAndroidId());
72-
List<String> wordMonthList = new ArrayList<>();
7387
List<Integer> wordEventCountList = new ArrayList<>();
7488
if (!wordLearningEvents.isEmpty()) {
75-
// Group event count by month (e.g. "Aug-2024", "Sep-2024")
76-
Map<String, Integer> eventCountByMonthMap = new HashMap<>();
77-
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("MMM-yyyy");
89+
// Group event count by week (e.g. "2024-09", "2024-26")
90+
Map<String, Integer> eventCountByWeekMap = new HashMap<>();
7891
for (WordLearningEvent event : wordLearningEvents) {
79-
String eventMonth = simpleDateFormat.format(event.getTimestamp().getTime());
80-
eventCountByMonthMap.put(eventMonth, eventCountByMonthMap.getOrDefault(eventMonth, 0) + 1);
92+
String eventWeek = simpleDateFormat.format(event.getTimestamp().getTime());
93+
eventCountByWeekMap.put(eventWeek, eventCountByWeekMap.getOrDefault(eventWeek, 0) + 1);
8194
}
8295

83-
// Iterate each month from 4 years ago until now
84-
Calendar calendar4YearsAgo = Calendar.getInstance();
85-
calendar4YearsAgo.add(Calendar.YEAR, -4);
86-
Calendar calendarNow = Calendar.getInstance();
87-
Calendar month = calendar4YearsAgo;
88-
while (!month.after(calendarNow)) {
89-
String monthAsString = simpleDateFormat.format(month.getTime());
90-
wordMonthList.add(monthAsString);
91-
92-
wordEventCountList.add(eventCountByMonthMap.getOrDefault(monthAsString, 0));
93-
94-
// Increase the date by 1 month
95-
month.add(Calendar.MONTH, 1);
96+
// Iterate each week from 6 months ago until now
97+
week = calendar6MonthsAgo;
98+
while (!week.after(calendarNow)) {
99+
String weekAsString = simpleDateFormat.format(week.getTime());
100+
wordEventCountList.add(eventCountByWeekMap.getOrDefault(weekAsString, 0));
101+
week.add(Calendar.WEEK_OF_YEAR, 1);
96102
}
97103
}
98-
model.addAttribute("wordMonthList", wordMonthList);
99104
model.addAttribute("wordEventCountList", wordEventCountList);
100105
model.addAttribute("wordLearningEvents", wordLearningEvents);
101106

102107

103108
// Prepare chart data - StoryBookLearningEvents
104109
List<StoryBookLearningEvent> storyBookLearningEvents = storyBookLearningEventDao.readAll(student.getAndroidId());
105-
List<String> storyBookMonthList = new ArrayList<>();
106110
List<Integer> storyBookEventCountList = new ArrayList<>();
107111
if (!storyBookLearningEvents.isEmpty()) {
108-
// Group event count by month (e.g. "Aug-2024", "Sep-2024")
109-
Map<String, Integer> eventCountByMonthMap = new HashMap<>();
110-
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("MMM-yyyy");
112+
// Group event count by week (e.g. "2024-09", "2024-26")
113+
Map<String, Integer> eventCountByWeekMap = new HashMap<>();
111114
for (StoryBookLearningEvent event : storyBookLearningEvents) {
112-
String eventMonth = simpleDateFormat.format(event.getTimestamp().getTime());
113-
eventCountByMonthMap.put(eventMonth, eventCountByMonthMap.getOrDefault(eventMonth, 0) + 1);
115+
String eventWeek = simpleDateFormat.format(event.getTimestamp().getTime());
116+
eventCountByWeekMap.put(eventWeek, eventCountByWeekMap.getOrDefault(eventWeek, 0) + 1);
114117
}
115118

116-
// Iterate each month from 4 years ago until now
117-
Calendar calendar4YearsAgo = Calendar.getInstance();
118-
calendar4YearsAgo.add(Calendar.YEAR, -4);
119-
Calendar calendarNow = Calendar.getInstance();
120-
Calendar month = calendar4YearsAgo;
121-
while (!month.after(calendarNow)) {
122-
String monthAsString = simpleDateFormat.format(month.getTime());
123-
storyBookMonthList.add(monthAsString);
124-
125-
storyBookEventCountList.add(eventCountByMonthMap.getOrDefault(monthAsString, 0));
126-
127-
// Increase the date by 1 month
128-
month.add(Calendar.MONTH, 1);
119+
// Iterate each week from 6 months ago until now
120+
week = calendar6MonthsAgo;
121+
while (!week.after(calendarNow)) {
122+
String weekAsString = simpleDateFormat.format(week.getTime());
123+
storyBookEventCountList.add(eventCountByWeekMap.getOrDefault(weekAsString, 0));
124+
week.add(Calendar.WEEK_OF_YEAR, 1);
129125
}
130126
}
131-
model.addAttribute("storyBookMonthList", storyBookMonthList);
132127
model.addAttribute("storyBookEventCountList", storyBookEventCountList);
133128
model.addAttribute("storyBookLearningEvents", storyBookLearningEvents);
134129

135130

136131
// Prepare chart data - VideoLearningEvents
137132
List<VideoLearningEvent> videoLearningEvents = videoLearningEventDao.readAll(student.getAndroidId());
138-
List<String> videoMonthList = new ArrayList<>();
139133
List<Integer> videoEventCountList = new ArrayList<>();
140134
if (!videoLearningEvents.isEmpty()) {
141-
// Group event count by month (e.g. "Aug-2024", "Sep-2024")
142-
Map<String, Integer> eventCountByMonthMap = new HashMap<>();
143-
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("MMM-yyyy");
135+
// Group event count by week (e.g. "2024-09", "2024-26")
136+
Map<String, Integer> eventCountByWeekMap = new HashMap<>();
144137
for (VideoLearningEvent event : videoLearningEvents) {
145-
String eventMonth = simpleDateFormat.format(event.getTimestamp().getTime());
146-
eventCountByMonthMap.put(eventMonth, eventCountByMonthMap.getOrDefault(eventMonth, 0) + 1);
138+
String eventWeek = simpleDateFormat.format(event.getTimestamp().getTime());
139+
eventCountByWeekMap.put(eventWeek, eventCountByWeekMap.getOrDefault(eventWeek, 0) + 1);
147140
}
148141

149-
// Iterate each month from 4 years ago until now
150-
Calendar calendar4YearsAgo = Calendar.getInstance();
151-
calendar4YearsAgo.add(Calendar.YEAR, -4);
152-
Calendar calendarNow = Calendar.getInstance();
153-
Calendar month = calendar4YearsAgo;
154-
while (!month.after(calendarNow)) {
155-
String monthAsString = simpleDateFormat.format(month.getTime());
156-
videoMonthList.add(monthAsString);
157-
158-
videoEventCountList.add(eventCountByMonthMap.getOrDefault(monthAsString, 0));
159-
160-
// Increase the date by 1 month
161-
month.add(Calendar.MONTH, 1);
142+
// Iterate each week from 6 months ago until now
143+
week = calendar6MonthsAgo;
144+
while (!week.after(calendarNow)) {
145+
String weekAsString = simpleDateFormat.format(week.getTime());
146+
videoEventCountList.add(eventCountByWeekMap.getOrDefault(weekAsString, 0));
147+
week.add(Calendar.WEEK_OF_YEAR, 1);
162148
}
163149
}
164-
model.addAttribute("videoMonthList", videoMonthList);
165150
model.addAttribute("videoEventCountList", videoEventCountList);
166151
model.addAttribute("videoLearningEvents", videoLearningEvents);
167152

src/main/webapp/WEB-INF/jsp/analytics/students/id.jsp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@
185185
<canvas id="wordChart"></canvas>
186186
<script>
187187
const wordLabels = [
188-
<c:forEach var="month" items="${wordMonthList}">'${month}',</c:forEach>
188+
<c:forEach var="week" items="${weekList}">'${week}',</c:forEach>
189189
];
190190
const wordData = {
191191
labels: wordLabels,
@@ -232,7 +232,7 @@
232232
<canvas id="storyBookChart"></canvas>
233233
<script>
234234
const storyBookLabels = [
235-
<c:forEach var="month" items="${storyBookMonthList}">'${month}',</c:forEach>
235+
<c:forEach var="week" items="${weekList}">'${week}',</c:forEach>
236236
];
237237
const storyBookData = {
238238
labels: storyBookLabels,
@@ -267,7 +267,7 @@
267267
<canvas id="videoChart"></canvas>
268268
<script>
269269
const videoLabels = [
270-
<c:forEach var="month" items="${videoMonthList}">'${month}',</c:forEach>
270+
<c:forEach var="week" items="${weekList}">'${week}',</c:forEach>
271271
];
272272
const videoData = {
273273
labels: videoLabels,

0 commit comments

Comments
 (0)