-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
MODBULKOPS-43 - FQM Integration - FQM Results handling
- Loading branch information
1 parent
0b76a12
commit 93c07c6
Showing
20 changed files
with
564 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 14 additions & 0 deletions
14
src/main/java/org/folio/bulkops/client/EntityTypeClient.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package org.folio.bulkops.client; | ||
|
||
import org.folio.querytool.domain.dto.EntityType; | ||
import org.springframework.cloud.openfeign.FeignClient; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.RequestHeader; | ||
|
||
import java.util.UUID; | ||
|
||
@FeignClient(name = "entity-types") | ||
public interface EntityTypeClient { | ||
@GetMapping("/{entityTypeId}") | ||
EntityType getEntityType(@RequestHeader UUID entityTypeId); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package org.folio.bulkops.client; | ||
|
||
import org.folio.querytool.domain.dto.QueryDetails; | ||
import org.folio.querytool.domain.dto.QueryIdentifier; | ||
import org.folio.querytool.domain.dto.SubmitQuery; | ||
import org.springframework.cloud.openfeign.FeignClient; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.PostMapping; | ||
import org.springframework.web.bind.annotation.RequestBody; | ||
import org.springframework.web.bind.annotation.RequestHeader; | ||
import org.springframework.web.bind.annotation.RequestParam; | ||
|
||
import java.util.List; | ||
import java.util.UUID; | ||
|
||
@FeignClient(name = "query") | ||
public interface QueryClient { | ||
|
||
@PostMapping("") | ||
QueryIdentifier executeQuery(@RequestBody SubmitQuery submitQuery); | ||
|
||
@GetMapping("/{queryId}") | ||
QueryDetails getQuery(@RequestHeader UUID queryId); | ||
|
||
@GetMapping("/{queryId}/sortedIds") | ||
List<UUID> getSortedIds(@RequestHeader UUID queryId, @RequestParam Integer offset, @RequestParam Integer limit); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
23 changes: 23 additions & 0 deletions
23
src/main/java/org/folio/bulkops/service/EntityTypeService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package org.folio.bulkops.service; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
import org.folio.bulkops.client.EntityTypeClient; | ||
import org.folio.bulkops.domain.dto.EntityType; | ||
import org.springframework.stereotype.Service; | ||
|
||
import java.util.UUID; | ||
|
||
@Service | ||
@RequiredArgsConstructor | ||
public class EntityTypeService { | ||
private final EntityTypeClient entityTypeClient; | ||
|
||
public EntityType getEntityTypeById(UUID entityTypeId) { | ||
var alias = entityTypeClient.getEntityType(entityTypeId).getLabelAlias(); | ||
return switch (alias) { | ||
case "Items" -> EntityType.ITEM; | ||
case "Users" -> EntityType.USER; | ||
default -> throw new IllegalArgumentException(String.format("Entity type %s is not supported", alias)); | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
package org.folio.bulkops.service; | ||
|
||
import static org.folio.bulkops.domain.dto.OperationStatusType.CANCELLED; | ||
import static org.folio.bulkops.domain.dto.OperationStatusType.FAILED; | ||
import static org.folio.bulkops.domain.dto.OperationStatusType.RETRIEVING_IDENTIFIERS; | ||
import static org.folio.bulkops.domain.dto.OperationStatusType.SAVED_IDENTIFIERS; | ||
import static org.folio.bulkops.util.Constants.NEW_LINE_SEPARATOR; | ||
import static org.folio.spring.scope.FolioExecutionScopeExecutionContextManager.getRunnableWithCurrentFolioContext; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.log4j.Log4j2; | ||
import org.folio.bulkops.client.QueryClient; | ||
import org.folio.bulkops.client.RemoteFileSystemClient; | ||
import org.folio.bulkops.domain.entity.BulkOperation; | ||
import org.folio.bulkops.repository.BulkOperationRepository; | ||
import org.folio.querytool.domain.dto.SubmitQuery; | ||
import org.springframework.stereotype.Service; | ||
|
||
import java.io.ByteArrayInputStream; | ||
import java.time.LocalDateTime; | ||
import java.util.UUID; | ||
import java.util.concurrent.ExecutorService; | ||
import java.util.concurrent.Executors; | ||
import java.util.stream.Collectors; | ||
|
||
@Service | ||
@Log4j2 | ||
@RequiredArgsConstructor | ||
public class QueryService { | ||
public static final String QUERY_FILENAME_TEMPLATE = "%1$s/Query-%1$s.csv"; | ||
|
||
private final QueryClient queryClient; | ||
private final BulkOperationRepository bulkOperationRepository; | ||
private final RemoteFileSystemClient remoteFileSystemClient; | ||
|
||
private final ExecutorService executor = Executors.newCachedThreadPool(); | ||
|
||
public UUID executeQuery(SubmitQuery submitQuery) { | ||
return queryClient.executeQuery(submitQuery).getQueryId(); | ||
} | ||
|
||
public BulkOperation checkQueryExecutionStatus(BulkOperation bulkOperation) { | ||
var queryResult = queryClient.getQuery(bulkOperation.getFqlQueryId()); | ||
return switch (queryResult.getStatus()) { | ||
case SUCCESS -> { | ||
if (queryResult.getTotalRecords() == 0) { | ||
yield failBulkOperation(bulkOperation, "No records found for the query"); | ||
} | ||
executor.execute(getRunnableWithCurrentFolioContext(() -> saveIdentifiers(bulkOperation))); | ||
bulkOperation.setStatus(RETRIEVING_IDENTIFIERS); | ||
yield bulkOperationRepository.save(bulkOperation); | ||
} | ||
case FAILED -> failBulkOperation(bulkOperation, queryResult.getFailureReason()); | ||
case CANCELLED -> cancelBulkOperation((bulkOperation)); | ||
case IN_PROGRESS -> bulkOperation; | ||
}; | ||
} | ||
|
||
private void saveIdentifiers(BulkOperation bulkOperation) { | ||
try { | ||
var identifiersString = queryClient.getSortedIds(bulkOperation.getFqlQueryId(), 0, Integer.MAX_VALUE).stream() | ||
.map(UUID::toString) | ||
.collect(Collectors.joining(NEW_LINE_SEPARATOR)); | ||
var path = String.format(QUERY_FILENAME_TEMPLATE, bulkOperation.getId()); | ||
remoteFileSystemClient.put(new ByteArrayInputStream(identifiersString.getBytes()), path); | ||
bulkOperation.setLinkToTriggeringCsvFile(path); | ||
bulkOperation.setStatus(SAVED_IDENTIFIERS); | ||
bulkOperationRepository.save(bulkOperation); | ||
} catch (Exception e) { | ||
var errorMessage = "Failed to save identifiers, reason: " + e.getMessage(); | ||
log.error(errorMessage); | ||
failBulkOperation(bulkOperation, errorMessage); | ||
} | ||
} | ||
|
||
private BulkOperation failBulkOperation(BulkOperation bulkOperation, String errorMessage) { | ||
bulkOperation.setStatus(FAILED); | ||
bulkOperation.setErrorMessage(errorMessage); | ||
bulkOperation.setEndTime(LocalDateTime.now()); | ||
return bulkOperationRepository.save(bulkOperation); | ||
} | ||
|
||
private BulkOperation cancelBulkOperation(BulkOperation bulkOperation) { | ||
bulkOperation.setStatus(CANCELLED); | ||
bulkOperation.setErrorMessage("Query execution was cancelled"); | ||
bulkOperation.setEndTime(LocalDateTime.now()); | ||
return bulkOperationRepository.save(bulkOperation); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 6 additions & 0 deletions
6
src/main/resources/db/changelog/changes/26-01-2024_add_query_fields_and_statuses.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
ALTER TABLE bulk_operation ADD COLUMN IF NOT EXISTS fql_query_id UUID; | ||
ALTER TABLE bulk_operation ADD COLUMN IF NOT EXISTS fql_query TEXT; | ||
|
||
ALTER TYPE OperationStatusType ADD VALUE IF NOT EXISTS 'EXECUTING_QUERY'; | ||
ALTER TYPE OperationStatusType ADD VALUE IF NOT EXISTS 'RETRIEVING_IDENTIFIERS'; | ||
ALTER TYPE OperationStatusType ADD VALUE IF NOT EXISTS 'SAVED_IDENTIFIERS'; |
Oops, something went wrong.