Name
sn_em_ai.EvtMgmtEmExtraDataJsonPipe
Description
No description available
Script
var EvtMgmtEmExtraDataJsonPipe = Class.create();
EvtMgmtEmExtraDataJsonPipe.prototype = {
initialize: function() {
this.evtMgmtCommons = new global.EvtMgmtCommons();
// Max records to fetch from em_extra_data_json on every call
this.maxRecords = gs.getProperty('evt_mgmt.em_extra_data_json.max_fetched_records', 100);
// Import JSON processing job
this.jsonProcessor = new EvtMgmtJsonToEventsProcessor();
// Table Mapping
this.CONSTS = {
TABLE: 'em_extra_data_json',
COLUMNS: {
JSON: 'json',
STATE: {
READY: 'Ready',
PROCESSED: 'Processed',
IGNORED: 'Ignored',
ERROR: 'Error',
},
SOURCE: {
NONE: '0',
LOG_ANALYTICS: '1',
}
}
};
},
type: 'EvtMgmtEmExtraDataJsonPipe',
/**
* Inserts the given json into the json column.
* @param source - The source that the JSON was retreived from.
* @param json - A valid JSON (as JSON or in String format through JSON.stringify()).
*/
insert: function(source, json, domainSysId) {
var isSuccess = true;
var message = '';
var state = this.CONSTS.COLUMNS.STATE.READY;
source = this.getValidSourceValue(source);
// Input validation:
// Validate "json" parameter is a valid JSON object or a non-empty string
var isValidJson = json instanceof Object && !(json instanceof Array) && (Object.keys(json).length > 0);
// Consider an erroneous input for:
// 1. An invalid json (empty object)
// 2. Any other input that is not a string
// NOTE: A non-empty string is considered as a valid value
if (!json || (!isValidJson && (typeof json !== 'string'))) {
isSuccess = false;
message = gs.getMessage('The json parameter is not of valid type (JSON object or a string)');
state = this.CONSTS.COLUMNS.STATE.ERROR;
}
// Stringify the json object, or any other input that is not a string
// We'd like to save the input even if it's erroneous
if (typeof json !== 'string') {
json = JSON.stringify(json);
}
// save original session domain
var originalSessionDomain = this.evtMgmtCommons.getCurrentDomainID();
// if domainSysId we got as a parameter to the function is empty, use "global" domain
domainSysId = this.evtMgmtCommons.returnGlobaDomainForEmpty(domainSysId);
// change session domain
this.evtMgmtCommons.changeDomain(domainSysId);
var gr = new GlideRecord(this.CONSTS.TABLE);
gr.initialize();
gr.json = json;
gr.notes = message;
gr.state = state;
gr.source = source;
gr.insert();
// restore original session domain
this.evtMgmtCommons.changeDomain(originalSessionDomain);
return {
'success': isSuccess,
'message': message
};
},
/**
* Fetches records from 'em_extra_data_json' table and sends them for processing.
*/
initiateProcessing: function() {
var json, statusMessage, timeBeforeProcess;
var originalDomain = this.evtMgmtCommons.getCurrentDomainID();
var gr = new GlideRecord(this.CONSTS.TABLE);
gr.addQuery('state', '=', this.CONSTS.COLUMNS.STATE.READY);
gr.setLimit(this.maxRecords);
gr.query();
while (gr.next()) {
// Reset statusMessage for every loop so if one record failed it won't affect the next records
statusMessage = undefined;
// Take time before process starts
timeBeforeProcess = new Date().getTime();
this.evtMgmtCommons.changeDomain(this.evtMgmtCommons.getRecordDomain(gr));
// Try to parse JSON
try {
json = JSON.parse(gr.getValue('json'));
} catch (e) {
statusMessage = gs.getMessage('EvtMgmtEmExtraDataJsonPipe - JSON parse error for gr with sys_id: {0}, and error message: {1}', [gr.sys_id, this.evtMgmtCommons.getExceptionMessage(e, true)]);
gs.error(statusMessage);
}
if (json && !statusMessage) {
// Try to process JSON after parse is successful
try {
statusMessage = this.jsonProcessor.process(gr.getValue('source'), json, gr.getValue('sys_id'));
} catch (e) {
// JSON process error
statusMessage = gs.getMessage('EvtMgmtEmExtraDataJsonPipe - JSON process error. Error message: {1}', [gr.sys_id, this.evtMgmtCommons.getExceptionMessage(e, true)]);
gs.error(statusMessage);
}
}
// Record the time when process ends
gr.process_duration_ms = (new Date().getTime()) - timeBeforeProcess;
// Expecting process to return an empty string, null or undefined on success
if (!statusMessage) {
gr.state = this.CONSTS.COLUMNS.STATE.PROCESSED;
} else {
gr.state = this.CONSTS.COLUMNS.STATE.ERROR;
gr.notes = String(statusMessage);
}
gr.update('JSON was processed on ' + (new GlideDateTime()).getValue());
}
this.evtMgmtCommons.changeDomain(originalDomain);
},
// Returns source value as stored in the database
getValidSourceValue: function(source) {
var sources = this.CONSTS.COLUMNS.SOURCE;
if (sources.hasOwnProperty(source.toUpperCase())) {
return sources[source.toUpperCase()];
} else {
for (var name in sources) {
if (parseInt(sources[name], 10) === parseInt(source, 10)) {
return String(parseInt(source, 10));
}
}
}
return 0;
},
};
Sys ID
60ffd914b74310107c038229ce11a958