Skip to content

Commit fb45f46

Browse files
authored
Merge pull request #75 from CommitField/feat/#59
Feat: ์‚ฌ์šฉ์ž ์ด๋ฆ„๋งŒ์œผ๋กœ ์‹œ์ฆŒ๋ณ„ ์ „์ฒด ์ปค๋ฐ‹ ์ˆ˜, ์—ฐ์† ์ปค๋ฐ‹ ์ˆ˜, ์ตœ๋Œ€ ์—ฐ์† ์ปค๋ฐ‹ ์ˆ˜ ๋ถˆ๋Ÿฌ์˜ค๊ธฐ
2 parents 13d200d + d3e8104 commit fb45f46

File tree

4 files changed

+187
-9
lines changed

4 files changed

+187
-9
lines changed

โ€Žsrc/main/java/cmf/commitField/domain/commit/totalCommit/controller/TotalCommitController.javaโ€Ž

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77
import org.springframework.web.bind.annotation.PathVariable;
88
import org.springframework.web.bind.annotation.RestController;
99

10+
import java.time.LocalDateTime;
11+
import java.time.temporal.TemporalAdjusters;
12+
1013
@RestController
1114
@RequiredArgsConstructor
1215
public class TotalCommitController {
@@ -16,4 +19,41 @@ public class TotalCommitController {
1619
public TotalCommitResponseDto getTotalCommits(@PathVariable String username) {
1720
return totalCommitService.getTotalCommitCount(username);
1821
}
22+
23+
// ๋ด„ ์‹œ์ฆŒ(3/1 - 5/31)
24+
@GetMapping("/api/commits/{username}/spring")
25+
public TotalCommitResponseDto getSpringSeasonCommits(@PathVariable String username) {
26+
int currentYear = LocalDateTime.now().getYear(); // ํ˜„์žฌ๋Š” ํ…Œ์ŠคํŠธ์šฉ์œผ๋กœ 2024 ๋Œ€์ž…
27+
LocalDateTime since = LocalDateTime.of(2024, 3, 1, 0, 0);
28+
LocalDateTime until = LocalDateTime.of(2024, 5, 31, 23, 59, 59);
29+
return totalCommitService.getSeasonCommits(username, since, until);
30+
}
31+
32+
// ์—ฌ๋ฆ„ ์‹œ์ฆŒ(6/1 - 8/31)
33+
@GetMapping("/api/commits/{username}/summer")
34+
public TotalCommitResponseDto getSummerSeasonCommits(@PathVariable String username) {
35+
int currentYear = LocalDateTime.now().getYear(); // ํ˜„์žฌ๋Š” ํ…Œ์ŠคํŠธ์šฉ์œผ๋กœ 2024 ๋Œ€์ž…
36+
LocalDateTime since = LocalDateTime.of(2024, 6, 1, 0, 0);
37+
LocalDateTime until = LocalDateTime.of(2024, 8, 31, 23, 59, 59);
38+
return totalCommitService.getSeasonCommits(username, since, until);
39+
}
40+
41+
// ๊ฐ€์„ ์‹œ์ฆŒ(9/1 - 11/30)
42+
@GetMapping("/api/commits/{username}/fall")
43+
public TotalCommitResponseDto getFallSeasonCommits(@PathVariable String username) {
44+
int currentYear = LocalDateTime.now().getYear(); // ํ˜„์žฌ๋Š” ํ…Œ์ŠคํŠธ์šฉ์œผ๋กœ 2024 ๋Œ€์ž…
45+
LocalDateTime since = LocalDateTime.of(2024, 9, 1, 0, 0);
46+
LocalDateTime until = LocalDateTime.of(2024, 11, 30, 23, 59, 59);
47+
return totalCommitService.getSeasonCommits(username, since, until);
48+
}
49+
50+
// ๊ฒจ์šธ ์‹œ์ฆŒ(์ด์ „ ๋…„๋„ 12/1 - ๋‹ค์Œ ๋…„๋„ 2/28)
51+
@GetMapping("/api/commits/{username}/winter")
52+
public TotalCommitResponseDto getWinterSeasonCommits(@PathVariable String username) {
53+
int currentYear = LocalDateTime.now().getYear(); // ํ˜„์žฌ๋Š” ํ…Œ์ŠคํŠธ์šฉ์œผ๋กœ 2024 ๋Œ€์ž…
54+
LocalDateTime since = LocalDateTime.of(2024 - 1, 12, 1, 0, 0);
55+
LocalDateTime until = LocalDateTime.of(2024, 2, 1, 23, 59, 59)
56+
.with(TemporalAdjusters.lastDayOfMonth());
57+
return totalCommitService.getSeasonCommits(username, since, until);
58+
}
1959
}

โ€Žsrc/main/java/cmf/commitField/domain/commit/totalCommit/dto/TotalCommitGraphQLResponse.javaโ€Ž

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
import lombok.Getter;
44
import lombok.NoArgsConstructor;
55

6+
import java.util.List;
7+
68
/*
79
์‘๋‹ต๊ตฌ์กฐ ํ† ๋Œ€๋กœ ๊ตฌํ˜„
810
{
@@ -37,7 +39,28 @@ public static class CommitUser {
3739
@Getter
3840
@NoArgsConstructor
3941
public static class ContributionsCollection {
40-
private long totalCommitContributions;
41-
private long restrictedContributionsCount;
42+
private long totalCommitContributions; // ๊ณต๊ฐœ ๋ ˆํฌ ์ปค๋ฐ‹
43+
private long restrictedContributionsCount; // ๋น„๊ณต๊ฐœ ๋ ˆํฌ ์ปค๋ฐ‹
44+
private ContributionCalendar contributionCalendar;
45+
}
46+
47+
@Getter
48+
@NoArgsConstructor
49+
public static class ContributionCalendar {
50+
private int totalContributions;
51+
private List<Week> weeks;
52+
}
53+
54+
@Getter
55+
@NoArgsConstructor
56+
public static class Week {
57+
private List<ContributionDay> contributionDays;
58+
}
59+
60+
@Getter
61+
@NoArgsConstructor
62+
public static class ContributionDay {
63+
private int contributionCount;
64+
private String date;
4265
}
4366
}

โ€Žsrc/main/java/cmf/commitField/domain/commit/totalCommit/dto/TotalCommitResponseDto.javaโ€Ž

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,21 @@
66

77
@Getter
88
@NoArgsConstructor
9-
@AllArgsConstructor
109
public class TotalCommitResponseDto {
1110
private long totalCommitContributions;
1211
private long restrictedContributionsCount;
12+
private int currentStreakDays;
13+
private int maxStreakDays;
14+
15+
public TotalCommitResponseDto(long totalCommitContributions, long restrictedContributionsCount, int currentStreakDays, int maxStreakDays) {
16+
this.totalCommitContributions = totalCommitContributions;
17+
this.restrictedContributionsCount = restrictedContributionsCount;
18+
this.currentStreakDays = currentStreakDays;
19+
this.maxStreakDays = maxStreakDays;
20+
}
21+
22+
public TotalCommitResponseDto(long totalCommitContributions, long restrictedContributionsCount) {
23+
this.totalCommitContributions = totalCommitContributions;
24+
this.restrictedContributionsCount = restrictedContributionsCount;
25+
}
1326
}

โ€Žsrc/main/java/cmf/commitField/domain/commit/totalCommit/service/TotalCommitService.javaโ€Ž

Lines changed: 108 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,12 @@
99
import org.springframework.stereotype.Service;
1010
import org.springframework.web.reactive.function.client.WebClient;
1111

12+
import java.time.LocalDate;
13+
import java.time.LocalDateTime;
14+
import java.time.format.DateTimeFormatter;
15+
import java.util.ArrayList;
16+
import java.util.Collections;
17+
import java.util.List;
1218
import java.util.Map;
1319

1420

@@ -23,14 +29,11 @@ public class TotalCommitService {
2329

2430
private final WebClient webClient = WebClient.builder()
2531
.baseUrl(BASE_URL)
26-
.defaultHeader(HttpHeaders.CONTENT_TYPE , MediaType.APPLICATION_JSON_VALUE)
32+
.defaultHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE)
2733
.build();
2834

35+
// ๊ธฐ์กด ๋ฉ”์„œ๋“œ
2936
public TotalCommitResponseDto getTotalCommitCount(String username) {
30-
// String query = String.format("""
31-
// {"query": "query { user(login: \\\"%s\\\") { contributionsCollection { totalCommitContributions restrictedContributionsCount } } }"}
32-
// """, username);
33-
// GraphQL ์ฟผ๋ฆฌ๋ฅผ Map์œผ๋กœ ๊ตฌ์„ฑ
3437
Map<String, String> requestBody = Map.of(
3538
"query", String.format(
3639
"query { user(login: \"%s\") { contributionsCollection { totalCommitContributions restrictedContributionsCount } } }",
@@ -45,11 +48,110 @@ public TotalCommitResponseDto getTotalCommitCount(String username) {
4548
.bodyToMono(TotalCommitGraphQLResponse.class)
4649
.block();
4750

48-
TotalCommitGraphQLResponse.ContributionsCollection contributions = response.getData().getUser().getContributionsCollection();
51+
TotalCommitGraphQLResponse.ContributionsCollection contributions =
52+
response.getData().getUser().getContributionsCollection();
4953

5054
return new TotalCommitResponseDto(
5155
contributions.getTotalCommitContributions(),
5256
contributions.getRestrictedContributionsCount()
5357
);
5458
}
59+
60+
// ์‹œ์ฆŒ๋ณ„ ์ปค๋ฐ‹ ๋ถ„์„
61+
public TotalCommitResponseDto getSeasonCommits(String username, LocalDateTime since, LocalDateTime until) {
62+
String query = String.format("""
63+
query {
64+
user(login: "%s") {
65+
contributionsCollection(from: "%s", to: "%s") {
66+
totalCommitContributions
67+
restrictedContributionsCount
68+
contributionCalendar {
69+
totalContributions
70+
weeks {
71+
contributionDays {
72+
contributionCount
73+
date
74+
}
75+
}
76+
}
77+
}
78+
}
79+
}
80+
""", username, since.format(DateTimeFormatter.ISO_DATE_TIME), until.format(DateTimeFormatter.ISO_DATE_TIME));
81+
82+
Map<String, String> requestBody = Map.of("query", query);
83+
84+
TotalCommitGraphQLResponse response = webClient.post()
85+
.header("Authorization", "bearer " + PAT)
86+
.bodyValue(requestBody)
87+
.retrieve()
88+
.bodyToMono(TotalCommitGraphQLResponse.class)
89+
.block();
90+
91+
if (response == null || response.getData() == null || response.getData().getUser() == null) {
92+
throw new RuntimeException("Failed to fetch GitHub data");
93+
}
94+
95+
TotalCommitGraphQLResponse.ContributionsCollection contributions =
96+
response.getData().getUser().getContributionsCollection();
97+
98+
List<LocalDate> commitDates = extractCommitDates(contributions.getContributionCalendar());
99+
StreakResult streaks = calculateStreaks(commitDates);
100+
101+
return new TotalCommitResponseDto(
102+
contributions.getTotalCommitContributions(),
103+
contributions.getRestrictedContributionsCount(),
104+
streaks.currentStreak,
105+
streaks.maxStreak
106+
);
107+
}
108+
109+
private List<LocalDate> extractCommitDates(TotalCommitGraphQLResponse.ContributionCalendar calendar) {
110+
List<LocalDate> dates = new ArrayList<>();
111+
calendar.getWeeks().forEach(week ->
112+
week.getContributionDays().forEach(day -> {
113+
if (day.getContributionCount() > 0) {
114+
dates.add(LocalDate.parse(day.getDate()));
115+
}
116+
})
117+
);
118+
return dates;
119+
}
120+
121+
@RequiredArgsConstructor
122+
private static class StreakResult {
123+
final int currentStreak;
124+
final int maxStreak;
125+
}
126+
127+
private StreakResult calculateStreaks(List<LocalDate> commitDates) {
128+
if (commitDates.isEmpty()) {
129+
return new StreakResult(0, 0);
130+
}
131+
132+
Collections.sort(commitDates);
133+
int currentStreak = 0;
134+
int maxStreak = 0;
135+
int tempStreak = 0;
136+
LocalDate previousDate = null;
137+
138+
for (LocalDate date : commitDates) {
139+
if (previousDate == null || date.minusDays(1).equals(previousDate)) {
140+
tempStreak++;
141+
} else {
142+
tempStreak = 1;
143+
}
144+
maxStreak = Math.max(maxStreak, tempStreak);
145+
previousDate = date;
146+
}
147+
148+
// ํ˜„์žฌ ์ŠคํŠธ๋ฆญ ๊ณ„์‚ฐ (๋งˆ์ง€๋ง‰ ์ปค๋ฐ‹์ด ์˜ค๋Š˜ ๋˜๋Š” ์–ด์ œ์ธ ๊ฒฝ์šฐ)
149+
LocalDate today = LocalDate.now();
150+
LocalDate lastCommitDate = commitDates.get(commitDates.size() - 1);
151+
if (lastCommitDate.equals(today) || lastCommitDate.equals(today.minusDays(1))) {
152+
currentStreak = tempStreak;
153+
}
154+
155+
return new StreakResult(currentStreak, maxStreak);
156+
}
55157
}

0 commit comments

Comments
ย (0)