Skip to content

Commit c84017f

Browse files
committed
test : 커밋 스케줄러 테스트
1 parent 8d9bdbc commit c84017f

File tree

10 files changed

+164
-3
lines changed

10 files changed

+164
-3
lines changed

src/main/java/cmf/commitField/CommitFieldApplication.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,13 @@
33
import org.springframework.boot.SpringApplication;
44
import org.springframework.boot.autoconfigure.SpringBootApplication;
55
import org.springframework.data.jpa.repository.config.EnableJpaAuditing;
6+
import org.springframework.scheduling.annotation.EnableScheduling;
67

78
@SpringBootApplication
89
@EnableJpaAuditing
10+
// 스케쥴링 활성화
11+
// 테스트시에만 주석 풀기
12+
@EnableScheduling
913
public class CommitFieldApplication {
1014

1115
public static void main(String[] args) {

src/main/java/cmf/commitField/domain/commit/scheduler/CommitScheduler.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,17 @@ public class CommitScheduler {
2323

2424
@Scheduled(fixedRate = 60000) // 1분마다 실행
2525
public void updateUserCommits() {
26+
log.info("🔍 updateUserCommits 실행중");
2627
List<User> activeUsers = userRepository.findAll(); // 💫 변경 필요, 차후 active 상태인 user만 찾게끔 변경해야 함.
2728

29+
log.info("🔍 Active User Count: {}", activeUsers.size());
30+
2831
for (User user : activeUsers) {
2932
Integer cachedCount = commitCacheService.getCachedCommitCount(user.getUsername());
3033
int newCommitCount = githubService.getUserCommitCount(user.getUsername());
3134

35+
log.info("🔍 User: {}, Commit Count: {}", user.getUsername(), newCommitCount);
36+
3237
if (cachedCount == null || cachedCount != newCommitCount) { // 변화가 있을 때만 처리
3338
commitCacheService.updateCachedCommitCount(user.getUsername(), newCommitCount);
3439
redpandaProducer.sendCommitUpdate(user.getUsername(), newCommitCount);
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package cmf.commitField.domain.commit.sinceCommit.entity;
2+
3+
import cmf.commitField.global.jpa.BaseEntity;
4+
import jakarta.persistence.*;
5+
import lombok.AllArgsConstructor;
6+
import lombok.Getter;
7+
import lombok.NoArgsConstructor;
8+
9+
import java.time.LocalDate;
10+
11+
@Entity
12+
@Table(name = "commit_history")
13+
@Getter
14+
@NoArgsConstructor
15+
@AllArgsConstructor
16+
public class CommitHistory extends BaseEntity {
17+
18+
@Column(nullable = false)
19+
private String username; // GitHub 사용자명
20+
21+
@Column(nullable = false)
22+
private int streak; // 연속 커밋 수
23+
24+
@Column(nullable = false)
25+
private LocalDate commitDate; // 커밋한 날짜
26+
}
27+
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package cmf.commitField.domain.commit.sinceCommit.repositoty;
2+
3+
import cmf.commitField.domain.commit.sinceCommit.entity.CommitHistory;
4+
import org.springframework.data.jpa.repository.JpaRepository;
5+
import org.springframework.stereotype.Repository;
6+
7+
import java.util.Optional;
8+
9+
@Repository
10+
public interface CommitHistoryRepository extends JpaRepository<CommitHistory, Long> {
11+
// 특정 유저의 최신 커밋 기록 조회
12+
Optional<CommitHistory> findTopByUsernameOrderByCommitDateDesc(String username);
13+
}

src/main/java/cmf/commitField/domain/commit/sinceCommit/service/CommitCacheService.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,10 @@ public class CommitCacheService {
1414
private final StringRedisTemplate redisTemplate;
1515

1616
public Integer getCachedCommitCount(String username) {
17-
String key = "commit:" + username;
18-
String value = redisTemplate.opsForValue().get(key);
19-
return value != null ? Integer.parseInt(value) : null;
17+
log.info("Redis Template: {}", redisTemplate);
18+
String key = "commit:" + username; // Redis 키 생성 (ex: commit:hongildong)
19+
String value = redisTemplate.opsForValue().get(key); // Redis에서 값 가져오기
20+
return value != null ? Integer.parseInt(value) : null; // 값이 있으면 정수 변환, 없으면 null 반환
2021
}
2122

2223
public void updateCachedCommitCount(String username, int count) {

src/main/java/cmf/commitField/domain/noti/noti/entity/Noti.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
package cmf.commitField.domain.noti.noti.entity;
22

3+
import cmf.commitField.domain.user.entity.User;
34
import cmf.commitField.global.jpa.BaseEntity;
45
import jakarta.persistence.Entity;
6+
import jakarta.persistence.ManyToOne;
57
import lombok.AllArgsConstructor;
68
import lombok.Getter;
79
import lombok.NoArgsConstructor;
@@ -17,4 +19,13 @@
1719
@Getter
1820
@Setter
1921
public class Noti extends BaseEntity {
22+
@ManyToOne
23+
private User actor;
24+
@ManyToOne
25+
private User receiver;
26+
private String relTypeCode;
27+
private long relId;
28+
private String typeCode;
29+
private String type2Code;
30+
private boolean read;
2031
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package cmf.commitField.domain.noti.noti.eventListener;
2+
3+
import cmf.commitField.domain.noti.noti.service.NotiService;
4+
import lombok.RequiredArgsConstructor;
5+
import org.springframework.stereotype.Component;
6+
7+
@Component
8+
@RequiredArgsConstructor
9+
public class NotiEventListener {
10+
private final NotiService notiService;
11+
12+
// public void listenPost(PostCreatedEvent event){
13+
// notiService.postCreated(event.getPost());
14+
// }
15+
//
16+
// public void consume(ChatMessageDto message){
17+
// System.out.println("Consumed message: " + message);
18+
// }
19+
//
20+
// public void consumeChatRoom1DLT(byte[] in){
21+
// String message = new String(in);
22+
// System.out.println("Failed message: " + message);
23+
// }
24+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package cmf.commitField.domain.noti.noti.repository;
2+
3+
import cmf.commitField.domain.noti.noti.entity.Noti;
4+
import org.springframework.data.jpa.repository.JpaRepository;
5+
import org.springframework.stereotype.Repository;
6+
7+
@Repository
8+
public interface NotiRepository extends JpaRepository<Noti, Long> {
9+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package cmf.commitField.domain.noti.noti.service;
2+
3+
import cmf.commitField.domain.noti.noti.repository.NotiRepository;
4+
import lombok.RequiredArgsConstructor;
5+
import lombok.extern.slf4j.Slf4j;
6+
import org.springframework.stereotype.Service;
7+
import org.springframework.transaction.annotation.Transactional;
8+
9+
@Service
10+
@RequiredArgsConstructor
11+
@Transactional(readOnly = true)
12+
@Slf4j
13+
public class NotiService {
14+
private final NotiRepository notiRepository;
15+
16+
public void sendCommitStreakNotification(String username, int streakCount) {
17+
log.info("🎉 {}님의 연속 커밋이 {}일로 증가했습니다!", username, streakCount);
18+
// 알림을 DB 저장 또는 웹소켓 / 이메일 / 푸시 알림 전송 가능
19+
}
20+
21+
// public CommitAnalysisResponseDto getCommitAnalysis(String owner, String repo, String username, LocalDateTime since, LocalDateTime until) {
22+
// List<SinceCommitResponseDto> commits = getSinceCommits(owner, repo, since, until);
23+
// StreakResult streakResult = calculateStreaks(commits);
24+
//
25+
// // 연속 커밋 수 Redis 업데이트 및 알림
26+
// streakService.updateStreak(username, streakResult.currentStreak, streakResult.maxStreak);
27+
//
28+
// return new CommitAnalysisResponseDto(commits, streakResult.currentStreak, streakResult.maxStreak);
29+
// }
30+
31+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package cmf.commitField.domain.noti.streak.service;
2+
3+
import cmf.commitField.domain.noti.noti.service.NotiService;
4+
import lombok.RequiredArgsConstructor;
5+
import org.springframework.data.redis.core.RedisTemplate;
6+
import org.springframework.stereotype.Service;
7+
8+
@Service
9+
@RequiredArgsConstructor
10+
public class StreakService {
11+
private final RedisTemplate<String, String> redisTemplate;
12+
13+
private final NotiService notiService;
14+
15+
public void updateStreak(String username, int newCurrentStreak, int newMaxStreak) {
16+
String currentStreakKey = "user:" + username + ":current_streak";
17+
String maxStreakKey = "user:" + username + ":max_streak";
18+
19+
int prevCurrentStreak = getStreak(currentStreakKey);
20+
int prevMaxStreak = getStreak(maxStreakKey);
21+
22+
// 연속 커밋이 증가했으면 Redis 업데이트 및 알림 발송
23+
if (newCurrentStreak > prevCurrentStreak) {
24+
// redis 업데이트
25+
redisTemplate.opsForValue().set(currentStreakKey, String.valueOf(newCurrentStreak));
26+
27+
// 알림 발송
28+
// notiService.sendCommitStreakNotification(username, newCurrentStreak);
29+
}
30+
}
31+
32+
private int getStreak(String key) {
33+
String value = redisTemplate.opsForValue().get(key);
34+
return (value != null) ? Integer.parseInt(value) : 0;
35+
}
36+
}

0 commit comments

Comments
 (0)