Skip to content
Merged
Show file tree
Hide file tree
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 @@ -2,8 +2,8 @@

import lombok.Builder;

public record CounselSessionStatRes(long totalSessionCount, long counseleeCountForThisMonth,
long totalCaringMessageCount, long counselHoursForThisMonth) {
public record CounselSessionStatRes(long counselHoursThisMonth, long counseleeCountForThisMonth,
long medicationCounselCountThisYear, long counselorCountThisYear) {

@Builder
public CounselSessionStatRes {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,6 @@ PageRes<CounselSession> findByCounseleeNameAndCounselorNameAndScheduledDateTimeA
List<CounselSession> findValidCounselSessionsByCounseleeId(String counseleeId);

void bulkUpdateCounselSessionNum(Map<String, Integer> sessionUpdates);

Long countDistinctCounselorsByCompletedSessionsInYear(int year);
}
Original file line number Diff line number Diff line change
Expand Up @@ -267,4 +267,17 @@ public void bulkUpdateCounselSessionNum(Map<String, Integer> sessionUpdates) {
.where(counselSession.id.in(sessionUpdates.keySet()))
.execute();
}

@Override
public Long countDistinctCounselorsByCompletedSessionsInYear(int year) {
return queryFactory
.select(counselSession.counselor.countDistinct())
.from(counselSession)
.where(
counselSession.status.eq(ScheduleStatus.COMPLETED),
counselSession.startDateTime.year().eq(year),
counselSession.counselor.isNotNull(),
counselSession.counselor.status.eq(CounselorStatus.ACTIVE))
.fetchOne();
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.springboot.api.counselsession.repository;

import java.time.LocalDateTime;
import java.util.Optional;

import org.springframework.data.jpa.repository.JpaRepository;
Expand All @@ -9,4 +10,6 @@
public interface MedicationCounselRepository extends JpaRepository<MedicationCounsel, String> {

Optional<MedicationCounsel> findByCounselSessionId(String counselSessionId);

long countByCreatedDatetimeBetween(LocalDateTime startDate, LocalDateTime endDate);
}
Original file line number Diff line number Diff line change
Expand Up @@ -301,35 +301,45 @@ public List<LocalDate> getSessionDatesByYearAndMonth(int year, int month) {
@Cacheable(value = "sessionStats")
@Transactional(readOnly = true)
public CounselSessionStatRes getSessionStats() {
long totalSessionCount = calculateTotalSessionCount();
Long counseleeCount = counselSessionRepository.countDistinctCounseleeForCurrentMonth();
long totalCaringMessageCount = counselSessionRepository.count();
double counselHours = calculateCounselHoursForThisMonth();
long counselHoursThisMonth = calculateCounselHoursForThisMonth();
Long counseleeCountForThisMonth = counselSessionRepository.countDistinctCounseleeForCurrentMonth();
long medicationCounselCountThisYear = calculateMedicationCounselCountThisYear();
long counselorCountThisYear = calculateCounselorCountThisYear();

return CounselSessionStatRes.builder()
.totalSessionCount(totalSessionCount)
.counseleeCountForThisMonth(counseleeCount)
.totalCaringMessageCount(totalCaringMessageCount)
.counselHoursForThisMonth((long) counselHours)
.counselHoursThisMonth(counselHoursThisMonth)
.counseleeCountForThisMonth(counseleeCountForThisMonth)
.medicationCounselCountThisYear(medicationCounselCountThisYear)
.counselorCountThisYear(counselorCountThisYear)
.build();
}

private long calculateTotalSessionCount() {
return counselSessionRepository.countByStatus(ScheduleStatus.COMPLETED);
private long calculateMedicationCounselCountThisYear() {
int currentYear = LocalDateTime.now().getYear();
return medicationCounselRepository.countByCreatedDatetimeBetween(
LocalDateTime.of(currentYear, 1, 1, 0, 0, 0),
LocalDateTime.of(currentYear + 1, 1, 1, 0, 0, 0)
);
}

private double calculateCounselHoursForThisMonth() {
private long calculateCounselorCountThisYear() {
int currentYear = LocalDateTime.now().getYear();
Long count = counselSessionRepository.countDistinctCounselorsByCompletedSessionsInYear(currentYear);
return count != null ? count : 0L;
}

private long calculateCounselHoursForThisMonth() {
int year = LocalDateTime.now().getYear();
int month = LocalDateTime.now().getMonthValue();
List<CounselSession> completedSessions = counselSessionRepository
.findCompletedSessionsByYearAndMonth(year, month);

return completedSessions.stream()
.mapToDouble(session -> {
.mapToLong(session -> {
Duration duration = Duration.between(
session.getStartDateTime(),
session.getEndDateTime());
return duration.toMinutes() / 60.0;
return (long) (duration.toMinutes() / 60.0);
})
.sum();
}
Expand Down