Name
sn_sow_em.IncidentsOnRelatedCIForRF
Description
Implements extension point sn_rf.ScriptingRuleInterface Use this interface to implement a script include for rule using Scripting evaluation type
Script
var IncidentsOnRelatedCIForRF = Class.create();
IncidentsOnRelatedCIForRF.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.INCIDENT_RECORD_NAME,
'label': this.emRfCommons.INCIDENT_RECORD_LABEL,
'type': 'reference', // this value should be either 'string' or 'reference'
'referenceTable': 'incident'
}
],
'metaData': {
'uniqueKey': [this.emRfCommons.INCIDENT_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 emAiInsight = new global.EmAiInsight();
//call related ci calculation
if (this.toRunCalculationOnAlert(currAlertGr.getUniqueValue())) {
var tasks = ["incident"];
emAiInsight.calculateRelatedTaskResults(currAlertGr.sys_id, tasks);
}
var relatedTaskGr = new GlideRecord("em_alert_related_task");
emAiInsight.getRelatedTaskResultsByType(relatedTaskGr, currAlertGr, "incident"); // add query conditions for finding related tasks to relatedTaskGrGr
relatedTaskGr.orderByDesc('incident.sys_created_on');
relatedTaskGr.setLimit(this.emRfCommons.TOP_N_RESULTS); //only 10 results
relatedTaskGr.query();
// log number of results
this.emRfCommons.logNumberOfResults(this.type, currAlertGr.number, relatedTaskGr.getRowCount());
var evaluationOutputs = this.emRfCommons.insertIncidentsToResultArray(relatedTaskGr, "incident");
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
};
}
},
toRunCalculationOnAlert: function(alertId) {
var insightGr = new GlideRecord('em_alert_insight_state');
insightGr.addQuery("alert", alertId);
insightGr.addQuery("process_state", '2'); // process_state Done - get last insight calculated on alert
insightGr.query();
if (insightGr.next()) { // alert insight has been calculated before, check if last calculate time is grater than min
var timeRange = gs.getProperty("evt_mgmt.related_incident_calculation_time_diff_in_hours", 24);
var timeRangeInMilliseconds = timeRange * 3600 * 1000; //1 hour in milliseconds
var now = new GlideDateTime();
var lastCalculationTime = new GlideDateTime(insightGr.getValue('current_calculation'));
var toCalculate = now.getNumericValue() - lastCalculationTime.getNumericValue() > timeRangeInMilliseconds;
return toCalculate;
}
// no calculation done on alert yet
return true;
},
type: 'IncidentsOnRelatedCIForRF'
});
Sys ID
d16d7ccc07410110b34ce06b0fd30014