Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

issue20 #23

Merged
merged 1 commit into from
Jul 29, 2024
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
1 change: 1 addition & 0 deletions mitube-app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ dependencies {
implementation 'org.springdoc:springdoc-openapi-starter-webmvc-ui:2.3.0'
implementation 'net.nurigo:sdk:4.3.0' // sms 서비스
implementation 'org.springframework.boot:spring-boot-starter-data-redis'
implementation 'org.springframework.boot:spring-boot-starter-actuator'
runtimeOnly 'com.h2database:h2'
runtimeOnly 'com.mysql:mysql-connector-j'
compileOnly 'org.projectlombok:lombok'
Expand Down
2 changes: 2 additions & 0 deletions mitube-app/src/main/java/com/misim/MitubeAppApplication.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableScheduling;

@SpringBootApplication
@EnableScheduling
public class MitubeAppApplication {

public static void main(String[] args) {
Expand Down
33 changes: 33 additions & 0 deletions mitube-app/src/main/java/com/misim/entity/ViewIncreaseRequest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.misim.entity;

import com.misim.util.TimeUtil;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.Table;
import java.time.LocalDateTime;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;

@Entity
@Getter
@Table(name = "view_increase_requests")
@NoArgsConstructor
public class ViewIncreaseRequest {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

private Long videoId;

private LocalDateTime requestDateTime;

@Builder
public ViewIncreaseRequest(Long videoId) {
this.videoId = videoId;
this.requestDateTime = TimeUtil.getNow();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.misim.repository;

import com.misim.entity.ViewIncreaseRequest;
import java.time.LocalDateTime;
import java.util.List;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

@Repository
public interface ViewIncreaseRequestRepository extends JpaRepository<ViewIncreaseRequest, Long> {

@Query("SELECT v.videoId, COUNT(*) as requestCount FROM ViewIncreaseRequest v where v.requestDateTime > :oneWeekAgo GROUP BY v.videoId ORDER BY requestCount DESC LIMIT 10")
List<Object[]> findTopIncreaseAmountVideoIds(@Param("oneWeekAgo") LocalDateTime oneWeekAgo);

void deleteByRequestDateTimeBefore(LocalDateTime oneWeekAgo);
}
7 changes: 7 additions & 0 deletions mitube-app/src/main/java/com/misim/service/AsyncService.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package com.misim.service;

import com.misim.entity.Video;
import com.misim.entity.ViewIncreaseRequest;
import com.misim.entity.WatchingInfo;
import com.misim.repository.VideoRepository;
import com.misim.repository.ViewIncreaseRequestRepository;
import com.misim.repository.WatchingInfoRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.scheduling.annotation.Async;
Expand All @@ -14,12 +16,17 @@ public class AsyncService {

private final VideoRepository videoRepository;
private final WatchingInfoRepository watchingInfoRepository;
private final ViewIncreaseRequestRepository viewIncreaseRequestRepository;

@Async
public void startWatchingVideo(Video video, WatchingInfo watchingInfo) {
videoRepository.save(video);
if (watchingInfo.getId() == null) {
watchingInfoRepository.save(watchingInfo);
}
viewIncreaseRequestRepository.save(ViewIncreaseRequest.builder()
.videoId(video.getId())
.build()
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public HomeResponse getHome(Long userId) {

return HomeResponse.builder()
.categoryList(VideoCategory.getCategoryList())
.hotVideoList(null)//videoService.getHotVideos())
.hotVideoList(videoService.getHotVideos())
.newVideoList(videoService.getNewVideos())
.watchingVideoList(videoService.getWatchingVideos(userId))
.subscribingChannelNewVideoList(videoService.getSubscribingChannelNewVideos(userId))
Expand Down
19 changes: 9 additions & 10 deletions mitube-app/src/main/java/com/misim/service/VideoService.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ public class VideoService {
private final SubscriptionRepository subscriptionRepository;
private final AsyncService asyncService;
private final ReactionService reactionService;
private final ViewIncreaseRequestService viewIncreaseRequestService;

public String uploadVideos(MultipartFile file) {

Expand Down Expand Up @@ -189,16 +190,14 @@ public List<VideoResponse> getNewVideos() {
return VideoResponse.convertVideos(videos);
}

// public List<VideoResponse> getHotVideos() {
//
// Set<View> viewSet = viewRepository.findHotTen();
//
// List<Long> videoIds = viewSet.stream().map(View::getVideoId).toList();
//
// List<Video> videos = videoRepository.findAllById(videoIds);
//
// return VideoResponse.convertVideos(videos);
// }
public List<VideoResponse> getHotVideos() {

List<Long> videoIds = viewIncreaseRequestService.getTopIncreasesForLastWeek();

List<Video> videos = videoRepository.findAllById(videoIds);

return VideoResponse.convertVideos(videos);
}

public List<VideoResponse> getWatchingVideos(Long userId) {

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.misim.service;

import com.misim.repository.ViewIncreaseRequestRepository;
import com.misim.util.TimeUtil;
import java.time.LocalDateTime;
import java.util.List;
import lombok.RequiredArgsConstructor;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@RequiredArgsConstructor
public class ViewIncreaseRequestService {

private final ViewIncreaseRequestRepository viewIncreaseRequestRepository;

public List<Long> getTopIncreasesForLastWeek() {
LocalDateTime oneWeekAgo = TimeUtil.getNow().minusDays(7);
List<Object[]> result = viewIncreaseRequestRepository.findTopIncreaseAmountVideoIds(oneWeekAgo);
return result.stream()
.map(row -> ((Number) row[0]).longValue())
.toList();
}

@Scheduled(fixedRate = 3600000) // 1시간 간격으로 실행
@Transactional
public void cleanOldRecords() {
LocalDateTime oneWeekAgo = TimeUtil.getNow().minusDays(7).plusMinutes(1);
viewIncreaseRequestRepository.deleteByRequestDateTimeBefore(oneWeekAgo);
}
}
Loading