This repository has been archived by the owner on Dec 17, 2024. It is now read-only.
forked from Onlineberatung/onlineBeratung-statisticsService
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: implementation of stop video call listener and business logic
- Loading branch information
Showing
16 changed files
with
505 additions
and
61 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
57 changes: 57 additions & 0 deletions
57
...java/de/caritas/cob/statisticsservice/api/statistics/listener/StartVideoCallListener.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package de.caritas.cob.statisticsservice.api.statistics.listener; | ||
|
||
import de.caritas.cob.statisticsservice.api.model.StartVideoCallSatisticsEventMessage; | ||
import de.caritas.cob.statisticsservice.api.service.UserStatisticsService; | ||
import de.caritas.cob.statisticsservice.api.statistics.model.StatisticsEvent; | ||
import de.caritas.cob.statisticsservice.api.statistics.model.StatisticsEventBuilder; | ||
import de.caritas.cob.statisticsservice.api.statistics.model.meta.StartVideoCallMetaData; | ||
import de.caritas.cob.statisticsservice.api.statistics.model.meta.VideoCallStatus; | ||
import java.time.temporal.ChronoUnit; | ||
import lombok.NonNull; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.amqp.rabbit.annotation.RabbitListener; | ||
import org.springframework.data.mongodb.core.MongoTemplate; | ||
import org.springframework.stereotype.Service; | ||
|
||
/** AMQP Listener for start video call message statistics event. */ | ||
@Service | ||
@RequiredArgsConstructor | ||
public class StartVideoCallListener { | ||
|
||
private final @NonNull MongoTemplate mongoTemplate; | ||
private final @NonNull UserStatisticsService userStatisticsService; | ||
|
||
/** | ||
* Consumer for start video call statics statistics event. | ||
* | ||
* @param eventMessage the {@link StartVideoCallSatisticsEventMessage} instance | ||
*/ | ||
@RabbitListener( | ||
id = "start-video-call-event-listener", | ||
queues = "#{rabbitMqConfig.QUEUE_NAME_START_VIDEO_CALL}", | ||
containerFactory = "simpleRabbitListenerContainerFactory") | ||
public void receiveMessage(StartVideoCallSatisticsEventMessage eventMessage) { | ||
|
||
StatisticsEvent statisticsEvent = | ||
StatisticsEventBuilder.getInstance( | ||
() -> | ||
userStatisticsService.retrieveSessionViaSessionId(eventMessage.getSessionId())) | ||
.withEventType(eventMessage.getEventType()) | ||
.withTimestamp(eventMessage.getTimestamp().truncatedTo(ChronoUnit.SECONDS).toInstant()) | ||
.withUserId(eventMessage.getUserId()) | ||
.withUserRole(eventMessage.getUserRole()) | ||
.withMetaData(buildMetaData(eventMessage)) | ||
.build(); | ||
|
||
mongoTemplate.insert(statisticsEvent); | ||
} | ||
|
||
private StartVideoCallMetaData buildMetaData(StartVideoCallSatisticsEventMessage eventMessage) { | ||
return StartVideoCallMetaData.builder() | ||
.videoCallUuid(eventMessage.getVideoCallUuid()) | ||
.duration(0) | ||
.timestampStop(null) | ||
.status(VideoCallStatus.ONGOING) | ||
.build(); | ||
} | ||
} |
94 changes: 94 additions & 0 deletions
94
.../java/de/caritas/cob/statisticsservice/api/statistics/listener/StopVideoCallListener.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
package de.caritas.cob.statisticsservice.api.statistics.listener; | ||
|
||
import de.caritas.cob.statisticsservice.api.model.EventType; | ||
import de.caritas.cob.statisticsservice.api.model.StopVideoCallStatisticsEventMessage; | ||
import de.caritas.cob.statisticsservice.api.statistics.model.StatisticsEvent; | ||
import de.caritas.cob.statisticsservice.api.statistics.model.meta.StartVideoCallMetaData; | ||
import de.caritas.cob.statisticsservice.api.statistics.model.meta.VideoCallStatus; | ||
import java.time.Duration; | ||
import java.util.List; | ||
import lombok.NonNull; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.amqp.AmqpException; | ||
import org.springframework.amqp.rabbit.annotation.RabbitListener; | ||
import org.springframework.data.mongodb.core.MongoTemplate; | ||
import org.springframework.data.mongodb.core.query.Criteria; | ||
import org.springframework.data.mongodb.core.query.Query; | ||
import org.springframework.stereotype.Service; | ||
|
||
/** AMQP Listener for stop video call message statistics event. */ | ||
@Service | ||
@RequiredArgsConstructor | ||
public class StopVideoCallListener { | ||
|
||
private final @NonNull MongoTemplate mongoTemplate; | ||
|
||
/** | ||
* Consumer for stop video call statics statistics event. | ||
* | ||
* @param eventMessage the {@link StopVideoCallStatisticsEventMessage} instance | ||
*/ | ||
@RabbitListener( | ||
id = "stop-video-call-event-listener", | ||
queues = "#{rabbitMqConfig.QUEUE_NAME_STOP_VIDEO_CALL}", | ||
containerFactory = "simpleRabbitListenerContainerFactory") | ||
public void receiveMessage(StopVideoCallStatisticsEventMessage eventMessage) { | ||
|
||
StatisticsEvent statisticsEvent = fetchVideoCallStartEvent(eventMessage); | ||
StartVideoCallMetaData metaData = (StartVideoCallMetaData) statisticsEvent.getMetaData(); | ||
|
||
statisticsEvent.setMetaData(enrichMetaData(metaData, eventMessage, statisticsEvent)); | ||
mongoTemplate.save(statisticsEvent); | ||
} | ||
|
||
private Query buildQuery(String videoCallUuid) { | ||
return new Query() | ||
.addCriteria(Criteria.where("metaData.videoCallUuid").is(videoCallUuid)); | ||
} | ||
|
||
private StartVideoCallMetaData enrichMetaData( | ||
StartVideoCallMetaData metaData, | ||
StopVideoCallStatisticsEventMessage eventMessage, | ||
StatisticsEvent statisticsEvent) { | ||
metaData.setDuration( | ||
Duration | ||
.between( | ||
statisticsEvent.getTimestamp(), | ||
eventMessage.getTimestamp().toInstant()) | ||
.getSeconds()); | ||
metaData.setTimestampStop(eventMessage.getTimestamp().toInstant()); | ||
metaData.setStatus(VideoCallStatus.FINISHED); | ||
return metaData; | ||
} | ||
|
||
private StatisticsEvent fetchVideoCallStartEvent( | ||
StopVideoCallStatisticsEventMessage eventMessage) { | ||
List<StatisticsEvent> statisticsEventList = | ||
mongoTemplate.find(buildQuery(eventMessage.getVideoCallUuid()), StatisticsEvent.class); | ||
|
||
checkIfListIsEmpty(eventMessage, statisticsEventList); | ||
checkForMultipleEvents(eventMessage, statisticsEventList); | ||
|
||
return statisticsEventList.get(0); | ||
} | ||
|
||
private void checkForMultipleEvents(StopVideoCallStatisticsEventMessage eventMessage, | ||
List<StatisticsEvent> statisticsEventList) { | ||
if (statisticsEventList.size() > 1) { | ||
throw new AmqpException( | ||
String.format( | ||
"More than one %s events for call uid %s found", | ||
EventType.START_VIDEO_CALL, eventMessage.getVideoCallUuid())); | ||
} | ||
} | ||
|
||
private void checkIfListIsEmpty(StopVideoCallStatisticsEventMessage eventMessage, | ||
List<StatisticsEvent> statisticsEventList) { | ||
if (statisticsEventList.isEmpty()) { | ||
throw new AmqpException( | ||
String.format( | ||
"No %s event for call uid %s found", | ||
EventType.START_VIDEO_CALL, eventMessage.getVideoCallUuid())); | ||
} | ||
} | ||
} |
Oops, something went wrong.