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-898: [frontend] fix: fix flag card logic #109

Closed
wants to merge 11 commits into from
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,19 @@ public class LeadTime {

private long jobFinishTime;

private long jobStartTime;

@Nullable
private Long noPRCommitTime;

@Nullable
private Long firstCommitTime;

private long pipelineCreateTime;

@Nullable
private Boolean isRevert;

@Nullable
private Long prLeadTime;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@ public class LeadTimeInfo {

private String jobFinishTime;

private String jobStartTime;

private String noPRCommitTime;

private String firstCommitTime;

@Nullable
private String prLeadTime;

Expand All @@ -33,33 +39,34 @@ public class LeadTimeInfo {
@Nullable
private String totalTime;

public LeadTimeInfo(LeadTime leadTime) {
if (leadTime != null) {
if (leadTime.getFirstCommitTimeInPr() != null) {
this.firstCommitTimeInPr = TimeUtil
.convertToISOFormat(String.valueOf(leadTime.getFirstCommitTimeInPr()));
}

if (leadTime.getPrCreatedTime() != null) {
this.prCreatedTime = TimeUtil.convertToISOFormat(String.valueOf(leadTime.getPrCreatedTime()));
}

if (leadTime.getPrMergedTime() != null) {
this.prMergedTime = TimeUtil.convertToISOFormat(String.valueOf(leadTime.getPrMergedTime()));
}

this.jobFinishTime = TimeUtil.convertToISOFormat(String.valueOf(leadTime.getJobFinishTime()));
private Boolean isRevert;

this.pipelineLeadTime = TimeUtil.msToHMS(leadTime.getPipelineLeadTime());

if (leadTime.getPrLeadTime() != null) {
this.prLeadTime = TimeUtil.msToHMS(leadTime.getPrLeadTime());
}
public LeadTimeInfo(LeadTime leadTime) {
if (leadTime == null) {
return;
}
this.firstCommitTimeInPr = convertToISOFormat(leadTime.getFirstCommitTimeInPr());
this.prCreatedTime = convertToISOFormat(leadTime.getPrCreatedTime());
this.prMergedTime = convertToISOFormat(leadTime.getPrMergedTime());
this.jobFinishTime = convertToISOFormat(leadTime.getJobFinishTime());
this.jobStartTime = convertToISOFormat(leadTime.getJobStartTime());
this.firstCommitTime = convertToISOFormat(leadTime.getFirstCommitTime());
this.noPRCommitTime = convertToISOFormat(leadTime.getNoPRCommitTime());

this.pipelineLeadTime = TimeUtil.msToHMS(leadTime.getPipelineLeadTime());
this.isRevert = leadTime.getIsRevert();

if (leadTime.getPrLeadTime() != null) {
this.prLeadTime = TimeUtil.msToHMS(leadTime.getPrLeadTime());
}

if (leadTime.getTotalTime() != 0) {
this.totalTime = TimeUtil.msToHMS(leadTime.getTotalTime());
}
if (leadTime.getTotalTime() != 0) {
this.totalTime = TimeUtil.msToHMS(leadTime.getTotalTime());
}
}

private String convertToISOFormat(Long time) {
return time != null ? TimeUtil.convertToISOFormat(String.valueOf(time)) : null;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -844,7 +844,8 @@ private CycleTimeInfoDTO getCycleTime(CardHistoryResponseDTO cardHistoryResponse
keyFlagged);
List<CycleTimeInfo> cycleTimeInfos = boardUtil.getCycleTimeInfos(statusChangedArray, realDoneStatus,
treatFlagCardAsBlock);
List<CycleTimeInfo> originCycleTimeInfos = boardUtil.getOriginCycleTimeInfos(statusChangedArray);
List<CycleTimeInfo> originCycleTimeInfos = boardUtil.getOriginCycleTimeInfos(statusChangedArray,
treatFlagCardAsBlock);

return CycleTimeInfoDTO.builder()
.cycleTimeInfos(cycleTimeInfos)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,6 @@
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
Expand Down Expand Up @@ -101,9 +99,10 @@ public void convertPipelineDataToCSV(List<PipelineCSVInfo> leadTimeData, String
File file = new File(fileName);
try (CSVWriter csvWriter = new CSVWriter(new FileWriter(file))) {
String[] headers = { "Organization", "Pipeline Name", "Pipeline Step", "Valid", "Build Number",
"Code Committer", "Pipeline Creator", "First Code Committed Time In PR", "Code Committed Time",
"PR Created Time", "PR Merged Time", "Deployment Completed Time", "Total Lead Time (HH:mm:ss)",
"PR Lead Time (HH:mm:ss)", "Pipeline Lead Time (HH:mm:ss)", "Status", "Branch" };
"Code Committer", "Pipeline Creator", "First Code Committed Time In PR", "PR Created Time",
"PR Merged Time", "No PR Committed Time", "Job Start Time", "Pipeline Start Time",
"Pipeline Finish Time", "Total Lead Time (HH:mm:ss)", "PR Lead Time (HH:mm:ss)",
"Pipeline Lead Time (HH:mm:ss)", "Status", "Branch", "Revert" };

csvWriter.writeNext(headers);

Expand All @@ -120,23 +119,21 @@ public void convertPipelineDataToCSV(List<PipelineCSVInfo> leadTimeData, String
}

private String[] getRowData(PipelineCSVInfo csvInfo) {
String committerName = null;
String commitDate = null;
if (csvInfo.getCommitInfo() != null) {
committerName = csvInfo.getCommitInfo().getCommit().getAuthor().getName();
commitDate = csvInfo.getCommitInfo().getCommit().getAuthor().getDate();
}

String creatorName = null;
if (csvInfo.getBuildInfo().getCreator() != null && csvInfo.getBuildInfo().getCreator().getName() != null) {
creatorName = csvInfo.getBuildInfo().getCreator().getName();
}
String committerName = null;
if (csvInfo.getBuildInfo().getAuthor() != null && csvInfo.getBuildInfo().getAuthor().getName() != null) {
committerName = String.valueOf(csvInfo.getBuildInfo().getAuthor().getName());
}

String organization = csvInfo.getOrganizationName();
String pipelineName = csvInfo.getPipeLineName();
String stepName = csvInfo.getStepName();
String valid = String.valueOf(csvInfo.getValid()).toLowerCase();
String buildNumber = String.valueOf(csvInfo.getBuildInfo().getNumber());

String state = csvInfo.getPiplineStatus().equals(CANCELED_STATUS) ? CANCELED_STATUS
: csvInfo.getDeployInfo().getState();
String branch = csvInfo.getBuildInfo().getBranch();
Expand All @@ -145,14 +142,18 @@ private String[] getRowData(PipelineCSVInfo csvInfo) {
String firstCommitTimeInPr = leadTimeInfo.getFirstCommitTimeInPr();
String prCreatedTime = leadTimeInfo.getPrCreatedTime();
String prMergedTime = leadTimeInfo.getPrMergedTime();
String jobFinishTime = csvInfo.getDeployInfo().getJobFinishTime();
String noPRCommitTime = leadTimeInfo.getNoPRCommitTime();
String jobStartTime = leadTimeInfo.getJobStartTime();
String pipelineStartTime = leadTimeInfo.getFirstCommitTime();
String pipelineFinishTime = csvInfo.getDeployInfo().getJobFinishTime();
String totalTime = leadTimeInfo.getTotalTime();
String prLeadTime = leadTimeInfo.getPrLeadTime();
String pipelineLeadTime = leadTimeInfo.getPipelineLeadTime();
String isRevert = leadTimeInfo.getIsRevert() == null ? "" : String.valueOf(leadTimeInfo.getIsRevert());

return new String[] { organization, pipelineName, stepName, valid, buildNumber, committerName, creatorName,
firstCommitTimeInPr, commitDate, prCreatedTime, prMergedTime, jobFinishTime, totalTime, prLeadTime,
pipelineLeadTime, state, branch };
firstCommitTimeInPr, prCreatedTime, prMergedTime, noPRCommitTime, jobStartTime, pipelineStartTime,
pipelineFinishTime, totalTime, prLeadTime, pipelineLeadTime, state, branch, isRevert };
}

public InputStreamResource getDataFromCSV(ReportType reportDataType, long csvTimeStamp) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package heartbeat.service.report;

import heartbeat.client.dto.codebase.github.Author;
import heartbeat.client.dto.codebase.github.CommitInfo;
import heartbeat.client.dto.codebase.github.LeadTime;
import heartbeat.client.dto.codebase.github.PipelineLeadTime;
Expand Down Expand Up @@ -56,7 +57,7 @@ public FetchedData.BuildKiteData fetchBuildKiteInfo(GenerateReportRequest reques
String endTime = request.getEndTime();
FetchedData.BuildKiteData result = new FetchedData.BuildKiteData();

request.getBuildKiteSetting().getDeploymentEnvList().stream().forEach(deploymentEnvironment -> {
request.getBuildKiteSetting().getDeploymentEnvList().forEach(deploymentEnvironment -> {
List<BuildKiteBuildInfo> buildKiteBuildInfo = getBuildKiteBuildInfo(startTime, endTime,
deploymentEnvironment, request.getBuildKiteSetting().getToken(),
request.getBuildKiteSetting().getPipelineCrews());
Expand Down Expand Up @@ -97,12 +98,15 @@ private PipelineCSVInfo getPipelineCSVInfo(CodebaseSetting codebaseSetting, Stri
BuildKiteBuildInfo buildInfo, List<String> pipelineSteps) {
DeployInfo deployInfo = buildKiteService.mapToDeployInfo(buildInfo, pipelineSteps, REQUIRED_STATES, startTime,
endTime);
CommitInfo commitInfo = null;
if (Objects.nonNull(codebaseSetting) && StringUtils.hasLength(codebaseSetting.getToken())
&& Objects.nonNull(deployInfo.getCommitId())) {
commitInfo = gitHubService.fetchCommitInfo(deployInfo.getCommitId(),
&& Objects.nonNull(deployInfo.getCommitId()) && Objects.isNull(buildInfo.getAuthor())) {
CommitInfo commitInfo = gitHubService.fetchCommitInfo(deployInfo.getCommitId(),
GithubUtil.getGithubUrlFullName(deploymentEnvironment.getRepository()), codebaseSetting.getToken());
Author author = commitInfo.getCommit().getAuthor();
buildInfo
.setAuthor(BuildKiteBuildInfo.Author.builder().name(author.getName()).email(author.getEmail()).build());
}

return PipelineCSVInfo.builder()
.organizationName(deploymentEnvironment.getOrgName())
.pipeLineName(deploymentEnvironment.getName())
Expand All @@ -111,7 +115,6 @@ private PipelineCSVInfo getPipelineCSVInfo(CodebaseSetting codebaseSetting, Stri
.piplineStatus(buildInfo.getState())
.buildInfo(buildInfo)
.deployInfo(deployInfo)
.commitInfo(commitInfo)
.leadTimeInfo(new LeadTimeInfo(filterLeadTime(buildKiteData, deploymentEnvironment, deployInfo)))
.build();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -205,9 +205,11 @@ private LeadTime parseNoMergeLeadTime(DeployInfo deployInfo, PipelineInfoOfRepos
deployInfo.getCommitId(), e.getMessage());
}

Long noPRCommitTime = null;
if (commitInfo.getCommit() != null && commitInfo.getCommit().getCommitter() != null
&& commitInfo.getCommit().getCommitter().getDate() != null) {
firstCommitTime = Instant.parse(commitInfo.getCommit().getCommitter().getDate()).toEpochMilli();
noPRCommitTime = Instant.parse(commitInfo.getCommit().getCommitter().getDate()).toEpochMilli();
firstCommitTime = noPRCommitTime;
}
else {
firstCommitTime = jobStartTime;
Expand All @@ -217,19 +219,32 @@ private LeadTime parseNoMergeLeadTime(DeployInfo deployInfo, PipelineInfoOfRepos
.commitId(deployInfo.getCommitId())
.pipelineCreateTime(pipelineCreateTime)
.jobFinishTime(jobFinishTime)
.jobStartTime(jobStartTime)
.noPRCommitTime(noPRCommitTime)
.firstCommitTime(firstCommitTime)
.pipelineLeadTime(jobFinishTime - firstCommitTime)
.totalTime(jobFinishTime - firstCommitTime)
.prLeadTime(prLeadTime)
.isRevert(isRevert(commitInfo))
.build();
}

private Boolean isRevert(CommitInfo commitInfo) {
Boolean isRevert = null;
if (commitInfo.getCommit() != null && commitInfo.getCommit().getMessage() != null) {
isRevert = commitInfo.getCommit().getMessage().toLowerCase().startsWith("revert");
}
return isRevert;
}

public LeadTime mapLeadTimeWithInfo(PullRequestInfo pullRequestInfo, DeployInfo deployInfo, CommitInfo commitInfo) {
if (pullRequestInfo.getMergedAt() == null) {
return null;
}
long prCreatedTime = Instant.parse(pullRequestInfo.getCreatedAt()).toEpochMilli();
long prMergedTime = Instant.parse(pullRequestInfo.getMergedAt()).toEpochMilli();
long jobFinishTime = Instant.parse(deployInfo.getJobFinishTime()).toEpochMilli();
long jobStartTime = Instant.parse(deployInfo.getJobStartTime()).toEpochMilli();
long pipelineCreateTime = Instant.parse(deployInfo.getPipelineCreateTime()).toEpochMilli();
long firstCommitTimeInPr;
if (commitInfo.getCommit() != null && commitInfo.getCommit().getCommitter() != null
Expand All @@ -243,16 +258,12 @@ public LeadTime mapLeadTimeWithInfo(PullRequestInfo pullRequestInfo, DeployInfo
long pipelineLeadTime = jobFinishTime - prMergedTime;
long prLeadTime;
long totalTime;
if (commitInfo.getCommit().getMessage().toLowerCase().startsWith("revert")) {
Boolean isRevert = isRevert(commitInfo);
if (Boolean.TRUE.equals(isRevert) || isNoFirstCommitTimeInPr(firstCommitTimeInPr)) {
prLeadTime = 0;
}
else {
if (firstCommitTimeInPr > 0) {
prLeadTime = prMergedTime - firstCommitTimeInPr;
}
else {
prLeadTime = prMergedTime - prCreatedTime;
}
prLeadTime = prMergedTime - firstCommitTimeInPr;
}
totalTime = prLeadTime + pipelineLeadTime;

Expand All @@ -265,10 +276,17 @@ public LeadTime mapLeadTimeWithInfo(PullRequestInfo pullRequestInfo, DeployInfo
.prCreatedTime(prCreatedTime)
.commitId(deployInfo.getCommitId())
.jobFinishTime(jobFinishTime)
.jobStartTime(jobStartTime)
.firstCommitTime(prMergedTime)
.pipelineCreateTime(pipelineCreateTime)
.isRevert(isRevert)
.build();
}

private static boolean isNoFirstCommitTimeInPr(long firstCommitTimeInPr) {
return firstCommitTimeInPr == 0;
}

public CommitInfo fetchCommitInfo(String commitId, String repositoryId, String token) {
try {
String realToken = BEARER_TITLE + token;
Expand Down
22 changes: 12 additions & 10 deletions backend/src/main/java/heartbeat/util/BoardUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,12 @@ public class BoardUtil {

private final WorkDay workDay;

public List<CycleTimeInfo> getOriginCycleTimeInfos(List<StatusChangedItem> statusChangedArray) {
public List<CycleTimeInfo> getOriginCycleTimeInfos(List<StatusChangedItem> statusChangedArray,
Boolean treatFlagCardAsBlock) {
List<StatusTimeStamp> flagTimeStamp = getFlagTimeStamps(statusChangedArray);
List<StatusTimeStamp> columnTimeStamp = getColumnTimeStamps(statusChangedArray);
List<CycleTimeInfo> originCycleTimeInfos = calculateOriginCycleTime(flagTimeStamp, columnTimeStamp);
List<CycleTimeInfo> originCycleTimeInfos = calculateOriginCycleTime(flagTimeStamp, columnTimeStamp,
treatFlagCardAsBlock);
return getCollectRemovedDuplicates(originCycleTimeInfos);
}

Expand All @@ -50,7 +52,7 @@ public List<CycleTimeInfo> getCycleTimeInfos(List<StatusChangedItem> statusChang
}

private List<CycleTimeInfo> calculateOriginCycleTime(List<StatusTimeStamp> flagTimeStamp,
List<StatusTimeStamp> columnTimeStamp) {
List<StatusTimeStamp> columnTimeStamp, Boolean treatFlagCardAsBlock) {
List<CycleTimeInfo> originCycleTimeInfos = new ArrayList<>();

for (StatusTimeStamp columnTimeStampItem : columnTimeStamp) {
Expand All @@ -62,9 +64,11 @@ private List<CycleTimeInfo> calculateOriginCycleTime(List<StatusTimeStamp> flagT
.build());
}

double totalFlagTimeInDays = calculateTotalFlagCycleTime(flagTimeStamp);
originCycleTimeInfos
.add(CycleTimeInfo.builder().day(totalFlagTimeInDays).column(CardStepsEnum.FLAG.getValue()).build());
if (Boolean.TRUE.equals(treatFlagCardAsBlock)) {
double totalFlagTimeInDays = calculateTotalFlagCycleTime(flagTimeStamp);
originCycleTimeInfos
.add(CycleTimeInfo.builder().day(totalFlagTimeInDays).column(CardStepsEnum.FLAG.getValue()).build());
}

return originCycleTimeInfos;
}
Expand Down Expand Up @@ -101,10 +105,8 @@ private List<CycleTimeInfo> calculateCycleTime(List<String> realDoneStatus, List
}
if (!isBlockColumnExisted(columnTimeStamp) && totalFlagTimeInDays > 0) {
double blockDays = totalFlagTimeInDays - totalFlagAndRealDoneOverlapTime;
cycleTimeInfos.add(CycleTimeInfo.builder()
.day(blockDays)
.column(CardStepsEnum.BLOCK.getValue().toUpperCase())
.build());
cycleTimeInfos.add(
CycleTimeInfo.builder().day(blockDays).column(CardStepsEnum.FLAG.getValue().toUpperCase()).build());
}

return cycleTimeInfos;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,24 @@ public class LeadTimeInfoTest {
@Test
void shouldCreateLeadTimeInfoWithLeadTime() {
LeadTimeInfo info = new LeadTimeInfo(LeadTime.builder()
.prLeadTime(47255635l)
.firstCommitTimeInPr(1672556350000l)
.prCreatedTime(1706067997l)
.prMergedTime(1706067997l)
.totalTime(57255635l)
.prLeadTime(47255635L)
.firstCommitTimeInPr(1672556350000L)
.prCreatedTime(1706067997L)
.prMergedTime(1706067997L)
.firstCommitTime(1706067997L)
.noPRCommitTime(1706067997L)
.totalTime(57255635L)
.isRevert(Boolean.FALSE)
.build());

assertEquals("13:7:35", info.getPrLeadTime());
assertEquals("2023-01-01T06:59:10Z", info.getFirstCommitTimeInPr());
assertEquals("1970-01-20T17:54:27Z", info.getPrMergedTime());
assertEquals("1970-01-20T17:54:27Z", info.getPrCreatedTime());
assertEquals("1970-01-20T17:54:27Z", info.getFirstCommitTime());
assertEquals("1970-01-20T17:54:27Z", info.getNoPRCommitTime());
assertEquals("15:54:15", info.getTotalTime());
assertEquals(Boolean.FALSE, info.getIsRevert());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -1166,7 +1166,7 @@ public void shouldProcessCustomFieldsForCardsWhenCallGetStoryPointsAndCycleTime(
when(jiraFeignClient.getJiraCardHistoryByCount(any(), any(), anyInt(), anyInt(), any()))
.thenReturn(CARD_HISTORY_MULTI_RESPONSE_BUILDER().build());
when(boardUtil.getCycleTimeInfos(any(), any(), any())).thenReturn(CYCLE_TIME_INFO_LIST());
when(boardUtil.getOriginCycleTimeInfos(any())).thenReturn(CYCLE_TIME_INFO_LIST());
when(boardUtil.getOriginCycleTimeInfos(any(), any())).thenReturn(CYCLE_TIME_INFO_LIST());

CardCollection doneCards = jiraService.getStoryPointsAndCycleTimeAndReworkInfoForDoneCards(
storyPointsAndCycleTimeRequest, jiraBoardSetting.getBoardColumns(), List.of("Zhang San"), "");
Expand Down
Loading
Loading