Skip to content

Commit

Permalink
549 send metadata from sample data to eq for routing piping and linki…
Browse files Browse the repository at this point in the history
…ng (#19)
  • Loading branch information
LukeLoze1 authored Dec 21, 2022
1 parent 88b5aed commit ef5bb06
Show file tree
Hide file tree
Showing 22 changed files with 737 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ public class MessageConsumerConfig {
@Value("${queueconfig.uac-update-subscription}")
private String uacUpdateSubscription;

@Value("${queueconfig.collection-exercise-update-subscription}")
private String collectionExerciseSubscription;

public MessageConsumerConfig(
ManagedMessageRecoverer managedMessageRecoverer, PubSubTemplate pubSubTemplate) {
this.managedMessageRecoverer = managedMessageRecoverer;
Expand All @@ -44,6 +47,11 @@ public MessageChannel uacUpdateInputChannel() {
return new DirectChannel();
}

@Bean
public MessageChannel collectionExerciseUpdateChannel() {
return new DirectChannel();
}

@Bean
public PubSubInboundChannelAdapter newCaseInbound(
@Qualifier("caseUpdateInputChannel") MessageChannel channel) {
Expand All @@ -60,6 +68,14 @@ public PubSubInboundChannelAdapter newUacInbound(
return makeAdapter(channel, subscription);
}

@Bean
public PubSubInboundChannelAdapter newCollectionExerciseInbound(
@Qualifier("collectionExerciseUpdateChannel") MessageChannel channel) {
String subscription =
toProjectSubscriptionName(collectionExerciseSubscription, sharedPubsubProject).toString();
return makeAdapter(channel, subscription);
}

private PubSubInboundChannelAdapter makeAdapter(MessageChannel channel, String subscriptionName) {
PubSubInboundChannelAdapter adapter =
new PubSubInboundChannelAdapter(pubSubTemplate, subscriptionName);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package uk.gov.ons.ssdc.rhservice.messaging;

import static uk.gov.ons.ssdc.rhservice.utils.JsonHelper.convertJsonBytesToEvent;

import org.springframework.integration.annotation.MessageEndpoint;
import org.springframework.integration.annotation.ServiceActivator;
import org.springframework.messaging.Message;
import org.springframework.transaction.annotation.Transactional;
import uk.gov.ons.ssdc.rhservice.model.dto.CollectionExerciseUpdateDTO;
import uk.gov.ons.ssdc.rhservice.model.dto.EventDTO;
import uk.gov.ons.ssdc.rhservice.model.repository.CollectionExerciseRepository;

@MessageEndpoint
public class CollectionExerciseUpdateReceiver {

private final CollectionExerciseRepository collectionExerciseRepository;

public CollectionExerciseUpdateReceiver(
CollectionExerciseRepository collectionExerciseRepository) {
this.collectionExerciseRepository = collectionExerciseRepository;
}

@Transactional
@ServiceActivator(inputChannel = "collectionExerciseUpdateChannel", adviceChain = "retryAdvice")
public void receiveMessage(Message<byte[]> message) {
EventDTO event = convertJsonBytesToEvent(message.getPayload());
CollectionExerciseUpdateDTO collectionExerciseUpdateDTO =
event.getPayload().getCollectionExerciseUpdate();
collectionExerciseRepository.writeCollectionExerciseUpdate(collectionExerciseUpdateDTO);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,28 @@
import uk.gov.ons.ssdc.rhservice.model.dto.EventDTO;
import uk.gov.ons.ssdc.rhservice.model.dto.UacUpdateDTO;
import uk.gov.ons.ssdc.rhservice.model.repository.UacRepository;
import uk.gov.ons.ssdc.rhservice.service.LaunchDataFieldSetter;

@MessageEndpoint
public class UacUpdateReceiver {
private final UacRepository uacRepository;
private final LaunchDataFieldSetter launchDataFieldSetter;

public UacUpdateReceiver(UacRepository uacRepository) {
public UacUpdateReceiver(
UacRepository uacRepository, LaunchDataFieldSetter launchDataFieldSetter) {
this.uacRepository = uacRepository;
this.launchDataFieldSetter = launchDataFieldSetter;
}

@ServiceActivator(inputChannel = "uacUpdateInputChannel", adviceChain = "retryAdvice")
public void receiveMessage(Message<byte[]> message) {
EventDTO event = convertJsonBytesToEvent(message.getPayload());
UacUpdateDTO uacUpdateDTO = event.getPayload().getUacUpdate();

if (uacUpdateDTO.isActive()) {
launchDataFieldSetter.stampLaunchDataFieldsOnUAC(uacUpdateDTO);
}

uacRepository.writeUAC(uacUpdateDTO);
}
}
Original file line number Diff line number Diff line change
@@ -1,19 +1,17 @@
package uk.gov.ons.ssdc.rhservice.model.dto;

import java.util.Date;
import java.util.Map;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@NoArgsConstructor
@AllArgsConstructor
public class CaseUpdateDTO {
private String caseId;
private String surveyId;
private String collectionExerciseId;
private boolean invalid;
private String refusalReceived;
private Map<String, String> sample;
private Map<String, String> sampleSensitive;
private String caseRef;
private Date createdAt;
private Date lastUpdatedAt;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package uk.gov.ons.ssdc.rhservice.model.dto;

import java.util.List;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@NoArgsConstructor
@AllArgsConstructor
public class CollectionExerciseUpdateDTO {
private String collectionExerciseId;
private List<CollectionInstrumentSelectionRule> collectionInstrumentRules;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package uk.gov.ons.ssdc.rhservice.model.dto;

import java.io.Serializable;
import java.util.List;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@NoArgsConstructor
@AllArgsConstructor
public class CollectionInstrumentSelectionRule implements Serializable {
private String collectionInstrumentUrl;
private List<EqLaunchSettings> eqLaunchSettings;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package uk.gov.ons.ssdc.rhservice.model.dto;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@NoArgsConstructor
@AllArgsConstructor
public class EqLaunchSettings {
private String sampleField;
private String launchDataFieldName;
private boolean mandatory;
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
package uk.gov.ons.ssdc.rhservice.model.dto;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@NoArgsConstructor
@AllArgsConstructor
public class EventDTO {
private EventHeaderDTO header;
private PayloadDTO payload;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,17 @@

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonInclude.Include;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@NoArgsConstructor
@AllArgsConstructor
@JsonInclude(Include.NON_NULL)
public class PayloadDTO {
private CaseUpdateDTO caseUpdate;
private UacUpdateDTO uacUpdate;
private CollectionExerciseUpdateDTO collectionExerciseUpdate;
private EqLaunchDTO eqLaunch;
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
package uk.gov.ons.ssdc.rhservice.model.dto;

import java.util.Map;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@NoArgsConstructor
@AllArgsConstructor
public class UacUpdateDTO {

private String caseId;
Expand All @@ -22,4 +27,6 @@ public class UacUpdateDTO {
private boolean receiptReceived;

private boolean eqLaunched;

private Map<String, String> launchData;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package uk.gov.ons.ssdc.rhservice.model.repository;

import java.util.Optional;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import uk.gov.ons.ssdc.rhservice.model.dto.CollectionExerciseUpdateDTO;
import uk.gov.ons.ssdc.rhservice.service.RHFirestoreClient;

@Service
public class CollectionExerciseRepository {
private final RHFirestoreClient rhFirestoreClient;

@Value("${cloud-storage.collection-exercise-schema-name}")
private String collectionExerciseSchemaName;

public CollectionExerciseRepository(RHFirestoreClient rhFirestoreClient) {
this.rhFirestoreClient = rhFirestoreClient;
}

public void writeCollectionExerciseUpdate(
final CollectionExerciseUpdateDTO collectionExerciseUpdateDTO) {
String id = collectionExerciseUpdateDTO.getCollectionExerciseId();
rhFirestoreClient.storeObject(collectionExerciseSchemaName, id, collectionExerciseUpdateDTO);
}

public Optional<CollectionExerciseUpdateDTO> readCollectionExerciseUpdate(
String collectionExerciseId) {
return rhFirestoreClient.retrieveObject(
CollectionExerciseUpdateDTO.class, collectionExerciseSchemaName, collectionExerciseId);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,10 @@ private Map<String, Object> getSurveyMetaData(UacUpdateDTO uacUpdateDTO) {
Map<String, String> data = new HashMap<>();
data.put("qid", uacUpdateDTO.getQid());

if (uacUpdateDTO.getLaunchData() != null) {
data.putAll(uacUpdateDTO.getLaunchData());
}

Map<String, Object> surveyMetaData = new HashMap<>();
surveyMetaData.put("data", data);
surveyMetaData.put("receipting_keys", List.of("qid"));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package uk.gov.ons.ssdc.rhservice.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.springframework.stereotype.Component;
import uk.gov.ons.ssdc.rhservice.model.dto.CaseUpdateDTO;
import uk.gov.ons.ssdc.rhservice.model.dto.EqLaunchSettings;
import uk.gov.ons.ssdc.rhservice.model.dto.UacUpdateDTO;
import uk.gov.ons.ssdc.rhservice.model.repository.CaseRepository;
import uk.gov.ons.ssdc.rhservice.model.repository.CollectionExerciseRepository;

@Component
public class LaunchDataFieldSetter {
private final CaseRepository caseRepository;
private final CollectionExerciseRepository collectionExerciseRepository;

public LaunchDataFieldSetter(
CaseRepository caseRepository, CollectionExerciseRepository collectionExerciseRepository) {
this.caseRepository = caseRepository;
this.collectionExerciseRepository = collectionExerciseRepository;
}

public void stampLaunchDataFieldsOnUAC(UacUpdateDTO uacUpdateDTO) {
List<EqLaunchSettings> eqLaunchDataSettings =
getEqLaunchSettingsFromCollectionExercise(
uacUpdateDTO.getCollectionExerciseId(), uacUpdateDTO.getCollectionInstrumentUrl());

if (eqLaunchDataSettings == null || eqLaunchDataSettings.isEmpty()) {
return;
}

CaseUpdateDTO caze = getCase(uacUpdateDTO.getCaseId());

Map<String, String> launchData = new HashMap<>();
for (EqLaunchSettings eqLaunchSettings : eqLaunchDataSettings) {

if (caze.getSample().containsKey(eqLaunchSettings.getSampleField())) {
launchData.put(
eqLaunchSettings.getLaunchDataFieldName(),
caze.getSample().get(eqLaunchSettings.getSampleField()));
} else if (eqLaunchSettings.isMandatory()) {
throw new RuntimeException(
"Expected field: "
+ eqLaunchSettings.getSampleField()
+ " missing on case id: "
+ caze.getCaseId());
}
}

uacUpdateDTO.setLaunchData(launchData);
}

private List<EqLaunchSettings> getEqLaunchSettingsFromCollectionExercise(
String collectionExerciseId, String collectionInstrumentUrl) {
return collectionExerciseRepository.readCollectionExerciseUpdate(collectionExerciseId)
.orElseThrow(
() -> new RuntimeException("Collection Exercise not found: " + collectionExerciseId))
.getCollectionInstrumentRules().stream()
.filter(
collexInstrumentRule ->
collexInstrumentRule.getCollectionInstrumentUrl().equals(collectionInstrumentUrl))
.findFirst()
.orElseThrow(
() ->
new RuntimeException(
"Collection Instrument Url not matched: " + collectionInstrumentUrl))
.getEqLaunchSettings();
}

private CaseUpdateDTO getCase(String caseId) {
return caseRepository
.readCaseUpdate(caseId)
.orElseThrow(() -> new RuntimeException("Not Found case ID: " + caseId));
}
}
5 changes: 4 additions & 1 deletion src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,16 @@ spring:
queueconfig:
case-update-subscription: event_case-update_rh
uac-update-subscription: event_uac-update_rh
collection-exercise-update-subscription: event_collection-exercise-update_rh
eq-launch-topic: event_eq-launch
shared-pubsub-project: shared-project


cloud-storage:
case-schema-name: case
uac-schema-name: uac
collection-exercise-schema-name: collection-exercise
survey-schema-name: survey
backoff:
initial: 100
multiplier: 1.2
Expand All @@ -34,7 +37,6 @@ exceptionmanager:
host: localhost
port: 8666


management:
endpoints:
enabled-by-default: false
Expand Down Expand Up @@ -62,3 +64,4 @@ logging:
level:
root: INFO
com.google.cloud.spring.pubsub.integration.inbound.PubSubInboundChannelAdapter: ERROR

Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package uk.gov.ons.ssdc.rhservice.exceptions;

public class CollectionExerciseNotFoundException extends Exception {
public CollectionExerciseNotFoundException(String message) {
super(message);
}
}
Loading

0 comments on commit ef5bb06

Please sign in to comment.