Skip to content

Commit

Permalink
fix: class cast exception for statistics
Browse files Browse the repository at this point in the history
  • Loading branch information
tkuzynow committed Apr 24, 2024
1 parent 784dc57 commit d8ee537
Showing 1 changed file with 23 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,10 @@ public class RegistrationStatisticsDTOConverter {
public RegistrationStatisticsResponseDTO convertStatisticsEvent(
StatisticsEvent rawEvent, StatisticEventsContainer statisticEventsContainer) {
RegistrationMetaData metadata = (RegistrationMetaData) rawEvent.getMetaData();
String maxArchiveDate = findMaxArchiveDate(rawEvent.getSessionId(), statisticEventsContainer.getArchiveSessionEvents());
String deleteAccountDate = findDeleteAccountDate(rawEvent.getUser().getId(), statisticEventsContainer.getDeleteAccountEvents());
String maxArchiveDate = findMaxArchiveDate(rawEvent.getSessionId(),
statisticEventsContainer.getArchiveSessionEvents());
String deleteAccountDate = findDeleteAccountDate(rawEvent.getUser().getId(),
statisticEventsContainer.getDeleteAccountEvents());

return new RegistrationStatisticsResponseDTO()
.userId(rawEvent.getUser().getId())
Expand All @@ -38,9 +40,14 @@ public RegistrationStatisticsResponseDTO convertStatisticsEvent(
.postalCode(metadata.getPostalCode())
.referer(metadata.getReferer())
.attendedVideoCallsCount(
countEventsPerAdviceSeekerMatchingOnMetadata(rawEvent.getUser().getId(), statisticEventsContainer.getVideoCallStartedEvents()))
.appointmentsBookedCount(countEventsPerAdviceSeekerMatchingOnMetadata(rawEvent.getUser().getId(), statisticEventsContainer.getBookingCreatedEvents()))
.consultantMessagesCount(countEventsPerAdviceSeekerMatchingOnMetadataByReceiverId(rawEvent.getUser().getId(), statisticEventsContainer.getConsultantMessageCreatedEvents()));
countEventsPerAdviceSeekerMatchingOnMetadata(rawEvent.getUser().getId(),
statisticEventsContainer.getVideoCallStartedEvents()))
.appointmentsBookedCount(
countEventsPerAdviceSeekerMatchingOnMetadata(rawEvent.getUser().getId(),
statisticEventsContainer.getBookingCreatedEvents()))
.consultantMessagesCount(
countEventsPerAdviceSeekerMatchingOnMetadataByReceiverId(rawEvent.getUser().getId(),
statisticEventsContainer.getConsultantMessageCreatedEvents()));
}

private Integer countEventsPerAdviceSeekerMatchingOnMetadata(String adviceSeekerId,
Expand All @@ -54,17 +61,18 @@ private Integer countEventsPerAdviceSeekerMatchingOnMetadataByReceiverId(String
Collection<StatisticsEvent> events) {

return events != null
? (int) getCountOfEventsPerAdviceSeekerMatchingOnMetadataByReceiverId(adviceSeekerId, events) : 0;
? (int) getCountOfEventsPerAdviceSeekerMatchingOnMetadataByReceiverId(adviceSeekerId,
events) : 0;
}

private long getCountOfEventsPerAdviceSeekerMatchingOnMetadata(String adviceSeekerId,
Collection<StatisticsEvent> statisticsEvents) {
return statisticsEvents.stream()
.filter(event -> event.getMetaData() instanceof AdviceSeekerAwareMetaData)
.filter(event -> {
AdviceSeekerAwareMetaData metaData = (AdviceSeekerAwareMetaData) event.getMetaData();
return metaData.getAdviceSeekerId() != null && metaData.getAdviceSeekerId()
.equals(adviceSeekerId);
AdviceSeekerAwareMetaData metaData = (AdviceSeekerAwareMetaData) event.getMetaData();
return metaData.getAdviceSeekerId() != null && metaData.getAdviceSeekerId()
.equals(adviceSeekerId);

})
.count();
Expand All @@ -85,14 +93,16 @@ private String getEndDate(String maxArchiveDate, String deleteAccountDate) {
return deleteAccountDate != null ? deleteAccountDate : maxArchiveDate;
}

private String findDeleteAccountDate(String userId, Collection<StatisticsEvent> deleteAccountEvents) {
private String findDeleteAccountDate(String userId,
Collection<StatisticsEvent> deleteAccountEvents) {
return deleteAccountEvents != null ? deleteAccountEvents.stream()
.filter(event -> event.getUser() != null && event.getUser().getId().equals(userId))
.map(event -> ((DeleteAccountMetaData) event.getMetaData()).getDeleteDate())
.findFirst().orElse(null) : null;
}

private String findMaxArchiveDate(Long sessionId, Collection<StatisticsEvent> archiveSessionEvents) {
private String findMaxArchiveDate(Long sessionId,
Collection<StatisticsEvent> archiveSessionEvents) {
var maxArchiveEvent = findMaxArchiveSessionEvent(sessionId, archiveSessionEvents);
if (maxArchiveEvent.isPresent()) {
ArchiveMetaData metaData = (ArchiveMetaData) maxArchiveEvent.get().getMetaData();
Expand All @@ -101,7 +111,8 @@ private String findMaxArchiveDate(Long sessionId, Collection<StatisticsEvent> ar
return null;
}

private Optional<StatisticsEvent> findMaxArchiveSessionEvent(Long sessionId, Collection<StatisticsEvent> archiveSessionEvents) {
private Optional<StatisticsEvent> findMaxArchiveSessionEvent(Long sessionId,
Collection<StatisticsEvent> archiveSessionEvents) {
return nonNull(archiveSessionEvents) ? archiveSessionEvents.stream()
.filter(event -> event.getSessionId() != null && event.getSessionId().equals(sessionId))
.max(comparing(StatisticsEvent::getTimestamp)) : Optional.empty();
Expand Down

0 comments on commit d8ee537

Please sign in to comment.