Name
global.PreSensorSimulation
Description
No description available
Script
var PreSensorSimulation = Class.create();
PreSensorSimulation.prototype = {
initialize: function() {},
/*
Inputs:
payload: JSON payload from a pattern. (although it can technically be anything)
patternId: Name of pattern for use by pre-sensors
pre_sensors: JSON Array of sys IDs of different pre_sensors which should be run on the payload
Output:
JSON value containing the updated payload
This function can/will throw exceptions if input formats do not match expected behavior, make sure to catch errors when using this.
*/
simulatePreSensors: function(payload, patternId, pre_sensors, probeId) {
// Validate the payload and patternId fields
this.payload = this.validatePayload(payload);
this.validatePatternId(patternId);
// Validate and parse the pre_sensors field into a GR to pass to processing function
var pre_sensorsGR = this.parsePreSensorsToGR(pre_sensors);
// Early exit if there are no pre-sensors to run
if (!pre_sensorsGR.hasNext()) {
return {
'payload': payload
};
}
// If we have a probeId, create Probe record using this id
// Otherwise, just create dummy Probe record
var probe;
if (probeId) {
var probeGr = new GlideRecord("ecc_queue");
probe = (probeGr.get("sys_id", probeId)) ? SncProbe.createProbeResponse(probeGr) : new SncProbe();
} else
probe = new SncProbe();
var params = {
'payload': this.payload,
'patternId': patternId,
'g_probe': probe
};
// Receives the following in the input:
// 1. payload value which is the json value that will be passed into the pre-sensors and modified
// 2. a list of pre-sensors which should be run on the payload
var successFunc = function(params, gr, successMessage) {
// do nothing
};
var failureFunc = function(params, gr, failureMessage) {
throw "Name of failed script: " + gr.getValue("name") + ". Failure Message: " + failureMessage;
};
var prePost = new PatternPrePostHook();
return prePost.runSelectedPreProcess(params, pre_sensorsGR, successFunc, failureFunc);
},
validatePayload: function(payload) {
if (JSUtil.nil(payload))
throw gs.getMessage("'payload' field is empty or not populated.");
payload = this.fixPayload(payload);
if (!this.hasJsonStructure(payload)) {
throw gs.getMessage("'payload' field is not a well formed JSON object.");
}
return payload;
},
validatePatternId: function(patternId) {
if (JSUtil.nil(patternId))
throw gs.getMessage("'patternId' field is empty or not populated.");
},
validatePreSensors: function(pre_sensors) {
if (JSUtil.nil(pre_sensors)) {
throw gs.getMessage("The 'pre_sensors' parameter is missing/is empty.");
}
if (!this.hasArrayStructure(pre_sensors)) {
throw gs.getMessage("The 'pre_sensors' parameter is not a well formed JSON array.");
}
},
parsePreSensorsToGR: function(pre_sensors) {
this.validatePreSensors(pre_sensors);
var pre_sensorsJSON = [];
try {
pre_sensorsJSON = JSON.parse(pre_sensors);
} catch (err) {
throw gs.getMessage("The 'pre_sensors' parameter could not be parsed as JSON. Error: {0}", err);
}
// Get the pre_sensor gr prepared to be sent to the prepost hook
var pre_sensorGR = new GlideRecord("sa_pattern_prepost_script");
// Setup condition on sys_id to add "OR" conditions to
var sys_id_query = pre_sensorGR.addQuery('sys_id', pre_sensorsJSON);
pre_sensorGR.orderBy("order");
pre_sensorGR.orderBy("name");
pre_sensorGR.query();
return pre_sensorGR;
},
hasJsonStructure: function(str) {
if (typeof str !== 'string')
return false;
try {
var result = JSON.parse(str);
return typeof result === 'object' && !(Array.isArray(result) || result instanceof Array);
} catch (err) {
return false;
}
},
hasArrayStructure: function(str) {
if (typeof str !== 'string')
return false;
try {
var result = JSON.parse(str);
return Array.isArray(result) || result instanceof Array;
} catch (err) {
return false;
}
},
fixPayload: function(payload) {
var replacements = {'\f':'\\f', '\n':'\\n', '\r':'\\r', '\t':'\\t'};
payload = payload.replace(/\f|\n|\r|\t/g, function(match) {
return replacements[match];});
return payload;
},
type: 'PreSensorSimulation'
};
Sys ID
3bb4c98bc79620105b31824d5ec2601d