diff --git a/src/main/java/ai/elimu/web/analytics/StoryBookLearningEventListController.java b/src/main/java/ai/elimu/web/analytics/StoryBookLearningEventListController.java index 95f9dfc5d..2e98725f2 100644 --- a/src/main/java/ai/elimu/web/analytics/StoryBookLearningEventListController.java +++ b/src/main/java/ai/elimu/web/analytics/StoryBookLearningEventListController.java @@ -2,7 +2,14 @@ import ai.elimu.dao.StoryBookLearningEventDao; import ai.elimu.model.analytics.StoryBookLearningEvent; + +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.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.springframework.beans.factory.annotation.Autowired; @@ -26,6 +33,36 @@ public String handleRequest(Model model) { List storyBookLearningEvents = storyBookLearningEventDao.readAllOrderedByTime(); model.addAttribute("storyBookLearningEvents", storyBookLearningEvents); + + // Prepare data for chart in UI + List monthList = new ArrayList<>(); + List eventCountList = new ArrayList<>(); + if (!storyBookLearningEvents.isEmpty()) { + // Group event count by month (e.g. "Aug-2024", "Sep-2024") + Map 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()); + monthList.add(monthAsString); + + eventCountList.add(eventCountByMonthMap.getOrDefault(monthAsString, 0)); + + // Increase the date by 1 month + month.add(Calendar.MONTH, 1); + } + } + model.addAttribute("monthList", monthList); + model.addAttribute("eventCountList", eventCountList); return "analytics/storybook-learning-event/list"; } diff --git a/src/main/webapp/WEB-INF/jsp/analytics/layout.jsp b/src/main/webapp/WEB-INF/jsp/analytics/layout.jsp index 5a4e18cea..0834dfa5f 100644 --- a/src/main/webapp/WEB-INF/jsp/analytics/layout.jsp +++ b/src/main/webapp/WEB-INF/jsp/analytics/layout.jsp @@ -45,111 +45,29 @@ diff --git a/src/main/webapp/WEB-INF/jsp/analytics/storybook-learning-event/list.jsp b/src/main/webapp/WEB-INF/jsp/analytics/storybook-learning-event/list.jsp index 295c83d1a..65555d661 100644 --- a/src/main/webapp/WEB-INF/jsp/analytics/storybook-learning-event/list.jsp +++ b/src/main/webapp/WEB-INF/jsp/analytics/storybook-learning-event/list.jsp @@ -10,20 +10,15 @@