Skip to content

Commit

Permalink
Cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
JayGreeeen committed Nov 22, 2024
1 parent 5e1162d commit f1bfebc
Show file tree
Hide file tree
Showing 14 changed files with 86 additions and 225 deletions.

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,11 @@

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;

import com.expediagroup.beekeeper.core.model.HousekeepingEntity;
import com.expediagroup.beekeeper.core.model.history.BeekeeperHistory;
Expand All @@ -21,7 +24,9 @@ public BeekeeperHistoryService(BeekeeperHistoryRepository beekeeperHistoryReposi
this.beekeeperHistoryRepository = beekeeperHistoryRepository;
}

public void saveHistory(HousekeepingEntity housekeepingEntity, String eventDetails, String status) {
public void saveHistory(HousekeepingEntity housekeepingEntity, String status) {
String eventDetails = createEventDetails(housekeepingEntity);

BeekeeperHistory event = BeekeeperHistory.builder()
.id(housekeepingEntity.getId())
.eventTimestamp(LocalDateTime.now())
Expand All @@ -35,4 +40,17 @@ public void saveHistory(HousekeepingEntity housekeepingEntity, String eventDetai
log.info("Saving activity in Beekeeper History table; {}", event);
beekeeperHistoryRepository.save(event);
}

private String createEventDetails(HousekeepingEntity entity) {
ObjectMapper mapper = new ObjectMapper();
mapper.findAndRegisterModules();
mapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
mapper.registerModule(new JavaTimeModule());
try {
return mapper.writeValueAsString(entity);
} catch (JsonProcessingException e) {
log.warn("Error encountered writing object as string", e);
}
return "{}";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,15 @@

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;

import com.expediagroup.beekeeper.core.TestApplication;
import com.expediagroup.beekeeper.core.model.HousekeepingMetadata;
import com.expediagroup.beekeeper.core.model.HousekeepingPath;
import com.expediagroup.beekeeper.core.model.HousekeepingStatus;
import com.expediagroup.beekeeper.core.model.PeriodDuration;
import com.expediagroup.beekeeper.core.model.history.BeekeeperHistory;
import com.expediagroup.beekeeper.core.model.history.ExpiredEventDetails;
import com.expediagroup.beekeeper.core.model.history.UnreferencedEventDetails;

@ExtendWith(SpringExtension.class)
@TestPropertySource(properties = {
Expand All @@ -51,14 +53,19 @@ public class BeekeeperHistoryRepositoryTest {

private static final int PAGE = 0;
private static final int PAGE_SIZE = 500;
protected final ObjectMapper mapper = new ObjectMapper();
protected ObjectMapper mapper;

@Autowired
private BeekeeperHistoryRepository repository;

@BeforeEach
public void setupDb() {
repository.deleteAll();

mapper = new ObjectMapper();
mapper.findAndRegisterModules();
mapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
mapper.registerModule(new JavaTimeModule());
}

@Test
Expand Down Expand Up @@ -109,27 +116,27 @@ public void unreferenced_multipleStatuses() throws JsonProcessingException {
}

protected BeekeeperHistory createExpiredEventDetails(HousekeepingStatus status) throws JsonProcessingException {
ExpiredEventDetails expiredEventDetails = ExpiredEventDetails.builder()
HousekeepingMetadata entity = HousekeepingMetadata.builder()
.cleanupAttempts(3)
.cleanupDelay("P1D")
.cleanupDelay(PeriodDuration.parse("P1D"))
.partitionName("event_date")
.cleanupTimestamp(COLUMN_TIMESTAMP.toString())
.creationTimestamp(COLUMN_TIMESTAMP)
.modifiedTimestamp(COLUMN_TIMESTAMP)
.build();

mapper.findAndRegisterModules();
String stringDetails = mapper.writeValueAsString(expiredEventDetails);
String stringDetails = mapper.writeValueAsString(entity);
return createHistoryEntry("EXPIRED", status, stringDetails);
}

protected BeekeeperHistory createUnreferencedEventDetails(HousekeepingStatus status) throws JsonProcessingException {
UnreferencedEventDetails expiredEventDetails = UnreferencedEventDetails.builder()
HousekeepingPath entity = HousekeepingPath.builder()
.cleanupAttempts(3)
.cleanupDelay("P1D")
.cleanupTimestamp(COLUMN_TIMESTAMP.toString())
.cleanupDelay(PeriodDuration.parse("P1D"))
.creationTimestamp(COLUMN_TIMESTAMP)
.modifiedTimestamp(COLUMN_TIMESTAMP)
.build();

mapper.findAndRegisterModules();
String stringDetails = mapper.writeValueAsString(expiredEventDetails);
String stringDetails = mapper.writeValueAsString(entity);
return createHistoryEntry("UNREFERENCED", status, stringDetails);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,16 @@
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;

import com.expediagroup.beekeeper.core.model.HousekeepingEntity;
import com.expediagroup.beekeeper.core.model.HousekeepingMetadata;
import com.expediagroup.beekeeper.core.model.HousekeepingPath;
import com.expediagroup.beekeeper.core.model.PeriodDuration;
import com.expediagroup.beekeeper.core.model.history.BeekeeperHistory;
import com.expediagroup.beekeeper.core.model.history.ExpiredEventDetails;
import com.expediagroup.beekeeper.core.model.history.UnreferencedEventDetails;
import com.expediagroup.beekeeper.core.repository.BeekeeperHistoryRepository;

@ExtendWith(MockitoExtension.class)
Expand All @@ -46,22 +49,22 @@ public void setup() {
}

@Test
void expiredHistory() {
void expiredHistory() throws JsonProcessingException {
HousekeepingMetadata metadata = createHousekeepingMetadata();
String details = ExpiredEventDetails.stringFromEntity(metadata);
String details = createEventDetails(metadata);
BeekeeperHistory history = createHistoryEvent(metadata, details, "DELETED");

beekeeperHistoryService.saveHistory(metadata, details, "DELETED");
beekeeperHistoryService.saveHistory(metadata, "DELETED");
verify(repository).save(history);
}

@Test
void unreferencedHistory() {
void unreferencedHistory() throws JsonProcessingException {
HousekeepingPath path = createHousekeepingPath();
String details = UnreferencedEventDetails.stringFromEntity(path);
String details = createEventDetails(path);
BeekeeperHistory history = createHistoryEvent(path, details, "SCHEDULED");

beekeeperHistoryService.saveHistory(path, details, "SCHEDULED");
beekeeperHistoryService.saveHistory(path, "SCHEDULED");
verify(repository).save(history);
}

Expand Down Expand Up @@ -102,4 +105,12 @@ private HousekeepingPath createHousekeepingPath() {
.lifecycleType(UNREFERENCED.name())
.build();
}

private String createEventDetails(HousekeepingEntity housekeepingEntity) throws JsonProcessingException {
ObjectMapper mapper = new ObjectMapper();
mapper.findAndRegisterModules();
mapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
mapper.registerModule(new JavaTimeModule());
return mapper.writeValueAsString(housekeepingEntity);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@
import com.expediagroup.beekeeper.cleanup.path.PathCleaner;
import com.expediagroup.beekeeper.core.model.HousekeepingMetadata;
import com.expediagroup.beekeeper.core.model.HousekeepingStatus;
import com.expediagroup.beekeeper.core.model.history.ExpiredEventDetails;
import com.expediagroup.beekeeper.core.repository.HousekeepingMetadataRepository;
import com.expediagroup.beekeeper.core.service.BeekeeperHistoryService;
import com.expediagroup.beekeeper.core.validation.S3PathValidator;
Expand Down Expand Up @@ -190,13 +189,10 @@ private Long countPartitionsForDatabaseAndTable(

private void saveHistory(HousekeepingMetadata metadata, HousekeepingStatus housekeepingStatus,
boolean dryRunEnabled) {
String eventDetails = ExpiredEventDetails.stringFromEntity(metadata);

String status = String.valueOf(housekeepingStatus);
if (dryRunEnabled) {
status = "DRY_RUN_" + housekeepingStatus;
}

historyService.saveHistory(metadata, eventDetails, status);
historyService.saveHistory(metadata, status);
}
}
Loading

0 comments on commit f1bfebc

Please sign in to comment.