Skip to content

Commit

Permalink
develop [develop] daily data report 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
euije committed Mar 25, 2024
1 parent dd3a54e commit dc7aad5
Show file tree
Hide file tree
Showing 20 changed files with 411 additions and 32 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package com.yello.server.domain.purchase.repository;

import com.yello.server.domain.purchase.entity.Gateway;
import com.yello.server.domain.purchase.entity.ProductType;
import com.yello.server.domain.purchase.entity.Purchase;
import com.yello.server.domain.user.entity.User;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Optional;
import org.springframework.data.jpa.repository.JpaRepository;
Expand All @@ -26,4 +28,10 @@ Optional<Purchase> findTopByUserAndProductTypeOrderByCreatedAtDesc(User user,
+ "and p.productType = 'yello_plus' "
+ "order by p.updatedAt DESC")
Optional<Purchase> findTopByStateAndUser(@Param("user") User user);

@Query("SELECT count(p) FROM Purchase p WHERE p.gateway = ?1 AND p.productType = ?2 AND ?3 <= p.createdAt AND p.createdAt < ?4")
Long countByStartAt(Gateway gateway, ProductType productType, LocalDateTime start, LocalDateTime end);

@Query("SELECT sum(p.price) FROM Purchase p WHERE p.gateway = ?1 AND p.productType = ?2 AND ?3 <= p.createdAt AND p.createdAt < ?4")
Long countPriceByStartAt(Gateway gateway, ProductType productType, LocalDateTime start, LocalDateTime end);
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package com.yello.server.domain.purchase.repository;

import com.yello.server.domain.purchase.entity.Gateway;
import com.yello.server.domain.purchase.entity.ProductType;
import com.yello.server.domain.purchase.entity.Purchase;
import com.yello.server.domain.user.entity.User;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Optional;

