Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: class cast exception for statistics #2

Merged
merged 1 commit into from
Apr 24, 2024
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 @@ -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
Loading