-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' into MODINVOICE-569
- Loading branch information
Showing
31 changed files
with
914 additions
and
95 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
Submodule acq-models
updated
14 files
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,20 @@ | ||
#%RAML 1.0 | ||
title: Audit outbox API | ||
version: v1.0 | ||
protocols: [ HTTP, HTTPS ] | ||
baseUri: http://github.com/folio-org/mod-invoice-storage | ||
|
||
documentation: | ||
- title: Audit outbox API | ||
content: This API is intended for internal use only by the Timer interface | ||
|
||
types: | ||
outbox-event-log: !include acq-models/mod-invoice-storage/schemas/outbox_event_log.json | ||
invoice-audit-event: !include acq-models/mod-invoice-storage/schemas/invoice_audit_event.json | ||
invoice-line-audit-event: !include acq-models/mod-invoice-storage/schemas/invoice_line_audit_event.json | ||
event-topic: !include acq-models/mod-invoice-storage/schemas/event_topic.json | ||
|
||
/invoice-storage/audit-outbox: | ||
/process: | ||
post: | ||
description: Read audit events from DB and send them to Kafka |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package org.folio.config; | ||
|
||
import org.folio.kafka.KafkaConfig; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
|
||
@Configuration | ||
public class KafkaConfiguration { | ||
|
||
@Value("${KAFKA_HOST:kafka}") | ||
private String kafkaHost; | ||
@Value("${KAFKA_PORT:9092}") | ||
private String kafkaPort; | ||
@Value("${OKAPI_URL:http://okapi:9130}") | ||
private String okapiUrl; | ||
@Value("${REPLICATION_FACTOR:1}") | ||
private int replicationFactor; | ||
@Value("${MAX_REQUEST_SIZE:1048576}") | ||
private int maxRequestSize; | ||
@Value("${ENV:folio}") | ||
private String envId; | ||
|
||
@Bean | ||
public KafkaConfig kafkaConfig() { | ||
return KafkaConfig.builder() | ||
.envId(envId) | ||
.kafkaHost(kafkaHost) | ||
.kafkaPort(kafkaPort) | ||
.okapiUrl(okapiUrl) | ||
.replicationFactor(replicationFactor) | ||
.maxRequestSize(maxRequestSize) | ||
.build(); | ||
} | ||
|
||
} |
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.dao; | ||
|
||
import static javax.ws.rs.core.Response.Status.NOT_FOUND; | ||
import static org.folio.rest.persist.PostgresClient.convertToPsqlStandard; | ||
|
||
import io.vertx.core.Future; | ||
import io.vertx.ext.web.handler.HttpException; | ||
import io.vertx.sqlclient.Row; | ||
import io.vertx.sqlclient.RowSet; | ||
|
||
public class DbUtils { | ||
|
||
private static final String TABLE_NAME_TEMPLATE = "%s.%s"; | ||
|
||
public static String getTenantTableName(String tenantId, String tableName) { | ||
return TABLE_NAME_TEMPLATE.formatted(convertToPsqlStandard(tenantId), tableName); | ||
} | ||
|
||
public static Future<Void> verifyEntityUpdate(RowSet<Row> updated) { | ||
return updated.rowCount() == 1 | ||
? Future.succeededFuture() | ||
: Future.failedFuture(new HttpException(NOT_FOUND.getStatusCode(), NOT_FOUND.getReasonPhrase())); | ||
} | ||
|
||
private DbUtils() {} | ||
|
||
} |
18 changes: 18 additions & 0 deletions
18
src/main/java/org/folio/dao/audit/AuditOutboxEventLogDAO.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,18 @@ | ||
package org.folio.dao.audit; | ||
|
||
import java.util.List; | ||
|
||
import org.folio.rest.jaxrs.model.OutboxEventLog; | ||
import org.folio.rest.persist.Conn; | ||
|
||
import io.vertx.core.Future; | ||
|
||
public interface AuditOutboxEventLogDAO { | ||
|
||
Future<List<OutboxEventLog>> getEventLogs(Conn conn, String tenantId); | ||
|
||
Future<Void> saveEventLog(Conn conn, OutboxEventLog eventLog, String tenantId); | ||
|
||
Future<Integer> deleteEventLogs(Conn conn, List<String> eventIds, String tenantId); | ||
|
||
} |
84 changes: 84 additions & 0 deletions
84
src/main/java/org/folio/dao/audit/AuditOutboxEventLogPostgresDAO.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,84 @@ | ||
package org.folio.dao.audit; | ||
|
||
import static org.folio.dao.DbUtils.getTenantTableName; | ||
|
||
import java.util.List; | ||
import java.util.UUID; | ||
|
||
import org.folio.rest.jaxrs.model.OutboxEventLog; | ||
import org.folio.rest.persist.Conn; | ||
import org.folio.service.util.OutboxEventFields; | ||
|
||
import io.vertx.core.Future; | ||
import io.vertx.sqlclient.Row; | ||
import io.vertx.sqlclient.SqlResult; | ||
import io.vertx.sqlclient.Tuple; | ||
import lombok.extern.log4j.Log4j2; | ||
import one.util.streamex.StreamEx; | ||
|
||
@Log4j2 | ||
public class AuditOutboxEventLogPostgresDAO implements AuditOutboxEventLogDAO { | ||
|
||
public static final String OUTBOX_TABLE_NAME = "outbox_event_log"; | ||
private static final String SELECT_SQL = "SELECT * FROM %s LIMIT 1000"; | ||
private static final String INSERT_SQL = "INSERT INTO %s (event_id, entity_type, action, payload) VALUES ($1, $2, $3, $4)"; | ||
private static final String BATCH_DELETE_SQL = "DELETE from %s where event_id = ANY ($1)"; | ||
|
||
/** | ||
* Get all event logs from outbox table. | ||
* | ||
* @param conn the sql connection from transaction | ||
* @param tenantId the tenant id | ||
* @return future of a list of fetched event logs | ||
*/ | ||
public Future<List<OutboxEventLog>> getEventLogs(Conn conn, String tenantId) { | ||
log.trace("getEventLogs:: Fetching event logs from outbox table for tenantId: '{}'", tenantId); | ||
var tableName = getTenantTableName(tenantId, OUTBOX_TABLE_NAME); | ||
return conn.execute(SELECT_SQL.formatted(tableName)) | ||
.map(rows -> StreamEx.of(rows.iterator()).map(this::convertDbRowToOutboxEventLog).toList()) | ||
.onFailure(t -> log.warn("getEventLogs:: Failed to fetch event logs for tenantId: '{}'", tenantId, t)); | ||
} | ||
|
||
/** | ||
* Saves event log to outbox table. | ||
* | ||
* @param conn the sql connection from transaction | ||
* @param eventLog the event log to save | ||
* @param tenantId the tenant id | ||
* @return future of id of the inserted entity | ||
*/ | ||
public Future<Void> saveEventLog(Conn conn, OutboxEventLog eventLog, String tenantId) { | ||
log.debug("saveEventLog:: Saving event log to outbox table with eventId: '{}'", eventLog.getEventId()); | ||
var tableName = getTenantTableName(tenantId, OUTBOX_TABLE_NAME); | ||
Tuple params = Tuple.of(eventLog.getEventId(), eventLog.getEntityType().value(), eventLog.getAction(), eventLog.getPayload()); | ||
return conn.execute(INSERT_SQL.formatted(tableName), params) | ||
.onFailure(t -> log.warn("saveEventLog:: Failed to save event log with id: '{}'", eventLog.getEventId(), t)) | ||
.mapEmpty(); | ||
} | ||
|
||
/** | ||
* Deletes outbox logs by event ids in batch. | ||
* | ||
* @param conn the sql connection from transaction | ||
* @param eventIds the event ids to delete | ||
* @param tenantId the tenant id | ||
* @return future of row count for deleted records | ||
*/ | ||
public Future<Integer> deleteEventLogs(Conn conn, List<String> eventIds, String tenantId) { | ||
log.debug("deleteEventLogs:: Deleting outbox logs by event ids in batch: '{}'", eventIds); | ||
var tableName = getTenantTableName(tenantId, OUTBOX_TABLE_NAME); | ||
var param = eventIds.stream().map(UUID::fromString).toArray(UUID[]::new); | ||
return conn.execute(BATCH_DELETE_SQL.formatted(tableName), Tuple.of(param)) | ||
.map(SqlResult::rowCount) | ||
.onFailure(t -> log.warn("deleteEventLogs: Failed to delete event logs by ids: '{}'", eventIds, t)); | ||
} | ||
|
||
private OutboxEventLog convertDbRowToOutboxEventLog(Row row) { | ||
return new OutboxEventLog() | ||
.withEventId(row.getUUID(OutboxEventFields.EVENT_ID.getName()).toString()) | ||
.withEntityType(OutboxEventLog.EntityType.fromValue(row.getString(OutboxEventFields.ENTITY_TYPE.getName()))) | ||
.withAction(row.getString(OutboxEventFields.ACTION.getName())) | ||
.withPayload(row.getString(OutboxEventFields.PAYLOAD.getName())); | ||
} | ||
|
||
} |
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
Oops, something went wrong.