-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
74d41f7
commit 5da7b09
Showing
31 changed files
with
217 additions
and
588 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,8 @@ spring: | |
profiles: | ||
include: | ||
- core | ||
application: | ||
name: techpick-api | ||
|
||
springdoc: | ||
swagger-ui: | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 33 additions & 0 deletions
33
backend/techpick-ranking/src/main/java/techpick/ranking/LinkViewCount.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package techpick.ranking; | ||
|
||
import java.time.LocalDate; | ||
|
||
import org.springframework.data.mongodb.core.mapping.Document; | ||
|
||
import jakarta.persistence.Id; | ||
import lombok.Getter; | ||
|
||
@Document(collection = "link_view_count") | ||
@Getter | ||
public class LinkViewCount { | ||
|
||
@Id | ||
private String id; | ||
|
||
private final LocalDate date; | ||
|
||
private final String url; | ||
|
||
private Long count; | ||
|
||
public LinkViewCount(LocalDate date, String url, Long count) { | ||
this.date = date; | ||
this.url = url; | ||
this.count = count; | ||
} | ||
|
||
public LinkViewCount incrementCount() { | ||
++this.count; | ||
return this; | ||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
backend/techpick-ranking/src/main/java/techpick/ranking/LinkViewCountRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package techpick.ranking; | ||
|
||
import java.time.LocalDate; | ||
import java.util.List; | ||
import java.util.Optional; | ||
|
||
import org.springframework.data.domain.Limit; | ||
import org.springframework.data.domain.Sort; | ||
import org.springframework.data.mongodb.repository.MongoRepository; | ||
import org.springframework.stereotype.Repository; | ||
|
||
@Repository | ||
public interface LinkViewCountRepository extends MongoRepository<LinkViewCount, String> { | ||
|
||
Optional<LinkViewCount> findLinkViewCountByDateAndUrl(LocalDate date, String url); | ||
|
||
List<LinkViewCount> findAllByDateOrderByCountDesc(LocalDate date); | ||
|
||
List<LinkViewCount> findByDate(LocalDate date, Sort sort, Limit limit); | ||
|
||
List<LinkViewCount> findByDateBetweenOrderByCountDesc( | ||
LocalDate startDate, | ||
LocalDate endDate, | ||
Limit limit | ||
); | ||
} |
64 changes: 64 additions & 0 deletions
64
backend/techpick-ranking/src/main/java/techpick/ranking/LinkViewRankingService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package techpick.ranking; | ||
|
||
import java.time.LocalDate; | ||
import java.time.Month; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
import org.springframework.amqp.rabbit.annotation.RabbitListener; | ||
import org.springframework.data.domain.Limit; | ||
import org.springframework.data.domain.Pageable; | ||
import org.springframework.data.domain.Sort; | ||
import org.springframework.stereotype.Service; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import techpick.core.rabbitmq.Event; | ||
import techpick.core.rabbitmq.RabbitmqConfig; | ||
|
||
@Slf4j | ||
@Service | ||
@RequiredArgsConstructor | ||
public class LinkViewRankingService { | ||
|
||
private final LinkViewCountRepository linkViewCountRepository; | ||
|
||
@RabbitListener(queues = {RabbitmqConfig.QUEUE.Q1}) | ||
public void readViewEvent(Event event) { | ||
var date = event.getOccuredOn().toLocalDate(); | ||
var url = event.getUrl(); | ||
var linkViewCount = linkViewCountRepository | ||
.findLinkViewCountByDateAndUrl(date, url) | ||
.orElseGet(() -> new LinkViewCount(date, url, 0L)) | ||
.incrementCount(); | ||
linkViewCountRepository.save(linkViewCount); | ||
} | ||
|
||
// DAY-RANK : GET TOP N (= count) | ||
public List<LinkViewCount> getTopViewsByDay(int maxCount) { | ||
var currentDay = LocalDate.now(); | ||
return linkViewCountRepository.findByDate(currentDay, Sort.by(Sort.Direction.DESC, "count"), | ||
Limit.of(maxCount)); | ||
} | ||
|
||
// WEEK-RANK : GET TOP N (= count) | ||
// 오늘 기준 지난 일주일 | ||
public List<LinkViewCount> getTopViewsByPast7Days(int maxCount) { | ||
var currentDay = LocalDate.now(); | ||
return linkViewCountRepository.findByDateBetweenOrderByCountDesc( | ||
currentDay.minusDays(7), | ||
currentDay.plusDays(1), // TODO: GT를 GTE로 바꾸는 방법이 뭘까? | ||
Limit.of(maxCount) | ||
); | ||
} | ||
|
||
// MONT:-RANK : GET TOP N (= count) | ||
public List<LinkViewCount> getTopViewByPast30Days(int maxCount) { | ||
var currentMonth = LocalDate.now(); | ||
return linkViewCountRepository.findByDateBetweenOrderByCountDesc( | ||
currentMonth.minusDays(30), | ||
currentMonth.plusDays(1), // TODO: GT를 GTE로 바꾸는 방법이 뭘까? | ||
Limit.of(maxCount) | ||
); | ||
} | ||
} |
47 changes: 47 additions & 0 deletions
47
backend/techpick-ranking/src/main/java/techpick/ranking/RankingController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package techpick.ranking; | ||
|
||
import java.time.LocalDate; | ||
import java.util.List; | ||
|
||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RequestParam; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
import io.swagger.v3.oas.annotations.tags.Tag; | ||
import lombok.RequiredArgsConstructor; | ||
|
||
@RestController | ||
@RequiredArgsConstructor | ||
@RequestMapping("/ranking") | ||
@Tag(name = "Rank API", description = "랭킹 API") | ||
public class RankingController { | ||
|
||
// TODO: 시간 나라별 기준 이런거 검토 필요합니다. | ||
private final LinkViewRankingService linkViewRankingService; | ||
|
||
@GetMapping("/daily") | ||
public ResponseEntity<?> getDailyRanking( | ||
@RequestParam int count | ||
) { | ||
var result = linkViewRankingService.getTopViewsByDay(count); | ||
return ResponseEntity.ok(result); | ||
} | ||
|
||
@GetMapping("/weekly") | ||
public ResponseEntity<?> getWeeklyRanking( | ||
@RequestParam int count | ||
) { | ||
var result = linkViewRankingService.getTopViewsByPast7Days(count); | ||
return ResponseEntity.ok(result); | ||
} | ||
|
||
@GetMapping("/monthly") | ||
public ResponseEntity<?> getMonthlyRanking( | ||
@RequestParam int count | ||
) { | ||
var result = linkViewRankingService.getTopViewByPast30Days(count); | ||
return ResponseEntity.ok(result); | ||
} | ||
} |
46 changes: 0 additions & 46 deletions
46
backend/techpick-ranking/src/main/java/techpick/ranking/application/ProducerController.java
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.