Skip to content
This repository has been archived by the owner on Dec 17, 2024. It is now read-only.

Commit

Permalink
feat: implementation of stop video call listener and business logic
Browse files Browse the repository at this point in the history
  • Loading branch information
daho4b committed Sep 24, 2021
1 parent d945345 commit e67cd2a
Show file tree
Hide file tree
Showing 16 changed files with 505 additions and 61 deletions.
41 changes: 39 additions & 2 deletions api/statisticsservice.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ components:
enum:
- "ASSIGN_SESSION"
- "CREATE_MESSAGE"
- "START_VIDEO_CALL"
- "STOP_VIDEO_CALL"

UserRole:
type: string
Expand All @@ -58,15 +60,18 @@ components:
type: object
required:
- eventType
- consultantId
- userId
- userRole
- timestamp
properties:
eventType:
$ref: '#/components/schemas/EventType'
consultantId:
userId:
type: string
description: The keycloak id of the consultant
example: d63f4cc0-215d-40e2-a866-2d3e910f0590
userRole:
$ref: '#/components/schemas/UserRole'
timestamp:
type: string
format: date-time
Expand Down Expand Up @@ -105,6 +110,38 @@ components:
description: indicates whether the message has an attachment
example: true

StartVideoCallSatisticsEventMessage:
type: object
required:
- sessionId
- videoCallUuid
allOf:
- $ref: '#/components/schemas/StatisticsEventMessage'
- type: object
properties:
sessionId:
type: integer
format: int64
description: The id of the session
example: 12345
videoCallUuid:
type: string
description: The uuid of the video call
example: 123e4567-e89b-12d3-a456-556642440000

StopVideoCallStatisticsEventMessage:
type: object
required:
- videoCallUuid
allOf:
- $ref: '#/components/schemas/StatisticsEventMessage'
- type: object
properties:
videoCallUuid:
type: string
description: The uuid of the video call
example: 123e4567-e89b-12d3-a456-556642440000

securitySchemes:
Bearer:
type: apiKey
Expand Down
18 changes: 18 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,24 @@
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>3.1.0</version>
<executions>
<execution>
<phase>generate-sources</phase>
<goals>
<goal>add-source</goal>
</goals>
<configuration>
<sources>
<source>${project.build.directory}/generated-sources</source>
</sources>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
<profiles>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
package de.caritas.cob.statisticsservice.api.statistics.events.assignsession;
package de.caritas.cob.statisticsservice.api.statistics.listener;

import de.caritas.cob.statisticsservice.api.model.AssignSessionStatisticsEventMessage;
import de.caritas.cob.statisticsservice.api.model.UserRole;
import de.caritas.cob.statisticsservice.api.service.UserStatisticsService;
import de.caritas.cob.statisticsservice.api.statistics.model.StatisticEventBuilder;
import de.caritas.cob.statisticsservice.api.statistics.model.StatisticsEventBuilder;
import de.caritas.cob.statisticsservice.api.statistics.model.StatisticsEvent;
import de.caritas.cob.statisticsservice.config.RabbitMqConfig;
import lombok.NonNull;
import lombok.RequiredArgsConstructor;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
Expand All @@ -19,24 +17,26 @@ public class AssignSessionListener {

private final @NonNull MongoTemplate mongoTemplate;
private final @NonNull UserStatisticsService userStatisticsService;
private @NonNull RabbitMqConfig rabbitMqConfig;

/**
* Consumer for assign session message statistics event.
*
* @param eventMessage the {@link AssignSessionStatisticsEventMessage} instance
*/
@RabbitListener(queues = "#{rabbitMqConfig.QUEUE_NAME_ASSIGN_SESSION}")
@RabbitListener(
id = "assing-session-event-listener",
queues = "#{rabbitMqConfig.QUEUE_NAME_ASSIGN_SESSION}",
containerFactory = "simpleRabbitListenerContainerFactory")
public void receiveMessage(AssignSessionStatisticsEventMessage eventMessage) {

StatisticsEvent statisticsEvent =
StatisticEventBuilder.getInstance(
StatisticsEventBuilder.getInstance(
() ->
userStatisticsService.retrieveSessionViaSessionId(eventMessage.getSessionId()))
.withEventType(eventMessage.getEventType())
.withTimestamp(eventMessage.getTimestamp().toInstant())
.withUserId(eventMessage.getConsultantId())
.withUserRole(UserRole.CONSULTANT)
.withUserId(eventMessage.getUserId())
.withUserRole(eventMessage.getUserRole())
.build();

mongoTemplate.insert(statisticsEvent);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
package de.caritas.cob.statisticsservice.api.statistics.events.createmessage;
package de.caritas.cob.statisticsservice.api.statistics.listener;

import de.caritas.cob.statisticsservice.api.model.CreateMessageStatisticsEventMessage;
import de.caritas.cob.statisticsservice.api.model.UserRole;
import de.caritas.cob.statisticsservice.api.service.UserStatisticsService;
import de.caritas.cob.statisticsservice.api.statistics.model.StatisticEventBuilder;
import de.caritas.cob.statisticsservice.api.statistics.model.StatisticsEventBuilder;
import de.caritas.cob.statisticsservice.api.statistics.model.StatisticsEvent;
import de.caritas.cob.statisticsservice.api.statistics.model.meta.CreateMessageMetaData;
import de.caritas.cob.statisticsservice.config.RabbitMqConfig;
import lombok.NonNull;
import lombok.RequiredArgsConstructor;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
Expand All @@ -18,7 +16,6 @@
@RequiredArgsConstructor
public class CreateMessageListener {

private final @NonNull RabbitMqConfig rabbitMqConfig;
private final @NonNull MongoTemplate mongoTemplate;
private final @NonNull UserStatisticsService userStatisticsService;

Expand All @@ -28,28 +25,26 @@ public class CreateMessageListener {
* @param eventMessage the {@link CreateMessageStatisticsEventMessage} instance
*/
@RabbitListener(
id = "create-message-event-listener",
queues = "#{rabbitMqConfig.QUEUE_NAME_CREATE_MESSAGE}",
containerFactory = "simpleRabbitListenerContainerFactory")
public void receiveMessage(CreateMessageStatisticsEventMessage eventMessage) {

StatisticsEvent statisticsEvent =
StatisticEventBuilder.getInstance(
StatisticsEventBuilder.getInstance(
() ->
userStatisticsService.retrieveSessionViaRcGroupId(eventMessage.getRcGroupId()))
.withEventType(eventMessage.getEventType())
.withTimestamp(eventMessage.getTimestamp().toInstant())
.withUserId(eventMessage.getConsultantId())
.withUserRole(UserRole.CONSULTANT)
.withUserId(eventMessage.getUserId())
.withUserRole(eventMessage.getUserRole())
.withMetaData(buildMetaData(eventMessage))
.build();

mongoTemplate.insert(statisticsEvent);
}

private CreateMessageMetaData buildMetaData(CreateMessageStatisticsEventMessage eventMessage) {
return CreateMessageMetaData
.builder()
.hasAttachment(eventMessage.getHasAttachment())
.build();
return CreateMessageMetaData.builder().hasAttachment(eventMessage.getHasAttachment()).build();
}
}
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();
}
}
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()));
}
}
}
Loading

0 comments on commit e67cd2a

Please sign in to comment.