Skip to content

Commit

Permalink
refactor(analytics): limit to 100 most recent video learning events (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
jo-elimu authored Nov 14, 2024
2 parents 334677a + 7154c16 commit 1d370d7
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,39 +34,46 @@ public String handleRequest(Model model) {
logger.info("handleRequest");

List<VideoLearningEvent> videoLearningEvents = videoLearningEventDao.readAllOrderedByTimestamp(OrderDirection.DESC);
logger.info("videoLearningEvents.size(): " + videoLearningEvents.size());
for (VideoLearningEvent videoLearningEvent : videoLearningEvents) {
videoLearningEvent.setAndroidId(AnalyticsHelper.redactAndroidId(videoLearningEvent.getAndroidId()));
}
model.addAttribute("videoLearningEvents", videoLearningEvents);

model.addAttribute("videoLearningEventCount", videoLearningEvents.size());
if (videoLearningEvents.size() <= 100) {
model.addAttribute("videoLearningEvents", videoLearningEvents);
} else {
model.addAttribute("videoLearningEvents", videoLearningEvents.subList(0, 100));
}

// Prepare chart data
List<String> monthList = new ArrayList<>();
List<String> weekList = new ArrayList<>();
List<Integer> eventCountList = new ArrayList<>();
if (!videoLearningEvents.isEmpty()) {
// Group event count by month (e.g. "Aug-2024", "Sep-2024")
// Group event count by week (e.g. "32-2024", "37-2024")
Map<String, Integer> eventCountByMonthMap = new HashMap<>();
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("MMM-yyyy");
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("ww-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);
// Iterate each week from 6 months ago until now
Calendar calendar6MonthsAgo = Calendar.getInstance();
calendar6MonthsAgo.add(Calendar.MONTH, -6); // 26 weeks
Calendar calendarNow = Calendar.getInstance();
Calendar month = calendar4YearsAgo;
while (!month.after(calendarNow)) {
String monthAsString = simpleDateFormat.format(month.getTime());
monthList.add(monthAsString);
Calendar week = calendar6MonthsAgo;
while (!week.after(calendarNow)) {
String weekAsString = simpleDateFormat.format(week.getTime());
weekList.add(weekAsString);

eventCountList.add(eventCountByMonthMap.getOrDefault(monthAsString, 0));
eventCountList.add(eventCountByMonthMap.getOrDefault(weekAsString, 0));

// Increase the date by 1 month
month.add(Calendar.MONTH, 1);
// Increase the date by 1 week
week.add(Calendar.WEEK_OF_YEAR, 1);
}
}
model.addAttribute("monthList", monthList);
model.addAttribute("weekList", weekList);
model.addAttribute("eventCountList", eventCountList);

return "analytics/video-learning-event/list";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<content:title>
VideoLearningEvents (${fn:length(videoLearningEvents)})
VideoLearningEvents (${videoLearningEventCount})
</content:title>

<content:section cssId="videoLearningEventsPage">
Expand All @@ -9,7 +9,7 @@
<canvas id="chart"></canvas>
<script>
const labels = [
<c:forEach var="month" items="${monthList}">'${month}',</c:forEach>
<c:forEach var="week" items="${weekList}">'${week}',</c:forEach>
];
const data = {
labels: labels,
Expand Down

0 comments on commit 1d370d7

Please sign in to comment.