Skip to content

Commit

Permalink
Merge pull request #4142 from SalesforceFoundation/feature/telemetry-…
Browse files Browse the repository at this point in the history
…batch-performance-and-rds

Telemetry for Batch Job Performance and RD Statistics
  • Loading branch information
patrick-yan-sf authored Apr 1, 2019
2 parents 71c98d1 + d130db0 commit b421aed
Show file tree
Hide file tree
Showing 15 changed files with 1,082 additions and 69 deletions.
9 changes: 8 additions & 1 deletion src/classes/OPP_OpportunityNaming_BATCH.cls
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,14 @@ public class OPP_OpportunityNaming_BATCH implements Database.Batchable<sObject>,
* @description Batch process execute method. Names and updates all opportunities in the current batch.
*/
public void execute(Database.BatchableContext BC, List<Opportunity> oppsToProcess) {
lastOppIdProcessed = oppsToProcess[oppsToProcess.size() - 1].Id;
// Since execution contexts might not run in order (ex., chunk size 10k,
// batch size 2k has 5 batches but they won't run in a guaranteed order)
// we first need to check if the last Id is greater than the value in lastOppIdProcessed
Id lastIdInScope = oppsToProcess[oppsToProcess.size() - 1].Id;

if (lastOppIdProcessed == null || lastIdInScope > lastOppIdProcessed) {
lastOppIdProcessed = lastIdInScope;
}

//save old opp names to see if we need an update
Map<Id, String> originalOppNamesById = new Map<Id, String>();
Expand Down
53 changes: 53 additions & 0 deletions src/classes/UTIL_FeatureManagement.cls
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
Copyright (c) 2019 Salesforce.org
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
* Neither the name of Salesforce.org nor the names of
its contributors may be used to endorse or promote products derived
from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
*/
/**
* @author Salesforce.org
* @date 2019
* @group Utilities
* @group-content
* @description Wrapper class to allow for FeatureManagement method mocking in tests
*
*/
public with sharing class UTIL_FeatureManagement {

/**
* @description Wrapper for System.FeatureManagement.setPackageBooleanValue
*/
public void setPackageBooleanValue(String apiName, Boolean value) {
System.FeatureManagement.setPackageBooleanValue(apiName, value);
}

/**
* @description Wrapper for System.FeatureManagement.setPackageIntegerValue
*/
public void setPackageIntegerValue(String apiName, Integer value) {
System.FeatureManagement.setPackageIntegerValue(apiName, value);
}
}
5 changes: 5 additions & 0 deletions src/classes/UTIL_FeatureManagement.cls-meta.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<ApexClass xmlns="urn:metadata.tooling.soap.sforce.com" fqn="UTIL_FeatureManagement">
<apiVersion>45.0</apiVersion>
<status>Active</status>
</ApexClass>
84 changes: 84 additions & 0 deletions src/classes/UTIL_FeatureManagement_TEST.cls
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
/*
Copyright (c) 2019 Salesforce.org
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
* Neither the name of Salesforce.org nor the names of
its contributors may be used to endorse or promote products derived
from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
*/
/**
* @author Salesforce.org
* @date 2019
* @group Utilities
* @group-content
* @description Test for UTIL_FeatureManagement class. Mainly for code coverage since you can't assert on
* System.FeatureManagement method execution
*
*/
@isTest
public with sharing class UTIL_FeatureManagement_TEST {
@isTest
private static void callsSetPackageBooleanValue() {
new UTIL_FeatureManagement().setPackageBooleanValue(
UTIL_OrgTelemetry_SVC.TelemetryParameterName.IsEnabled_HouseholdAcctModel.name(),
false
);
}

@isTest
private static void callsSetPackageIntegerValue() {
new UTIL_FeatureManagement().setPackageIntegerValue(
UTIL_OrgTelemetry_SVC.TelemetryParameterName.Data_CountRdOppsAll.name(),
1
);
}

/*******************************************************************************************************************
* @description Mock for UTIL_FeatureManagement instance
*/
public class Mock implements System.StubProvider {
public Map<String, Boolean> packageBooleanValuesByName = new Map<String, Boolean>();
public Map<String, Integer> packageIntegerValuesByName = new Map<String, Integer>();

public Object handleMethodCall(
Object stubbedObject,
String stubbedMethodName,
Type returnType,
List<Type> listOfParamTypes,
List<String> listOfParamNames,
List<Object> listOfArgs
) {
switch on stubbedMethodName {
when 'setPackageBooleanValue' {
packageBooleanValuesByName.put((String) listOfArgs[0], (Boolean) listOfArgs[1]);
}

when 'setPackageIntegerValue' {
packageIntegerValuesByName.put((String) listOfArgs[0], (Integer) listOfArgs[1]);
}
}
return null;
}
}
}
5 changes: 5 additions & 0 deletions src/classes/UTIL_FeatureManagement_TEST.cls-meta.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<ApexClass xmlns="urn:metadata.tooling.soap.sforce.com" fqn="UTIL_FeatureManagement_TEST">
<apiVersion>45.0</apiVersion>
<status>Active</status>
</ApexClass>
20 changes: 18 additions & 2 deletions src/classes/UTIL_OrgTelemetry_BATCH.cls
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ public without sharing class UTIL_OrgTelemetry_BATCH implements Database.Batchab
UTIL_OrgTelemetry_SVC.TelemetryBatchCategory.Org_Environment,
UTIL_OrgTelemetry_SVC.TelemetryBatchCategory.Data_MaxNumRelatedOpps,
UTIL_OrgTelemetry_SVC.TelemetryBatchCategory.Data_CountRdOpps,
UTIL_OrgTelemetry_SVC.TelemetryBatchCategory.Data_CountRecurringDonations,
UTIL_OrgTelemetry_SVC.TelemetryBatchCategory.Data_CountErrorLog
};

