Skip to content

Commit

Permalink
Added field NumberOfDaysToRetainLogs__c to control log retention for …
Browse files Browse the repository at this point in the history
…scenarios
  • Loading branch information
jongpie committed Oct 26, 2021
1 parent f343e6d commit 0901c64
Show file tree
Hide file tree
Showing 6 changed files with 103 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,29 @@
<behavior>Edit</behavior>
<field>IsEnabled__c</field>
</layoutItems>
</layoutColumns>
<style>TwoColumnsTopToBottom</style>
</layoutSections>
<layoutSections>
<customLabel>true</customLabel>
<detailHeading>true</detailHeading>
<editHeading>true</editHeading>
<label>Rule Details</label>
<layoutColumns>
<layoutItems>
<behavior>Required</behavior>
<field>Scenario__c</field>
</layoutItems>
</layoutColumns>
<layoutColumns>
<layoutItems>
<behavior>Required</behavior>
<field>UserLoggingLevelOverride__c</field>
</layoutItems>
<layoutItems>
<behavior>Edit</behavior>
<field>NumberOfDaysToRetainLogs__c</field>
</layoutItems>
</layoutColumns>
<style>TwoColumnsTopToBottom</style>
</layoutSections>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?xml version="1.0" encoding="UTF-8" ?>
<CustomField xmlns="http://soap.sforce.com/2006/04/metadata">
<fullName>NumberOfDaysToRetainLogs__c</fullName>
<externalId>false</externalId>
<fieldManageability>SubscriberControlled</fieldManageability>
<inlineHelpText
>This value is used to set the field Log__c.LogRetentionDate__c, which is then used by LogBatchPurger to delete old logs. To keep logs indefinitely, set this field to blank (null).</inlineHelpText>
<label>Days to Retain Logs</label>
<precision>4</precision>
<required>false</required>
<scale>0</scale>
<type>Number</type>
<unique>false</unique>
</CustomField>
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
<columns>Scenario__c</columns>
<columns>IsEnabled__c</columns>
<columns>UserLoggingLevelOverride__c</columns>
<columns>NumberOfDaysToRetainLogs__c</columns>
<filterScope>Everything</filterScope>
<label>All</label>
</ListView>
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<inlineHelpText
>This value is used to set the field Log__c.LogRetentionDate__c, which is then used by LogBatchPurger to delete old logs. To keep logs indefinitely, set this field to blank (null).</inlineHelpText>
<label>Days to Retain Logs</label>
<precision>3</precision>
<precision>4</precision>
<required>false</required>
<scale>0</scale>
<trackTrending>false</trackTrending>
Expand Down
51 changes: 43 additions & 8 deletions nebula-logger/main/log-management/classes/LogHandler.cls
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,23 @@
*/
public without sharing class LogHandler extends LoggerSObjectHandler {
@TestVisible
private static Map<String, LogStatus__mdt> logStatusByName = loadActiveLogStatuses();
private static final Map<String, LogStatus__mdt> LOG_STATUS_NAME_TO_STATUS = loadActiveLogStatuses();
@TestVisible
private static final Map<String, LoggerScenarioRule__mdt> SCENARIO_TO_MOCK_SCENARIO_RULE = new Map<String, LoggerScenarioRule__mdt>();

@TestVisible
private List<Log__c> logs;
@TestVisible
private Map<Id, Log__c> oldLogsById;

private static Map<String, LogStatus__mdt> loadActiveLogStatuses() {
Map<String, LogStatus__mdt> logStatusByName = new Map<String, LogStatus__mdt>();
Map<String, LogStatus__mdt> logStatusNameToStatus = new Map<String, LogStatus__mdt>();
for (LogStatus__mdt logStatus : LogStatus__mdt.getAll().values()) {
if (logStatus.IsActive__c == true) {
logStatusByName.put(logStatus.MasterLabel, logStatus);
logStatusNameToStatus.put(logStatus.MasterLabel, logStatus);
}
}

return logStatusByName;
return logStatusNameToStatus;
}

/**
Expand Down Expand Up @@ -61,7 +62,7 @@ public without sharing class LogHandler extends LoggerSObjectHandler {
private void setClosedStatusFields() {
for (Log__c log : this.logs) {
// Determine if the status is considered closed (based on `LogStatus__mdt` custom metadata)
LogStatus__mdt logStatus = logStatusByName.get(log.Status__c);
LogStatus__mdt logStatus = LOG_STATUS_NAME_TO_STATUS.get(log.Status__c);
if (logStatus != null) {
log.IsClosed__c = logStatus.IsClosed__c;
log.IsResolved__c = logStatus.IsResolved__c;
Expand All @@ -79,6 +80,7 @@ public without sharing class LogHandler extends LoggerSObjectHandler {
}

private void setLogRetentionDate() {
Map<String, LoggerScenarioRule__mdt> scenarioToScenarioRule = queryScenarioRules(this.logs);
for (Log__c log : this.logs) {
// If the retention date has already been populated, leave it as-is
if (log.LogRetentionDate__c != null) {
Expand All @@ -88,9 +90,18 @@ public without sharing class LogHandler extends LoggerSObjectHandler {
// Load the logging user's settings
User loggingUser = new User(Id = log.LoggedBy__c, ProfileId = log.ProfileId__c);
LoggerSettings__c loggingUserSettings = Logger.getUserSettings(loggingUser);
Integer daysToRetainLog = Integer.valueOf(loggingUserSettings.DefaultNumberOfDaysToRetainLogs__c);

// When loggerSettings.DefaultNumberOfDaysToRetainLogs__c is null, assume that the log should be kept forever,
// Load the configured scenario rule (if one exists)
LoggerScenarioRule__mdt matchingScenarioRule = scenarioToScenarioRule.get(log.Scenario__c);
if (Test.isRunningTest() == true) {
matchingScenarioRule = SCENARIO_TO_MOCK_SCENARIO_RULE.get(log.Scenario__c);
}

Integer daysToRetainLog = Integer.valueOf(
matchingScenarioRule != null ? matchingScenarioRule.NumberOfDaysToRetainLogs__c : loggingUserSettings.DefaultNumberOfDaysToRetainLogs__c
);

// When daysToRetainLog is null, assume that the log should be kept forever,
// and set the retention date to null so that LogBatchPurger filters out/ignores the record
log.LogRetentionDate__c = daysToRetainLog == null ? null : System.today().addDays(daysToRetainLog);
}
Expand Down Expand Up @@ -145,4 +156,28 @@ public without sharing class LogHandler extends LoggerSObjectHandler {
}
Database.insert(logShares, false);
}

private static Map<String, LoggerScenarioRule__mdt> queryScenarioRules(List<Log__c> logs) {
List<String> scenarios = new List<String>();
for (Log__c log : logs) {
if (log.Scenario__c != null) {
scenarios.add(log.Scenario__c);
}
}

Map<String, LoggerScenarioRule__mdt> scenarioToScenarioRule = new Map<String, LoggerScenarioRule__mdt>();
for (LoggerScenarioRule__mdt scenarioRule : [
SELECT Scenario__c, NumberOfDaysToRetainLogs__c
FROM LoggerScenarioRule__mdt
WHERE IsEnabled__c = TRUE AND Scenario__c IN :scenarios
]) {
scenarioToScenarioRule.put(scenarioRule.Scenario__c, scenarioRule);
}
return scenarioToScenarioRule;
}

@TestVisible
private static void setMockScenarioRule(LoggerScenarioRule__mdt scenarioRule) {
SCENARIO_TO_MOCK_SCENARIO_RULE.put(scenarioRule.Scenario__c, scenarioRule);
}
}
35 changes: 25 additions & 10 deletions nebula-logger/tests/log-management/classes/LogHandler_Tests.cls
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ private class LogHandler_Tests {
LogStatus__mdt closedStatus = new LogStatus__mdt(IsActive__c = true, MasterLabel = SECOND_STATUS, IsClosed__c = true, IsResolved__c = true);
logStatusByName.put(closedStatus.MasterLabel, closedStatus);

LogHandler.logStatusByName = logStatusByName;
LogHandler.LOG_STATUS_NAME_TO_STATUS.putAll(logStatusByName);
}

@IsTest
Expand Down Expand Up @@ -116,25 +116,40 @@ private class LogHandler_Tests {
}

@IsTest
static void it_should_set_retention_date_when_configured() {
static void it_should_set_retention_date_when_configured_via_logger_settings() {
Integer daysToRetainLog = 90;
Date expectedRetentionDate = System.today().addDays(daysToRetainLog);

LoggerSettings__c settings = Logger.getUserSettings();
settings.DefaultNumberOfDaysToRetainLogs__c = daysToRetainLog;
upsert settings;

Test.startTest();
Logger.getUserSettings().DefaultNumberOfDaysToRetainLogs__c = daysToRetainLog;
upsert Logger.getUserSettings();

Log__c log = new Log__c(LoggedBy__c = UserInfo.getUserId(), TransactionId__c = '1234');
insert log;

Test.stopTest();

log = [SELECT Id, LogRetentionDate__c FROM Log__c WHERE Id = :log.Id];
System.assertEquals(expectedRetentionDate, log.LogRetentionDate__c);
}

@IsTest
static void it_should_set_retention_date_when_configured_via_scenario_rules() {
Integer defaultDaysToRetainLog = 1;
Integer daysToRetainLog = 90;
Date expectedRetentionDate = System.today().addDays(daysToRetainLog);
Logger.getUserSettings().DefaultNumberOfDaysToRetainLogs__c = defaultDaysToRetainLog;
upsert Logger.getUserSettings();
String transactionScenarioName = 'some scenario';
LoggerScenarioRule__mdt scenarioRule = new LoggerScenarioRule__mdt(
Scenario__c = transactionScenarioName,
NumberOfDaysToRetainLogs__c = daysToRetainLog
);
LogHandler.setMockScenarioRule(scenarioRule);

Log__c log = new Log__c(LoggedBy__c = UserInfo.getUserId(), Scenario__c = transactionScenarioName, TransactionId__c = '1234');
insert log;

log = [SELECT Id, Scenario__c, LogRetentionDate__c FROM Log__c WHERE Id = :log.Id];
System.assertEquals(expectedRetentionDate, log.LogRetentionDate__c);
}

@IsTest
static void it_should_have_null_retention_date_when_no_retention_configured() {
LoggerSettings__c settings = Logger.getUserSettings();
Expand Down

0 comments on commit 0901c64

Please sign in to comment.