Skip to content

Commit

Permalink
ADM-727:[backend][docs]feat: Merge two API into one (#897)
Browse files Browse the repository at this point in the history
  • Loading branch information
Lei010 authored Jan 10, 2024
2 parents fb3d989 + 3a66895 commit 75383d2
Show file tree
Hide file tree
Showing 14 changed files with 402 additions and 331 deletions.
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

0 comments on commit 75383d2

Please sign in to comment.