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

CIRC-2167 Address high memory consumption in mod-circulation for Ques… #1529

Open
wants to merge 3 commits into
base: v24.2.5_dummy_search_slips
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package org.folio.circulation.domain.configuration;

import static org.folio.circulation.support.json.JsonPropertyFetcher.getBooleanProperty;

import java.lang.invoke.MethodHandles;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import io.vertx.core.json.JsonObject;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.ToString;

@AllArgsConstructor
@Getter
@ToString(onlyExplicitlyIncluded = true)
public class PrintHoldRequestsConfiguration {

protected static final Logger log = LogManager.getLogger(MethodHandles.lookup().lookupClass());

@ToString.Include
private final boolean printHoldRequestsEnabled;

public static PrintHoldRequestsConfiguration from(JsonObject jsonObject) {
try {
return new PrintHoldRequestsConfiguration(
getBooleanProperty(jsonObject, "printHoldRequestsEnabled")
);
}
catch (IllegalArgumentException e) {
log.error("Failed to parse PrintHoldRequests configuration");
return null;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import org.folio.circulation.domain.ConfigurationService;
import org.folio.circulation.domain.MultipleRecords;
import org.folio.circulation.domain.anonymization.config.LoanAnonymizationConfiguration;
import org.folio.circulation.domain.configuration.PrintHoldRequestsConfiguration;
import org.folio.circulation.domain.configuration.TlrSettingsConfiguration;
import org.folio.circulation.support.Clients;
import org.folio.circulation.support.GetManyRecordsClient;
Expand Down Expand Up @@ -56,6 +57,13 @@ public CompletableFuture<Result<TlrSettingsConfiguration>> lookupTlrSettings() {
return findAndMapFirstConfiguration(queryResult, TlrSettingsConfiguration::from);
}

public CompletableFuture<Result<PrintHoldRequestsConfiguration>> lookupPrintHoldRequestsEnabled() {
Result<CqlQuery> queryResult = defineModuleNameAndConfigNameFilter(
"SETTINGS", "PRINT_HOLD_REQUESTS");

return findAndMapFirstConfiguration(queryResult, PrintHoldRequestsConfiguration::from);
}

/**
* Gets loan history tenant configuration - settings for loan anonymization
*
Expand Down
20 changes: 19 additions & 1 deletion src/main/java/org/folio/circulation/resources/SlipsResource.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import static org.folio.circulation.support.fetching.RecordFetching.findWithMultipleCqlIndexValues;
import static org.folio.circulation.support.http.client.CqlQuery.exactMatch;
import static org.folio.circulation.support.http.client.CqlQuery.exactMatchAny;
import static org.folio.circulation.support.results.Result.ofAsync;
import static org.folio.circulation.support.results.Result.succeeded;
import static org.folio.circulation.support.results.ResultBinding.flatMapResult;
import static org.folio.circulation.support.utils.LogUtil.collectionAsString;
Expand All @@ -35,7 +34,9 @@
import org.folio.circulation.domain.RequestStatus;
import org.folio.circulation.domain.RequestType;
import org.folio.circulation.domain.ServicePoint;
import org.folio.circulation.domain.configuration.PrintHoldRequestsConfiguration;
import org.folio.circulation.domain.notice.TemplateContextUtil;
import org.folio.circulation.infrastructure.storage.ConfigurationRepository;
import org.folio.circulation.infrastructure.storage.ServicePointRepository;
import org.folio.circulation.infrastructure.storage.inventory.ItemRepository;
import org.folio.circulation.infrastructure.storage.inventory.LocationRepository;
Expand Down Expand Up @@ -108,6 +109,7 @@ private void getMany(RoutingContext routingContext) {
final var servicePointRepository = new ServicePointRepository(clients);
final var patronGroupRepository = new PatronGroupRepository(clients);
final var departmentRepository = new DepartmentRepository(clients);
final var configurationRepository = new ConfigurationRepository(clients);
final UUID servicePointId = UUID.fromString(
routingContext.request().getParam(SERVICE_POINT_ID_PARAM));

Expand All @@ -118,6 +120,22 @@ private void getMany(RoutingContext routingContext) {
return;
}

configurationRepository.lookupPrintHoldRequestsEnabled().thenAccept(result -> {
if (result.succeeded()) {
PrintHoldRequestsConfiguration printHoldRequestsConfiguration = result.value();
if (printHoldRequestsConfiguration == null ||
!printHoldRequestsConfiguration.isPrintHoldRequestsEnabled()) {
context.writeResultToHttpResponse(succeeded(JsonHttpResponse.ok(new JsonObject())));
}
}
}).exceptionally(throwable -> {
log.info("getMany:: Failed to retrieve print hold requests configuration");
log.error("getMany:: Failed to retrieve print hold requests configuration: {}",
throwable.getMessage());
context.writeResultToHttpResponse(succeeded(JsonHttpResponse.ok(new JsonObject())));
return null;
});

fetchLocationsForServicePoint(servicePointId, clients)
.thenComposeAsync(r -> r.after(locations -> fetchItemsForLocations(locations,
itemRepository, LocationRepository.using(clients, servicePointRepository))))
Expand Down
33 changes: 32 additions & 1 deletion src/test/java/api/requests/StaffSlipsTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ class StaffSlipsTests extends APITests {
void responseContainsNoSlipsForNonExistentServicePointId(SlipsType slipsType) {
UUID servicePointId = servicePointsFixture.cd1().getId();
ItemResource item = itemsFixture.basedUponSmallAngryPlanet();

configurationsFixture.enablePrintHoldRequests();
requestsFixture.place(new RequestBuilder()
.withStatus(RequestStatus.OPEN_NOT_YET_FILLED.getValue())
.page()
Expand All @@ -91,6 +91,7 @@ void responseContainsNoSlipsForNonExistentServicePointId(SlipsType slipsType) {
void responseContainsNoSlipsForWrongServicePointId(SlipsType slipsType) {
UUID servicePointId = servicePointsFixture.cd1().getId();
ItemResource item = itemsFixture.basedUponSmallAngryPlanet();
configurationsFixture.enablePrintHoldRequests();

requestsFixture.place(new RequestBuilder()
.withStatus(RequestStatus.OPEN_NOT_YET_FILLED.getValue())
Expand All @@ -109,6 +110,7 @@ void responseContainsNoSlipsForWrongServicePointId(SlipsType slipsType) {
@ParameterizedTest
@EnumSource(value = SlipsType.class, mode = EnumSource.Mode.EXCLUDE, names = {"SEARCH_SLIPS"})
void responseContainsNoSlipsWhenThereAreNoItems(SlipsType slipsType) {
configurationsFixture.enablePrintHoldRequests();
UUID servicePointId = servicePointsFixture.cd1().getId();
Response response = slipsType.get(servicePointId);

Expand All @@ -121,6 +123,7 @@ void responseContainsNoSlipsWhenThereAreNoItems(SlipsType slipsType) {
void responseContainsNoPickSlipsWhenItemHasOpenRequestWithWrongStatus(SlipsType slipsType) {
UUID servicePointId = servicePointsFixture.cd1().getId();
ItemResource item = itemsFixture.basedUponSmallAngryPlanet();
configurationsFixture.enablePrintHoldRequests();

if (slipsType == SlipsType.SEARCH_SLIPS) {
checkOutFixture.checkOutByBarcode(item);
Expand All @@ -145,6 +148,7 @@ void responseContainsNoPickSlipsWhenItemHasOpenRequestWithWrongStatus(SlipsType
void responseContainsSearchSlipsForItemWithAllowedStatus(ItemStatus itemStatus) {
UUID servicePointId = servicePointsFixture.cd1().getId();
ItemResource item = itemsFixture.basedUponNod(b -> b.withStatus(itemStatus.getValue()));
configurationsFixture.enablePrintHoldRequests();

requestsFixture.place(new RequestBuilder()
.hold()
Expand Down Expand Up @@ -183,6 +187,7 @@ private static Collection<ItemStatus> getAllowedStatusesForHoldRequest() {
})
void responseContainsSlipWithAllAvailableTokens(String countryCode, String primaryAddress,
String slipsTypeName) {
configurationsFixture.enablePrintHoldRequests();
SlipsType slipsType = SlipsType.valueOf(slipsTypeName);
IndividualResource servicePoint = servicePointsFixture.cd1();
UUID servicePointId = servicePoint.getId();
Expand Down Expand Up @@ -325,6 +330,7 @@ void responseContainsSlipWithAllAvailableTokens(String countryCode, String prima

@Test
void responseContainsPickSlipsForRequestsOfTypePageOnly() {
configurationsFixture.enablePrintHoldRequests();
UUID servicePointId = servicePointsFixture.cd1().getId();
val item = itemsFixture.basedUponSmallAngryPlanet();
val james = usersFixture.james();
Expand Down Expand Up @@ -353,6 +359,28 @@ void responseContainsPickSlipsForRequestsOfTypePageOnly() {
assertResponseContains(response, SlipsType.PICK_SLIPS, item, firstRequest, james);
}

@Test
void responseReturnsNoPickSlipsWhenPrintHoldIsDisabled() {
configurationsFixture.disablePrintHoldRequests();
UUID servicePointId = servicePointsFixture.cd1().getId();
val item = itemsFixture.basedUponSmallAngryPlanet();
val james = usersFixture.james();

RequestBuilder requestBuilder = new RequestBuilder()
.withStatus(RequestStatus.OPEN_NOT_YET_FILLED.getValue())
.page()
.withPickupServicePointId(servicePointId)
.forItem(item)
.by(james);

requestsClient.create(requestBuilder);

Response response = SlipsType.PICK_SLIPS.get(servicePointId);

assertThat(response.getStatusCode(), is(HTTP_OK));
assertEquals("{}", response.getJson().encode());
}

@Disabled
@Test
void responseContainsSearchSlipsForRequestsOfTypeHoldOnly() {
Expand Down Expand Up @@ -387,6 +415,7 @@ void responseContainsSearchSlipsForRequestsOfTypeHoldOnly() {
@ParameterizedTest
@EnumSource(value = SlipsType.class, mode = EnumSource.Mode.EXCLUDE, names = {"SEARCH_SLIPS"})
void responseIncludesItemsFromDifferentLocationsForSameServicePoint(SlipsType slipsType) {
configurationsFixture.enablePrintHoldRequests();
UUID circDesk1 = servicePointsFixture.cd1().getId();

// Circ desk 1: Second floor
Expand Down Expand Up @@ -442,6 +471,7 @@ void responseIncludesItemsFromDifferentLocationsForSameServicePoint(SlipsType sl
@ParameterizedTest
@EnumSource(value = SlipsType.class, mode = EnumSource.Mode.EXCLUDE, names = {"SEARCH_SLIPS"})
void responseDoesNotIncludeSlipsFromDifferentServicePoint(SlipsType slipsType) {
configurationsFixture.enablePrintHoldRequests();
UUID circDesk1 = servicePointsFixture.cd1().getId();
UUID circDesk4 = servicePointsFixture.cd4().getId();

Expand Down Expand Up @@ -505,6 +535,7 @@ void responseDoesNotIncludeSlipsFromDifferentServicePoint(SlipsType slipsType) {
@ParameterizedTest
@EnumSource(value = SlipsType.class, mode = EnumSource.Mode.EXCLUDE, names = {"SEARCH_SLIPS"})
void responseContainsSlipsWhenServicePointHasManyLocations(SlipsType slipsType) {
configurationsFixture.enablePrintHoldRequests();
final UUID servicePointId = servicePointsFixture.cd1().getId();
final int numberOfLocations = 100;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package api.support.builders;

import io.vertx.core.json.JsonObject;
import lombok.AllArgsConstructor;
import lombok.NoArgsConstructor;
import lombok.With;

@With
@AllArgsConstructor
@NoArgsConstructor(force = true)
public class PrintHoldRequestConfigurationBuilder extends JsonBuilder implements Builder {

private final boolean printHoldRequestsEnabled;

@Override
public JsonObject create() {
JsonObject request = new JsonObject();
request.put("printHoldRequestsEnabled", printHoldRequestsEnabled);
return request;
}
}
9 changes: 9 additions & 0 deletions src/test/java/api/support/fixtures/ConfigurationExample.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import java.util.UUID;

import api.support.builders.ConfigRecordBuilder;
import api.support.builders.PrintHoldRequestConfigurationBuilder;
import api.support.builders.TlrSettingsConfigurationBuilder;
import io.vertx.core.json.JsonObject;

Expand Down Expand Up @@ -40,6 +41,14 @@ public static ConfigRecordBuilder schedulerNoticesLimitConfiguration(String limi
DEFAULT_NOTIFICATION_SCHEDULER_CONFIG_NAME, limit);
}

public static ConfigRecordBuilder setPrintHoldRequestsEnabled(boolean enabled) {
return new ConfigRecordBuilder("SETTINGS", "PRINT_HOLD_REQUESTS",
new PrintHoldRequestConfigurationBuilder()
.withPrintHoldRequestsEnabled(enabled)
.create()
.encodePrettily());
}

public static ConfigRecordBuilder tlrFeatureEnabled() {
return new ConfigRecordBuilder("SETTINGS", "TLR",
new TlrSettingsConfigurationBuilder()
Expand Down
18 changes: 18 additions & 0 deletions src/test/java/api/support/fixtures/ConfigurationsFixture.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
public class ConfigurationsFixture {
private final ResourceClient client;
private UUID tlrConfigurationEntryId = null;
private UUID printHoldRequestConfigurationEntryId = null;

public ConfigurationsFixture(ResourceClient client) {
this.client = client;
Expand All @@ -17,6 +18,16 @@ public void enableTlrFeature() {
tlrConfigurationEntryId = client.create(ConfigurationExample.tlrFeatureEnabled()).getId();
}

public void enablePrintHoldRequests() {
deletePrintHoldRequestConfig();
printHoldRequestConfigurationEntryId = client.create(ConfigurationExample.setPrintHoldRequestsEnabled(true)).getId();
}

public void disablePrintHoldRequests() {
deletePrintHoldRequestConfig();
printHoldRequestConfigurationEntryId = client.create(ConfigurationExample.setPrintHoldRequestsEnabled(false)).getId();
}

public void disableTlrFeature() {
deleteTlrFeatureConfig();
tlrConfigurationEntryId = client.create(ConfigurationExample.tlrFeatureDisabled()).getId();
Expand All @@ -29,6 +40,13 @@ public void deleteTlrFeatureConfig() {
}
}

public void deletePrintHoldRequestConfig() {
if (printHoldRequestConfigurationEntryId != null) {
client.delete(printHoldRequestConfigurationEntryId);
printHoldRequestConfigurationEntryId = null;
}
}

public void configureTlrFeature(boolean isTlrFeatureEnabled, boolean tlrHoldShouldFollowCirculationRules,
UUID confirmationTemplateId, UUID cancellationTemplateId, UUID expirationTemplateId) {

Expand Down