Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(analytics): limit to 100 most recent video learning events #1950

Merged
merged 1 commit into from
Nov 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading