Skip to content

Commit

Permalink
ADM-999 [frontend][backend]: fix bug (#1597)
Browse files Browse the repository at this point in the history
  • Loading branch information
zhou-yinyuan authored Sep 5, 2024
1 parent c8cc1a2 commit 5f11cba
Show file tree
Hide file tree
Showing 7 changed files with 176 additions and 115 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import heartbeat.client.dto.pipeline.buildkite.BuildKiteBuildInfo;
import heartbeat.client.dto.pipeline.buildkite.PageBuildKitePipelineInfoDTO;
import heartbeat.client.dto.pipeline.buildkite.PageStepsInfoDto;
import heartbeat.exception.InternalServerErrorException;
import lombok.RequiredArgsConstructor;
import lombok.extern.log4j.Log4j2;
import org.springframework.cache.annotation.Cacheable;
Expand Down Expand Up @@ -72,52 +73,73 @@ public PageBuildKitePipelineInfoDTO getPipelineInfoList(String orgSlug, String b

@Cacheable(cacheNames = "pageOrganization", key = "#token+'-'+#page+'-'+#perPage")
public PageOrganizationsInfoDTO getGitHubOrganizations(String token, int page, int perPage) {
log.info("Start to get paginated github organization info, page: {}", page);
ResponseEntity<List<OrganizationsInfoDTO>> allOrganizations = gitHubFeignClient.getAllOrganizations(token,
perPage, page);
log.info("Successfully get paginated github organization info, page: {}", page);

int totalPage = parseTotalPage(allOrganizations.getHeaders().get(BUILD_KITE_LINK_HEADER));

return PageOrganizationsInfoDTO.builder().pageInfo(allOrganizations.getBody()).totalPage(totalPage).build();
try {
log.info("Start to get paginated github organization info, page: {}", page);
ResponseEntity<List<OrganizationsInfoDTO>> allOrganizations = gitHubFeignClient.getAllOrganizations(token,
perPage, page);
log.info("Successfully get paginated github organization info, page: {}", page);
int totalPage = parseTotalPage(allOrganizations.getHeaders().get(BUILD_KITE_LINK_HEADER));
return PageOrganizationsInfoDTO.builder().pageInfo(allOrganizations.getBody()).totalPage(totalPage).build();
}
catch (Exception e) {
log.info("Error to get paginated github organization info, page: {}, exception: {}", page, e);
throw new InternalServerErrorException(
String.format("Error to get paginated github organization info, page: %s, exception: %s", page, e));
}
}

@Cacheable(cacheNames = "pageRepo", key = "#token+'-'+#organization+'-'+#page+'-'+#perPage")
public PageReposInfoDTO getGitHubRepos(String token, String organization, int page, int perPage) {
log.info("Start to get paginated github repo info, page: {}", page);
ResponseEntity<List<ReposInfoDTO>> allRepos = gitHubFeignClient.getAllRepos(token, organization, perPage, page);
log.info("Successfully get paginated github repo info, page: {}", page);

int totalPage = parseTotalPage(allRepos.getHeaders().get(BUILD_KITE_LINK_HEADER));

return PageReposInfoDTO.builder().pageInfo(allRepos.getBody()).totalPage(totalPage).build();
try {
log.info("Start to get paginated github repo info, page: {}", page);
ResponseEntity<List<ReposInfoDTO>> allRepos = gitHubFeignClient.getAllRepos(token, organization, perPage,
page);
log.info("Successfully get paginated github repo info, page: {}", page);
int totalPage = parseTotalPage(allRepos.getHeaders().get(BUILD_KITE_LINK_HEADER));
return PageReposInfoDTO.builder().pageInfo(allRepos.getBody()).totalPage(totalPage).build();
}
catch (Exception e) {
log.info("Error to get paginated github repo info, page: {}, exception: {}", page, e);
throw new InternalServerErrorException(
String.format("Error to get paginated github repo info, page: %s, exception: %s", page, e));
}
}

@Cacheable(cacheNames = "pageBranch", key = "#token+'-'+#organization+'-'+#repo+'-'+#page+'-'+#perPage")
public PageBranchesInfoDTO getGitHubBranches(String token, String organization, String repo, int page,
int perPage) {
log.info("Start to get paginated github branch info, page: {}", page);
ResponseEntity<List<BranchesInfoDTO>> allRepos = gitHubFeignClient.getAllBranches(token, organization, repo,
perPage, page);
log.info("Successfully get paginated github branch info, page: {}", page);

int totalPage = parseTotalPage(allRepos.getHeaders().get(BUILD_KITE_LINK_HEADER));

return PageBranchesInfoDTO.builder().pageInfo(allRepos.getBody()).totalPage(totalPage).build();
try {
log.info("Start to get paginated github branch info, page: {}", page);
ResponseEntity<List<BranchesInfoDTO>> allRepos = gitHubFeignClient.getAllBranches(token, organization, repo,
perPage, page);
log.info("Successfully get paginated github branch info, page: {}", page);
int totalPage = parseTotalPage(allRepos.getHeaders().get(BUILD_KITE_LINK_HEADER));
return PageBranchesInfoDTO.builder().pageInfo(allRepos.getBody()).totalPage(totalPage).build();
}
catch (Exception e) {
log.info("Error to get paginated github branch info, page: {}, exception: {}", page, e);
throw new InternalServerErrorException(
String.format("Error to get paginated github branch info, page: %s, exception: %s", page, e));
}
}

@Cacheable(cacheNames = "pagePullRequest",
key = "#token+'-'+#organization+'-'+#repo+'-'+#branch+'-'+#page+'-'+#perPage")
public PagePullRequestInfoDTO getGitHubPullRequest(String token, String organization, String repo, String branch,
int page, int perPage) {
log.info("Start to get paginated github pull request info, page: {}", page);
ResponseEntity<List<PullRequestInfoDTO>> allPullRequests = gitHubFeignClient.getAllPullRequests(token,
organization, repo, perPage, page, branch, "all");
log.info("Successfully get paginated github pull request info, page: {}", page);

int totalPage = parseTotalPage(allPullRequests.getHeaders().get(BUILD_KITE_LINK_HEADER));

return PagePullRequestInfoDTO.builder().pageInfo(allPullRequests.getBody()).totalPage(totalPage).build();
try {
log.info("Start to get paginated github pull request info, page: {}", page);
ResponseEntity<List<PullRequestInfoDTO>> allPullRequests = gitHubFeignClient.getAllPullRequests(token,
organization, repo, perPage, page, branch, "all");
log.info("Successfully get paginated github pull request info, page: {}", page);
int totalPage = parseTotalPage(allPullRequests.getHeaders().get(BUILD_KITE_LINK_HEADER));
return PagePullRequestInfoDTO.builder().pageInfo(allPullRequests.getBody()).totalPage(totalPage).build();
}
catch (Exception e) {
log.info("Error to get paginated github pull request info, page: {}, exception: {}", page, e);
throw new InternalServerErrorException(
String.format("Error to get paginated github pull request info, page: %s, exception: %s", page, e));
}
}

private int parseTotalPage(@Nullable List<String> linkHeader) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,6 @@
import org.springframework.stereotype.Service;

import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
Expand Down Expand Up @@ -377,26 +375,23 @@ public List<String> getAllOrganizations(String token) {
organizationNames.addAll(firstPageStepsInfo.stream().map(OrganizationsInfoDTO::getLogin).toList());
}
if (totalPage > 1) {
List<CompletableFuture<List<OrganizationsInfoDTO>>> futures = IntStream.range(initPage + 1, totalPage + 1)
.mapToObj(page -> getGitHubOrganizationAsync(realToken, page))
.toList();

List<String> orgNamesOtherFirstPage = futures.stream()
.map(CompletableFuture::join)
.flatMap(Collection::stream)
.map(OrganizationsInfoDTO::getLogin)
.toList();
organizationNames.addAll(orgNamesOtherFirstPage);
for (int i = initPage + 1; i < totalPage + 1; i = i + BATCH_SIZE) {
List<OrganizationsInfoDTO> organizationNamesOtherFirstPageList = IntStream
.range(i, Math.min(i + BATCH_SIZE, totalPage + 1))
.parallel()
.mapToObj(page -> cachePageService.getGitHubOrganizations(realToken, page, PER_PAGE).getPageInfo())
.flatMap(Collection::stream)
.toList();
List<String> orgNamesOtherFirstPage = organizationNamesOtherFirstPageList.stream()
.map(OrganizationsInfoDTO::getLogin)
.toList();
organizationNames.addAll(orgNamesOtherFirstPage);
}
}
log.info("Successfully to get all organizations");
return organizationNames;
}

private CompletableFuture<List<OrganizationsInfoDTO>> getGitHubOrganizationAsync(String token, int page) {
return CompletableFuture.supplyAsync(
() -> cachePageService.getGitHubOrganizations(token, page, PER_PAGE).getPageInfo(), customTaskExecutor);
}

public List<String> getAllRepos(String token, String organization, long endTime) {
log.info("Start to get all repos, organization: {}, endTime: {}", organization, endTime);
Instant endTimeInstant = Instant.ofEpochMilli(endTime);
Expand All @@ -415,28 +410,23 @@ public List<String> getAllRepos(String token, String organization, long endTime)
.toList());
}
if (totalPage > 1) {
List<CompletableFuture<List<ReposInfoDTO>>> futures = IntStream.range(initPage + 1, totalPage + 1)
.mapToObj(page -> getGitHubReposAsync(realToken, organization, page))
.toList();

List<String> repoNamesOtherFirstPage = futures.stream()
.map(CompletableFuture::join)
.flatMap(Collection::stream)
.filter(it -> Instant.parse(it.getCreatedAt()).isBefore(endTimeInstant))
.map(ReposInfoDTO::getName)
.toList();
repoNames.addAll(repoNamesOtherFirstPage);
for (int i = initPage + 1; i < totalPage + 1; i = i + BATCH_SIZE) {
List<ReposInfoDTO> repoNamesOtherFirstPageList = IntStream
.range(i, Math.min(i + BATCH_SIZE, totalPage + 1))
.parallel()
.mapToObj(page -> cachePageService.getGitHubRepos(realToken, organization, page, PER_PAGE)
.getPageInfo())
.flatMap(Collection::stream)
.filter(it -> Instant.parse(it.getCreatedAt()).isBefore(endTimeInstant))
.toList();
List<String> repoName = repoNamesOtherFirstPageList.stream().map(ReposInfoDTO::getName).toList();
repoNames.addAll(repoName);
}
}
log.info("Successfully to get all repos, organization: {}", organization);
return repoNames;
}

