-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #86 from lcj1204/feat/manager/POT-63
[POT_63][Feat] 레포트 조회 API 구현
- Loading branch information
Showing
16 changed files
with
348 additions
and
3 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
14 changes: 14 additions & 0 deletions
14
pothole-core/src/main/java/pothole_solution/core/global/config/AppConfig.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,14 @@ | ||
package pothole_solution.core.global.config; | ||
|
||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import pothole_solution.core.global.util.formatter.LocalDateFormatter; | ||
|
||
@Configuration | ||
public class AppConfig { | ||
@Bean | ||
public LocalDateFormatter localDateFormatter() { | ||
return new LocalDateFormatter(); | ||
} | ||
|
||
} |
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
22 changes: 22 additions & 0 deletions
22
...le-core/src/main/java/pothole_solution/core/global/util/formatter/LocalDateFormatter.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,22 @@ | ||
package pothole_solution.core.global.util.formatter; | ||
|
||
import org.jetbrains.annotations.NotNull; | ||
import org.springframework.format.Formatter; | ||
|
||
import java.time.LocalDate; | ||
import java.time.format.DateTimeFormatter; | ||
import java.util.Locale; | ||
|
||
public class LocalDateFormatter implements Formatter<LocalDate> { | ||
@NotNull | ||
@Override | ||
public LocalDate parse(@NotNull String text, @NotNull Locale locale) { | ||
return LocalDate.parse(text, DateTimeFormatter.ofPattern("yyyy-MM-dd")); | ||
} | ||
|
||
@NotNull | ||
@Override | ||
public String print(@NotNull LocalDate object, @NotNull Locale locale) { | ||
return DateTimeFormatter.ofPattern("yyyy-MM-dd").format(object); | ||
} | ||
} |
53 changes: 53 additions & 0 deletions
53
pothole-core/src/main/java/pothole_solution/core/infra/p6spy/P6SpyFormatter.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,53 @@ | ||
package pothole_solution.core.infra.p6spy; | ||
|
||
import com.p6spy.engine.common.ConnectionInformation; | ||
import com.p6spy.engine.event.JdbcEventListener; | ||
import com.p6spy.engine.spy.P6SpyOptions; | ||
import com.p6spy.engine.spy.appender.MessageFormattingStrategy; | ||
import org.hibernate.engine.jdbc.internal.FormatStyle; | ||
import org.springframework.stereotype.Component; | ||
import org.springframework.util.StringUtils; | ||
|
||
import java.sql.SQLException; | ||
|
||
@Component | ||
public class P6SpyFormatter extends JdbcEventListener implements MessageFormattingStrategy { | ||
|
||
@Override | ||
public void onAfterGetConnection(ConnectionInformation connectionInformation, SQLException e) { | ||
P6SpyOptions.getActiveInstance().setLogMessageFormat(getClass().getName()); | ||
} | ||
|
||
@Override | ||
public String formatMessage(int connectionId, String now, long elapsed, String category, String prepared, String sql, String url) { | ||
StringBuilder sb = new StringBuilder(); | ||
sb.append(category).append(" ").append(elapsed).append("ms"); | ||
if (StringUtils.hasText(sql)) { | ||
// sb.append(highlight(format(sql))); | ||
sb.append(format(sql)); | ||
} | ||
return sb.toString(); | ||
} | ||
|
||
private String format(String sql) { | ||
if (isDDL(sql)) { | ||
return FormatStyle.DDL.getFormatter().format(sql); | ||
} else if (isBasic(sql)) { | ||
return FormatStyle.BASIC.getFormatter().format(sql); | ||
} | ||
return sql; | ||
} | ||
|
||
private String highlight(String sql) { | ||
return FormatStyle.HIGHLIGHT.getFormatter().format(sql); | ||
} | ||
|
||
private boolean isDDL(String sql) { | ||
return sql.startsWith("create") || sql.startsWith("alter") || sql.startsWith("comment"); | ||
} | ||
|
||
private boolean isBasic(String sql) { | ||
return sql.startsWith("select") || sql.startsWith("insert") || sql.startsWith("update") || sql.startsWith("delete"); | ||
} | ||
|
||
} |
12 changes: 12 additions & 0 deletions
12
...pi/src/main/java/pothole_solution/manager/global/converter/ReportPeriodEnumConverter.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,12 @@ | ||
package pothole_solution.manager.global.converter; | ||
|
||
import org.jetbrains.annotations.NotNull; | ||
import org.springframework.core.convert.converter.Converter; | ||
import pothole_solution.manager.report.entity.ReportPeriod; | ||
|
||
public class ReportPeriodEnumConverter implements Converter<String, ReportPeriod> { | ||
@Override | ||
public ReportPeriod convert(@NotNull String period) { | ||
return ReportPeriod.enumOf(period); | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
...le-manager-api/src/main/java/pothole_solution/manager/global/formatter/EnumFormatter.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,15 @@ | ||
package pothole_solution.manager.global.formatter; | ||
|
||
import org.jetbrains.annotations.NotNull; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.format.FormatterRegistry; | ||
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; | ||
import pothole_solution.manager.global.converter.ReportPeriodEnumConverter; | ||
|
||
@Configuration | ||
public class EnumFormatter implements WebMvcConfigurer { | ||
@Override | ||
public void addFormatters(@NotNull FormatterRegistry registry) { | ||
registry.addConverter(new ReportPeriodEnumConverter()); | ||
} | ||
} |
34 changes: 34 additions & 0 deletions
34
...anager-api/src/main/java/pothole_solution/manager/report/controller/ReportController.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,34 @@ | ||
package pothole_solution.manager.report.controller; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.format.annotation.DateTimeFormat; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RequestParam; | ||
import org.springframework.web.bind.annotation.RestController; | ||
import pothole_solution.core.global.util.response.BaseResponse; | ||
import pothole_solution.manager.report.dto.RespPotDngrCntByPeriodDto; | ||
import pothole_solution.manager.report.entity.ReportPeriod; | ||
import pothole_solution.manager.report.service.ReportService; | ||
|
||
import java.time.LocalDate; | ||
import java.util.List; | ||
|
||
@Slf4j | ||
@RestController | ||
@RequiredArgsConstructor | ||
@RequestMapping("/pothole/v1/manager") | ||
public class ReportController { | ||
private final ReportService reportService; | ||
|
||
@GetMapping("/pothole-report") | ||
public BaseResponse<List<RespPotDngrCntByPeriodDto>> getPotDngrCntByPeriod(@RequestParam(value = "startDate") @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate startDate, | ||
@RequestParam(value = "endDate") @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate endDate, | ||
@RequestParam(value = "reportPeriod") ReportPeriod reportPeriod) { | ||
|
||
List<RespPotDngrCntByPeriodDto> periodPotholeCounts = reportService.getPeriodPotholeDangerousCount(startDate, endDate, reportPeriod); | ||
|
||
return new BaseResponse<>(periodPotholeCounts); | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
...ager-api/src/main/java/pothole_solution/manager/report/dto/RespPotDngrCntByPeriodDto.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,15 @@ | ||
package pothole_solution.manager.report.dto; | ||
|
||
import lombok.AllArgsConstructor; | ||
import lombok.Getter; | ||
|
||
@Getter | ||
@AllArgsConstructor | ||
public class RespPotDngrCntByPeriodDto { | ||
String period; | ||
Long dangerous0to20; | ||
Long dangerous20to40; | ||
Long dangerous40to60; | ||
Long dangerous60to80; | ||
Long dangerous80to100; | ||
} |
24 changes: 24 additions & 0 deletions
24
pothole-manager-api/src/main/java/pothole_solution/manager/report/entity/ReportPeriod.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,24 @@ | ||
package pothole_solution.manager.report.entity; | ||
|
||
import lombok.AllArgsConstructor; | ||
import lombok.Getter; | ||
import pothole_solution.core.global.exception.CustomException; | ||
|
||
@Getter | ||
@AllArgsConstructor | ||
public enum ReportPeriod { | ||
MONTHLY("YYYY-MM"), | ||
WEEKLY("YYYY-MM-W"), | ||
DAILY("YYYY-MM-DD"), | ||
AUTO("auto"); | ||
|
||
private final String queryOfPeriod; | ||
|
||
public static ReportPeriod enumOf(String period) { | ||
try { | ||
return ReportPeriod.valueOf(period.toUpperCase()); | ||
} catch (RuntimeException e) { | ||
throw CustomException.MISMATCH_PERIOD; | ||
} | ||
} | ||
} |
57 changes: 57 additions & 0 deletions
57
...pi/src/main/java/pothole_solution/manager/report/repository/ReportQueryDslRepository.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,57 @@ | ||
package pothole_solution.manager.report.repository; | ||
|
||
import com.querydsl.core.types.Projections; | ||
import com.querydsl.core.types.dsl.Expressions; | ||
import com.querydsl.core.types.dsl.NumberExpression; | ||
import com.querydsl.core.types.dsl.StringTemplate; | ||
import com.querydsl.jpa.impl.JPAQueryFactory; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.stereotype.Repository; | ||
import pothole_solution.manager.report.dto.RespPotDngrCntByPeriodDto; | ||
|
||
import java.time.LocalDateTime; | ||
import java.util.List; | ||
|
||
import static pothole_solution.core.domain.pothole.entity.QPothole.pothole; | ||
|
||
@Slf4j | ||
@Repository | ||
@RequiredArgsConstructor | ||
public class ReportQueryDslRepository { | ||
private final JPAQueryFactory jpaQueryFactory; | ||
|
||
public List<RespPotDngrCntByPeriodDto> getPotDngrCntByPeriod(LocalDateTime startDate, LocalDateTime endDate, String queryOfPeriod) { | ||
return jpaQueryFactory | ||
.select( | ||
Projections.constructor(RespPotDngrCntByPeriodDto.class, | ||
convertDateFormat(queryOfPeriod), | ||
countDangerousBetween(1, 20), | ||
countDangerousBetween(21, 40), | ||
countDangerousBetween(41, 60), | ||
countDangerousBetween(61, 80), | ||
countDangerousBetween(81, 100) | ||
) | ||
) | ||
.from(pothole) | ||
.where(pothole.createdAt.between(startDate, endDate)) | ||
.groupBy(convertDateFormat(queryOfPeriod)) | ||
.orderBy(convertDateFormat(queryOfPeriod).asc()) | ||
.fetch(); | ||
} | ||
|
||
private static StringTemplate convertDateFormat(String queryOfPeriod) { | ||
return Expressions.stringTemplate( | ||
"to_char({0},'" + queryOfPeriod + "')" | ||
, pothole.createdAt); | ||
} | ||
|
||
private static NumberExpression<Long> countDangerousBetween(int from, int to) { | ||
return Expressions.cases() | ||
.when(pothole.dangerous.between(from, to)) | ||
.then(1L) | ||
.otherwise(0L) | ||
.sum(); | ||
} | ||
|
||
} |
18 changes: 18 additions & 0 deletions
18
...nager-api/src/main/java/pothole_solution/manager/report/service/AutoPeriodCalculator.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,18 @@ | ||
package pothole_solution.manager.report.service; | ||
|
||
import java.time.LocalDate; | ||
import java.time.temporal.ChronoUnit; | ||
|
||
public class AutoPeriodCalculator { | ||
private static final int CRITERIA_OF_MONTHLY = 3; | ||
private static final int CRITERIA_OF_WEEKLY = 3; | ||
|
||
protected static boolean isMonthly(LocalDate startDate, LocalDate endDate) { | ||
return ChronoUnit.MONTHS.between(startDate, endDate) >= CRITERIA_OF_MONTHLY; | ||
} | ||
|
||
protected static boolean isWeekly(LocalDate startDate, LocalDate endDate) { | ||
return ChronoUnit.MONTHS.between(startDate, endDate) < CRITERIA_OF_MONTHLY | ||
&& ChronoUnit.WEEKS.between(startDate, endDate) >= CRITERIA_OF_WEEKLY; | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
pothole-manager-api/src/main/java/pothole_solution/manager/report/service/ReportService.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,11 @@ | ||
package pothole_solution.manager.report.service; | ||
|
||
import pothole_solution.manager.report.entity.ReportPeriod; | ||
import pothole_solution.manager.report.dto.RespPotDngrCntByPeriodDto; | ||
|
||
import java.time.LocalDate; | ||
import java.util.List; | ||
|
||
public interface ReportService { | ||
List<RespPotDngrCntByPeriodDto> getPeriodPotholeDangerousCount(LocalDate startDate, LocalDate endDate, ReportPeriod period); | ||
} |
Oops, something went wrong.