Skip to content

Commit

Permalink
DMP-4376 Add ARM RPO logs for Dynatrace
Browse files Browse the repository at this point in the history
Added dynatrace logging for ARM RPO Search and Polling
  • Loading branch information
karen-hedges committed Dec 4, 2024
1 parent f818833 commit a070a2e
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,14 @@
import uk.gov.hmcts.darts.arm.service.ArmRpoService;
import uk.gov.hmcts.darts.authorisation.component.UserIdentity;
import uk.gov.hmcts.darts.common.entity.ArmRpoExecutionDetailEntity;
import uk.gov.hmcts.darts.common.entity.UserAccountEntity;
import uk.gov.hmcts.darts.common.service.FileOperationService;
import uk.gov.hmcts.darts.log.api.LogApi;

import java.io.File;
import java.io.IOException;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import static java.util.Objects.isNull;
Expand All @@ -38,13 +39,14 @@ public class ArmRpoPollServiceImpl implements ArmRpoPollService {
private final ArmDataManagementConfiguration armDataManagementConfiguration;
private final LogApi logApi;

private List<File> tempProductionFiles;
private List<File> tempProductionFiles = new ArrayList<>();

private List<Integer> allowableFailedStates = null;
private List<Integer> allowableFailedStates = new ArrayList<>();


@Override
public void pollArmRpo(boolean isManualRun) {
log.info("Polling ARM RPO service - isManualRun: {}", isManualRun);
setupFailedStatuses();
Integer executionId = null;
tempProductionFiles = new ArrayList<>();
Expand All @@ -55,14 +57,15 @@ public void pollArmRpo(boolean isManualRun) {
logApi.armRpoPollingFailed(executionId);
return;
}
executionId = armRpoExecutionDetailEntity.getId();

var bearerToken = armApiService.getArmBearerToken();
if (isNull(bearerToken)) {
log.warn("Unable to get bearer token to poll ARM RPO");
logApi.armRpoPollingFailed(executionId);
return;
}

executionId = armRpoExecutionDetailEntity.getId();
var userAccount = userIdentity.getUserAccount();

// step to call ARM RPO API to get the extended searches by matter
Expand All @@ -83,17 +86,7 @@ public void pollArmRpo(boolean isManualRun) {
var productionOutputFiles = armRpoApi.getProductionOutputFiles(bearerToken, executionId, userAccount);

for (var productionExportFileId : productionOutputFiles) {
String productionExportFilename = generateTempProductionExportFilename(productionExportFileId);
// step to call ARM RPO API to download the production export file
var inputStream = armRpoApi.downloadProduction(bearerToken, executionId, productionExportFileId, userAccount);
log.info("About to save production export file to temp workspace {}", productionExportFilename);
Path tempProductionFile = fileOperationService.saveFileToTempWorkspace(
inputStream,
productionExportFilename,
armDataManagementConfiguration,
true
);
tempProductionFiles.add(tempProductionFile.toFile());
processProductionFiles(productionExportFileId, bearerToken, executionId, userAccount);
}
if (CollectionUtils.isNotEmpty(tempProductionFiles)) {
// step to call ARM RPO API to remove the production
Expand All @@ -111,37 +104,52 @@ public void pollArmRpo(boolean isManualRun) {
log.error("Error while polling ARM RPO", e);
logApi.armRpoPollingFailed(executionId);
} finally {
try {
cleanUpTempFiles();
} catch (Exception e) {
log.error("Error while cleaning up ARM RPO polling service temp files", e);
}
cleanUpTempFiles();
}
}

private void processProductionFiles(String productionExportFileId, String bearerToken, Integer executionId,
UserAccountEntity userAccount) throws IOException {
String productionExportFilename = generateTempProductionExportFilename(productionExportFileId);
// step to call ARM RPO API to download the production export file
var inputStream = armRpoApi.downloadProduction(bearerToken, executionId, productionExportFileId, userAccount);
log.info("About to save production export file to temp workspace {}", productionExportFilename);
Path tempProductionFile = fileOperationService.saveFileToTempWorkspace(
inputStream,
productionExportFilename,
armDataManagementConfiguration,
true
);
tempProductionFiles.add(tempProductionFile.toFile());
}

private void setupFailedStatuses() {
if (CollectionUtils.isEmpty(allowableFailedStates)) {
allowableFailedStates = Collections.unmodifiableList(List.of(
allowableFailedStates = List.of(
ArmRpoHelper.getExtendedSearchesByMatterRpoState().getId(),
ArmRpoHelper.getMasterIndexFieldByRecordClassSchemaSecondaryRpoState().getId(),
ArmRpoHelper.createExportBasedOnSearchResultsTableRpoState().getId(),
ArmRpoHelper.getExtendedProductionsByMatterRpoState().getId(),
ArmRpoHelper.getProductionOutputFilesRpoState().getId(),
ArmRpoHelper.downloadProductionRpoState().getId(),
ArmRpoHelper.removeProductionRpoState().getId()
));
);
}
}

private void cleanUpTempFiles() {
for (var tempProductionFile : tempProductionFiles) {
if (tempProductionFile.exists()) {
if (tempProductionFile.delete()) {
log.info("Deleted temp production file {}", tempProductionFile.getName());
} else {
log.warn("Failed to delete temp production file {}", tempProductionFile.getName());
try {
for (var tempProductionFile : tempProductionFiles) {
if (tempProductionFile.exists()) {
if (tempProductionFile.delete()) {
log.info("Deleted temp production file {}", tempProductionFile.getName());
} else {
log.warn("Failed to delete temp production file {}", tempProductionFile.getName());
}
}
}
} catch (Exception e) {
log.error("Error while cleaning up ARM RPO polling service temp files", e);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.junit.jupiter.api.io.TempDir;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import uk.gov.hmcts.darts.arm.config.ArmDataManagementConfiguration;
Expand All @@ -26,6 +25,7 @@
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.List;

import static org.mockito.ArgumentMatchers.any;
Expand Down Expand Up @@ -63,16 +63,21 @@ class ArmRpoPollServiceImplTest {
@TempDir
private File tempDirectory;

private final List<File> tempProductionFiles = new ArrayList<>();
private final List<Integer> allowableFailedStates = new ArrayList<>();

private static final Integer EXECUTION_ID = 1;
private ArmRpoExecutionDetailEntity armRpoExecutionDetailEntity;
private static final ArmRpoHelperMocks ARM_RPO_HELPER_MOCKS = new ArmRpoHelperMocks();

@InjectMocks
private ArmRpoPollServiceImpl armRpoPollService;


@BeforeEach
void setUp() {
armRpoPollService = new ArmRpoPollServiceImpl(armRpoApi, armApiService, armRpoService, userIdentity, fileOperationService,
armDataManagementConfiguration, logApi, tempProductionFiles, allowableFailedStates);

lenient().when(userIdentity.getUserAccount()).thenReturn(userAccountEntity);

armRpoExecutionDetailEntity = new ArmRpoExecutionDetailEntity();
Expand All @@ -81,7 +86,7 @@ void setUp() {
}

@Test
void pollArmRpo_shouldPollSuccessfullyWithSaveBackgroundCompleted() throws IOException {
void pollArmRpo_shouldPollSuccessfully_whenSaveBackgroundCompleted() throws IOException {
// given
armRpoExecutionDetailEntity.setArmRpoStatus(ARM_RPO_HELPER_MOCKS.getCompletedRpoStatus());
armRpoExecutionDetailEntity.setArmRpoState(ARM_RPO_HELPER_MOCKS.getSaveBackgroundSearchRpoState());
Expand Down Expand Up @@ -122,7 +127,7 @@ void pollArmRpo_shouldPollSuccessfullyWithSaveBackgroundCompleted() throws IOExc
}

@Test
void pollArmRpo_shouldPollSuccessfullyWithSaveBackgroundCompletedForManualRun() throws IOException {
void pollArmRpo_shouldPollSuccessfully_whenSaveBackgroundCompletedForManualRun() throws IOException {
// given
armRpoExecutionDetailEntity.setArmRpoStatus(ARM_RPO_HELPER_MOCKS.getCompletedRpoStatus());
armRpoExecutionDetailEntity.setArmRpoState(ARM_RPO_HELPER_MOCKS.getSaveBackgroundSearchRpoState());
Expand Down Expand Up @@ -163,7 +168,7 @@ void pollArmRpo_shouldPollSuccessfullyWithSaveBackgroundCompletedForManualRun()
}

@Test
void pollArmRpo_shouldPollSuccessfullyWithCreateExportBasedOnSearchResultsTableInProgress() throws IOException {
void pollArmRpo_shouldPollSuccessfully_whenCreateExportBasedOnSearchResultsTableInProgress() throws IOException {
// given
armRpoExecutionDetailEntity.setArmRpoStatus(ARM_RPO_HELPER_MOCKS.getInProgressRpoStatus());
armRpoExecutionDetailEntity.setArmRpoState(ARM_RPO_HELPER_MOCKS.getCreateExportBasedOnSearchResultsTableRpoState());
Expand Down Expand Up @@ -204,7 +209,7 @@ void pollArmRpo_shouldPollSuccessfullyWithCreateExportBasedOnSearchResultsTableI
}

@Test
void pollArmRpo_shouldPollSuccessfullyWithCreateExportBasedOnSearchResultsTableForManualRun() throws IOException {
void pollArmRpo_shouldPollSuccessfully_whenCreateExportBasedOnSearchResultsTableForManualRun() throws IOException {
// given
armRpoExecutionDetailEntity.setArmRpoStatus(ARM_RPO_HELPER_MOCKS.getInProgressRpoStatus());
armRpoExecutionDetailEntity.setArmRpoState(ARM_RPO_HELPER_MOCKS.getCreateExportBasedOnSearchResultsTableRpoState());
Expand Down Expand Up @@ -245,7 +250,7 @@ void pollArmRpo_shouldPollSuccessfullyWithCreateExportBasedOnSearchResultsTableF
}

@Test
void pollArmRpo_shouldPollSuccessfullyWithDownloadProductionFailedOnPreviousAttemptForManualRun() throws IOException {
void pollArmRpo_shouldPollSuccessfully_whenDownloadProductionFailedOnPreviousAttemptForManualRun() throws IOException {
// given
armRpoExecutionDetailEntity.setArmRpoStatus(ARM_RPO_HELPER_MOCKS.getFailedRpoStatus());
armRpoExecutionDetailEntity.setArmRpoState(ARM_RPO_HELPER_MOCKS.getDownloadProductionRpoState());
Expand Down Expand Up @@ -286,7 +291,7 @@ void pollArmRpo_shouldPollSuccessfullyWithDownloadProductionFailedOnPreviousAtte
}

@Test
void pollArmRpo_shouldPollNotFindLatestExecutionDetailOnStepDownloadProductionFailed() throws IOException {
void pollArmRpo_shouldPollNotFindLatestExecutionDetail_OnStepDownloadProductionFailed() {
// given
armRpoExecutionDetailEntity.setArmRpoStatus(ARM_RPO_HELPER_MOCKS.getFailedRpoStatus());
armRpoExecutionDetailEntity.setArmRpoState(ARM_RPO_HELPER_MOCKS.getDownloadProductionRpoState());
Expand All @@ -302,7 +307,7 @@ void pollArmRpo_shouldPollNotFindLatestExecutionDetailOnStepDownloadProductionFa
}

@Test
void pollArmRpo_shouldPollNotFindLatestExecutionDetailForManualRunOnStepDownloadProductionInProgress() throws IOException {
void pollArmRpo_shouldPollNotFindLatestExecutionDetailForManualRun_OnStepDownloadProductionInProgress() {
// given
armRpoExecutionDetailEntity.setArmRpoStatus(ARM_RPO_HELPER_MOCKS.getInProgressRpoStatus());
armRpoExecutionDetailEntity.setArmRpoState(ARM_RPO_HELPER_MOCKS.getDownloadProductionRpoState());
Expand Down Expand Up @@ -399,7 +404,7 @@ void pollArmRpo_shouldHandleNoProductionFiles() {
}

@Test
void pollArmRpo_shouldHandleExceptionDuringPollingOnCreateExportBasedOnSearchResultsTableStep() {
void pollArmRpo_shouldHandleExceptionDuringPolling_whenCreateExportBasedOnSearchResultsTableStep() {
// given
armRpoExecutionDetailEntity.setArmRpoStatus(ARM_RPO_HELPER_MOCKS.getCompletedRpoStatus());
armRpoExecutionDetailEntity.setArmRpoState(ARM_RPO_HELPER_MOCKS.getSaveBackgroundSearchRpoState());
Expand Down Expand Up @@ -429,7 +434,8 @@ private List<MasterIndexFieldByRecordClassSchema> createHeaderColumns() {
false),
createMasterIndexFieldByRecordClassSchema("109b6bf1-57a0-48ec-b22e-c7248dc74f91", "Contributor", "contributor", "string", false),
createMasterIndexFieldByRecordClassSchema("893048bf-1e7c-4811-9abf-00cd77a715cf", "Record Date", "recordDate", "date", false),
createMasterIndexFieldByRecordClassSchema("fdd0fcbb-da46-4af1-a627-ac255c12bb23", "ObjectId", "bf_012", "number", false)
createMasterIndexFieldByRecordClassSchema("fdd0fcbb-da46-4af1-a627-ac255c12bb23", "ObjectId", "bf_012", "number", false),
createMasterIndexFieldByRecordClassSchema("1fa3bf91-5234-432d-bebb-b339dc3aaccf", "Region", "bf_012", "number", true)
);
}

Expand Down

0 comments on commit a070a2e

Please sign in to comment.