Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
bvsharp committed Jun 26, 2024
1 parent e8acb9e commit 07b6a16
Show file tree
Hide file tree
Showing 5 changed files with 220 additions and 159 deletions.
51 changes: 21 additions & 30 deletions src/main/java/org/folio/fqm/repository/DataRefreshRepository.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,26 +29,17 @@ public class DataRefreshRepository {

public static final Field<String> CURRENCY_FIELD = field("currency", String.class);
public static final Field<Double> EXCHANGE_RATE_FIELD = field("exchange_rate", Double.class);
public static final String EXCHANGE_RATE_TABLE = "currency_exchange_rates";

private static final String REFRESH_MATERIALIZED_VIEW_CONCURRENTLY_SQL = "REFRESH MATERIALIZED VIEW CONCURRENTLY ";
private static final String REFRESH_MATERIALIZED_VIEW_SQL = "REFRESH MATERIALIZED VIEW ";
private static final String GET_EXCHANGE_RATE_PATH = "finance/exchange-rate";
private static final String GET_LOCALE_SETTINGS_PATH = "configurations/entries";
private static final String EXCHANGE_RATE_TABLE = "currency_exchange_rates";
private static final Map<String, String> GET_LOCALE_SETTINGS_PARAMS = Map.of(
"query", "(module==ORG and configName==localeSettings)"
);

private static final List<String> MATERIALIZED_VIEW_NAMES = List.of(
"drv_circulation_loan_status",
"drv_inventory_item_status",
"drv_pol_payment_status",
"drv_pol_receipt_status",
"drv_inventory_statistical_code_full",
"drv_languages"
);

static final List<String> SYSTEM_SUPPORTED_CURRENCIES = List.of(
private static final List<String> SYSTEM_SUPPORTED_CURRENCIES = List.of(
"USD",
"EUR",
"JPY",
Expand Down Expand Up @@ -88,24 +79,24 @@ public class DataRefreshRepository {

private final SimpleHttpClient simpleHttpClient;

public DataRefreshResponse refreshData(String tenantId) {
List<String> failedConcurrentRefreshes = refreshMaterializedViews(tenantId, MATERIALIZED_VIEW_NAMES, true);
List<String> failedRefreshes = refreshMaterializedViews(tenantId, failedConcurrentRefreshes, false);
List<String> successRefreshes = new ArrayList<>(MATERIALIZED_VIEW_NAMES
.stream()
.filter(matView -> !failedRefreshes.contains(matView))
.toList());
if (refreshExchangeRates(tenantId)) {
successRefreshes.add(EXCHANGE_RATE_TABLE);
} else {
failedRefreshes.add(EXCHANGE_RATE_TABLE);
}
return new DataRefreshResponse()
.successfulRefresh(successRefreshes)
.failedRefresh(failedRefreshes);
}

private List<String> refreshMaterializedViews(String tenantId, List<String> viewsToRefresh, boolean refreshConcurrently) {
// public DataRefreshResponse refreshData(String tenantId) {
// List<String> failedConcurrentRefreshes = refreshMaterializedViews(tenantId, MATERIALIZED_VIEW_NAMES, true);
// List<String> failedRefreshes = refreshMaterializedViews(tenantId, failedConcurrentRefreshes, false);
// List<String> successRefreshes = new ArrayList<>(MATERIALIZED_VIEW_NAMES
// .stream()
// .filter(matView -> !failedRefreshes.contains(matView))
// .toList());
// if (refreshExchangeRates(tenantId)) {
// successRefreshes.add(EXCHANGE_RATE_TABLE);
// } else {
// failedRefreshes.add(EXCHANGE_RATE_TABLE);
// }
// return new DataRefreshResponse()
// .successfulRefresh(successRefreshes)
// .failedRefresh(failedRefreshes);
// }

public List<String> refreshMaterializedViews(String tenantId, List<String> viewsToRefresh, boolean refreshConcurrently) {
List<String> failedRefreshes = new ArrayList<>();
String refreshType = refreshConcurrently ? "concurrently" : "non-concurrently";
for (String matViewName : viewsToRefresh) {
Expand All @@ -125,7 +116,7 @@ private List<String> refreshMaterializedViews(String tenantId, List<String> view
return failedRefreshes;
}

private boolean refreshExchangeRates(String tenantId) {
public boolean refreshExchangeRates(String tenantId) {
log.info("Refreshing exchange rates");
String fullTableName = tenantId + "_mod_fqm_manager." + EXCHANGE_RATE_TABLE;
String systemCurrency = getSystemCurrencyCode();
Expand Down
29 changes: 28 additions & 1 deletion src/main/java/org/folio/fqm/service/DataRefreshService.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,39 @@
import org.folio.fqm.repository.DataRefreshRepository;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

import static org.folio.fqm.repository.DataRefreshRepository.EXCHANGE_RATE_TABLE;

@Service
@RequiredArgsConstructor
public class DataRefreshService {
private final DataRefreshRepository dataRefreshRepository;

static final List<String> MATERIALIZED_VIEW_NAMES = List.of(
"drv_circulation_loan_status",
"drv_inventory_item_status",
"drv_pol_payment_status",
"drv_pol_receipt_status",
"drv_inventory_statistical_code_full",
"drv_languages"
);

public DataRefreshResponse refreshData(String tenantId) {
return dataRefreshRepository.refreshData(tenantId);
List<String> failedConcurrentRefreshes = dataRefreshRepository.refreshMaterializedViews(tenantId, MATERIALIZED_VIEW_NAMES, true);
List<String> failedRefreshes = dataRefreshRepository.refreshMaterializedViews(tenantId, failedConcurrentRefreshes, false);
List<String> successRefreshes = new ArrayList<>(MATERIALIZED_VIEW_NAMES
.stream()
.filter(matView -> !failedRefreshes.contains(matView))
.toList());
if (dataRefreshRepository.refreshExchangeRates(tenantId)) {
successRefreshes.add(EXCHANGE_RATE_TABLE);
} else {
failedRefreshes.add(EXCHANGE_RATE_TABLE);
}
return new DataRefreshResponse()
.successfulRefresh(successRefreshes)
.failedRefresh(failedRefreshes);
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.folio.fqm.controller;

import org.folio.fqm.domain.dto.DataRefreshResponse;
import org.folio.fqm.resource.DataRefreshController;
import org.folio.fqm.service.DataRefreshService;
import org.folio.spring.FolioExecutionContext;
Expand All @@ -12,6 +13,8 @@
import org.springframework.test.web.servlet.RequestBuilder;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;

import java.util.List;

import static org.mockito.Mockito.doNothing;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
Expand All @@ -31,13 +34,16 @@ class DataRefreshControllerTest {
@Test
void refreshDataTest() throws Exception {
String tenantId = "tenant_01";
DataRefreshResponse expectedResponse = new DataRefreshResponse()
.successfulRefresh(List.of())
.failedRefresh(List.of());
RequestBuilder requestBuilder = MockMvcRequestBuilders.post("/entity-types/materialized-views/refresh")
.header(XOkapiHeaders.TENANT, tenantId)
.contentType(APPLICATION_JSON);
when(executionContext.getTenantId()).thenReturn(tenantId);
doNothing().when(dataRefreshService).refreshData(tenantId);
when(dataRefreshService.refreshData(tenantId)).thenReturn(expectedResponse);
mockMvc.perform(requestBuilder)
.andExpect(status().isNoContent());
.andExpect(status().isOk());
verify(dataRefreshService, times(1)).refreshData(tenantId);
}
}
Loading

0 comments on commit 07b6a16

Please sign in to comment.