Commit 2073e4f1 authored by Figue Orange's avatar Figue Orange
Browse files

refacto: clean code

parent 558cb19f
Pipeline #278427 passed with stages
in 28 minutes and 10 seconds
......@@ -42,21 +42,18 @@ public class CustomStatLocationIndexImpl implements CustomStatLocationIndex {
@Override
public Optional<LocationStat> findByIdentifier(String id, Instant periodStart) {
NativeSearchQuery searchQuery = new NativeSearchQueryBuilder()
.withQuery(QueryBuilders.matchQuery("id", id))
.build();
SearchHits<LocationStat> LocationStats = operations
SearchHits<LocationStat> locationStats = operations
.search(
searchQuery, LocationStat.class,
IndexCoordinates.of(getIndexCoordinates(periodStart).getIndexName())
);
log.info("(int) Searchhits : {}", LocationStats);
if (!LocationStats.getSearchHits().isEmpty()) {
log.info(
"(int) FindBy identifier : {}",
LocationStats.getSearchHits().stream().findFirst().get().getContent()
);
return Optional.of(LocationStats.getSearchHits().stream().findFirst().get().getContent());
if (!locationStats.getSearchHits().isEmpty()) {
return Optional.of(locationStats.getSearchHits().stream().findFirst().get().getContent());
} else {
return Optional.empty();
}
......
......@@ -4,7 +4,7 @@ import fr.gouv.clea.consumer.model.LocationStat;
import java.util.Optional;
public interface CustomStatLocationIndex {
public interface CustomStatLocationRepository {
Optional<LocationStat> saveWithIndexTargeting(LocationStat locationStat);
......
......@@ -19,7 +19,7 @@ import java.util.Optional;
@RequiredArgsConstructor
@Slf4j
public class CustomStatLocationIndexImpl implements CustomStatLocationIndex {
public class CustomStatLocationRepositoryImpl implements CustomStatLocationRepository {
private final ElasticsearchOperations operations;
......@@ -41,25 +41,36 @@ public class CustomStatLocationIndexImpl implements CustomStatLocationIndex {
@Override
public Optional<LocationStat> findByIdentifier(LocationStat locationStat) {
NativeSearchQuery searchQuery = new NativeSearchQueryBuilder()
.withQuery(QueryBuilders.matchQuery("id.keyword", locationStat.getId()))
.build();
SearchHits<LocationStat> LocationStats = operations
SearchHits<LocationStat> locationStatsHits = operations
.search(
searchQuery, LocationStat.class,
IndexCoordinates.of(getIndexCoordinates(locationStat).getIndexName())
);
log.info("Searchhits : {}", LocationStats);
if (!LocationStats.getSearchHits().isEmpty()) {
log.info("FindBy identifier : {}", LocationStats.getSearchHits().stream().findFirst().get().getContent());
return Optional.of(LocationStats.getSearchHits().stream().findFirst().get().getContent());
log.debug("SearchHits for {} : {}", locationStat.getId(), locationStatsHits);
if (!locationStatsHits.getSearchHits().isEmpty()) {
log.debug(
"Found match for idenfifier {} : {}",
locationStat.getId(),
locationStatsHits.getSearchHits().stream().findFirst().get().getContent()
);
return Optional.of(locationStatsHits.getSearchHits().stream().findFirst().get().getContent());
} else {
return Optional.empty();
}
}
private <S extends LocationStat> IndexCoordinates getIndexCoordinates(S locationStat) {
String indexName = "health-clealocations-" + LocalDate.ofInstant(
locationStat.getPeriodStart(),
ZoneOffset.UTC
......
......@@ -5,7 +5,6 @@ import org.springframework.data.elasticsearch.repository.ElasticsearchRepository
import org.springframework.stereotype.Repository;
@Repository
public interface StatLocationIndex
extends ElasticsearchRepository<LocationStat, String>, CustomStatLocationIndex {
public interface LocationStatRepository
extends ElasticsearchRepository<LocationStat, String>, CustomStatLocationRepository {
}
......@@ -5,5 +5,5 @@ import org.springframework.data.elasticsearch.repository.ElasticsearchRepository
import org.springframework.stereotype.Repository;
@Repository
public interface ReportStatIndex extends ElasticsearchRepository<ReportStatEntity, Long> {
public interface ReportStatRepository extends ElasticsearchRepository<ReportStatEntity, Long> {
}
......@@ -6,8 +6,8 @@ import fr.gouv.clea.consumer.model.LocationStat;
import fr.gouv.clea.consumer.model.ReportStat;
import fr.gouv.clea.consumer.model.ReportStatEntity;
import fr.gouv.clea.consumer.model.Visit;
import fr.gouv.clea.consumer.repository.statistiques.ReportStatIndex;
import fr.gouv.clea.consumer.repository.statistiques.StatLocationIndex;
import fr.gouv.clea.consumer.repository.statistiques.LocationStatRepository;
import fr.gouv.clea.consumer.repository.statistiques.ReportStatRepository;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.elasticsearch.core.ElasticsearchOperations;
......@@ -29,9 +29,9 @@ public class StatisticsService {
private final CleaKafkaProperties cleaKafkaProperties;
private final StatLocationIndex statLocationIndex;
private final LocationStatRepository locationStatRepository;
private final ReportStatIndex reportStatIndex;
private final ReportStatRepository reportStatRepository;
private final VenueConsumerProperties properties;
......@@ -43,7 +43,7 @@ public class StatisticsService {
public void logStats(Visit visit) {
final var statLocation = toLocationStat(visit);
statLocationIndex.findByIdentifier(statLocation)
locationStatRepository.findByIdentifier(statLocation)
.map(existingLocationStat -> {
if (visit.isBackward()) {
existingLocationStat.setBackwardVisits(existingLocationStat.getBackwardVisits() + 1);
......@@ -53,8 +53,8 @@ public class StatisticsService {
return existingLocationStat;
})
.ifPresentOrElse(
statLocationIndex::saveWithIndexTargeting,
() -> statLocationIndex.saveWithIndexTargeting(statLocation)
locationStatRepository::saveWithIndexTargeting,
() -> locationStatRepository.saveWithIndexTargeting(statLocation)
);
log.debug("Location stat saved to elastic: {}", statLocation);
......@@ -83,6 +83,16 @@ public class StatisticsService {
);
}
public void logStats(ReportStat reportStat) {
if (!operations.indexOps(ReportStatEntity.class).exists()) {
operations.indexOps(ReportStatEntity.class).create();
}
var saved = reportStatRepository.save(reportStat.toEntity());
log.info("Report stat saved to elastic: {}", saved);
}
private LocationStat toLocationStat(Visit visit) {
final var secondsToRemove = visit.getQrCodeScanTime().getEpochSecond()
% properties.getStatSlotDurationInSeconds();
......@@ -108,14 +118,4 @@ public class StatisticsService {
}
public void logStats(ReportStat reportStat) {
if (!operations.indexOps(ReportStatEntity.class).exists()) {
operations.indexOps(ReportStatEntity.class).create();
}
var saved = reportStatIndex.save(reportStat.toEntity());
log.debug("Report stat saved to elastic: {}", saved);
}
}
......@@ -6,8 +6,8 @@ import fr.gouv.clea.consumer.model.LocationStat;
import fr.gouv.clea.consumer.model.ReportStat;
import fr.gouv.clea.consumer.model.ReportStatEntity;
import fr.gouv.clea.consumer.model.Visit;
import fr.gouv.clea.consumer.repository.statistiques.ReportStatIndex;
import fr.gouv.clea.consumer.repository.statistiques.StatLocationIndex;
import fr.gouv.clea.consumer.repository.statistiques.LocationStatRepository;
import fr.gouv.clea.consumer.repository.statistiques.ReportStatRepository;
import fr.gouv.clea.consumer.service.StatisticsService;
import fr.inria.clea.lsp.utils.TimeUtils;
import org.apache.commons.lang3.RandomUtils;
......@@ -75,13 +75,13 @@ class StatistiquesServiceIT {
private StatisticsService serviceMock;
@Mock
private StatLocationIndex statLocationIndexMock;
private LocationStatRepository locationStatRepositoryMock;
@Autowired
private ReportStatIndex reportStatIndex;
private ReportStatRepository reportStatRepository;
@Autowired
private StatLocationIndex statLocationIndex;
private LocationStatRepository locationStatRepository;
@Autowired
private ElasticsearchOperations template;
......@@ -134,7 +134,7 @@ class StatistiquesServiceIT {
service.logStats(visit);
List<LocationStat> stats = new ArrayList<>();
statLocationIndex.findAll().forEach((stats::add));
locationStatRepository.findAll().forEach((stats::add));
assertThat(stats.size()).isEqualTo(1L);
LocationStat locationStat = stats.get(0);
......@@ -158,20 +158,20 @@ class StatistiquesServiceIT {
.qrCodeScanTime(TODAY_AT_8AM.plus(15, ChronoUnit.MINUTES))
.build();
service.logStats(visit1);
long before = statLocationIndex.count();
long before = locationStatRepository.count();
Visit visit2 = defaultVisit().toBuilder()
.qrCodeScanTime(TODAY_AT_8AM.plus(15, ChronoUnit.MINUTES))
.build();
service.logStats(visit2);
long after = statLocationIndex.count();
long after = locationStatRepository.count();
assertThat(before).isEqualTo(after);
template.indexOps(LocationStat.class).refresh();
List<LocationStat> stats = new ArrayList<>();
statLocationIndex.findAll().forEach((stats::add));
locationStatRepository.findAll().forEach((stats::add));
assertThat(stats.size()).isEqualTo(1L);
LocationStat locationStat = stats.get(0);
......@@ -191,7 +191,7 @@ class StatistiquesServiceIT {
service.logStats(visit1);
service.logStats(visit2);
assertThat(statLocationIndex.count()).isEqualTo(2);
assertThat(locationStatRepository.count()).isEqualTo(2);
}
@Test
......@@ -202,7 +202,7 @@ class StatistiquesServiceIT {
service.logStats(visit1);
service.logStats(visit2);
assertThat(statLocationIndex.count()).isEqualTo(2);
assertThat(locationStatRepository.count()).isEqualTo(2);
}
@Test
......@@ -231,9 +231,9 @@ class StatistiquesServiceIT {
service.logStats(visit3);
service.logStats(visit4);
assertThat(statLocationIndex.count()).isEqualTo(1);
assertThat(locationStatRepository.count()).isEqualTo(1);
final var statLocation = statLocationIndex.findById(generateLocationStatKey(visit1)).get();
final var statLocation = locationStatRepository.findById(generateLocationStatKey(visit1)).get();
assertThat(statLocation.getBackwardVisits()).as("backward visits").isEqualTo(3l);
assertThat(statLocation.getForwardVisits()).as("forward visits").isEqualTo(1l);
......@@ -252,7 +252,7 @@ class StatistiquesServiceIT {
service.logStats(visit1);
service.logStats(visit2);
assertThat(statLocationIndex.count()).isEqualTo(2);
assertThat(locationStatRepository.count()).isEqualTo(2);
}
@Test
......@@ -264,7 +264,7 @@ class StatistiquesServiceIT {
service.logStats(visit2);
List<LocationStat> stats = new ArrayList<>();
statLocationIndex.findAll().forEach(stats::add);
locationStatRepository.findAll().forEach(stats::add);
assertThat(stats.size()).isEqualTo(2);
}
......@@ -283,9 +283,9 @@ class StatistiquesServiceIT {
service.logStats(reportStat);
assertThat(reportStatIndex.count()).isEqualTo(1);
assertThat(reportStatRepository.count()).isEqualTo(1);
final var stat = reportStatIndex.findAll().iterator().next();
final var stat = reportStatRepository.findAll().iterator().next();
assertThat(stat.getId()).isInstanceOf(String.class);
assertThat(stat.getId()).isNotBlank();
......@@ -307,15 +307,15 @@ class StatistiquesServiceIT {
template.indexOps(LocationStat.class).refresh();
List<LocationStat> stats = new ArrayList<>();
statLocationIndex.findAll().forEach(stats::add);
locationStatRepository.findAll().forEach(stats::add);
assertThat(stats.size()).isEqualTo(1);
Optional<LocationStat> statLocation = statLocationIndex.findById(generateLocationStatKey(visit1));
Optional<LocationStat> statLocation = locationStatRepository.findById(generateLocationStatKey(visit1));
LocationStat locationStat = statLocation.get();
locationStat.setForwardVisits(1);
statLocationIndex.save(locationStat);
locationStatRepository.save(locationStat);
template.indexOps(LocationStat.class).refresh();
LocationStat locationStatLocked1 = LocationStat.builder()
......@@ -332,13 +332,13 @@ class StatistiquesServiceIT {
Thread thread1 = new Thread("t1") {
public void run() {
statLocationIndex.save(locationStatLocked1);
locationStatRepository.save(locationStatLocked1);
}
};
Thread thread2 = new Thread("t2") {
public void run() {
statLocationIndex.save(locationStatLocked2);
locationStatRepository.save(locationStatLocked2);
}
};
thread1.start();
......@@ -351,7 +351,7 @@ class StatistiquesServiceIT {
Visit visit1 = defaultVisit().toBuilder().build();
Mockito.when(statLocationIndexMock.findById(anyString()))
Mockito.when(locationStatRepositoryMock.findById(anyString()))
.thenThrow(new RuntimeException("Whatever reason"))
.thenThrow(new RuntimeException("Whatever reason"))
.thenThrow(new RuntimeException("Whatever reason"));
......
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