Skip to content

Commit

Permalink
Merge pull request #187 from NyashaMuusha/JMPI-add-matching-rule-to-a…
Browse files Browse the repository at this point in the history
…udit-trail

Jmpi add matching rule to audit trail
  • Loading branch information
NyashaMuusha authored Mar 14, 2024
2 parents b5183ea + 80dfe79 commit bd32f54
Show file tree
Hide file tree
Showing 19 changed files with 274 additions and 144 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@ CREATE TABLE IF NOT EXISTS audit_trail (
id UUID NOT NULL DEFAULT gen_random_uuid(),
insertedAt TIMESTAMP NOT NULL DEFAULT now(),
createdAt TIMESTAMP NOT NULL,
interactionID VARCHAR(64),
goldenID VARCHAR(64),
event VARCHAR(256),
eventType VARCHAR(256),
eventData JSONB,
CONSTRAINT PKEY_AUDIT_TRAIL PRIMARY KEY (id)
);
CREATE INDEX IF NOT EXISTS idx_gid ON audit_trail(goldenID);
CREATE INDEX IF NOT EXISTS idx_iid ON audit_trail(interactionID);
CREATE INDEX IF NOT EXISTS idx_eventdata ON audit_trail USING GIN (eventData jsonb_path_ops);
CREATE INDEX IF NOT EXISTS idx_eventtype ON audit_trail(eventType);
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import org.jembi.jempi.shared.models.AuditEvent;

import java.sql.SQLException;
import java.sql.Types;
import java.util.Locale;

import static org.jembi.jempi.shared.models.GlobalConstants.PSQL_TABLE_AUDIT_TRAIL;
Expand Down Expand Up @@ -53,17 +54,17 @@ CONSTRAINT PKEY_AUDIT_TRAIL PRIMARY KEY (id)
*/
}

