forked from thoughtworks/HeartBeat
-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[backend] Refactor report export with CSV (#945)
* [backend] Refactor report export with CSA * [backend] Fix deny dot star check * ADM-739 refactor[backend] * ADM-739 refactor[backend] * ADM-739 add test[backend] --------- Co-authored-by: Jianxun.Ma <jianxun.ma@rea-group.com> Co-authored-by: guzhongren <guzhongren@live.cn>
- Loading branch information
1 parent
5fe0abb
commit bcca032
Showing
13 changed files
with
236 additions
and
139 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
15 changes: 15 additions & 0 deletions
15
backend/src/main/java/heartbeat/controller/report/dto/request/MetricType.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 heartbeat.controller.report.dto.request; | ||
|
||
public enum MetricType { | ||
|
||
BOARD, DORA; | ||
|
||
public static MetricType fromValue(String type) { | ||
return switch (type) { | ||
case "board" -> BOARD; | ||
case "dora" -> DORA; | ||
default -> throw new IllegalArgumentException("ReportType not found!"); | ||
}; | ||
} | ||
|
||
} |
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
28 changes: 28 additions & 0 deletions
28
backend/src/main/java/heartbeat/service/report/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,28 @@ | ||
package heartbeat.service.report; | ||
|
||
import heartbeat.controller.report.dto.request.ReportType; | ||
import heartbeat.exception.NotFoundException; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.core.io.InputStreamResource; | ||
import org.springframework.stereotype.Service; | ||
|
||
import static heartbeat.service.report.scheduler.DeleteExpireCSVScheduler.EXPORT_CSV_VALIDITY_TIME; | ||
|
||
@Service | ||
@RequiredArgsConstructor | ||
public class ReportService { | ||
|
||
private final CSVFileGenerator csvFileGenerator; | ||
|
||
public InputStreamResource exportCsv(ReportType reportType, long csvTimestamp) { | ||
if (isExpiredTimeStamp(csvTimestamp)) { | ||
throw new NotFoundException("Failed to fetch CSV data due to CSV not found"); | ||
} | ||
return csvFileGenerator.getDataFromCSV(reportType, csvTimestamp); | ||
} | ||
|
||
private boolean isExpiredTimeStamp(long timeStamp) { | ||
return timeStamp < System.currentTimeMillis() - EXPORT_CSV_VALIDITY_TIME; | ||
} | ||
|
||
} |
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
25 changes: 25 additions & 0 deletions
25
backend/src/test/java/heartbeat/controller/report/dto/request/MetricTypeTest.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,25 @@ | ||
package heartbeat.controller.report.dto.request; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
import static org.assertj.core.api.Assertions.assertThatThrownBy; | ||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
||
class MetricTypeTest { | ||
|
||
@Test | ||
public void shouldConvertValueToType() { | ||
MetricType boardType = MetricType.fromValue("board"); | ||
MetricType doraType = MetricType.fromValue("dora"); | ||
|
||
assertEquals(boardType, MetricType.BOARD); | ||
assertEquals(doraType, MetricType.DORA); | ||
} | ||
|
||
@Test | ||
public void shouldThrowExceptionWhenDateTypeNotSupported() { | ||
assertThatThrownBy(() -> MetricType.fromValue("unknown")).isInstanceOf(IllegalArgumentException.class) | ||
.hasMessageContaining("ReportType not found!"); | ||
} | ||
|
||
} |
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.