Expand Down Expand Up @@ -85,11 +86,26 @@ public without sharing class UTIL_OrgTelemetry_BATCH implements Database.Batchab
public void finish(Database.BatchableContext info) {
Integer cnt = Database.countQuery('SELECT Count() FROM npe03__Recurring_Donation__c LIMIT 1');
if (cnt == 1) {
Database.executeBatch(new UTIL_OrgTelemetry_SObject_BATCH(
UTIL_OrgTelemetry_SObject_BATCH.TelemetryBatchJobMode.RECURRING_DONATIONS), 2000);
Database.executeBatch(
new UTIL_OrgTelemetry_SObject_BATCH(
UTIL_OrgTelemetry_SObject_BATCH.TelemetryBatchJobMode.OPPORTUNITIES
),
UTIL_OrgTelemetry_SObject_BATCH.OPPORTUNITIES_BATCH_SIZE
);

Database.executeBatch(
new UTIL_OrgTelemetry_SObject_BATCH(
UTIL_OrgTelemetry_SObject_BATCH.TelemetryBatchJobMode.RECURRING_DONATIONS
),
UTIL_OrgTelemetry_SObject_BATCH.RECURRING_DONATIONS_BATCH_SIZE
);

} else {
System.FeatureManagement.setPackageIntegerValue(UTIL_OrgTelemetry_SVC.TelemetryParameterName.Data_CountRdOppsAll.name(), 0);
System.FeatureManagement.setPackageIntegerValue(UTIL_OrgTelemetry_SVC.TelemetryParameterName.Data_CountRdOppsOpenEnded.name(), 0);

System.FeatureManagement.setPackageIntegerValue(UTIL_OrgTelemetry_SVC.TelemetryParameterName.Data_CountRecurringDonationsAll.name(), 0);
System.FeatureManagement.setPackageIntegerValue(UTIL_OrgTelemetry_SVC.TelemetryParameterName.Data_CountRdsWithDiffAmount.name(), 0);
}
}
}
Loading

0 comments on commit b421aed

Please sign in to comment.