void addAuditEvent(final AuditEvent event) {
void addAuditEvent(final AuditEvent auditEvent) {
psqlClient.connect(AppConfig.POSTGRESQL_AUDIT_DB);

try (var preparedStatement = psqlClient.prepareStatement(String.format(Locale.ROOT, """
INSERT INTO %s (createdAt, interactionID, goldenID, event)
VALUES (?, ?, ?, ?);
INSERT INTO %s (createdAt, eventType, eventData)
VALUES (?, ?, ?::json);
""", PSQL_TABLE_AUDIT_TRAIL)
.stripIndent())) {
preparedStatement.setTimestamp(1, event.createdAt());
preparedStatement.setString(2, event.interactionID());
preparedStatement.setString(3, event.goldenID());
preparedStatement.setString(4, event.event());
.stripIndent())) {
preparedStatement.setTimestamp(1, auditEvent.createdAt());
preparedStatement.setString(2, auditEvent.eventType().name());
preparedStatement.setObject(3, auditEvent.eventData(), Types.OTHER);
preparedStatement.executeUpdate();
} catch (SQLException e) {
LOGGER.error(e.getLocalizedMessage(), e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -505,7 +505,7 @@ public record GetGoldenRecordAuditTrailRequest(
String uid) implements Event {
}

public record GetGoldenRecordAuditTrailResponse(List<AuditEvent> auditTrail) {
public record GetGoldenRecordAuditTrailResponse(List<ApiModels.ApiAuditTrail.LinkingAuditEntry> auditTrail) {
}

public record GetInteractionAuditTrailRequest(
Expand All @@ -519,7 +519,7 @@ public record SQLDashboardDataRequest(
ActorRef<SQLDashboardDataResponse> replyTo) implements Event {
}

public record GetInteractionAuditTrailResponse(List<AuditEvent> auditTrail) {
public record GetInteractionAuditTrailResponse(List<ApiModels.ApiAuditTrail.LinkingAuditEntry> auditTrail) {
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.jembi.jempi.shared.models.AuditEvent;
import org.jembi.jempi.shared.models.ApiModels;
import org.jembi.jempi.shared.models.GlobalConstants;
import org.jembi.jempi.shared.models.LinkingAuditEventData;
import org.jembi.jempi.shared.utils.AuditTrailBridge;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.*;

import static org.jembi.jempi.shared.models.GlobalConstants.PSQL_TABLE_AUDIT_TRAIL;

Expand All @@ -25,49 +26,67 @@ final class PsqlAuditTrail {
psqlClient = new PsqlClient(pgServer, pgPort, pgDatabase, pgUser, pgPassword);
}

List<AuditEvent> goldenRecordAuditTrail(final String uid) {
List<ApiModels.ApiAuditTrail.LinkingAuditEntry> goldenRecordAuditTrail(final String uid) {
psqlClient.connect();
final var list = new ArrayList<AuditEvent>();
final var list = new ArrayList<ApiModels.ApiAuditTrail.LinkingAuditEntry>();
try (PreparedStatement preparedStatement = psqlClient.prepareStatement(String.format(Locale.ROOT,
"""
SELECT * FROM %s where goldenID = ?;
""",
PSQL_TABLE_AUDIT_TRAIL)
.stripIndent())) {
preparedStatement.setString(1, uid);
"SELECT * FROM %s WHERE eventType = ? AND eventData ->> 'goldenID' = ?",
PSQL_TABLE_AUDIT_TRAIL))) {
preparedStatement.setString(1, GlobalConstants.AuditEventType.LINKING_EVENT.name());
preparedStatement.setString(2, uid);
ResultSet rs = preparedStatement.executeQuery();
while (rs.next()) {
final var insertedAt = rs.getTimestamp(2);
final var createdAt = rs.getTimestamp(3);
final var interactionID = rs.getString(4);
final var goldenID = rs.getString(5);
final var event = rs.getString(6);
list.add(new AuditEvent(createdAt, insertedAt, interactionID, goldenID, event));
final var insertTime = rs.getString(2);
final var createdTime = rs.getString(3);
final var eventType = rs.getString(4);
final var eventData = rs.getString(5);
if (Objects.equals(eventType, GlobalConstants.AuditEventType.LINKING_EVENT.name())) {
LinkingAuditEventData deserializeEventData = AuditTrailBridge.getDeserializeEventData(eventData, LinkingAuditEventData.class);
list.add(new ApiModels.ApiAuditTrail.LinkingAuditEntry(
insertTime,
createdTime,
deserializeEventData.interaction_id(),
deserializeEventData.goldenID(),
deserializeEventData.message(),
deserializeEventData.score(),
deserializeEventData.linkingRule().name()
));
}
}
} catch (Exception e) {
LOGGER.error(e);
}
return list;
}

List<AuditEvent> interactionRecordAuditTrail(final String uid) {
List<ApiModels.ApiAuditTrail.LinkingAuditEntry> interactionRecordAuditTrail(final String uid) {
psqlClient.connect();
final var list = new ArrayList<AuditEvent>();
try (PreparedStatement preparedStatement = psqlClient.prepareStatement(String.format(Locale.ROOT,
"""
SELECT * FROM %s where interactionID = ?;
""",
PSQL_TABLE_AUDIT_TRAIL)
.stripIndent())) {
preparedStatement.setString(1, uid);
final var list = new ArrayList<ApiModels.ApiAuditTrail.LinkingAuditEntry>();
try (PreparedStatement preparedStatement = psqlClient.prepareStatement(String.format(
Locale.ROOT,
"SELECT * FROM %s WHERE eventType = ? AND eventData ->> 'interaction_id' = ?",
PSQL_TABLE_AUDIT_TRAIL))) {
preparedStatement.setString(1, GlobalConstants.AuditEventType.LINKING_EVENT.name());
preparedStatement.setString(2, uid);
ResultSet rs = preparedStatement.executeQuery();
while (rs.next()) {
final var insertedAt = rs.getTimestamp(2);
final var createdAt = rs.getTimestamp(3);
final var interactionID = rs.getString(4);
final var goldenID = rs.getString(5);
final var event = rs.getString(6);
list.add(new AuditEvent(createdAt, insertedAt, interactionID, goldenID, event));
final var insertTime = rs.getString(2);
final var createdTime = rs.getString(3);
final var eventType = rs.getString(4);
final var eventData = rs.getString(5);

if (Objects.equals(eventType, GlobalConstants.AuditEventType.LINKING_EVENT.name())) {
LinkingAuditEventData deserializeEventData = AuditTrailBridge.getDeserializeEventData(eventData, LinkingAuditEventData.class);
list.add(new ApiModels.ApiAuditTrail.LinkingAuditEntry(
insertTime,
createdTime,
deserializeEventData.interaction_id(),
deserializeEventData.goldenID(),
deserializeEventData.message(),
deserializeEventData.score(),
deserializeEventData.linkingRule().name()
));
}
}
} catch (Exception e) {
LOGGER.error(e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,33 +111,24 @@ public static Route getGoldenRecordAuditTrail(
final ActorRef<BackEnd.Event> backEnd) {
return parameter("gid",
uid -> onComplete(Ask.getGoldenRecordAuditTrail(actorSystem, backEnd, uid),
result -> {
if (!result.isSuccess()) {
LOGGER.warn("IM_A_TEAPOT");
}
return result.isSuccess()
? complete(StatusCodes.OK,
ApiModels.ApiAuditTrail.fromAuditTrail(result.get().auditTrail()),
JSON_MARSHALLER)
: complete(ApiModels.getHttpErrorResponse(GlobalConstants.IM_A_TEA_POT));
}));
result -> result.isSuccess()
? complete(StatusCodes.OK,
result.get().auditTrail(),
JSON_MARSHALLER)
: complete(ApiModels.getHttpErrorResponse(StatusCodes.IM_A_TEAPOT))));

}

public static Route getInteractionAuditTrail(
final ActorSystem<Void> actorSystem,
final ActorRef<BackEnd.Event> backEnd) {
return parameter("iid",
uid -> onComplete(Ask.getInteractionAuditTrail(actorSystem, backEnd, uid),
result -> {
if (!result.isSuccess()) {
LOGGER.warn("IM_A_TEAPOT");
}
return result.isSuccess()
? complete(StatusCodes.OK,
ApiModels.ApiAuditTrail.fromAuditTrail(result.get().auditTrail()),
JSON_MARSHALLER)
: complete(ApiModels.getHttpErrorResponse(GlobalConstants.IM_A_TEA_POT));
}));
result -> result.isSuccess()
? complete(StatusCodes.OK,
result.get().auditTrail(),
JSON_MARSHALLER)
: complete(ApiModels.getHttpErrorResponse(StatusCodes.IM_A_TEAPOT))));
}

public static Route patchIidNewGidLink(
Expand Down
Loading

0 comments on commit bd32f54

Please sign in to comment.