private CompletableFuture<List<ReposInfoDTO>> getGitHubReposAsync(String token, String organization, int page) {
return CompletableFuture.supplyAsync(
() -> cachePageService.getGitHubRepos(token, organization, page, PER_PAGE).getPageInfo(),
customTaskExecutor);
}

public List<String> getAllBranches(String token, String organization, String repo) {
log.info("Start to get all branches, organization: {}, repo: {}", organization, repo);
int initPage = 1;
Expand All @@ -451,28 +441,24 @@ public List<String> getAllBranches(String token, String organization, String rep
branchNames.addAll(firstPageStepsInfo.stream().map(BranchesInfoDTO::getName).toList());
}
if (totalPage > 1) {
List<CompletableFuture<List<BranchesInfoDTO>>> futures = IntStream.range(initPage + 1, totalPage + 1)
.mapToObj(page -> getGitHubBranchesAsync(realToken, organization, repo, page))
.toList();

List<String> branchNamesOtherFirstPage = futures.stream()
.map(CompletableFuture::join)
.flatMap(Collection::stream)
.map(BranchesInfoDTO::getName)
.toList();
branchNames.addAll(branchNamesOtherFirstPage);
for (int i = initPage + 1; i < totalPage + 1; i = i + BATCH_SIZE) {
List<BranchesInfoDTO> branchNamesOtherFirstPageList = IntStream
.range(i, Math.min(i + BATCH_SIZE, totalPage + 1))
.parallel()
.mapToObj(page -> cachePageService.getGitHubBranches(realToken, organization, repo, page, PER_PAGE)
.getPageInfo())
.flatMap(Collection::stream)
.toList();
List<String> branchNamesOtherFirstPage = branchNamesOtherFirstPageList.stream()
.map(BranchesInfoDTO::getName)
.toList();
branchNames.addAll(branchNamesOtherFirstPage);
}
}
log.info("Successfully to get all branches, organization: {}, repo: {}", organization, repo);
return branchNames;
}

