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 @@ -4,16 +4,16 @@
import java.util.List;
import java.util.Map;

import kernel360.trackyweb.drive.application.dto.internal.NonOperatedCar;
import kernel360.trackyweb.drive.application.dto.internal.OperationTotalCount;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Component;

import kernel360.trackycore.core.common.exception.ErrorCode;
import kernel360.trackycore.core.common.exception.GlobalException;
import kernel360.trackycore.core.domain.entity.DriveEntity;
import kernel360.trackyweb.drive.application.dto.internal.NonOperatedCar;
import kernel360.trackyweb.drive.application.dto.internal.OperationCarCount;
import kernel360.trackyweb.drive.application.dto.internal.OperationTotalCount;
import kernel360.trackyweb.drive.domain.DriveHistory;
import kernel360.trackyweb.drive.infrastructure.repository.DriveDomainRepository;
import lombok.RequiredArgsConstructor;
Expand All @@ -22,58 +22,58 @@
@RequiredArgsConstructor
public class DriveDomainProvider {

private final DriveDomainRepository driveDomainRepository;
private final DriveDomainRepository driveDomainRepository;

public Page<DriveEntity> searchDrivesByFilter(
String search,
String mdn,
LocalDate startDateTime,
LocalDate endDateTime,
Pageable pageable) {
return driveDomainRepository.searchByFilter(search, mdn, startDateTime, endDateTime, pageable);
}
public Page<DriveEntity> searchDrivesByFilter(
String search,
String mdn,
LocalDate startDateTime,
LocalDate endDateTime,
Pageable pageable) {
return driveDomainRepository.searchByFilter(search, mdn, startDateTime, endDateTime, pageable);
}

public Page<DriveEntity> findRunningDriveList(
String search,
Pageable pageable
) {
return driveDomainRepository.findRunningDriveList(search, pageable);
}
public Page<DriveEntity> findRunningDriveList(
String search,
Pageable pageable
) {
return driveDomainRepository.findRunningDriveList(search, pageable);
}

public DriveEntity findRunningDriveById(Long driveId) {
return driveDomainRepository.findRunningDriveById(driveId)
.orElseThrow(() -> GlobalException.throwError(ErrorCode.NOT_REALTIME_DRIVE));
}
public DriveEntity findRunningDriveById(Long driveId) {
return driveDomainRepository.findRunningDriveById(driveId)
.orElseThrow(() -> GlobalException.throwError(ErrorCode.NOT_REALTIME_DRIVE));
}

public DriveHistory findByDriveId(Long driveId) {
return driveDomainRepository.findByDriveId(driveId)
.orElseThrow(() -> GlobalException.throwError(ErrorCode.DRIVE_NOT_FOUND));
}
public DriveHistory findByDriveId(Long driveId) {
return driveDomainRepository.findByDriveId(driveId)
.orElseThrow(() -> GlobalException.throwError(ErrorCode.DRIVE_NOT_FOUND));
}

public List<DriveEntity> findByMdn(String mdn) {
return driveDomainRepository.findDriveListByMdn(mdn);
}
public List<DriveEntity> findByMdn(String mdn) {
return driveDomainRepository.findDriveListByMdn(mdn);
}

public Double getTotalDriveDistance() {
return driveDomainRepository.getTotalDriveDistance();
}
public Double getTotalDriveDistance() {
return driveDomainRepository.getTotalDriveDistance();
}

public Long getTotalDriveDurationInMinutes() {
return driveDomainRepository.getTotalDriveDurationInMinutes();
}
public Long getTotalDriveDurationInMinutes() {
return driveDomainRepository.getTotalDriveDurationInMinutes();
}

//일일 통계 - 당일 운행 차량 수 (distinct mdn)
public Map<Long, Integer> countDailyOperationCar(LocalDate targetDate) {
return OperationCarCount.toMap(driveDomainRepository.getDailyOperationCar(targetDate));
}
//일일 통계 - 당일 운행 차량 수 (distinct mdn)
public Map<Long, Integer> countDailyOperationCar(LocalDate targetDate) {
return OperationCarCount.toMap(driveDomainRepository.getDailyOperationCar(targetDate));
}

//일일 통계 - 당일 총 운행 수
public Map<Long, Integer> countDailyTotalOperation(LocalDate targetDate) {
return OperationTotalCount.toMap(driveDomainRepository.getDailyTotalOperation(targetDate));
}
//일일 통계 - 당일 총 운행 수
public Map<Long, Integer> countDailyTotalOperation(LocalDate targetDate) {
return OperationTotalCount.toMap(driveDomainRepository.getDailyTotalOperation(targetDate));
}

//월별 통계 - 미운행 차량 수
public Map<Long, Integer> getNonOperatedCars(LocalDate targetDate) {
return NonOperatedCar.toMap(driveDomainRepository.getNonOperatedCars(targetDate));
}
//월별 통계 - 미운행 차량 수
public Map<Long, Integer> getNonOperatedCars(LocalDate targetDate) {
return NonOperatedCar.toMap(driveDomainRepository.getNonOperatedCars(targetDate));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import java.time.LocalDate;
import java.time.YearMonth;
import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Service;
Expand All @@ -15,6 +14,7 @@
import kernel360.trackyweb.statistic.application.dto.response.MonthlyStatisticResponse;
import kernel360.trackyweb.statistic.domain.provider.DailyStatisticProvider;
import kernel360.trackyweb.statistic.domain.provider.MonthlyStatisticProvider;
import kernel360.trackyweb.timedistance.domain.provider.TimeDistanceDomainProvider;
import lombok.RequiredArgsConstructor;

@Service
Expand All @@ -23,6 +23,7 @@ public class StatisticService {

private final DailyStatisticProvider dailyStatisticProvider;
private final MonthlyStatisticProvider monthlyStatisticProvider;
private final TimeDistanceDomainProvider timeDistanceDomainProvider;
private final BizProvider bizProvider;

public ApiResponse<DailyStatisticResponse> getDailyStatistic(String bizUuid, LocalDate date) {
Expand All @@ -31,34 +32,30 @@ public ApiResponse<DailyStatisticResponse> getDailyStatistic(String bizUuid, Loc

DailyStatisticEntity dailyStatistic = dailyStatisticProvider.getDailyStatistic(bizId, date);

List<Integer> list = new ArrayList<>();
for (int i = 0; i < 24; i++) {
list.add(1);
}
DailyStatisticResponse response = DailyStatisticResponse.from(dailyStatistic, list);
long[] hourlyDriveCounts = timeDistanceDomainProvider.getHourlyDriveCounts(bizId, date);

DailyStatisticResponse response = DailyStatisticResponse.from(dailyStatistic, hourlyDriveCounts);

return ApiResponse.success(response);
}

public ApiResponse<MonthlyStatisticResponse> getMonthlyStatistic(String bizUuid, YearMonth date) {
public ApiResponse<MonthlyStatisticResponse> getMonthlyStatistic(String bizUuid, YearMonth date,
YearMonth targetDate) {

if (targetDate == null) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

targetDate가 null인 경우는 언제가 있나요?
그리고 - 1년 + 1월은 어떤 의도를 가진 코드인가요?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

targetDate가 null인 경우는 언제가 있나요? 그리고 - 1년 + 1월은 어떤 의도를 가진 코드인가요?

date는 기준이 되는 월이고 targetDate는 그래프를 어디까지 보여줄지 결정하는 변수였는데 고정 12개월로 보여주는걸로 되어서 지금은 사용하지 않는 변수입니다

-1년 +1월은 -11개월보다 작년까지의 데이터를 보여준다는 뜻이 잘 드러나는 것 같아서 쓴건데 이것도 좀 이상하긴 하네요...

둘 다 수정해서 다시 푸시하겠습니다

targetDate = date.minusYears(1).plusMonths(1);
}

Long bizId = bizProvider.getBiz(bizUuid).getId();

LocalDate localDate = date.atDay(1);
MonthlyStatisticEntity monthlyStatistic = monthlyStatisticProvider.getMonthlyStatistic(bizId, date.atDay(1));

MonthlyStatisticEntity monthlyStatistic = monthlyStatisticProvider.getMonthlyStatistic(bizId, localDate);
List<MonthlyStatisticResponse.MonthlyStats> monthlyStats = monthlyStatisticProvider.getMonthlyDataTuples(bizId,
date.atDay(1),
targetDate.atDay(1));

List<Integer> list = new ArrayList<>();
for (int i = 0; i < 12; i++) {
list.add(1);
}
List<Long> list2 = new ArrayList<>();
for (int i = 0; i < 12; i++) {
list2.add(1L);
}
MonthlyStatisticResponse response = MonthlyStatisticResponse.from(monthlyStatistic, list, list2);
MonthlyStatisticResponse response = MonthlyStatisticResponse.from(monthlyStatistic, monthlyStats);

return ApiResponse.success(response);
}

}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package kernel360.trackyweb.statistic.application.dto.response;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.IntStream;

import kernel360.trackycore.core.domain.entity.DailyStatisticEntity;

Expand All @@ -15,22 +15,23 @@ public record Summary(
double averageOperationRate,
long totalDrivingSeconds,
int totalDriveCount,
double totalDrivingDistanceKm // 저장 단위는 km로 가정
double totalDrivingDistance
) {
}

public record HourlyStat(
int hour,
int driveCount
long driveCount
) {
}

public static DailyStatisticResponse from(DailyStatisticEntity e, List<Integer> hourlyDriveCounts) {
List<DailyStatisticResponse.HourlyStat> hourlyStats = IntStream.range(0, 24)
.mapToObj(i -> new DailyStatisticResponse.HourlyStat(
i,
hourlyDriveCounts.get(i)
)).toList();
public static DailyStatisticResponse from(DailyStatisticEntity e, long[] hourlyDriveCounts) {
List<HourlyStat> hourlyStats = new ArrayList<>();

for (int i = 0; i < hourlyDriveCounts.length; i++) {
HourlyStat hourlyStat = new HourlyStat(i, hourlyDriveCounts[i]);
hourlyStats.add(hourlyStat);
}

return new DailyStatisticResponse(
new Summary(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,44 +1,32 @@
package kernel360.trackyweb.statistic.application.dto.response;

import java.util.List;
import java.util.stream.IntStream;

import kernel360.trackycore.core.domain.entity.MonthlyStatisticEntity;

public record MonthlyStatisticResponse(
Summary summary,
List<MonthlyStat> monthlyStat
List<MonthlyStats> monthlyStats
) {
public record Summary(
int totalCarCount,
int nonOperatingCarCount,
double averageOperationRate,
long totalDrivingSeconds,
int totalDriveCount,
double totalDrivingDistanceKm // 저장 단위는 km로 가정
double totalDrivingDistance
) {
}

public record MonthlyStat(
public record MonthlyStats(
int year,
int month,
int driveCount,
long driveDistance
double driveDistance
) {
}

public static MonthlyStatisticResponse from(MonthlyStatisticEntity e,
List<Integer> monthlyDriveCounts,
List<Long> monthlyDriveDistances
) {
int currentMonth = e.getDate().getMonthValue();
List<MonthlyStat> monthlyStats = IntStream.range(0, currentMonth)
.mapToObj(i -> new MonthlyStat(
e.getDate().getYear(),
i + 1,
monthlyDriveCounts.get(i),
monthlyDriveDistances.get(i)
)).toList();
public static MonthlyStatisticResponse from(MonthlyStatisticEntity e, List<MonthlyStats> monthlyStats) {

return new MonthlyStatisticResponse(
new Summary(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import kernel360.trackycore.core.common.exception.ErrorCode;
import kernel360.trackycore.core.common.exception.GlobalException;
import kernel360.trackycore.core.domain.entity.MonthlyStatisticEntity;
import kernel360.trackyweb.statistic.application.dto.response.MonthlyStatisticResponse;
import kernel360.trackyweb.statistic.infrastructure.repository.monthly.MonthlyStatisticDomainRepository;
import lombok.RequiredArgsConstructor;

Expand Down Expand Up @@ -42,4 +43,10 @@ public void saveMonthlyStatistic(List<MonthlyStatisticEntity> resultEntities) {
}
}
}

public List<MonthlyStatisticResponse.MonthlyStats> getMonthlyDataTuples(Long bizId, LocalDate localDate,
LocalDate localDate1) {

return monthlyStatisticRepository.getMonthlyStats(bizId, localDate, localDate1);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,4 @@
public interface DailyStatisticDomainRepository extends DailyStatisticRepository, DailyStatisticRepositoryCustom {

Optional<DailyStatisticEntity> findByBizIdAndDate(Long bizId, LocalDate date);

}
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@
import kernel360.trackycore.core.infrastructure.repository.MonthlyStatisticRepository;

public interface MonthlyStatisticDomainRepository
extends MonthlyStatisticRepository {
extends MonthlyStatisticRepository, MonthlyStatisticDomainRepositoryCustom {

Optional<MonthlyStatisticEntity> findByBizIdAndDate(Long bizId, LocalDate date);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package kernel360.trackyweb.statistic.infrastructure.repository.monthly;

import java.time.LocalDate;
import java.util.List;

import kernel360.trackyweb.statistic.application.dto.response.MonthlyStatisticResponse;

public interface MonthlyStatisticDomainRepositoryCustom {

List<MonthlyStatisticResponse.MonthlyStats> getMonthlyStats(Long bizId, LocalDate currentDate,
LocalDate targetDate);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package kernel360.trackyweb.statistic.infrastructure.repository.monthly;

import java.time.LocalDate;
import java.util.List;

import org.springframework.stereotype.Repository;

import com.querydsl.core.types.Projections;
import com.querydsl.jpa.impl.JPAQueryFactory;

import kernel360.trackycore.core.domain.entity.QMonthlyStatisticEntity;
import kernel360.trackyweb.statistic.application.dto.response.MonthlyStatisticResponse;
import lombok.RequiredArgsConstructor;

@Repository
@RequiredArgsConstructor
public class MonthlyStatisticDomainRepositoryImpl implements MonthlyStatisticDomainRepositoryCustom {

private final JPAQueryFactory queryFactory;

@Override
public List<MonthlyStatisticResponse.MonthlyStats> getMonthlyStats(Long bizId, LocalDate currentDate,
LocalDate targetDate) {
QMonthlyStatisticEntity e = QMonthlyStatisticEntity.monthlyStatisticEntity;

return queryFactory.select(
Projections.constructor(
MonthlyStatisticResponse.MonthlyStats.class,
e.date.year(),
e.date.month(),
e.totalDriveCount.sum(),
e.totalDriveDistance.sum()
))
.from(e)
.where(
e.bizId.eq(bizId)
.and(e.date.between(targetDate, currentDate))
)
.groupBy(e.date.yearMonth())
.orderBy(e.date.yearMonth().asc())
.fetch();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,9 @@ public ApiResponse<DailyStatisticResponse> getDailyStatistic(
public ApiResponse<MonthlyStatisticResponse> getMonthlyStatistic(
@AuthenticationPrincipal MemberPrincipal memberPrincipal,
@RequestParam(name = "date")
YearMonth date
YearMonth date,
YearMonth targetDate
) {
return statisticService.getMonthlyStatistic(memberPrincipal.bizUuid(), date);
return statisticService.getMonthlyStatistic(memberPrincipal.bizUuid(), date, targetDate);
}
}
Loading