Name
sn_sow_em.KBRecommendationsForRF
Description
Implements extension point sn_rf.ScriptingRuleInterface Use this interface to implement a script include for rule using Scripting evaluation type
Script
var KBRecommendationsForRF = Class.create();
KBRecommendationsForRF.prototype = Object.extendsObject(sn_rf.RFScriptingRuleSNC, {
initialize: function() {
this.emRfCommons = new sn_sow_em.EmRfCommons();
},
/**********
* Generates output schema for 'Scripting' evaluation type and returns the expected output structure of getEvaluationOutputs()
*
* @return JSON containing list of output's metadata
**********/
getEvaluationOutputSchema: function() {
return {
'status': 'success',
'schema': [
// List of records to show in pill picker
{
'name': this.emRfCommons.KB_RECORD_NAME,
'label': this.emRfCommons.KB_LABEL,
'type': 'reference', // this value should be either 'string' or 'reference'
// if type is 'reference'
'referenceTable': 'kb_knowledge' // table name
}
],
'metaData': {
'uniqueKey': [this.emRfCommons.KB_RECORD_NAME], // name of the fields to uniquely identify a recommendation
'confidence': '' // name of the field which will have confidence score for the recommendation
}
};
},
/**********
* Generates evaluation outputs for given record and inputs
*
* @param param.contextRecord Current record for which we are evaluating
* @return JSON containing list of evaluation outputs
**********/
getEvaluationOutputs: function(param) {
var currAlertGr = param.contextRecord;
this.emRfCommons.logStartTime(this.type, currAlertGr.number);
var startTimeGdt = new GlideDateTime();
try {
var alertId = currAlertGr.sys_id;
var evaluationOutputs = [];
var evtMgmtAlertMgmtMediator = new global.EvtMgmtAlertMgmtMediator();
var searchResult = evtMgmtAlertMgmtMediator.submitSearchRequest(currAlertGr.short_description, this.emRfCommons.TOP_N_RESULTS);
if (!gs.nil(searchResult)) {
var returnedResults = searchResult.meta.returned_results;
// log number of results
this.emRfCommons.logNumberOfResults(this.type, currAlertGr.number, returnedResults);
for (var i = 0; i < searchResult.results.length; i++) {
var singleResult = searchResult.results[i];
var kbId = singleResult.id; // the kbId is recived in the format of "kb_knowledge:42d45135474321009db4b5b0"
if (kbId.startsWith(this.emRfCommons.KB_PREFIX)) { //remove all characters except the KB sys_id
kbId = kbId.slice(this.emRfCommons.KB_PREFIX.length);
var singlePrediction = {};
singlePrediction[this.emRfCommons.KB_RECORD_NAME] = kbId;
evaluationOutputs.push(singlePrediction);
} else {
gs.info(this.type+' : KB sys_id format is different than expected.');
}
}
}
this.emRfCommons.logEndTime(this.type, currAlertGr.number, startTimeGdt );
return {
'status': 'success',
'evaluationOutputs': evaluationOutputs
};
} catch (e) {
var errorMessage = this.emRfCommons.logErrorMessage(this.type, currAlertGr.number, currAlertGr.getUniqueValue(), e);
return {
'status': 'error',
'errorCode': 40001,
'errorMessage': errorMessage
};
}
},
type: 'KBRecommendationsForRF'
});
Sys ID
e3dba8c007010110b34ce06b0fd30060