Skip to content

Commit

Permalink
DMP-4385 Filter out 0 second audios from ATS processing
Browse files Browse the repository at this point in the history
Updated the ATS filtering to filter out audio files that have less than a second start and end time, start or end time is null or start time is after end time
  • Loading branch information
karen-hedges committed Jan 3, 2025
1 parent 1e0ae75 commit 1decdf4
Show file tree
Hide file tree
Showing 3 changed files with 132 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -100,10 +100,12 @@ public List<AudioFileInfo> processAudioForPlaybacks(Map<MediaEntity, Path> media

List<AudioFileInfo> concatenatedAndMergedAudioFileInfos = new ArrayList<>();
if (isNotEmpty(audioFileInfos)) {
// Used for logging only
String audioFilenames = audioFileInfos.stream().map(AudioFileInfo::getMediaFile).collect(Collectors.joining(", "));

List<ChannelAudio> concatenationsList = new ArrayList<>();


if (isWellFormedAudio(audioFileInfos)) {
log.debug("Audio files {} are well formed", audioFilenames);
List<ChannelAudio> concatenatedAudios = concatenateByChannelWithGaps(audioFileInfos);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import java.nio.file.Path;
import java.time.OffsetDateTime;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
Expand All @@ -47,6 +48,7 @@
import java.util.concurrent.ExecutionException;
import java.util.stream.Collectors;

import static java.util.Objects.nonNull;
import static uk.gov.hmcts.darts.audio.enums.MediaRequestStatus.FAILED;
import static uk.gov.hmcts.darts.audio.enums.MediaRequestStatus.OPEN;
import static uk.gov.hmcts.darts.audiorequests.model.AudioRequestType.DOWNLOAD;
Expand Down Expand Up @@ -223,7 +225,15 @@ private void processAudioRequest(MediaRequestEntity mediaRequestEntity) {
List<MediaEntity> filterMediaByMediaRequestTimeframeAndSortByStartTimeAndChannel(List<MediaEntity> mediaEntitiesForRequest,
MediaRequestEntity mediaRequestEntity) {
return mediaEntitiesForRequest.stream()
.filter(media -> nonNull(media.getStart()))
.filter(media -> nonNull(media.getEnd()))
// Filter out media where the media start and media end times are the same
.filter(media -> !media.getStart().truncatedTo(ChronoUnit.SECONDS).isEqual(media.getEnd().truncatedTo(ChronoUnit.SECONDS)))
// Filter out media where the media start time is after the media end time
.filter(media -> media.getStart().isBefore(media.getEnd()))
// Filter out media where the media end time is after the media request start time
.filter(media -> mediaRequestEntity.getStartTime().isBefore(media.getEnd()))
// Filter out media where the media start time is before the media request end time
.filter(media -> media.getStart().isBefore(mediaRequestEntity.getEndTime()))
.sorted(MEDIA_START_TIME_CHANNEL_COMPARATOR)
.collect(Collectors.toList());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,119 @@ void testHandleKedaInvocationForMediaRequestsCaseNull() {
}

@Test
void filterMediaByMediaRequestDatesWithStartDateExactRequestAndEndDateExactRequest() {
void filterMediaByMediaRequestTimeframeAndSortByStartTimeAndChannel_ReturnsFilteredData_WhichContainsMediaWithStartDateAndEndDateTheSame() {
List<MediaEntity> mediaEntities = createMediaEntities(TIME_12_00, TIME_12_20, TIME_12_20, TIME_12_20, TIME_12_40, TIME_13_00);
MediaRequestEntity mediaRequestEntity = createMediaRequest(TIME_12_00, TIME_13_00);
List<MediaEntity> mediaEntitiesResult = audioTransformationService.filterMediaByMediaRequestTimeframeAndSortByStartTimeAndChannel(
mediaEntities,
mediaRequestEntity
);

assertEquals(2, mediaEntitiesResult.size());
assertEquals(TIME_12_00, mediaEntitiesResult.get(0).getStart());
assertEquals(TIME_12_20, mediaEntitiesResult.get(0).getEnd());
assertEquals(TIME_12_40, mediaEntitiesResult.get(1).getStart());
assertEquals(TIME_13_00, mediaEntitiesResult.get(1).getEnd());
}

@Test
void filterMediaByMediaRequestTimeframeAndSortByStartTimeAndChannel_ReturnsFilteredData_WhichContainsOnlyMediaWithStartDateAndEndDateTheSame() {
List<MediaEntity> mediaEntities = createMediaEntities(TIME_12_00, TIME_12_00, TIME_12_20, TIME_12_20, TIME_13_00, TIME_13_00);
MediaRequestEntity mediaRequestEntity = createMediaRequest(TIME_12_00, TIME_13_00);
List<MediaEntity> mediaEntitiesResult = audioTransformationService.filterMediaByMediaRequestTimeframeAndSortByStartTimeAndChannel(
mediaEntities,
mediaRequestEntity
);

assertEquals(0, mediaEntitiesResult.size());
}

@Test
void filterMediaByMediaRequestTimeframeAndSortByStartTimeAndChannel_ReturnsFilteredData_WhichContainsMediaWithStartDateAfterEndDate() {
List<MediaEntity> mediaEntities = createMediaEntities(TIME_12_00, TIME_12_20, TIME_12_40, TIME_12_20, TIME_12_40, TIME_13_00);
MediaRequestEntity mediaRequestEntity = createMediaRequest(TIME_12_00, TIME_13_00);
List<MediaEntity> mediaEntitiesResult = audioTransformationService.filterMediaByMediaRequestTimeframeAndSortByStartTimeAndChannel(
mediaEntities,
mediaRequestEntity
);

assertEquals(2, mediaEntitiesResult.size());
assertEquals(TIME_12_00, mediaEntitiesResult.get(0).getStart());
assertEquals(TIME_12_20, mediaEntitiesResult.get(0).getEnd());
assertEquals(TIME_12_40, mediaEntitiesResult.get(1).getStart());
assertEquals(TIME_13_00, mediaEntitiesResult.get(1).getEnd());
}

@Test
void filterMediaByMediaRequestTimeframeAndSortByStartTimeAndChannel_ReturnsFilteredData_WhichContainsOnlyMediaWithStartDateAfterEndDate() {
List<MediaEntity> mediaEntities = createMediaEntities(TIME_12_20, TIME_12_00, TIME_12_40, TIME_12_20, TIME_13_00, TIME_12_40);
MediaRequestEntity mediaRequestEntity = createMediaRequest(TIME_12_00, TIME_13_00);
List<MediaEntity> mediaEntitiesResult = audioTransformationService.filterMediaByMediaRequestTimeframeAndSortByStartTimeAndChannel(
mediaEntities,
mediaRequestEntity
);

assertEquals(0, mediaEntitiesResult.size());
}

@Test
void filterMediaByMediaRequestTimeframeAndSortByStartTimeAndChannel_ReturnsFilteredData_WhichContainsMediaWithNullStartDate() {
List<MediaEntity> mediaEntities = createMediaEntities(TIME_12_00, TIME_12_20, null, TIME_12_20, TIME_12_40, TIME_13_00);
MediaRequestEntity mediaRequestEntity = createMediaRequest(TIME_12_00, TIME_13_00);
List<MediaEntity> mediaEntitiesResult = audioTransformationService.filterMediaByMediaRequestTimeframeAndSortByStartTimeAndChannel(
mediaEntities,
mediaRequestEntity
);

assertEquals(2, mediaEntitiesResult.size());
assertEquals(TIME_12_00, mediaEntitiesResult.get(0).getStart());
assertEquals(TIME_12_20, mediaEntitiesResult.get(0).getEnd());
assertEquals(TIME_12_40, mediaEntitiesResult.get(1).getStart());
assertEquals(TIME_13_00, mediaEntitiesResult.get(1).getEnd());
}

@Test
void filterMediaByMediaRequestTimeframeAndSortByStartTimeAndChannel_ReturnsFilteredData_WhichContainsOnlyMediaWithNullStartDates() {
List<MediaEntity> mediaEntities = createMediaEntities(null, TIME_12_00, null, TIME_12_20, null, TIME_12_40);
MediaRequestEntity mediaRequestEntity = createMediaRequest(TIME_12_00, TIME_13_00);
List<MediaEntity> mediaEntitiesResult = audioTransformationService.filterMediaByMediaRequestTimeframeAndSortByStartTimeAndChannel(
mediaEntities,
mediaRequestEntity
);

assertEquals(0, mediaEntitiesResult.size());
}

@Test
void filterMediaByMediaRequestTimeframeAndSortByStartTimeAndChannel_ReturnsFilteredData_WhichContainsMediaWithNullEndDate() {
List<MediaEntity> mediaEntities = createMediaEntities(TIME_12_00, TIME_12_20, TIME_12_40, null, TIME_12_40, TIME_13_00);
MediaRequestEntity mediaRequestEntity = createMediaRequest(TIME_12_00, TIME_13_00);
List<MediaEntity> mediaEntitiesResult = audioTransformationService.filterMediaByMediaRequestTimeframeAndSortByStartTimeAndChannel(
mediaEntities,
mediaRequestEntity
);

assertEquals(2, mediaEntitiesResult.size());
assertEquals(TIME_12_00, mediaEntitiesResult.get(0).getStart());
assertEquals(TIME_12_20, mediaEntitiesResult.get(0).getEnd());
assertEquals(TIME_12_40, mediaEntitiesResult.get(1).getStart());
assertEquals(TIME_13_00, mediaEntitiesResult.get(1).getEnd());
}

@Test
void filterMediaByMediaRequestTimeframeAndSortByStartTimeAndChannel_ReturnsFilteredData_WhichContainsOnlyMediaNullEndDates() {
List<MediaEntity> mediaEntities = createMediaEntities(TIME_12_20, null, TIME_12_40, null, TIME_13_00, null);
MediaRequestEntity mediaRequestEntity = createMediaRequest(TIME_12_00, TIME_13_00);
List<MediaEntity> mediaEntitiesResult = audioTransformationService.filterMediaByMediaRequestTimeframeAndSortByStartTimeAndChannel(
mediaEntities,
mediaRequestEntity
);

assertEquals(0, mediaEntitiesResult.size());
}

@Test
void filterMediaByMediaRequestTimeframeAndSortByStartTimeAndChannel_ReturnsFilteredData_WithStartDateExactRequestAndEndDateExactRequest() {
List<MediaEntity> mediaEntities = createMediaEntities(TIME_12_00, TIME_12_20, TIME_12_20, TIME_12_40, TIME_12_40, TIME_13_00);
MediaRequestEntity mediaRequestEntity = createMediaRequest(TIME_12_00, TIME_13_00);
List<MediaEntity> mediaEntitiesResult = audioTransformationService.filterMediaByMediaRequestTimeframeAndSortByStartTimeAndChannel(
Expand All @@ -260,7 +372,7 @@ void filterMediaByMediaRequestDatesWithStartDateExactRequestAndEndDateExactReque
}

@Test
void filterMediaByMediaRequestDatesWithStartDateAfterRequestAndEndDateBeforeRequest() {
void filterMediaByMediaRequestTimeframeAndSortByStartTimeAndChannel_ReturnsFilteredData_WithStartDateAfterRequestAndEndDateBeforeRequest() {
List<MediaEntity> mediaEntities = createMediaEntities(TIME_12_01, TIME_12_20, TIME_12_20, TIME_12_40, TIME_12_40, TIME_12_59);
MediaRequestEntity mediaRequestEntity = createMediaRequest(TIME_12_00, TIME_13_00);
List<MediaEntity> mediaEntitiesResult = audioTransformationService.filterMediaByMediaRequestTimeframeAndSortByStartTimeAndChannel(
Expand All @@ -273,7 +385,7 @@ void filterMediaByMediaRequestDatesWithStartDateAfterRequestAndEndDateBeforeRequ
}

@Test
void filterMediaByMediaRequestDatesWithStartDateBeforeRequestAndEndDateAfterRequest() {
void filterMediaByMediaRequestTimeframeAndSortByStartTimeAndChannel_ReturnsFilteredData_WithStartDateBeforeRequestAndEndDateAfterRequest() {
List<MediaEntity> mediaEntities = createMediaEntities(TIME_11_59, TIME_12_20, TIME_12_20, TIME_12_40, TIME_12_40, TIME_13_01);
MediaRequestEntity mediaRequestEntity = createMediaRequest(TIME_12_00, TIME_13_00);
List<MediaEntity> mediaEntitiesResult = audioTransformationService.filterMediaByMediaRequestTimeframeAndSortByStartTimeAndChannel(
Expand All @@ -286,7 +398,7 @@ void filterMediaByMediaRequestDatesWithStartDateBeforeRequestAndEndDateAfterRequ
}

@Test
void filterMediaByMediaRequestDatesWithStartDateAndEndDateExactMiddleMediaMatch() {
void filterMediaByMediaRequestTimeframeAndSortByStartTimeAndChannel_ReturnsFilteredData_WithStartDateAndEndDateExactMiddleMediaMatch() {
List<MediaEntity> mediaEntities = createMediaEntities(TIME_11_59, TIME_12_20, TIME_12_20, TIME_12_40, TIME_12_40, TIME_13_01);
MediaRequestEntity mediaRequestEntity = createMediaRequest(TIME_12_20, TIME_12_40);
List<MediaEntity> mediaEntitiesResult = audioTransformationService.filterMediaByMediaRequestTimeframeAndSortByStartTimeAndChannel(
Expand All @@ -299,7 +411,7 @@ void filterMediaByMediaRequestDatesWithStartDateAndEndDateExactMiddleMediaMatch(
}

@Test
void filterMediaByMediaRequestDatesWithStartDateBetweenRequestAndEndDateBetweenRequest() {
void filterMediaByMediaRequestTimeframeAndSortByStartTimeAndChannel_ReturnsFilteredData_WithStartDateBetweenRequestAndEndDateBetweenRequest() {
List<MediaEntity> mediaEntities = createMediaEntities(TIME_12_00, TIME_12_20, TIME_12_20, TIME_12_40, TIME_12_40, TIME_13_00);
MediaRequestEntity mediaRequestEntity = createMediaRequest(TIME_12_21, TIME_12_39);
List<MediaEntity> mediaEntitiesResult = audioTransformationService.filterMediaByMediaRequestTimeframeAndSortByStartTimeAndChannel(
Expand All @@ -312,7 +424,7 @@ void filterMediaByMediaRequestDatesWithStartDateBetweenRequestAndEndDateBetweenR
}

@Test
void filterMediaByMediaRequestDatesWithRequestStartAndEndDateOutSideMediaRange() {
void filterMediaByMediaRequestTimeframeAndSortByStartTimeAndChannel_ReturnsFilteredData_WithRequestStartAndEndDateOutSideMediaRange() {
List<MediaEntity> mediaEntities = createMediaEntities(TIME_12_00, TIME_12_20, TIME_12_20, TIME_12_40, TIME_12_40, TIME_12_59);
MediaRequestEntity mediaRequestEntity = createMediaRequest(TIME_13_00, TIME_13_01);
List<MediaEntity> mediaEntitiesResult = audioTransformationService.filterMediaByMediaRequestTimeframeAndSortByStartTimeAndChannel(
Expand All @@ -323,7 +435,7 @@ void filterMediaByMediaRequestDatesWithRequestStartAndEndDateOutSideMediaRange()
}

@Test
void filterMediaByMediaRequestDatesWithRequestStartTimeMatchesAnAudioEndTime() {
void filterMediaByMediaRequestTimeframeAndSortByStartTimeAndChannel_ReturnsFilteredData_WithRequestStartTimeMatchesAnAudioEndTime() {
List<MediaEntity> mediaEntities = createMediaEntities(TIME_09_59, TIME_10_00, TIME_10_05, TIME_10_10, TIME_10_15, TIME_10_20);
MediaRequestEntity mediaRequestEntity = createMediaRequest(TIME_10_00, TIME_10_10);
List<MediaEntity> mediaEntitiesResult = audioTransformationService.filterMediaByMediaRequestTimeframeAndSortByStartTimeAndChannel(
Expand All @@ -337,7 +449,7 @@ void filterMediaByMediaRequestDatesWithRequestStartTimeMatchesAnAudioEndTime() {
}

@Test
void filterMediaByMediaRequestDatesWithRequestEndTimeMatchesAnAudioStartTime() {
void filterMediaByMediaRequestTimeframeAndSortByStartTimeAndChannel_ReturnsFilteredData_WithRequestEndTimeMatchesAnAudioStartTime() {
List<MediaEntity> mediaEntities = createMediaEntities(TIME_09_59, TIME_10_00, TIME_10_05, TIME_10_10, TIME_10_15, TIME_10_20);
MediaRequestEntity mediaRequestEntity = createMediaRequest(TIME_09_59, TIME_10_15);
List<MediaEntity> mediaEntitiesResult = audioTransformationService.filterMediaByMediaRequestTimeframeAndSortByStartTimeAndChannel(
Expand Down

0 comments on commit 1decdf4

Please sign in to comment.