Skip to content

Commit

Permalink
Merge pull request #134 from 4m9d/feat/#133/develop-rating-update-log…
Browse files Browse the repository at this point in the history
…ic(swm-388)

[SWM-388] Feat : develop rating update logic
  • Loading branch information
Son-GyeongSik authored Nov 3, 2023
2 parents 64b36dd + bad70dc commit c850323
Show file tree
Hide file tree
Showing 17 changed files with 109 additions and 34 deletions.
4 changes: 3 additions & 1 deletion src/main/java/com/m9d/sroom/ai/AiScheduler.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,16 @@
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import static com.m9d.sroom.ai.constant.AiConstant.PERIOD_OF_GPT_REQUEST;

@Component
@Slf4j
@RequiredArgsConstructor
public class AiScheduler {

private final AiService aiService;

@Scheduled(cron = "0/5 * * * * *")
@Scheduled(cron = PERIOD_OF_GPT_REQUEST)
public void requestToGptRepeat() {
aiService.saveResultFromFastApi();
}
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/com/m9d/sroom/ai/constant/AiConstant.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.m9d.sroom.ai.constant;

public class AiConstant {
public static final String PERIOD_OF_GPT_REQUEST = "0/5 * * * * *";
}
3 changes: 3 additions & 0 deletions src/main/java/com/m9d/sroom/common/entity/PlaylistEntity.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ public class PlaylistEntity {

private Timestamp updatedAt;

private Float averageRating;

public static RowMapper<PlaylistEntity> getRowMapper() {
return (rs, rowNum) -> PlaylistEntity.builder()
.playlistId(rs.getLong("playlist_id"))
Expand All @@ -56,6 +58,7 @@ public static RowMapper<PlaylistEntity> getRowMapper() {
.title(rs.getString("title"))
.publishedAt(rs.getTimestamp("published_at"))
.videoCount(rs.getInt("video_count"))
.averageRating(rs.getFloat("average_rating"))
.build();
}

Expand Down
3 changes: 3 additions & 0 deletions src/main/java/com/m9d/sroom/common/entity/VideoEntity.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ public class VideoEntity {

private Integer materialStatus;

private Float averageRating;

public static RowMapper<VideoEntity> getRowMapper() {
return (rs, rowNum) -> VideoEntity.builder()
.videoId(rs.getLong("video_id"))
Expand All @@ -76,6 +78,7 @@ public static RowMapper<VideoEntity> getRowMapper() {
.publishedAt(rs.getTimestamp("published_at"))
.membership(rs.getBoolean("membership"))
.materialStatus(rs.getInt("material_status"))
.averageRating(rs.getFloat("average_rating"))
.build();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ public PlaylistEntity updateById(Long playlistId, PlaylistEntity playlist) {
playlist.getTitle(),
playlist.getPublishedAt(),
playlist.getVideoCount(),
playlist.getAverageRating(),
playlistId);
return getById(playlistId);

Expand Down Expand Up @@ -94,4 +95,9 @@ public List<PlaylistEntity> getViewCountOrderByChannel(String channel, int limit
public List<PlaylistEntity> getLatestOrderByChannel(String channel, int limit) {
return jdbcTemplate.query(PlaylistRepositorySql.GET_LATEST_ORDER_BY_CHANNEL, PlaylistEntity.getRowMapper(), channel, limit);
}

@Override
public Integer updateRating() {
return jdbcTemplate.update(PlaylistRepositorySql.UPDATE_RATING);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,6 @@ public interface PlaylistRepository {
List<PlaylistEntity> getViewCountOrderByChannel(String channel, int limit);

List<PlaylistEntity> getLatestOrderByChannel(String channel, int limit);

Integer updateRating();
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,22 @@ class PlaylistRepositorySql {
public static final String GET_BY_ID = """
SELECT
playlist_id, playlist_code, channel, thumbnail, accumulated_rating, review_count, is_available, description,
duration, updated_at, title, published_at, video_count
duration, updated_at, title, published_at, video_count, average_rating
FROM PLAYLIST
WHERE playlist_id = ?
"""
public static final String GET_BY_CODE = """
SELECT
playlist_id, playlist_code, channel, thumbnail, accumulated_rating, review_count, is_available, description,
duration, updated_at, title, published_at, video_count
duration, updated_at, title, published_at, video_count, average_rating
FROM PLAYLIST
WHERE playlist_code = ?
"""
public static final String UPDATE_BY_ID = """
UPDATE
PLAYLIST SET
channel = ?, thumbnail = ?, accumulated_rating = ?, review_count = ?, is_available = ?, description = ?,
duration = ?, updated_at = ?, title = ?, published_at = ?, video_count = ?
duration = ?, updated_at = ?, title = ?, published_at = ?, video_count = ?, average_rating = ?
WHERE playlist_id = ?
"""
public static final String GET_CODE_SET_BY_MEMBER_ID_QUERY = """
Expand All @@ -41,20 +41,16 @@ class PlaylistRepositorySql {
public static final String GET_TOP_RATED_ORDER = """
SELECT
playlist_id, playlist_code, channel, thumbnail, accumulated_rating, review_count, is_available, description,
duration, updated_at, title, published_at, video_count,
CASE
WHEN review_count = 0 THEN 0
ELSE CAST(accumulated_rating AS DOUBLE) / review_count
END AS rating
duration, updated_at, title, published_at, video_count, average_rating
FROM PLAYLIST
ORDER BY rating DESC
ORDER BY average_rating DESC
LIMIT ?
"""

public static final String GET_RANDOM_BY_CHANNEL = """
SELECT
playlist_id, playlist_code, channel, thumbnail, accumulated_rating, review_count, is_available, description,
duration, updated_at, title, published_at, video_count
duration, updated_at, title, published_at, video_count, average_rating
FROM PLAYLIST
WHERE channel = ?
ORDER BY RAND()
Expand All @@ -64,7 +60,7 @@ class PlaylistRepositorySql {
public static final String GET_VIEW_COUNT_ORDER_BY_CHANNEL = """
SELECT
p.playlist_id, p.playlist_code, p.channel, p.thumbnail, p.accumulated_rating, p.review_count, p.is_available,
p.description, p.duration, p.updated_at, p.title, p.published_at, p.video_count
p.description, p.duration, p.updated_at, p.title, p.published_at, p.video_count, p.average_rating
FROM PLAYLIST p
JOIN PLAYLISTVIDEO pv ON p.playlist_id = pv.playlist_id
JOIN VIDEO v ON pv.video_id = v.video_id
Expand All @@ -77,10 +73,16 @@ class PlaylistRepositorySql {
public static final String GET_LATEST_ORDER_BY_CHANNEL = """
SELECT
playlist_id, playlist_code, channel, thumbnail, accumulated_rating, review_count, is_available, description,
duration, updated_at, title, published_at, video_count
duration, updated_at, title, published_at, video_count, average_rating
FROM PLAYLIST
WHERE channel = ?
ORDER BY published_at DESC
LIMIT ?
"""

public static final String UPDATE_RATING = """
UPDATE PLAYLIST
SET average_rating = accumulated_rating / review_count
WHERE review_count > 0;
"""
}
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ public VideoEntity updateById(Long videoId, VideoEntity video) {
video.getPublishedAt(),
video.isMembership(),
video.getMaterialStatus(),
video.getAverageRating(),
video.getVideoId());
return getByCode(video.getVideoCode());
}
Expand Down Expand Up @@ -118,4 +119,9 @@ public List<VideoEntity> getViewCountOrderByChannel(String channel, int limit) {
public List<VideoEntity> getLatestOrderByChannel(String channel, int limit) {
return jdbcTemplate.query(VideoRepositorySql.GET_LATEST_ORDER_BY_CHANNEL, VideoEntity.getRowMapper(), channel, limit);
}

@Override
public Integer updateRating() {
return jdbcTemplate.update(VideoRepositorySql.UPDATE_RATING);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,6 @@ public interface VideoRepository {
List<VideoEntity> getViewCountOrderByChannel(String channel, int limit);

List<VideoEntity> getLatestOrderByChannel(String channel, int limit);

Integer updateRating();
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class VideoRepositorySql {
SELECT
video_id, video_code, duration, channel, thumbnail, accumulated_rating, review_count, summary_id,
is_available, description, chapter_usage, title, language, license, updated_at, view_count, published_at,
membership, material_status
membership, material_status, average_rating
FROM VIDEO
WHERE video_code = ?
"""
Expand All @@ -22,7 +22,7 @@ class VideoRepositorySql {
SELECT
video_id, video_code, duration, channel, thumbnail, accumulated_rating, review_count, summary_id,
is_available, description, chapter_usage, title, language, license, updated_at, view_count, published_at,
membership, material_status
membership, material_status, average_rating
FROM VIDEO
WHERE video_id = ?
"""
Expand All @@ -31,29 +31,25 @@ class VideoRepositorySql {
SELECT
video_id, video_code, duration, channel, thumbnail, accumulated_rating, review_count, summary_id,
is_available, description, chapter_usage, title, language, license, updated_at, view_count, published_at,
membership, material_status,
CASE
WHEN review_count = 0 THEN 0
ELSE CAST(accumulated_rating AS DOUBLE) / review_count
END AS rating
membership, material_status, average_rating
FROM VIDEO
ORDER BY rating DESC
ORDER BY average_rating DESC
LIMIT ?
"""

public static final String UPDATE_BY_ID = """
UPDATE VIDEO
SET duration = ?, channel = ?, thumbnail = ?, accumulated_rating = ?, review_count = ?, summary_id = ?,
is_available = ?, description = ?, chapter_usage = ?, title = ?, language = ?, license = ?, updated_at = ?,
view_count = ?, published_at = ?, membership = ?, material_status = ?
view_count = ?, published_at = ?, membership = ?, material_status = ?, average_rating = ?
WHERE video_id = ?
"""

public static final String GET_LIST_BY_PLAYLIST_ID = """
SELECT
v.video_id, v.video_code, v.duration, v.channel, v.thumbnail, v.accumulated_rating, v.review_count,
v.summary_id, v.is_available, v.description, v.chapter_usage, v.title, v.language, v.license, v.updated_at,
v.view_count, v.published_at, v.membership, v.material_status, pv.video_index
v.view_count, v.published_at, v.membership, v.material_status, pv.video_index, v.average_rating
FROM VIDEO v
JOIN PLAYLISTVIDEO pv
ON v.video_id = pv.video_id
Expand All @@ -70,8 +66,8 @@ class VideoRepositorySql {
public static final String GET_RANDOM_BY_CHANNEL = """
SELECT
video_id, video_code, duration, channel, thumbnail, accumulated_rating, review_count, summary_id,
is_available, description, chapter_usage, title, language, license, updated_at, view_count, published_at,
membership, material_status
is_available, description, chapter_usage, title, language, license, updated_at, view_count, published_at,
membership, material_status, average_rating
FROM VIDEO
WHERE channel = ?
ORDER BY RAND()
Expand All @@ -82,7 +78,7 @@ class VideoRepositorySql {
SELECT
video_id, video_code, duration, channel, thumbnail, accumulated_rating, review_count, summary_id,
is_available, description, chapter_usage, title, language, license, updated_at, view_count, published_at,
membership, material_status
membership, material_status, average_rating
FROM VIDEO
WHERE channel = ?
ORDER BY view_count DESC
Expand All @@ -93,10 +89,16 @@ class VideoRepositorySql {
SELECT
video_id, video_code, duration, channel, thumbnail, accumulated_rating, review_count, summary_id,
is_available, description, chapter_usage, title, language, license, updated_at, view_count, published_at,
membership, material_status
membership, material_status, average_rating
FROM VIDEO
WHERE channel = ?
ORDER BY published_at DESC
LIMIT ?
"""

public static final String UPDATE_RATING = """
UPDATE VIDEO
SET average_rating = accumulated_rating / review_count
WHERE review_count > 0;
"""
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
import java.util.*;
import java.util.stream.Collectors;

import static com.m9d.sroom.recommendation.constant.RecommendationConstant.TOP_RATED_LECTURES_COUNT;

@Slf4j
@Service
public class RecommendationService {
Expand All @@ -40,8 +42,8 @@ public Recommendations getRecommendations(Long memberId) {
List<RecommendLecture> generalRecommendLectureList = new ArrayList<>();
List<RecommendLecture> channelRecommendLectureList = getRecommendsByChannel(memberId);

generalRecommendLectureList.addAll(getRecommendLectures(videoService.getTopRatedVideos(10)));
generalRecommendLectureList.addAll(getRecommendLectures(playlistService.getTopRatedPlaylists(10)));
generalRecommendLectureList.addAll(getRecommendLectures(videoService.getTopRatedVideos(TOP_RATED_LECTURES_COUNT)));
generalRecommendLectureList.addAll(getRecommendLectures(playlistService.getTopRatedPlaylists(TOP_RATED_LECTURES_COUNT)));

Set<String> enrolledLectureSet = lectureService.getEnrolledLectures(memberId);

Expand All @@ -53,7 +55,9 @@ public Recommendations getRecommendations(Long memberId) {
Collections.shuffle(generalRecommendLectureList);
Collections.shuffle(channelRecommendLectureList);
Recommendations recommendations = Recommendations.builder()
.generalRecommendations(generalRecommendLectureList)
.generalRecommendations(generalRecommendLectureList.stream()
.limit(20)
.collect(Collectors.toList()))
.channelRecommendations(channelRecommendLectureList.stream()
.limit(20)
.collect(Collectors.toList()))
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.m9d.sroom.recommendation.constant;

public class RecommendationConstant {
public static final int TOP_RATED_LECTURES_COUNT = 20;
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package com.m9d.sroom.review.controller;
package com.m9d.sroom.review;

import com.m9d.sroom.review.dto.LectureBriefList4Review;
import com.m9d.sroom.review.dto.ReviewSubmitRequest;
import com.m9d.sroom.review.dto.ReviewSubmitResponse;
import com.m9d.sroom.review.service.ReviewService;
import com.m9d.sroom.review.ReviewService;
import com.m9d.sroom.util.JwtUtil;
import com.m9d.sroom.util.annotation.Auth;
import io.swagger.v3.oas.annotations.Operation;
Expand Down
21 changes: 21 additions & 0 deletions src/main/java/com/m9d/sroom/review/ReviewScheduler.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.m9d.sroom.review;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import static com.m9d.sroom.review.constant.ReviewConstant.PERIOD_OF_RATING_UPDATE;

@Component
@Slf4j
@RequiredArgsConstructor
public class ReviewScheduler {

private final ReviewService reviewService;

@Scheduled(cron = PERIOD_OF_RATING_UPDATE)
public void temporalUpdateRating() {
reviewService.updateRating();
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.m9d.sroom.review.service;
package com.m9d.sroom.review;

import com.m9d.sroom.common.entity.*;
import com.m9d.sroom.common.repository.coursevideo.CourseVideoRepository;
Expand Down Expand Up @@ -223,4 +223,11 @@ LectureBrief4Review getVideoCountData(Long lectureId) {
public List<ReviewBrief> getReviewInfo(String videoCode, int offset, int limit) {
return reviewRepository.getBriefListByCode(videoCode, offset, limit);
}

@Transactional
public void updateRating() {
int updateVideoCount = videoRepository.updateRating();
int updatePlaylistCount = playlistRepository.updateRating();
log.info("Update Rating : updated video count = {}, updated playlist count = {}",updateVideoCount, updatePlaylistCount);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.m9d.sroom.review.constant;

public class ReviewConstant {
public static final String PERIOD_OF_RATING_UPDATE = "0 0/5 * * * *";
}
2 changes: 1 addition & 1 deletion src/main/java/com/m9d/sroom/search/SearchService.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import com.m9d.sroom.search.exception.TwoOnlyParamTrueException;
import com.m9d.sroom.search.exception.VideoIndexParamException;
import com.m9d.sroom.playlist.PlaylistService;
import com.m9d.sroom.review.service.ReviewService;
import com.m9d.sroom.review.ReviewService;
import com.m9d.sroom.video.VideoService;
import com.m9d.sroom.youtube.vo.SearchInfo;
import com.m9d.sroom.youtube.vo.SearchItemInfo;
Expand Down

0 comments on commit c850323

Please sign in to comment.