Commit 6334296d authored by calocedre TAC's avatar calocedre TAC
Browse files

remove RiskConfigurationService

parent 8c952b5a
package fr.gouv.clea.config;
import fr.gouv.clea.dto.SinglePlaceCluster;
import fr.gouv.clea.dto.SinglePlaceExposedVisits;
import fr.gouv.clea.identification.ExposedVisitPartitioner;
import fr.gouv.clea.identification.RiskConfigurationService;
import fr.gouv.clea.identification.SinglePlaceExposedVisitsItemWriter;
import fr.gouv.clea.identification.SinglePlaceExposedVisitsProcessor;
import fr.gouv.clea.identification.reader.ExposedVisitItemReader;
import fr.gouv.clea.identification.reader.SinglePlaceExposedVisitItemReader;
import fr.gouv.clea.indexation.model.output.ClusterFile;
import fr.gouv.clea.indexation.processors.IndexationProcessor;
import fr.gouv.clea.indexation.readers.IndexationReader;
import fr.gouv.clea.indexation.writers.IndexationWriter;
import static fr.gouv.clea.config.BatchConstants.CLUSTERMAP_JOB_CONTEXT_KEY;
import static fr.gouv.clea.config.BatchConstants.EXPOSED_VISITS_TABLE;
import static fr.gouv.clea.config.BatchConstants.LTID_COLUMN;
import static fr.gouv.clea.config.BatchConstants.PERIOD_COLUMN;
import static fr.gouv.clea.config.BatchConstants.TIMESLOT_COLUMN;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import javax.sql.DataSource;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
......@@ -31,13 +32,19 @@ import org.springframework.context.annotation.Configuration;
import org.springframework.core.task.SimpleAsyncTaskExecutor;
import org.springframework.core.task.TaskExecutor;
import javax.sql.DataSource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import fr.gouv.clea.clea.scoring.configuration.risk.RiskConfiguration;
import fr.gouv.clea.dto.SinglePlaceCluster;
import fr.gouv.clea.dto.SinglePlaceExposedVisits;
import fr.gouv.clea.identification.ExposedVisitPartitioner;
import fr.gouv.clea.identification.SinglePlaceExposedVisitsItemWriter;
import fr.gouv.clea.identification.SinglePlaceExposedVisitsProcessor;
import fr.gouv.clea.identification.reader.ExposedVisitItemReader;
import fr.gouv.clea.identification.reader.SinglePlaceExposedVisitItemReader;
import fr.gouv.clea.indexation.model.output.ClusterFile;
import fr.gouv.clea.indexation.processors.IndexationProcessor;
import fr.gouv.clea.indexation.readers.IndexationReader;
import fr.gouv.clea.indexation.writers.IndexationWriter;
import static fr.gouv.clea.config.BatchConstants.*;
@Configuration
public class BatchConfig {
......@@ -49,7 +56,7 @@ public class BatchConfig {
private StepBuilderFactory stepBuilderFactory;
@Autowired
private RiskConfigurationService riskConfigurationService;
private RiskConfiguration riskConfiguration;
@Autowired
private BatchProperties properties;
......@@ -92,7 +99,7 @@ public class BatchConfig {
.listener(promotionListener())
.<SinglePlaceExposedVisits, SinglePlaceCluster>chunk(properties.getChunkSize())
.reader(reader)
.processor(new SinglePlaceExposedVisitsProcessor(properties, riskConfigurationService))
.processor(new SinglePlaceExposedVisitsProcessor(properties, riskConfiguration))
.writer(new SinglePlaceExposedVisitsItemWriter())
.build();
}
......
package fr.gouv.clea.identification;
import fr.gouv.clea.clea.scoring.configuration.risk.RiskConfiguration;
import fr.gouv.clea.clea.scoring.configuration.risk.RiskRule;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.util.Optional;
/**
*
* Mock implementation that return same value for any venue type/categories
*
*/
@Component
public class RiskConfigurationService {
@Autowired
private RiskConfiguration riskConfiguration;
public Optional<RiskRule> evaluate(int venueType, int venueCategory1, int venueCategory2) {
return Optional.of(riskConfiguration.getConfigurationFor(venueType, venueCategory1, venueCategory2));
}
}
package fr.gouv.clea.identification;
import fr.gouv.clea.clea.scoring.configuration.risk.RiskConfiguration;
import fr.gouv.clea.clea.scoring.configuration.risk.RiskRule;
import fr.gouv.clea.config.BatchProperties;
import fr.gouv.clea.dto.ClusterPeriod;
......@@ -7,20 +8,17 @@ import fr.gouv.clea.dto.SinglePlaceCluster;
import fr.gouv.clea.dto.SinglePlaceExposedVisits;
import fr.gouv.clea.entity.ExposedVisit;
import fr.gouv.clea.utils.ExposedVisitComparator;
import lombok.extern.slf4j.Slf4j;
import org.springframework.batch.item.ItemProcessor;
import java.util.Optional;
@Slf4j
public class SinglePlaceExposedVisitsProcessor implements ItemProcessor<SinglePlaceExposedVisits, SinglePlaceCluster> {
private final BatchProperties properties;
private final RiskConfigurationService riskConfigurationService;
private final RiskConfiguration riskConfiguration;
public SinglePlaceExposedVisitsProcessor(BatchProperties properties, RiskConfigurationService riskConfigurationService) {
public SinglePlaceExposedVisitsProcessor(BatchProperties properties, RiskConfiguration riskConfiguration) {
this.properties = properties;
this.riskConfigurationService = riskConfigurationService;
this.riskConfiguration = riskConfiguration;
}
@Override
......@@ -34,13 +32,7 @@ public class SinglePlaceExposedVisitsProcessor implements ItemProcessor<SinglePl
ClusterPeriod backPeriod = null;
ClusterPeriod forwardPeriod = null;
Optional<RiskRule> riskRuleOptional = riskConfigurationService.evaluate(cluster.getVenueType(), cluster.getVenueCategory1(), cluster.getVenueCategory2());
if (riskRuleOptional.isEmpty()) {
log.warn("No Risk configuration for [type={},categ1={},categ2={}]", cluster.getVenueType(), cluster.getVenueCategory1(), cluster.getVenueCategory2());
return null;
}
final RiskRule riskRule = riskRuleOptional.get();
final RiskRule riskRule = riskConfiguration.getConfigurationFor(cluster.getVenueType(), cluster.getVenueCategory1(), cluster.getVenueCategory2());
// Sorted visits by period then slot
record.getVisits().sort(new ExposedVisitComparator());
......
package fr.gouv.clea.identification;
import fr.gouv.clea.clea.scoring.configuration.risk.RiskRule;
import fr.gouv.clea.config.BatchProperties;
import fr.gouv.clea.dto.ClusterPeriod;
import fr.gouv.clea.dto.SinglePlaceCluster;
import fr.gouv.clea.dto.SinglePlaceExposedVisits;
import fr.gouv.clea.entity.ExposedVisit;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.Mockito.when;
import java.util.UUID;
import org.assertj.core.data.Offset;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import java.util.Optional;
import java.util.UUID;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.Mockito.when;
import fr.gouv.clea.clea.scoring.configuration.risk.RiskConfiguration;
import fr.gouv.clea.clea.scoring.configuration.risk.RiskRule;
import fr.gouv.clea.config.BatchProperties;
import fr.gouv.clea.dto.ClusterPeriod;
import fr.gouv.clea.dto.SinglePlaceCluster;
import fr.gouv.clea.dto.SinglePlaceExposedVisits;
import fr.gouv.clea.entity.ExposedVisit;
@ExtendWith(MockitoExtension.class)
class SinglePlaceExposedVisitsProcessorTest {
......@@ -25,7 +26,7 @@ class SinglePlaceExposedVisitsProcessorTest {
private BatchProperties properties = new BatchProperties();
@Mock
RiskConfigurationService riskConfigurationServiceMock;
RiskConfiguration riskConfigurationMock;
public SinglePlaceExposedVisitsProcessorTest() {
properties.durationUnitInSeconds = 180;
......@@ -37,7 +38,7 @@ class SinglePlaceExposedVisitsProcessorTest {
@Test
void noClusterPeriod() {
when(riskConfigurationServiceMock.evaluate(anyInt(), anyInt(), anyInt())).thenReturn(buildRiskRule());
when(riskConfigurationMock.getConfigurationFor(anyInt(), anyInt(), anyInt())).thenReturn(buildRiskRule());
SinglePlaceExposedVisits spe = new SinglePlaceExposedVisits();
spe.setLocationTemporaryPublicId(UUID_SAMPLE);
......@@ -45,14 +46,14 @@ class SinglePlaceExposedVisitsProcessorTest {
spe.setVenueCategory1(3);
spe.setVenueCategory2(2);
SinglePlaceCluster res = new SinglePlaceExposedVisitsProcessor(properties, riskConfigurationServiceMock).process(spe);
SinglePlaceCluster res = new SinglePlaceExposedVisitsProcessor(properties, riskConfigurationMock).process(spe);
assertThat(res).isNull();
}
@Test
void noClusterVisits() throws Exception {
when(riskConfigurationServiceMock.evaluate(anyInt(), anyInt(), anyInt())).thenReturn(buildRiskRule());
when(riskConfigurationMock.getConfigurationFor(anyInt(), anyInt(), anyInt())).thenReturn(buildRiskRule());
SinglePlaceExposedVisits spe = new SinglePlaceExposedVisits();
spe.setLocationTemporaryPublicId(UUID_SAMPLE);
......@@ -63,7 +64,7 @@ class SinglePlaceExposedVisitsProcessorTest {
spe.addVisit(ExposedVisit.builder().periodStart(periodStart).timeSlot(0).forwardVisits(0).build());
spe.addVisit(ExposedVisit.builder().periodStart(periodStart).timeSlot(1).forwardVisits(0).build());
SinglePlaceCluster res = new SinglePlaceExposedVisitsProcessor(properties, riskConfigurationServiceMock).process(spe);
SinglePlaceCluster res = new SinglePlaceExposedVisitsProcessor(properties, riskConfigurationMock).process(spe);
assertThat(res).isNull();
}
......@@ -71,7 +72,7 @@ class SinglePlaceExposedVisitsProcessorTest {
@Test
void oneClusterPeriod() {
when(riskConfigurationServiceMock.evaluate(anyInt(), anyInt(), anyInt())).thenReturn(buildRiskRule());
when(riskConfigurationMock.getConfigurationFor(anyInt(), anyInt(), anyInt())).thenReturn(buildRiskRule());
SinglePlaceExposedVisits spe = new SinglePlaceExposedVisits();
spe.setLocationTemporaryPublicId(UUID_SAMPLE);
......@@ -86,7 +87,7 @@ class SinglePlaceExposedVisitsProcessorTest {
spe.addVisit(ExposedVisit.builder().periodStart(periodStart).timeSlot(4).forwardVisits(0).build());
SinglePlaceCluster res = new SinglePlaceExposedVisitsProcessor(properties, riskConfigurationServiceMock).process(spe);
SinglePlaceCluster res = new SinglePlaceExposedVisitsProcessor(properties, riskConfigurationMock).process(spe);
assertThat(res).isNotNull();
assertThat(res.getPeriods()).hasSize(1);
......@@ -101,7 +102,7 @@ class SinglePlaceExposedVisitsProcessorTest {
@Test
void manyClusterPeriodSlots() {
when(riskConfigurationServiceMock.evaluate(anyInt(), anyInt(), anyInt())).thenReturn(buildRiskRule());
when(riskConfigurationMock.getConfigurationFor(anyInt(), anyInt(), anyInt())).thenReturn(buildRiskRule());
long anotherPeriodStart = 3822422480L;
......@@ -116,7 +117,7 @@ class SinglePlaceExposedVisitsProcessorTest {
spe.addVisit(ExposedVisit.builder().periodStart(periodStart).timeSlot(2).forwardVisits(0).build());
spe.addVisit(ExposedVisit.builder().periodStart(anotherPeriodStart).timeSlot(0).forwardVisits(1).build());
SinglePlaceCluster res = new SinglePlaceExposedVisitsProcessor(properties, riskConfigurationServiceMock).process(spe);
SinglePlaceCluster res = new SinglePlaceExposedVisitsProcessor(properties, riskConfigurationMock).process(spe);
assertThat(res).isNotNull();
assertThat(res.getPeriods()).hasSize(2);
......@@ -134,7 +135,7 @@ class SinglePlaceExposedVisitsProcessorTest {
@Test
void forwardRiskLevel() {
when(riskConfigurationServiceMock.evaluate(anyInt(), anyInt(), anyInt())).thenReturn(buildRiskRule());
when(riskConfigurationMock.getConfigurationFor(anyInt(), anyInt(), anyInt())).thenReturn(buildRiskRule());
SinglePlaceExposedVisits spe = new SinglePlaceExposedVisits();
spe.setLocationTemporaryPublicId(UUID_SAMPLE);
......@@ -143,19 +144,19 @@ class SinglePlaceExposedVisitsProcessorTest {
spe.setVenueCategory2(2);
spe.addVisit(ExposedVisit.builder().periodStart(periodStart).timeSlot(0).forwardVisits(100).build());
SinglePlaceCluster res = new SinglePlaceExposedVisitsProcessor(properties, riskConfigurationServiceMock).process(spe);
SinglePlaceCluster res = new SinglePlaceExposedVisitsProcessor(properties, riskConfigurationMock).process(spe);
assertThat(res).isNotNull();
assertThat(res.getPeriods()).hasSize(1);
ClusterPeriod p = res.getPeriods().get(0);
Optional<RiskRule> riskLevelEvaluation = riskConfigurationServiceMock.evaluate(spe.getVenueType(), spe.getVenueCategory1(), spe.getVenueCategory2());
riskLevelEvaluation.ifPresent(evaluatedRiskLevel -> assertThat(p.getRiskLevel()).as("riskLevel").isCloseTo(evaluatedRiskLevel.getRiskLevelForward(), Offset.offset(0.01f)));
RiskRule evaluatedRiskLevel = riskConfigurationMock.getConfigurationFor(spe.getVenueType(), spe.getVenueCategory1(), spe.getVenueCategory2());
assertThat(p.getRiskLevel()).as("riskLevel").isCloseTo(evaluatedRiskLevel.getRiskLevelForward(), Offset.offset(0.01f));
}
@Test
void backwardRiskLevel() {
when(riskConfigurationServiceMock.evaluate(anyInt(), anyInt(), anyInt())).thenReturn(buildRiskRule());
when(riskConfigurationMock.getConfigurationFor(anyInt(), anyInt(), anyInt())).thenReturn(buildRiskRule());
SinglePlaceExposedVisits spe = new SinglePlaceExposedVisits();
spe.setLocationTemporaryPublicId(UUID_SAMPLE);
......@@ -164,22 +165,22 @@ class SinglePlaceExposedVisitsProcessorTest {
spe.setVenueCategory2(2);
spe.addVisit(ExposedVisit.builder().periodStart(periodStart).timeSlot(0).backwardVisits(100).build());
SinglePlaceCluster res = new SinglePlaceExposedVisitsProcessor(properties, riskConfigurationServiceMock).process(spe);
SinglePlaceCluster res = new SinglePlaceExposedVisitsProcessor(properties, riskConfigurationMock).process(spe);
assertThat(res).isNotNull();
assertThat(res.getPeriods()).hasSize(1);
ClusterPeriod p = res.getPeriods().get(0);
Optional<RiskRule> riskLevelEvaluation = riskConfigurationServiceMock.evaluate(spe.getVenueType(), spe.getVenueCategory1(), spe.getVenueCategory2());
riskLevelEvaluation.ifPresent(evaluatedRiskLevel -> assertThat(p.getRiskLevel()).as("riskLevel").isCloseTo(evaluatedRiskLevel.getRiskLevelBackward(), Offset.offset(0.01f)));
RiskRule evaluatedRiskLevel = riskConfigurationMock.getConfigurationFor(spe.getVenueType(), spe.getVenueCategory1(), spe.getVenueCategory2());
assertThat(p.getRiskLevel()).as("riskLevel").isCloseTo(evaluatedRiskLevel.getRiskLevelBackward(), Offset.offset(0.01f));
}
private Optional<RiskRule> buildRiskRule() {
return Optional.of(RiskRule.builder()
private RiskRule buildRiskRule() {
return RiskRule.builder()
.clusterThresholdForward(1)
.clusterThresholdBackward(3)
.riskLevelForward(3.0f)
.riskLevelBackward(2.0f)
.build());
.build();
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment