Pre-Score Lookup Plugin
The Pre-Score Lookup plugin is a template plugin showing how a lookup to a MongoDB collection can be performed in the Pre Scoring Logic and stored in params to be used in the remainder of the runtime structure.
How does it work?
The Pre-Score Lookup plugin is passed the MongoDB connection. An aggregation pipeline can then be used to return the desired data. The result of the aggregation pipeline is then stored in params, making it available for use in the remainder of the runtime process. Data from the contacts and response logging collections can be accessed using the getContactsLoggingDetails and getResponseLoggingDetails functions respectively.
Java Code
This Java class named PreScoreLookup extends PreScoreSuper, which provides functionality that can be used across different pre-scoring plugins.
Then, there is a static method named getPrePredict(MongoClient mongoClient, JSONObject params, CqlSession session). This function takes three parameters: a MongoClient object (which is used to interact with a MongoDB database), a JSONObject object params (which is used the pass data through the runtime process), and a CqlSession object session (which provides a session for executing CQL commands on a Cassandra database.)
The getContactsLoggingDetails and getResponseLoggingDetails functions can be used to access data from the contacts and responses logging collections respectively. These functions take the following arguments
getContactsLoggingDetails(MongoClient mongoClient, JSONObject params, boolean customer_match, boolean predictor_match, boolean unwind_offers, String limit, String offer, String start_date)mongoClientis the mongo connection passed togetPrePredictparamsis the params object used to pass information through the runtime processcustomer_matchis a boolean indicating whether the logs should be matched to the current customer numberpredictor_matchis a boolean indicating whether the logs should be matched to the current predictorunwind_offersis a boolean indicating whether the offers object returned from the logs should be unwound before returning the resultslimitset the number of items to be returned, use an empty string to set no limitstart_dateis the date and time before which the logs should not be matched, use an empty string to set no start date The fullPreScoreLookupclass is shown below:
package com.ecosystem.plugin.customer;
import com.datastax.oss.driver.api.core.CqlSession;
import com.mongodb.client.MongoClient;
import org.json.JSONArray;
import org.json.JSONObject;
import java.io.IOException;
/**
* Perform a mongo lookup and store the results in params for subsequent usage
*/
public class PreScoreLookup extends PreScoreSuper {
public PreScoreLookup() throws Exception {
}
/**
* Pre-pre predict
*/
public void getPrePredict() {
}
/**
* getPrePredict
* @param mongoClient The mongo connection
* @param params The params object used to pass data through the runtime process
* @param session The cassandra connection
* @return params
*/
public static JSONObject getPrePredict(MongoClient mongoClient, JSONObject params, CqlSession session) throws IOException {
try {
/* Get the data from the logs */
JSONArray resultArrayContacts = getContactsLoggingDetails(mongoClient, params, true, true, false, "100", "", "");
JSONArray resultArrayResponses = getResponseLoggingDetails(mongoClient, params, true, true, true, "100", "", "");
/* Write the results to params to be passed through the runtime */
JSONObject loggingDetails = new JSONObject();
loggingDetails.put("resultArrayContacts", resultArrayContacts);
loggingDetails.put("resultArrayResponses", resultArrayResponses);
params.put("prescore_data_lookup",loggingDetails);
} catch (Exception e) {
LOGGER.error("PreScoreLookup:E001:UUID: Lookup failed, prescore data lookup not written to params." + e.getMessage());
}
return params;
}
}