Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ADM-727:[backend][docs]feat: Merge two API into one #897

Merged
merged 7 commits into from
Jan 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions backend/src/main/java/heartbeat/config/WebConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import heartbeat.controller.board.dto.request.BoardType;
import heartbeat.controller.report.dto.request.DataType;
import heartbeat.controller.report.dto.request.ReportType;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.convert.converter.Converter;
import org.springframework.format.FormatterRegistry;
Expand All @@ -25,6 +26,13 @@ public DataType convert(String source) {
return DataType.fromValue(source);
}
});

registry.addConverter(new Converter<String, ReportType>() {
@Override
public ReportType convert(String type) {
return ReportType.fromValue(type);
}
});
}

}
Original file line number Diff line number Diff line change
@@ -1,28 +1,25 @@
package heartbeat.controller.report;

import heartbeat.controller.report.dto.request.DataType;
import heartbeat.controller.report.dto.request.ReportType;
import heartbeat.controller.report.dto.request.GenerateReportRequest;
import heartbeat.controller.report.dto.request.ExportCSVRequest;
import heartbeat.controller.report.dto.request.GenerateBoardReportRequest;
import heartbeat.controller.report.dto.request.GenerateDoraReportRequest;
import heartbeat.controller.report.dto.response.CallbackResponse;
import heartbeat.controller.report.dto.response.ReportResponse;
import heartbeat.exception.BaseException;
import heartbeat.handler.AsyncExceptionHandler;
import heartbeat.service.report.GenerateReporterService;
import heartbeat.util.IdUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.InputStreamResource;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.PostMapping;

import java.util.concurrent.CompletableFuture;

Expand All @@ -35,8 +32,6 @@ public class GenerateReportController {

private final GenerateReporterService generateReporterService;

private final AsyncExceptionHandler asyncExceptionHandler;

@Value("${callback.interval}")
private Integer interval;

Expand All @@ -62,62 +57,15 @@ public ResponseEntity<ReportResponse> generateReport(@PathVariable String report
return ResponseEntity.status(HttpStatus.OK).body(reportResponse);
}

@PostMapping("/board")
public ResponseEntity<CallbackResponse> generateBoardReport(@RequestBody GenerateBoardReportRequest request) {
log.info(
"Start to generate board report, _metrics: {}, _considerHoliday: {}, _startTime: {}, _endTime: {}, _boardReportId: {}",
request.getMetrics(), request.getConsiderHoliday(), request.getStartTime(), request.getEndTime(),
IdUtil.getBoardReportId(request.getCsvTimeStamp()));
generateReporterService.initializeMetricsDataReadyInHandler(request.getCsvTimeStamp(), request.getMetrics());
CompletableFuture.runAsync(() -> {
try {
ReportResponse reportResponse = generateReporterService
.generateReporter(request.convertToReportRequest());
generateReporterService.saveReporterInHandler(reportResponse,
IdUtil.getBoardReportId(request.getCsvTimeStamp()));
generateReporterService.updateMetricsDataReadyInHandler(request.getCsvTimeStamp(),
request.getMetrics());
log.info(
"Successfully generate board report, _metrics: {}, _considerHoliday: {}, _startTime: {}, _endTime: {}, _boardReportId: {}",
request.getMetrics(), request.getConsiderHoliday(), request.getStartTime(),
request.getEndTime(), IdUtil.getBoardReportId(request.getCsvTimeStamp()));

}
catch (BaseException e) {
asyncExceptionHandler.put(IdUtil.getBoardReportId(request.getCsvTimeStamp()), e);
}
});

String callbackUrl = "/reports/" + request.getCsvTimeStamp();
return ResponseEntity.status(HttpStatus.ACCEPTED)
.body(CallbackResponse.builder().callbackUrl(callbackUrl).interval(interval).build());
}

@PostMapping("/dora")
public ResponseEntity<CallbackResponse> generateDoraReport(@RequestBody GenerateDoraReportRequest request) {
log.info(
"Start to generate dora report, _metrics: {}, _considerHoliday: {}, _startTime: {}, _endTime: {}, _doraReportId: {}",
request.getMetrics(), request.getConsiderHoliday(), request.getStartTime(), request.getEndTime(),
IdUtil.getDoraReportId(request.getCsvTimeStamp()));
generateReporterService.initializeMetricsDataReadyInHandler(request.getCsvTimeStamp(), request.getMetrics());
@PostMapping("{reportType}")
public ResponseEntity<CallbackResponse> generateReport(@PathVariable ReportType reportType,
@RequestBody GenerateReportRequest request) {
CompletableFuture.runAsync(() -> {
try {
ReportResponse reportResponse = generateReporterService
.generateReporter(request.convertToReportRequest());
generateReporterService.saveReporterInHandler(reportResponse,
IdUtil.getDoraReportId(request.getCsvTimeStamp()));
generateReporterService.updateMetricsDataReadyInHandler(request.getCsvTimeStamp(),
request.getMetrics());
log.info(
"Successfully generate dora report, _metrics: {}, _considerHoliday: {}, _startTime: {}, _endTime: {}, _doraReportId: {}",
request.getMetrics(), request.getConsiderHoliday(), request.getStartTime(),
request.getEndTime(), IdUtil.getDoraReportId(request.getCsvTimeStamp()));
}
catch (BaseException e) {
asyncExceptionHandler.put(IdUtil.getDoraReportId(request.getCsvTimeStamp()), e);
switch (reportType) {
case BOARD -> generateReporterService.generateBoardReport(request);
case DORA -> generateReporterService.generateDoraReport(request);
}
});

String callbackUrl = "/reports/" + request.getCsvTimeStamp();
return ResponseEntity.status(HttpStatus.ACCEPTED)
.body(CallbackResponse.builder().callbackUrl(callbackUrl).interval(interval).build());
Expand Down

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package heartbeat.controller.report.dto.request;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.google.gson.Gson;
import heartbeat.util.MetricsUtil;
import jakarta.validation.constraints.NotBlank;
import lombok.AllArgsConstructor;
import lombok.Builder;
Expand Down Expand Up @@ -35,4 +37,23 @@ public class GenerateReportRequest {
@NotBlank
private String csvTimeStamp;

public GenerateReportRequest convertToPipelineRequest(GenerateReportRequest request) {
List<String> pipelineMetrics = MetricsUtil
.getPipelineMetrics(request.getMetrics().stream().map(String::toLowerCase).toList());
Gson gson = new Gson();
GenerateReportRequest result = gson.fromJson(gson.toJson(request), GenerateReportRequest.class);

result.setMetrics(pipelineMetrics);
return result;
}

public GenerateReportRequest convertToSourceControlRequest(GenerateReportRequest request) {
List<String> codebaseMetrics = MetricsUtil
.getCodeBaseMetrics(request.getMetrics().stream().map(String::toLowerCase).toList());
Gson gson = new Gson();
GenerateReportRequest result = gson.fromJson(gson.toJson(request), GenerateReportRequest.class);
result.setMetrics(codebaseMetrics);
return result;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package heartbeat.controller.report.dto.request;

public enum ReportType {

BOARD, DORA;

public static ReportType fromValue(String type) {
return switch (type) {
case "board" -> BOARD;
case "dora" -> DORA;
default -> throw new IllegalArgumentException("ReportType not found!");
};
}

}
Loading
Loading