private CompletableFuture<List<BranchesInfoDTO>> getGitHubBranchesAsync(String token, String organization,
String repo, int page) {
return CompletableFuture.supplyAsync(
() -> cachePageService.getGitHubBranches(token, organization, repo, page, PER_PAGE).getPageInfo(),
customTaskExecutor);
}

public List<String> getAllCrews(String token, String organization, String repo, String branch, long startTime,
long endTime) {
log.info("Start to get all crews, organization: {}, repo: {}, branch: {}, startTime: {}, endTime: {}",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import heartbeat.client.dto.pipeline.buildkite.BuildKiteJob;
import heartbeat.client.dto.pipeline.buildkite.BuildKitePipelineDTO;
import heartbeat.client.dto.pipeline.buildkite.PageStepsInfoDto;
import heartbeat.exception.InternalServerErrorException;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
Expand All @@ -34,6 +35,8 @@

import static org.assertj.core.api.Assertions.assertThat;
import static org.hibernate.validator.internal.util.Contracts.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.when;
Expand Down Expand Up @@ -213,6 +216,15 @@ void shouldReturnPageOrganizationsInfoDtoWhenFetchPageOrganizationsInfoSuccessGi
assertThat(pageOrganizationsInfoDTO.getTotalPage()).isEqualTo(2);
}

@Test
void shouldThrowExceptionWhenFetchPageOrganizationsInfoThrow500() {
when(gitHubFeignClient.getAllOrganizations(MOCK_TOKEN, 100, 1)).thenThrow(RuntimeException.class);

InternalServerErrorException internalServerErrorException = assertThrows(InternalServerErrorException.class, () -> cachePageService.getGitHubOrganizations(MOCK_TOKEN, 1, 100));
assertEquals(500, internalServerErrorException.getStatus());
assertEquals("Error to get paginated github organization info, page: 1, exception: java.lang.RuntimeException", internalServerErrorException.getMessage());
}

@Test
void shouldReturnPageReposInfoDtoWhenFetchPageReposInfoSuccessGivenExist() throws IOException {
String organization = "test-org";
Expand All @@ -228,6 +240,18 @@ void shouldReturnPageReposInfoDtoWhenFetchPageReposInfoSuccessGivenExist() throw
assertThat(pageReposInfoDTO.getTotalPage()).isEqualTo(2);
}

@Test
void shouldThrowExceptionWhenFetchPageRepoInfoThrow500() {
String organization = "test-org";
when(gitHubFeignClient.getAllRepos(MOCK_TOKEN, organization, 100, 1)).thenThrow(RuntimeException.class);

InternalServerErrorException internalServerErrorException = assertThrows(InternalServerErrorException.class,
() -> cachePageService.getGitHubRepos(MOCK_TOKEN, organization, 1, 100));
assertEquals(500, internalServerErrorException.getStatus());
assertEquals("Error to get paginated github repo info, page: 1, exception: java.lang.RuntimeException",
internalServerErrorException.getMessage());
}

@Test
void shouldReturnPageBranchesInfoDtoWhenFetchPageBranchesInfoSuccessGivenExist() throws IOException {
String organization = "test-org";
Expand All @@ -245,6 +269,20 @@ void shouldReturnPageBranchesInfoDtoWhenFetchPageBranchesInfoSuccessGivenExist()
assertThat(pageBranchesInfoDTO.getTotalPage()).isEqualTo(2);
}

@Test
void shouldThrowExceptionWhenFetchPageBranchInfoThrow500() {
String organization = "test-org";
String repo = "test-repo";
when(gitHubFeignClient.getAllBranches(MOCK_TOKEN, organization, repo, 100, 1))
.thenThrow(RuntimeException.class);

InternalServerErrorException internalServerErrorException = assertThrows(InternalServerErrorException.class,
() -> cachePageService.getGitHubBranches(MOCK_TOKEN, organization, repo, 1, 100));
assertEquals(500, internalServerErrorException.getStatus());
assertEquals("Error to get paginated github branch info, page: 1, exception: java.lang.RuntimeException",
internalServerErrorException.getMessage());
}

@Test
void shouldReturnPagePullRequestInfoDtoWhenFetchPullRequestInfoSuccessGivenExist() throws IOException {
String organization = "test-org";
Expand All @@ -264,6 +302,21 @@ void shouldReturnPagePullRequestInfoDtoWhenFetchPullRequestInfoSuccessGivenExist
assertThat(pagePullRequestInfoDTO.getTotalPage()).isEqualTo(2);
}

@Test
void shouldThrowExceptionWhenFetchPagePullRequestInfoThrow500() {
String organization = "test-org";
String repo = "test-repo";
String branch = "test-branch";
when(gitHubFeignClient.getAllPullRequests(MOCK_TOKEN, organization, repo, 100, 1, branch, "all"))
.thenThrow(RuntimeException.class);

InternalServerErrorException internalServerErrorException = assertThrows(InternalServerErrorException.class,
() -> cachePageService.getGitHubPullRequest(MOCK_TOKEN, organization, repo, branch, 1, 100));
assertEquals(500, internalServerErrorException.getStatus());
assertEquals("Error to get paginated github pull request info, page: 1, exception: java.lang.RuntimeException",
internalServerErrorException.getMessage());
}

private static <T> ResponseEntity<List<T>> getResponseEntity(HttpHeaders httpHeaders, String pathname)
throws IOException {
ObjectMapper mapper = new ObjectMapper();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
import {
updateShouldGetBoardConfig,
updateShouldGetPipelineConfig,
updateShouldGetSourceControlConfig,
} from '@src/context/Metrics/metricsSlice';
import {
IRangeOnChangeData,
SortedDateRangeType,
sortFn,
TProps,
} from '@src/containers/ConfigStep/DateRangePicker/types';
import { updateShouldGetBoardConfig, updateShouldGetPipelineConfig } from '@src/context/Metrics/metricsSlice';
import { selectDateRange, selectDateRangeSortType, updateDateRange } from '@src/context/config/configSlice';
import { DateRangePickerGroupContainer } from '@src/containers/ConfigStep/DateRangePicker/style';
import { DateRangePicker } from '@src/containers/ConfigStep/DateRangePicker/DateRangePicker';
Expand Down Expand Up @@ -56,6 +60,7 @@ export const DateRangePickerGroup = ({ onError }: TProps) => {
const dispatchUpdateConfig = () => {
dispatch(updateShouldGetBoardConfig(true));
dispatch(updateShouldGetPipelineConfig(true));
dispatch(updateShouldGetSourceControlConfig(true));
dispatch(updateShouldMetricsLoaded(true));
};

Expand Down
Loading

0 comments on commit 5f11cba

Please sign in to comment.