Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,20 @@
import de.muenchen.dave.domain.dtos.bearbeiten.BackendIdDTO;
import de.muenchen.dave.domain.dtos.external.DetectionDTO;
import de.muenchen.dave.domain.dtos.laden.LadeZaehldatumDTO;
import de.muenchen.dave.domain.elasticsearch.Zaehlung;
import de.muenchen.dave.domain.mapper.DetectorMapper;
import de.muenchen.dave.domain.mapper.HochrechnungsfaktorMapper;
import de.muenchen.dave.exceptions.BrokenInfrastructureException;
import de.muenchen.dave.exceptions.DataNotFoundException;
import de.muenchen.dave.repositories.relationaldb.HochrechnungsfaktorRepository;
import de.muenchen.dave.repositories.relationaldb.ZeitintervallRepository;
import de.muenchen.dave.services.ZaehlstelleIndexService;
import jakarta.transaction.Transactional;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

Expand All @@ -31,15 +36,19 @@ public class ExternalDetectorService {

private final ZeitintervallRepository zeitintervallRepository;

private final ZaehlstelleIndexService zaehlstelleIndexService;

public ExternalDetectorService(final DetectorMapper detectorMapper,
final HochrechnungsfaktorMapper hochrechnungsfaktorMapper,
final HochrechnungsfaktorRepository hochrechnungsfaktorRepository,
final ZeitintervallRepository zeitintervallRepository) {
final ZeitintervallRepository zeitintervallRepository,
final ZaehlstelleIndexService zaehlstelleIndexService) {

this.detectorMapper = detectorMapper;
this.hochrechnungsfaktorMapper = hochrechnungsfaktorMapper;
this.hochrechnungsfaktorRepository = hochrechnungsfaktorRepository;
this.zeitintervallRepository = zeitintervallRepository;
this.zaehlstelleIndexService = zaehlstelleIndexService;
}

@Transactional
Expand Down Expand Up @@ -85,8 +94,53 @@ private Hochrechnung createHochrechnung(Hochrechnungsfaktor hochrechnungsfaktor,
return hochrechnung;
}

private List<DetectionDTO> addMissingKnotenarme(List<DetectionDTO> detections) throws DataNotFoundException {
if (detections.isEmpty()) {
return null;
}
DetectionDTO detection = detections.get(0);
UUID zaehlungId = detection.getZaehlungId();
LocalDateTime start = detection.getStartUhrzeit();
LocalDateTime ende = detection.getEndeUhrzeit();

List<DetectionDTO> zeroDetectionsToAdd = new ArrayList<>();

Zaehlung zaehlung = zaehlstelleIndexService.getZaehlung(detection.getZaehlungId().toString());
zaehlung.getFahrbeziehungen().forEach(f -> {
int von = f.getVon();
int nach = f.getNach();
if (!detectionHasDataForFahrbeziehung(detections, von, nach)) {
log.debug("Detection enthält keine Daten für die Fahrbeziehung von {} nach {}, füge diese mit Zählwert 0 hinzu", von, nach);
DetectionDTO d = new DetectionDTO();
d.setStartUhrzeit(start);
d.setEndeUhrzeit(ende);
d.setZaehlungId(zaehlungId);
d.setVon(von);
d.setNach(nach);
d.setBusse(0);
d.setFussgaenger(0);
d.setFahrradfahrer(0);
d.setKraftraeder(0);
d.setLastzuege(0);
d.setLkw(0);
d.setPkw(0);
zeroDetectionsToAdd.add(d);
}
});
detections.addAll(zeroDetectionsToAdd);
return detections;
}

private boolean detectionHasDataForFahrbeziehung(List<DetectionDTO> detections, Integer von, Integer nach) {
return detections.stream()
.anyMatch(d -> d.getVon() == von && d.getNach() == nach);
}

public BackendIdDTO saveLatestDetections(List<DetectionDTO> detections) throws BrokenInfrastructureException, DataNotFoundException {
log.debug("saveLatestDetections");
//get all Knotenarme for Zaehlung and set counts to zero for all arms that are not included in the detection
detections = addMissingKnotenarme(detections);

BackendIdDTO backendIdDto = new BackendIdDTO();
for (DetectionDTO detection : detections) {
backendIdDto = saveDetection(detection);
Expand Down
Loading