-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
[#106] ์ ์ฒด, ์ฐ๊ฐ, ์๊ฐ ๋ชจ๋ ์๊ฐ๋ ๋ฐ์ดํฐ ๋ญํน ์กฐํ API ๊ตฌ์ถ
- Loading branch information
Showing
9 changed files
with
238 additions
and
79 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
42 changes: 42 additions & 0 deletions
42
...ava/com/hobak/happinessql/domain/report/application/ReportTimeOfPeriodRankingService.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,42 @@ | ||
package com.hobak.happinessql.domain.report.application; | ||
|
||
import com.hobak.happinessql.domain.record.domain.Record; | ||
import com.hobak.happinessql.domain.record.repository.RecordRepository; | ||
import com.hobak.happinessql.domain.report.dto.TimeOfDayHappinessDto; | ||
import com.hobak.happinessql.domain.user.domain.User; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.stereotype.Service; | ||
|
||
import java.time.LocalDate; | ||
import java.time.LocalDateTime; | ||
import java.util.List; | ||
|
||
@Service | ||
@RequiredArgsConstructor | ||
public class ReportTimeOfPeriodRankingService { | ||
|
||
private final RecordRepository recordRepository; | ||
|
||
public List<TimeOfDayHappinessDto> getAllTimeOfDayRankings(User user) { | ||
List<Record> records = recordRepository.findAllByUser(user); | ||
return TimeOfDayHappinessAnalyzer.getTimeOfDayRankings(records); | ||
} | ||
|
||
public List<TimeOfDayHappinessDto> getYearlyTimeOfDayRankings(User user) { | ||
int currentYear = LocalDate.now().getYear(); | ||
LocalDateTime startOfYear = LocalDateTime.of(currentYear, 1, 1, 0, 0); | ||
LocalDateTime endOfYear = LocalDateTime.of(currentYear, 12, 31, 23, 59, 59); | ||
List<Record> records = recordRepository.findAllByCreatedAtBetweenAndUser(startOfYear, endOfYear, user); | ||
return TimeOfDayHappinessAnalyzer.getTimeOfDayRankings(records); | ||
} | ||
|
||
public List<TimeOfDayHappinessDto> getMonthlyTimeOfDayRankings(User user) { | ||
LocalDate today = LocalDate.now(); | ||
LocalDate startOfMonth = today.withDayOfMonth(1); | ||
LocalDate endOfMonth = today.withDayOfMonth(today.lengthOfMonth()); | ||
LocalDateTime startOfMonthDateTime = startOfMonth.atStartOfDay(); | ||
LocalDateTime endOfMonthDateTime = endOfMonth.atTime(23, 59, 59); | ||
List<Record> records = recordRepository.findAllByCreatedAtBetweenAndUser(startOfMonthDateTime, endOfMonthDateTime, user); | ||
return TimeOfDayHappinessAnalyzer.getTimeOfDayRankings(records); | ||
} | ||
} |
121 changes: 121 additions & 0 deletions
121
...main/java/com/hobak/happinessql/domain/report/application/TimeOfDayHappinessAnalyzer.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,121 @@ | ||
package com.hobak.happinessql.domain.report.application; | ||
|
||
import com.hobak.happinessql.domain.record.domain.Record; | ||
import com.hobak.happinessql.domain.report.converter.ReportConverter; | ||
import com.hobak.happinessql.domain.report.domain.TimeOfDay; | ||
import com.hobak.happinessql.domain.report.dto.TimeOfDayHappinessDto; | ||
|
||
import java.util.*; | ||
import java.util.stream.Collectors; | ||
|
||
public class TimeOfDayHappinessAnalyzer { | ||
|
||
public static TimeOfDay getHappiestTimeOfDay(List<Record> records) { | ||
if (records == null || records.isEmpty()) { | ||
return null; | ||
} | ||
|
||
// ์๊ฐ๋๋ฅผ ๊ธฐ์ค์ผ๋ก Record ๊ทธ๋ฃนํ | ||
Map<TimeOfDay, List<Record>> timeOfDayRecordsMap = groupRecordsByTimeOfDay(records); | ||
|
||
// ์๊ฐ๋๋ณ ํ๊ท ํ๋ณต๋์ ๋น๋ ๊ณ์ฐ | ||
Map<TimeOfDay, Double> timeOfDayAverageHappiness = calculateTimeOfDayAverageHappiness(timeOfDayRecordsMap); | ||
Map<TimeOfDay, Integer> timeOfDayFrequency = calculateTimeOfDayFrequency(timeOfDayRecordsMap); | ||
|
||
// ํ๊ท ํ๋ณต๋๊ฐ ๊ฐ์ฅ ๋์ ์๊ฐ๋ ์ฐพ๊ธฐ | ||
double maxHappiness = Collections.max(timeOfDayAverageHappiness.values()); | ||
|
||
// ํ๊ท ํ๋ณต๋๊ฐ ๊ฐ์ฅ ๋์ ์๊ฐ๋๋ค ์ค ๋น๋๊ฐ ๊ฐ์ฅ ๋์ ์๊ฐ๋ ์ฐพ๊ธฐ | ||
Optional<TimeOfDay> happiestTimeOfDay = findHappiestTimeOfDay(timeOfDayAverageHappiness, timeOfDayFrequency, maxHappiness); | ||
|
||
// ํ๊ท ํ๋ณต๋์ ๋น๋๊ฐ ๊ฐ์ ์๊ฐ๋๊ฐ ์ฌ๋ฌ ๊ฐ์ธ ๊ฒฝ์ฐ, ๋๋ค์ผ๋ก ์ ํ | ||
List<TimeOfDay> happiestTimesOfDay = findTimesOfDayWithMaxHappiness(timeOfDayAverageHappiness, maxHappiness); | ||
|
||
if (happiestTimesOfDay.size() > 1) { | ||
Collections.shuffle(happiestTimesOfDay); | ||
return happiestTimesOfDay.get(0); | ||
} else { | ||
return happiestTimeOfDay.orElse(null); | ||
} | ||
} | ||
|
||
public static List<TimeOfDayHappinessDto> getTimeOfDayRankings(List<Record> records) { | ||
List<TimeOfDayHappinessDto> timeOfDayRankings = new ArrayList<>(); | ||
|
||
// ์๊ฐ๋๋ณ ํ๊ท ํ๋ณต๋์ ๋น๋ ๊ณ์ฐ | ||
Map<TimeOfDay, List<Record>> timeOfDayRecordsMap = groupRecordsByTimeOfDay(records); | ||
Map<TimeOfDay, Double> timeOfDayAverageHappiness = calculateTimeOfDayAverageHappiness(timeOfDayRecordsMap); | ||
Map<TimeOfDay, Integer> timeOfDayFrequency = calculateTimeOfDayFrequency(timeOfDayRecordsMap); | ||
|
||
// ํ๊ท ํ๋ณต๋์ ๋น๋๋ฅผ ๊ธฐ์ค์ผ๋ก ์๊ฐ๋๋ค์ ์ ๋ ฌ | ||
List<TimeOfDay> sortedTimesOfDay = sortTimesOfDay(timeOfDayAverageHappiness, timeOfDayFrequency); | ||
|
||
// ์์ N๊ฐ์ ์๊ฐ๋ ์ ์ | ||
for (int i = 0; i < sortedTimesOfDay.size(); i++) { | ||
TimeOfDay timeOfDay = sortedTimesOfDay.get(i); | ||
TimeOfDayHappinessDto timeOfDayDto = ReportConverter.toTimeOfDayHappinessDto(i + 1, timeOfDay); | ||
timeOfDayRankings.add(timeOfDayDto); | ||
} | ||
|
||
return timeOfDayRankings; | ||
} | ||
|
||
private static Map<TimeOfDay, List<Record>> groupRecordsByTimeOfDay(List<Record> records) { | ||
return records.stream() | ||
.collect(Collectors.groupingBy(record -> TimeOfDay.of(record.getCreatedAt().getHour()))); | ||
} | ||
|
||
private static Map<TimeOfDay, Double> calculateTimeOfDayAverageHappiness(Map<TimeOfDay, List<Record>> timeOfDayRecordsMap) { | ||
Map<TimeOfDay, Double> timeOfDayAverageHappiness = new HashMap<>(); | ||
timeOfDayRecordsMap.forEach((timeOfDay, recordList) -> { | ||
double averageHappiness = recordList.stream() | ||
.mapToInt(Record::getHappiness) | ||
.average() | ||
.orElse(Double.NaN); | ||
timeOfDayAverageHappiness.put(timeOfDay, averageHappiness); | ||
}); | ||
return timeOfDayAverageHappiness; | ||
} | ||
|
||
private static Map<TimeOfDay, Integer> calculateTimeOfDayFrequency(Map<TimeOfDay, List<Record>> timeOfDayRecordsMap) { | ||
Map<TimeOfDay, Integer> timeOfDayFrequency = new HashMap<>(); | ||
timeOfDayRecordsMap.forEach((timeOfDay, recordList) -> { | ||
timeOfDayFrequency.put(timeOfDay, recordList.size()); | ||
}); | ||
return timeOfDayFrequency; | ||
} | ||
|
||
private static Optional<TimeOfDay> findHappiestTimeOfDay(Map<TimeOfDay, Double> timeOfDayAverageHappiness, | ||
Map<TimeOfDay, Integer> timeOfDayFrequency, | ||
double maxHappiness) { | ||
return timeOfDayAverageHappiness.entrySet().stream() | ||
.filter(entry -> entry.getValue() == maxHappiness) | ||
.max(Comparator.comparing(entry -> timeOfDayFrequency.get(entry.getKey()))) | ||
.map(Map.Entry::getKey); | ||
} | ||
|
||
private static List<TimeOfDay> findTimesOfDayWithMaxHappiness(Map<TimeOfDay, Double> timeOfDayAverageHappiness, | ||
double maxHappiness) { | ||
return timeOfDayAverageHappiness.entrySet().stream() | ||
.filter(entry -> entry.getValue() == maxHappiness) | ||
.map(Map.Entry::getKey) | ||
.collect(Collectors.toList()); | ||
} | ||
|
||
private static List<TimeOfDay> sortTimesOfDay(Map<TimeOfDay, Double> timeOfDayAverageHappiness, | ||
Map<TimeOfDay, Integer> timeOfDayFrequency) { | ||
return timeOfDayAverageHappiness.entrySet().stream() | ||
.sorted((entry1, entry2) -> { | ||
int compare = Double.compare(entry2.getValue(), entry1.getValue()); | ||
if (compare == 0) { | ||
compare = Integer.compare(timeOfDayFrequency.get(entry2.getKey()), timeOfDayFrequency.get(entry1.getKey())); | ||
} | ||
if (compare == 0) { | ||
compare = entry1.getKey().name().compareTo(entry2.getKey().name()); | ||
} | ||
return compare; | ||
}) | ||
.map(Map.Entry::getKey) | ||
.collect(Collectors.toList()); | ||
} | ||
} |
52 changes: 0 additions & 52 deletions
52
...ain/java/com/hobak/happinessql/domain/report/application/TimePeriodHappinessAnalyzer.java
This file was deleted.
Oops, something went wrong.
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
Oops, something went wrong.