Expand All @@ -26,4 +28,8 @@ Optional<Purchase> findTopByUserAndProductTypeOrderByCreatedAtDesc(User user,
void delete(Purchase purchase);

Purchase getTopByStateAndUserId(User user);

Long countByStartAt(Gateway gateway, ProductType productType, LocalDateTime start, LocalDateTime end);

Long countPriceByStartAt(Gateway gateway, ProductType productType, LocalDateTime start, LocalDateTime end);
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@
import static com.yello.server.global.common.ErrorCode.NOT_EQUAL_TRANSACTION_EXCEPTION;
import static com.yello.server.global.common.ErrorCode.NOT_FOUND_USER_SUBSCRIBE_EXCEPTION;

import com.yello.server.domain.purchase.entity.Gateway;
import com.yello.server.domain.purchase.entity.ProductType;
import com.yello.server.domain.purchase.entity.Purchase;
import com.yello.server.domain.purchase.exception.PurchaseNotFoundException;
import com.yello.server.domain.user.entity.User;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Optional;
import lombok.RequiredArgsConstructor;
Expand Down Expand Up @@ -66,4 +68,14 @@ public Purchase getTopByStateAndUserId(User user) {
return purchaseJpaRepository.findTopByStateAndUser(user)
.orElseThrow(() -> new PurchaseNotFoundException(NOT_FOUND_USER_SUBSCRIBE_EXCEPTION));
}

@Override
public Long countByStartAt(Gateway gateway, ProductType productType, LocalDateTime start, LocalDateTime end) {
return purchaseJpaRepository.countByStartAt(gateway, productType, start, end);
}

@Override
public Long countPriceByStartAt(Gateway gateway, ProductType productType, LocalDateTime start, LocalDateTime end) {
return purchaseJpaRepository.countPriceByStartAt(gateway, productType, start, end);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.yello.server.domain.statistics.dto;

import com.yello.server.domain.purchase.entity.Gateway;
import com.yello.server.domain.purchase.entity.ProductType;
import java.util.List;
import lombok.Builder;

@Builder
public record RevenueVO(
List<RevenueItem> revenueItemList
) {

@Builder
public record RevenueItem(
Gateway gateway,
ProductType productType,
Long purchaseCount,
Long totalPrice
) {

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
public record SignUpVO(
Long count,
Long maleCount,
Long femaleCount
Long femaleCount,
Long deleteAtCount
) {

}
16 changes: 16 additions & 0 deletions src/main/java/com/yello/server/domain/statistics/dto/VoteItem.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.yello.server.domain.statistics.dto;

import com.yello.server.domain.group.entity.UserGroupType;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;

@Getter
@Builder
@AllArgsConstructor
public class VoteItem {

UserGroupType groupType;
int yearInfo;
Long totalCount;
}
11 changes: 11 additions & 0 deletions src/main/java/com/yello/server/domain/statistics/dto/VoteVO.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.yello.server.domain.statistics.dto;

import java.util.List;
import lombok.Builder;

@Builder
public record VoteVO(
List<VoteItem> voteItemList
) {

}
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.yello.server.domain.statistics.entity;

import com.yello.server.domain.statistics.dto.RevenueVO;
import com.yello.server.domain.statistics.dto.SignUpVO;
import com.yello.server.domain.statistics.dto.VoteVO;
import com.yello.server.global.common.entity.JsonConverter;
import com.yello.server.global.common.entity.ZonedDateTimeConverter;
import jakarta.persistence.Column;
Expand Down Expand Up @@ -37,22 +39,15 @@ public class StatisticsDaily {
@Convert(converter = ZonedDateTimeConverter.class)
private ZonedDateTime endAt;

/**
* json
*/
@Column
@Column(length = 2047)
@Convert(converter = JsonConverter.class)
private SignUpVO signUp;

/**
* json
*/
@Column
private String revenue;

/**
* json
*/
@Column
private String vote;
@Column(length = 2047)
@Convert(converter = JsonConverter.class)
private RevenueVO revenue;

@Column(length = 2047)
@Convert(converter = JsonConverter.class)
private VoteVO vote;
}
Original file line number Diff line number Diff line change
@@ -1,17 +1,26 @@
package com.yello.server.domain.statistics.service;

import com.yello.server.domain.purchase.entity.Gateway;
import com.yello.server.domain.purchase.entity.ProductType;
import com.yello.server.domain.purchase.repository.PurchaseRepository;
import com.yello.server.domain.statistics.dto.NewStatisticsUserGroupVO;
import com.yello.server.domain.statistics.dto.RevenueVO;
import com.yello.server.domain.statistics.dto.RevenueVO.RevenueItem;
import com.yello.server.domain.statistics.dto.SchoolAttackStatisticsVO;
import com.yello.server.domain.statistics.dto.SignUpVO;
import com.yello.server.domain.statistics.dto.VoteItem;
import com.yello.server.domain.statistics.dto.VoteVO;
import com.yello.server.domain.statistics.dto.response.StatisticsUserGroupSchoolAttackResponse;
import com.yello.server.domain.statistics.entity.StatisticsDaily;
import com.yello.server.domain.statistics.entity.StatisticsUserGroup;
import com.yello.server.domain.statistics.repository.StatisticsRepository;
import com.yello.server.domain.user.entity.Gender;
import com.yello.server.domain.user.repository.UserRepository;
import com.yello.server.domain.vote.repository.VoteRepository;
import com.yello.server.global.common.util.ConstantUtil;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZonedDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import lombok.RequiredArgsConstructor;
Expand All @@ -24,8 +33,10 @@
@Transactional(readOnly = true)
public class StatisticsService {

private final PurchaseRepository purchaseRepository;
private final StatisticsRepository statisticsRepository;
private final UserRepository userRepository;
private final VoteRepository voteRepository;

@Transactional
public void writeUserGroupStatistics() {
Expand Down Expand Up @@ -102,22 +113,53 @@ public StatisticsUserGroupSchoolAttackResponse getSchoolAttackStatisticsLikeGrou
}

@Transactional
public StatisticsDaily writeDailyServiceStatistics() {
final ZonedDateTime now = ZonedDateTime.now(ConstantUtil.GlobalZoneId);
final Long count = userRepository.count();
final Long countFemale = userRepository.countAllByGender(Gender.FEMALE);
final Long countMale = userRepository.countAllByGender(Gender.MALE);
public StatisticsDaily writeDailyServiceStatistics(LocalDate startAt, LocalDate endAt) {
final LocalDateTime startTime = startAt.atStartOfDay();
final LocalDateTime endTime = endAt.atStartOfDay();

final Long count = userRepository.count(startTime, endTime);
final Long countFemale = userRepository.countAllByGender(Gender.FEMALE, startTime, endTime);
final Long countMale = userRepository.countAllByGender(Gender.MALE, startTime, endTime);
final Long countDeletedAt = userRepository.countDeletedAt(startTime, endTime);
final SignUpVO signUpVO = SignUpVO.builder()
.count(count)
.femaleCount(countFemale)
.maleCount(countMale)
.deleteAtCount(countDeletedAt)
.build();

List<RevenueItem> revenueItemList = new ArrayList<>();
for (Gateway gateway : Gateway.values()) {
for (ProductType productType : ProductType.values()) {
final Long purchaseCount = purchaseRepository.countByStartAt(gateway, productType, startTime, endTime);
final Long totalPrice = purchaseRepository.countPriceByStartAt(gateway, productType, startTime,
endTime);

revenueItemList.add(RevenueItem.builder()
.gateway(gateway)
.productType(productType)
.purchaseCount(purchaseCount)
.totalPrice(totalPrice)
.build());
}
}
final RevenueVO revenueVO = RevenueVO.builder()
.revenueItemList(revenueItemList)
.build();

List<VoteItem> voteItemList = voteRepository.countDailyVoteData(startTime, endTime);
final VoteVO voteVO = VoteVO.builder()
.voteItemList(voteItemList)
.build();

final StatisticsDaily saved = statisticsRepository.save(StatisticsDaily.builder()
.startAt(now.minusDays(1))
.endAt(now)
.startAt(startAt.atStartOfDay(ConstantUtil.GlobalZoneId))
.endAt(endAt.atStartOfDay(ConstantUtil.GlobalZoneId))
.signUp(signUpVO)
.revenue(revenueVO)
.vote(voteVO)
.build());

return statisticsRepository.getById(saved.getId());
}
}
Original file line number Diff line number Diff line change
@@ -1,16 +1,21 @@
package com.yello.server.domain.statistics.task;

import com.yello.server.domain.statistics.entity.StatisticsDaily;
import com.yello.server.domain.statistics.service.StatisticsService;
import com.yello.server.global.common.util.ConstantUtil;
import com.yello.server.infrastructure.slack.dto.response.SlackChannel;
import com.yello.server.infrastructure.slack.factory.SlackWebhookMessageFactory;
import com.yello.server.infrastructure.slack.service.SlackService;
import java.io.IOException;
import java.time.ZonedDateTime;
import lombok.RequiredArgsConstructor;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RestController;

@Component
@RequiredArgsConstructor
@RestController
public class StatisticsTask {

private final SlackWebhookMessageFactory messageFactory;
Expand All @@ -22,9 +27,21 @@ public void writeUserGroupStatistics() {
statisticsService.writeUserGroupStatistics();
}

@Scheduled(cron = "*/10 * * * * *", zone = ConstantUtil.GlobalZoneIdLabel)
@Scheduled(cron = "1 0 0 * * *", zone = ConstantUtil.GlobalZoneIdLabel)
public void calculateDailyStatistics() throws IOException {
// final StatisticsDaily statisticsDaily = statisticsService.writeDailyServiceStatistics();
// slackService.send(SlackChannel.DATA_DAILY, messageFactory.generateDataDailyPayload(statisticsDaily));
final ZonedDateTime now = ZonedDateTime.now(ConstantUtil.GlobalZoneId);
final StatisticsDaily statisticsDaily = statisticsService.writeDailyServiceStatistics(
now.minusDays(1).toLocalDate(), now.toLocalDate());

slackService.send(SlackChannel.DATA_DAILY, messageFactory.generateDataDailyPayload(statisticsDaily));
}

// @GetMapping("/statistics")
// public void kk(@RequestParam("date") String date) throws IOException {
// LocalDate now = LocalDate.parse(date, DateTimeFormatter.ISO_DATE);
// final StatisticsDaily statisticsDaily = statisticsService.writeDailyServiceStatistics(
// now.minusDays(1), now);
//
// slackService.send(SlackChannel.DATA_DAILY, messageFactory.generateDataDailyPayload(statisticsDaily));
// }
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.yello.server.domain.user.entity.Gender;
import com.yello.server.domain.user.entity.User;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Optional;
import org.springframework.data.domain.Page;
Expand Down Expand Up @@ -113,12 +114,21 @@ List<User> findAllByOtherGroupContainingYelloId(@Param("groupName") String group
"where u.deviceToken = :deviceToken")
Optional<User> findByDeviceTokenNotFiltered(@Param("deviceToken") String deviceToken);

@Query("SELECT count(u) FROM User u WHERE ?1 <= u.createdAt AND u.createdAt < ?2")
Long count(LocalDateTime startCreatedAt, LocalDateTime endCreatedAt);

@Query("SELECT count(u) FROM User u WHERE ?1 <= u.deletedAt AND u.deletedAt < ?2")
Long countDeletedAt(LocalDateTime startDeletedAt, LocalDateTime endDeletedAt);

Long countAllByYelloIdContaining(String yelloId);

Long countAllByNameContaining(String name);

Long countAllByGender(Gender gender);

@Query("SELECT count(u) FROM User u WHERE u.gender = ?1 AND ?2 <= u.createdAt AND u.createdAt < ?3")
Long countAllByGender(Gender gender, LocalDateTime startCreatedAt, LocalDateTime endCreatedAt);

@Query("select u from User u "
+ "where LOWER(u.yelloId) like LOWER(CONCAT('%', :yelloId, '%'))")
Page<User> findAllByYelloIdContaining(Pageable pageable, @Param("yelloId") String yelloId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.yello.server.domain.user.entity.Gender;
import com.yello.server.domain.user.entity.User;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Optional;
import org.springframework.data.domain.Page;
Expand Down Expand Up @@ -66,12 +67,18 @@ List<User> findAllByGroupNameContainingAndFriendListNotContaining(String keyword

Long count();

Long count(LocalDateTime startCreatedAt, LocalDateTime endCreatedAt);

Long countDeletedAt(LocalDateTime startDeletedAt, LocalDateTime endDeletedAt);

Long countAllByYelloIdContaining(String yelloId);

Long countAllByNameContaining(String name);

Long countAllByGender(Gender gender);

Long countAllByGender(Gender gender, LocalDateTime startCreatedAt, LocalDateTime endCreatedAt);

Page<User> findAll(Pageable pageable);

Page<User> findAllByYelloIdContaining(Pageable pageable, String yelloId);
Expand Down
Loading

0 comments on commit dc7aad5

Please sign in to comment.