@RequiresApi(Build.VERSION_CODES.O) fun processVideoData(videoDetails: List<VideoDetailResponse>): List<HourlyVideoData> { val formatter = DateTimeFormatter.ofPattern("hh:mm a") // Create 24 hour slots val hourSlots = (0..23).map { hour -> val startTime = LocalTime.of(hour, 0) val endTime = startTime.plusHours(1) "${startTime.format(formatter)} - ${endTime.format(formatter)}" } // Initialize map with empty lists for each hour slot val groupedData = hourSlots.associateWith { mutableListOf<VideoDetailResponse>() } videoDetails.forEach { videoDetail -> val startDateTime = Instant.ofEpochSecond(videoDetail.starttimestamp!!) .atZone(ZoneId.systemDefault()) .toLocalDateTime() // Determine the hour slot for this video val hourSlot = "${startDateTime.withMinute(0).format(formatter)} - ${startDateTime.withMinute(0).plusHours(1).format(formatter)}" // Add video to the corresponding hour slot groupedData[hourSlot]?.add(videoDetail) } return groupedData.map { (hourSlot, details) -> HourlyVideoData(hourSlot, details) }.sortedBy { it.hoursTime } }
You can use the following code to get the hourly video data. fun processVideoData(videoId: String) { val video = Video.findById(videoId) val videoData = video?.data?.videoData val videoDataList = videoData?.map { it.data } val videoDataHourly = videoDataList?.map { it.hour