Skip to content
Merged
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,12 @@ public interface MetricFactRepository extends JpaRepository<MetricFact, Long> {
BigDecimal sumSpendsByUserIdAndOrgIdAndProvider(@Param("userId") Long userId, @Param("orgId") Long orgId,
@Param("provider") Provider provider);

//전체 지표 조회 로직에서 사용
// 해당 프로젝트의 가장 최신 데이터 날짜를 가져오는 쿼리
@Query("SELECT MAX(m.timeBucket) FROM MetricFact m")
Optional<LocalDateTime> findLatestTimeBucket();
// 전체 지표 조회 로직에서 사용
// 해당 조직(orgId)의 가장 최신 timeBucket을 가져오는 쿼리
@Query("SELECT MAX(m.timeBucket) FROM MetricFact m " +
"JOIN m.project p " +
"WHERE p.organization.id = :orgId")
Optional<LocalDateTime> findLatestTimeBucketByOrgId(@Param("orgId") Long orgId);
Comment thread
ojy0903 marked this conversation as resolved.

// orgId에 속한 모든 프로젝트의 지표중 해당 MetricFact 가 속한 AdCampaign 의 status 가 ON_GOING 인 지표를 지정된 기간 범위 합산
@Query("SELECT " +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import com.whereyouad.WhereYouAd.domains.advertisement.domain.constant.Status;
import com.whereyouad.WhereYouAd.domains.advertisement.exception.AdvertisementHandler;
import com.whereyouad.WhereYouAd.domains.advertisement.exception.code.AdvertisementErrorCode;
import com.whereyouad.WhereYouAd.domains.advertisement.domain.constant.BudgetFieldType;
import com.whereyouad.WhereYouAd.domains.advertisement.persistence.entity.BudgetHistory;
import com.whereyouad.WhereYouAd.domains.advertisement.persistence.repository.AdCampaignRepository;
import com.whereyouad.WhereYouAd.domains.advertisement.domain.constant.Provider;
Expand Down Expand Up @@ -109,14 +108,16 @@ public DashboardResponse.AggregatedSummaryResponse getAggregatedMetrics(Long use
throw new DashboardException(ProjectErrorCode.ACCESS_FORBIDDEN);
}

//DB 내부 Mock data 중 가장 최근의 timeBucket 값 추출
LocalDateTime latestDate = metricFactRepository.findLatestTimeBucket()
//DB 내부 Mock data 중 조직별로 가장 최근의 timeBucket 값 추출
LocalDateTime latestDate = metricFactRepository.findLatestTimeBucketByOrgId(orgId)
.orElse(LocalDateTime.now());

//DB 내부 Mock data 중 가장 최근의 timeBucket 값 기반 한달전, 두달전 기준 정립
LocalDateTime oneMonthAgo = latestDate.minusMonths(1);
//집계에서 가장 최신 timeBucket 을 포함하기 위해 하루 뒤를 상한으로 사용
LocalDateTime currentEnd = latestDate.plusDays(1);

LocalDateTime twoMonthsAgo = latestDate.minusMonths(2);
//가장 최근 timeBucket 기준 한달전, 두달전 기준 정립
LocalDateTime oneMonthAgo = currentEnd.minusMonths(1);
LocalDateTime twoMonthsAgo = currentEnd.minusMonths(2);

MetricSumProjection currentProjection;
MetricSumProjection pastProjection;
Expand All @@ -125,7 +126,7 @@ public DashboardResponse.AggregatedSummaryResponse getAggregatedMetrics(Long use
//시간값들과 회원이 속한 project 리스트 기반 projection 으로 DB 에서
//TotalImpressions, TotalClicks, TotalConversions, TotalSpend, TotalRevenue 를 집계해서 가져오기
currentProjection = metricFactRepository.findMetricsSumByOrgIdAndDateRange(
orgId, oneMonthAgo, latestDate, OrgStatus.ACTIVE, Status.ON_GOING
orgId, oneMonthAgo, currentEnd, OrgStatus.ACTIVE, Status.ON_GOING
); //가장 최근 ~ 한달 전의 집계 projection

pastProjection = metricFactRepository.findMetricsSumByOrgIdAndDateRange(
Expand All @@ -141,7 +142,7 @@ public DashboardResponse.AggregatedSummaryResponse getAggregatedMetrics(Long use
throw new DashboardException(DashboardErrorCode.PROVIDER_NOT_VALID);
}
currentProjection = metricFactRepository.findMetricsSumByOrgIdAndProvider(
orgId, provider, oneMonthAgo, latestDate, OrgStatus.ACTIVE, Status.ON_GOING);
orgId, provider, oneMonthAgo, currentEnd, OrgStatus.ACTIVE, Status.ON_GOING);
pastProjection = metricFactRepository.findMetricsSumByOrgIdAndProvider(
orgId, provider, twoMonthsAgo, oneMonthAgo, OrgStatus.ACTIVE, Status.ON_GOING);
}
Expand Down
Loading