Commit 493211d3 authored by Jujube Orange's avatar Jujube Orange
Browse files

Merge branch 'hotfix/TAC-750-Bad_increment'

parents e2e17b3e f5753291
Pipeline #257318 canceled with stages
in 0 seconds
......@@ -3,7 +3,7 @@
<parent>
<artifactId>clea-server</artifactId>
<groupId>fr.gouv.clea</groupId>
<version>1.1.1</version>
<version>1.1.2</version>
</parent>
<modelVersion>4.0.0</modelVersion>
......
......@@ -5,7 +5,7 @@
<parent>
<groupId>fr.gouv.clea</groupId>
<artifactId>clea-server</artifactId>
<version>1.1.1</version>
<version>1.1.2</version>
</parent>
<artifactId>clea-client</artifactId>
......
......@@ -4,7 +4,7 @@
<parent>
<groupId>fr.gouv.clea</groupId>
<artifactId>clea-server</artifactId>
<version>1.1.1</version>
<version>1.1.2</version>
</parent>
<artifactId>clea-qr-simulator</artifactId>
......
......@@ -5,7 +5,7 @@
<parent>
<groupId>fr.gouv.clea</groupId>
<artifactId>clea-server</artifactId>
<version>1.1.1</version>
<version>1.1.2</version>
</parent>
<artifactId>clea-scoring-conf</artifactId>
......
......@@ -6,7 +6,7 @@
<parent>
<groupId>fr.gouv.clea</groupId>
<artifactId>clea-server</artifactId>
<version>1.1.1</version>
<version>1.1.2</version>
</parent>
<artifactId>clea-venue-consumer</artifactId>
......
......@@ -28,17 +28,12 @@ public class StatService implements IStatService {
@Override
public void logStats(Visit visit) {
StatLocationKey statLocationKey = StatLocationKey.builder()
.period(this.getStatPeriod(visit))
.venueType(visit.getVenueType())
.venueCategory1(visit.getVenueCategory1())
.venueCategory2(visit.getVenueCategory2())
.build();
StatLocationKey statLocationKey = buildKey(visit);
var statLocation = newStatLocation(statLocationKey, visit);
Optional<StatLocation> optional=repository.findById(statLocationKey);
if(optional.isPresent()) {
repository.updateByIncrement(optional.get());
repository.updateByIncrement(statLocation);
}else {
try {
repository.insert(statLocation);
......@@ -56,6 +51,8 @@ public class StatService implements IStatService {
);
}
protected StatLocation newStatLocation(StatLocationKey statLocationKey, Visit visit) {
return StatLocation.builder()
.statLocationKey(statLocationKey)
......@@ -64,6 +61,14 @@ public class StatService implements IStatService {
.build();
}
public StatLocationKey buildKey(Visit visit) {
return StatLocationKey.builder()
.period(getStatPeriod(visit))
.venueType(visit.getVenueType())
.venueCategory1(visit.getVenueCategory1())
.venueCategory2(visit.getVenueCategory2())
.build();
}
protected Instant getStatPeriod(Visit visit) {
long secondsToRemove = visit.getQrCodeScanTime().getEpochSecond() % config.getStatSlotDurationInSeconds();
return visit.getQrCodeScanTime().minus(secondsToRemove, ChronoUnit.SECONDS).truncatedTo(ChronoUnit.SECONDS);
......
package fr.gouv.clea.consumer.service.impl;
import fr.gouv.clea.consumer.model.StatLocation;
import fr.gouv.clea.consumer.model.StatLocationKey;
import fr.gouv.clea.consumer.model.Visit;
import fr.gouv.clea.consumer.repository.IStatLocationJpaRepository;
import fr.gouv.clea.consumer.service.IStatService;
......@@ -13,7 +14,10 @@ import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.transaction.annotation.Transactional;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import java.time.Instant;
import java.time.temporal.ChronoUnit;
import java.util.List;
......@@ -36,7 +40,10 @@ class StatServiceTest {
@Autowired
private IStatLocationJpaRepository repository;
@Autowired
private IStatService service;
private StatService service;
@PersistenceContext
EntityManager entityManager;
static Visit defaultVisit() {
return Visit.builder()
......@@ -134,22 +141,39 @@ class StatServiceTest {
}
@Test
@Transactional
void should_get_same_stat_period_when_visits_scantimes_are_in_same_stat_slot() {
Visit visit1 = defaultVisit().toBuilder()
.qrCodeScanTime(TODAY_AT_8AM)
.isBackward(true)
.build();
Visit visit2 = defaultVisit().toBuilder()
.qrCodeScanTime(TODAY_AT_8AM.plus(15, ChronoUnit.MINUTES)) // same stat slot
.isBackward(true)
.build();
Visit visit3 = defaultVisit().toBuilder()
.qrCodeScanTime(TODAY_AT_8AM.plus(28, ChronoUnit.MINUTES)) // same stat slot
.isBackward(false)
.build();
Visit visit4 = defaultVisit().toBuilder()
.qrCodeScanTime(TODAY_AT_8AM.plus(29, ChronoUnit.MINUTES)) // same stat slot
.isBackward(true)
.build();
service.logStats(visit1);
service.logStats(visit2);
service.logStats(visit3);
service.logStats(visit4);
entityManager.flush();
entityManager.clear();
assertThat(repository.count()).isEqualTo(1);
StatLocationKey key = service.buildKey(visit1);
StatLocation statLocation= repository.getOne(key);
assertThat(statLocation.getBackwardVisits()).as("back visits").isEqualTo(3l);
assertThat(statLocation.getForwardVisits()).as("forward visits").isEqualTo(1l);
}
@Test
......
......@@ -6,7 +6,7 @@
<parent>
<groupId>fr.gouv.clea</groupId>
<artifactId>clea-server</artifactId>
<version>1.1.1</version>
<version>1.1.2</version>
</parent>
<artifactId>clea-ws-rest</artifactId>
......
......@@ -18,7 +18,7 @@
<groupId>fr.gouv.clea</groupId>
<artifactId>clea-server</artifactId>
<version>1.1.1</version>
<version>1.1.2</version>
<name>clea-server</name>
<packaging>pom</packaging>
<description>Tous Anti-Covid Cluster Exposure Verification (Clea) server project</description>
......
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