Skip to content

Commit

Permalink
refs : #18643 : Added Cloneable features for Initialization Demo-POC
Browse files Browse the repository at this point in the history
  • Loading branch information
remiges-deepak committed May 29, 2024
1 parent bfcd8e5 commit bad7365
Show file tree
Hide file tree
Showing 3 changed files with 175 additions and 72 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import com.remiges.logharbour.model.LogharbourRequestBo;
import com.remiges.logharbour.model.LoginUser;
import com.remiges.logharbour.util.LHLogger;
import com.remiges.logharbour.util.LogHarbour;

@RestController
public class LHLoggerController {
Expand Down Expand Up @@ -146,10 +147,10 @@ public enum LogPri_t {
@PostMapping("/activity-log")
public String postActivityLogs() throws JsonProcessingException {

LoginUser loginUser = new LoginUser("1", "Test", "7977754045");
LoginUser loginUser = new LoginUser("2", "Suraj", "948577548");
LoggerContext loggerContext = new LoggerContext(LogPriorityLevels.INFO);

logHarbour.setLogDetails("Kra", "Linux System", "Adhaar Kyc Module", LogPriority.INFO, "Kra User",
logHarbour.setLogDetails("Kra", "Linux System", "Adhaar Kyc Module", LogPriority.INFO, "User1",
"Insert", LHLogger.class.getName().toString(), "Instance Id", Status.SUCCESS, "", "IP:127.0.2.1",
loggerContext);

Expand All @@ -161,19 +162,19 @@ public String postActivityLogs() throws JsonProcessingException {
@PostMapping("/changes-log")
public String postChangeLogs() throws JsonProcessingException {

LoginUser loginUser = new LoginUser("1", "Test", "7977754045");
LoginUser loginUser = new LoginUser("2", "Suraj", "948577548");

ChangeInfo changeInfo = new ChangeInfo();
changeInfo.setEntity(loginUser.getName());
changeInfo.setOp("Updating a User Details");
changeInfo.setOp("Updating userId");

List<ChangeDetails> changeDetails = new ArrayList<>();
changeDetails.add(new ChangeDetails("Mobile", loginUser.getMobile(), "7977712312"));
changeDetails.add(new ChangeDetails("id", loginUser.getId(), "12"));
changeInfo.setChanges(changeDetails);

LoggerContext loggerContext = new LoggerContext(LogPriorityLevels.INFO);

logHarbour.setLogDetails("Kra", "Linux System", "Adhaar Kyc Module", LogPriority.INFO, "Kra User",
logHarbour.setLogDetails("Kra", "Linux System", "Adhaar Kyc Module", LogPriority.INFO, "User1",
"Insert", LHLogger.class.getName().toString(), "Instance Id", Status.SUCCESS, "", "IP:127.0.2.1",
loggerContext);
logHarbour.logDataChange("Log Data change", changeInfo);
Expand All @@ -197,4 +198,34 @@ public String postDebugLogs() throws JsonProcessingException {
return "Debug Data log posted Successfully";
}

// working on cloning ----POC----
LoggerContext loggerContext = new LoggerContext(LogPriorityLevels.INFO);
LogHarbour l1 = new LogHarbour("Kra", "Linux", "Aadhar KYC module", LogPriority.INFO, "User1", "Insert",
LHLogger.class.getName().toString(), "Instance Id", Status.SUCCESS, " ", "IP:127.0.2.1", loggerContext);

@PostMapping("/clone-log")
public String activityLogs() throws JsonProcessingException {

LoginUser loginUser = new LoginUser("2", "Suraj", "948577548");

// logHarbour.setLogDetails("Kra", "Linux System", "Adhaar Kyc Module",
// LogPriority.INFO, "User1",
// "Insert", LHLogger.class.getName().toString(), "Instance Id", Status.SUCCESS,
// "", "IP:127.0.2.1",
// loggerContext);

LogHarbour clonedL1 = l1.clone();

System.out.println("Cloning Objects -----------------------------------------" + clonedL1);

clonedL1.setApp("Faiyaz");

System.out.println("Cloning Objects -----------------------------------------" + clonedL1);
System.out.println("Original Objects -----------------------------------------" + l1);

// logHarbour.logActivity("Log Activitiy Test", loginUser);
return "Activity Data log posted Successfully";

}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package com.remiges.logharbour.util;

import com.remiges.logharbour.model.LogEntry.LogPriority;
import com.remiges.logharbour.model.LogEntry.Status;

import lombok.Data;

import com.remiges.logharbour.model.LoggerContext;

@Data
public class LogHarbour implements Cloneable {

private Params params;

public LogHarbour(String app, String system, String module, LogPriority pri, String who, String op, String clazz,
String instanceId, Status status, String error, String remoteIP, LoggerContext loggerContext) {
this.params = new Params(app, system, module, pri, who, op, clazz, instanceId, status, error, remoteIP,
loggerContext);
}

// Override the clone method
@Override
public LogHarbour clone() {
try {
LogHarbour cloned = (LogHarbour) super.clone();
cloned.params = this.params; // Share the same Params object
return cloned;
} catch (CloneNotSupportedException e) {
throw new InternalError(e);
}
}

public void setApp(String app) {
this.params.setApp(app);
}

@Data
private static class Params {
private String app;
private String system;
private String module;
private LogPriority pri;
private String who;
private String op;
private String clazz;
private String instanceId;
private Status status;
private String error;
private String remoteIP;
private LoggerContext loggerContext;

public Params(String app, String system, String module, LogPriority pri, String who, String op, String clazz,
String instanceId, Status status, String error, String remoteIP, LoggerContext loggerContext) {
this.app = app;
this.system = system;
this.module = module;
this.pri = pri;
this.who = who;
this.op = op;
this.clazz = clazz;
this.instanceId = instanceId;
this.status = status;
this.error = error;
this.remoteIP = remoteIP;
this.loggerContext = loggerContext;
}

@Override
public String toString() {
return "Params [app=" + app + ", system=" + system + ", module=" + module + ", pri=" + pri + ", who=" + who
+ ", op=" + op + ", clazz=" + clazz + ", instanceId=" + instanceId + ", status=" + status
+ ", error=" + error + ", remoteIP=" + remoteIP + ", loggerContext=" + loggerContext + "]";
}

}
}
128 changes: 62 additions & 66 deletions logharbour/src/main/java/com/remiges/logharbour/util/LHLogger.java
Original file line number Diff line number Diff line change
Expand Up @@ -326,11 +326,6 @@ public List<LogEntry> getChanges(String queryToken, String app, String who, Stri
logs = logEntryRepository.findByAppAndClassNameAndInstanceIdAndLogType(app, className, instance, logType);
}

if (who != null && !who.isEmpty()) {
logs = logEntryRepository.findLogEntries(app, className, instance, who, Instant.EPOCH.toString(),
Instant.now().toString());
}

if (field != null && !field.isEmpty()) {
logs = logs.stream()
.filter(log -> {
Expand Down Expand Up @@ -376,80 +371,81 @@ public List<LogEntry> getChanges(String queryToken, String app, String who, Stri
* @throws Exception If an error occurs during log retrieval or processing.
*/

private static final int LOGHARBOUR_GETLOGS_MAXREC = 5;
private static final int LOGHARBOUR_GETLOGS_MAXREC = 5;

public GetLogsResponse getLogs(String queryToken, String app, String who, String className, String instance,
String op, String fromtsStr, String totsStr, int ndays, String logType,
String remoteIP, LogEntry.LogPriority pri, String searchAfterTs,
String searchAfterDocId) throws Exception {


public GetLogsResponse getLogs(String queryToken, String app, String who, String className, String instance,
String op, String fromtsStr, String totsStr, int ndays, String logType,
String remoteIP, LogEntry.LogPriority pri, String searchAfterTs,
String searchAfterDocId) throws Exception {

Instant fromts = null;
Instant tots = null;
//String priValue = pri != null ? pri.toString() : null;
// Parse the timestamps
try {
if (fromtsStr != null && !fromtsStr.isEmpty()) {
fromts = Instant.parse(fromtsStr);
}
if (totsStr != null && !totsStr.isEmpty()) {
tots = Instant.parse(totsStr);
Instant fromts = null;
Instant tots = null;
// String priValue = pri != null ? pri.toString() : null;
// Parse the timestamps
try {
if (fromtsStr != null && !fromtsStr.isEmpty()) {
fromts = Instant.parse(fromtsStr);
}
if (totsStr != null && !totsStr.isEmpty()) {
tots = Instant.parse(totsStr);
}
} catch (DateTimeParseException e) {
throw new IllegalArgumentException(
"Invalid timestamp format. Please provide timestamps in ISO 8601 format.");
}
} catch (DateTimeParseException e) {
throw new IllegalArgumentException(
"Invalid timestamp format. Please provide timestamps in ISO 8601 format.");
}

// Validate timestamp range
if (fromts != null && tots != null && fromts.isAfter(tots)) {
throw new IllegalArgumentException("fromts must be before tots");
}

List<LogEntry> combinedLogs;
if (logType == null) {
combinedLogs = logEntryRepository.findChangeLogs(fromtsStr, totsStr, who, pri.toString(), remoteIP, op);
combinedLogs.addAll(logEntryRepository.findActivityLogs(fromtsStr, totsStr, who, pri.toString(), remoteIP, op));
} else {
switch (logType) {
case "A":
combinedLogs = logEntryRepository.findActivityLogs(fromtsStr, totsStr, who, pri.toString(), remoteIP, op);
break;
case "C":
combinedLogs = logEntryRepository.findChangeLogs(fromtsStr, totsStr, who, pri.toString(), remoteIP, op);
break;
default:
combinedLogs = logEntryRepository.findChangeLogs(fromtsStr, totsStr, who, pri.toString(), remoteIP, op);
combinedLogs.addAll(logEntryRepository.findActivityLogs(fromtsStr, totsStr, who, pri.toString(), remoteIP, op));
break;
// Validate timestamp range
if (fromts != null && tots != null && fromts.isAfter(tots)) {
throw new IllegalArgumentException("fromts must be before tots");
}
}

// Apply additional filters...
// Skipping filters for brevity...

int totalLogs = combinedLogs.size();
List<LogEntry> combinedLogs;
if (logType == null) {
combinedLogs = logEntryRepository.findChangeLogs(fromtsStr, totsStr, who, pri.toString(), remoteIP, op);
combinedLogs
.addAll(logEntryRepository.findActivityLogs(fromtsStr, totsStr, who, pri.toString(), remoteIP, op));
} else {
switch (logType) {
case "A":
combinedLogs = logEntryRepository.findActivityLogs(fromtsStr, totsStr, who, pri.toString(),
remoteIP, op);
break;
case "C":
combinedLogs = logEntryRepository.findChangeLogs(fromtsStr, totsStr, who, pri.toString(), remoteIP,
op);
break;
default:
combinedLogs = logEntryRepository.findChangeLogs(fromtsStr, totsStr, who, pri.toString(), remoteIP,
op);
combinedLogs.addAll(
logEntryRepository.findActivityLogs(fromtsStr, totsStr, who, pri.toString(), remoteIP, op));
break;
}
}

// Apply pagination...
// Apply additional filters...
// Skipping filters for brevity...

int end = Math.min(LOGHARBOUR_GETLOGS_MAXREC, combinedLogs.size());
List<LogEntry> paginatedLogs = combinedLogs.subList(0, end);
int totalLogs = combinedLogs.size();

// Set next searchAfterTs and searchAfterDocId for the next batch
String nextSearchAfterTs = null;
String nextSearchAfterDocId = null;
// Apply pagination...

if (!paginatedLogs.isEmpty() && paginatedLogs.size() == end) {
LogEntry lastLog = paginatedLogs.get(paginatedLogs.size() - 1);
nextSearchAfterTs = lastLog.getWhen();
nextSearchAfterDocId = lastLog.getId().toString();
}
int end = Math.min(LOGHARBOUR_GETLOGS_MAXREC, combinedLogs.size());
List<LogEntry> paginatedLogs = combinedLogs.subList(0, end);

// Create and return the response
return new GetLogsResponse(paginatedLogs, totalLogs, end, null, nextSearchAfterTs, nextSearchAfterDocId);
}
// Set next searchAfterTs and searchAfterDocId for the next batch
String nextSearchAfterTs = null;
String nextSearchAfterDocId = null;

if (!paginatedLogs.isEmpty() && paginatedLogs.size() == end) {
LogEntry lastLog = paginatedLogs.get(paginatedLogs.size() - 1);
nextSearchAfterTs = lastLog.getWhen();
nextSearchAfterDocId = lastLog.getId().toString();
}

// Create and return the response
return new GetLogsResponse(paginatedLogs, totalLogs, end, null, nextSearchAfterTs, nextSearchAfterDocId);
}

public List<LogEntry> getSetlogs(LogharbourRequestBo logharbourRequestBo) throws Exception {

Expand Down

0 comments on commit bad7365

Please sign in to comment.