PA Scripts - How to get mean time between critical incidents - Platform analytics

GerardAlleB
Tera Contributor

Hi,



Requirement (POC): Platform analytics - "Mean time between critical incidents"

Problem: To get the time difference using opened by field.

 

Reference:

GerardAlleB_0-1736314829641.png

Reference script: (OOTB - Incident.CloseTime.Hours)

 

 

 

 

 

var diff=function(x,y){return y.dateNumericValue() - x.dateNumericValue();};
var hours=function(x,y){return diff(x,y)/(60*60*1000);};
hours(current.opened_at, current.closed_at);

 

 

 

 

 

 



I created new indicator called: Mean time between critical incidents [see config below], basically I want to get the difference of Current Opened date - Previous Opened date

GerardAlleB_1-1736315182646.png

 

 

The pa_script looks like this.

 

 

 

 

 

 

var currentOpened, previousOpened;
 currentOpened = current.opened_at;

var grPrevRecord = new GlideRecord('incident');
grPrevRecord.addQuery('opened_at', '<', currentOpened); // Find incidents opened before the current one
grPrevRecord.orderByDesc('opened_at'); // Sort descending by opened_at
grPrevRecord.addQuery('priority', 1); // Filter by priority = 1
grPrevRecord.setLimit(1); // Only get the most recent record before the current
grPrevRecord.query();

if (grPrevRecord.next()) {
    previousOpened = grPrevRecord.opened_at; //Store previous record's opened date
}

// Define the difference and hours functions
var diff = function(x, y) {
    return x.dateNumericValue() - y.dateNumericValue(); // Difference in milliseconds
};
var hours = function(x, y) {
    return diff(x, y) / (60 * 60 * 1000); // Convert milliseconds to hours
};

// Calculate the time difference between the previous and current incidents
hours(previousOpened, currentOpened);

 

 

 

 

 

 

*Workaround*(not the best practice)
I am considering creating another calendar field same as "Opened" field on the incident table and populate it via script only when there is a critical incident created. This is to re-use the same code.

1 REPLY 1

Bert_c1
Kilo Patron

You post is confusing.  You state:

 

Requirement (POC): Platform analytics - "Mean time between critical incidents"
Problem: To get the time difference using opened by field.

 

Yet, your code references opened_at and closed_at fields.

 

You are using 'dateNumericValue' which operates on duration fields

 

 

dateNumericValue()
Returns the number of milliseconds since January 1, 1970, 00:00:00 GMT for a duration field. Does not require the creation of a GlideDateTime object because the duration field is already a GlideDateTime object.

 

 

https://developer.servicenow.com/dev.do#!/reference/api/washingtondc/server_legacy/c_GlideElementAPI...

 

You need to use 'getNumericValue' on GlideDateTime fields. See example script below

 

 

var openedTimes = [];
var inc = new GlideRecord('incident');
inc.addEncodedQuery('priority=1');
inc.orderBy('opened_at');
inc.query();
var noIncs = inc.getRowCount();
gs.info('Found : ' + noIncs);
while (inc.next()) {
	gs.info(inc.number + ', opened_at: ' + inc.opened_at);
	var openedAt = new GlideDateTime(inc.opened_at);
	openedTimes.push(openedAt.getNumericValue()/1000);
}
// gs.info(openedTimes);
var diffSum = 0;
for (i = 1; i < openedTimes.length; i++) {
	gs.info('diff = ' + (openedTimes[i] - openedTimes[i-1]));
	diffSum += openedTimes[i] - openedTimes[i-1];
}
gs.info('Total secs: ' + diffSum + ', average = ' + (diffSum/openedTimes.length));

 

 

Good luck on your goal. seem you want Average Open time and not "Mean time between critical incidents". The above code does that.

 

You can use a Metric to calculate the duration between 'opened_at' and 'closed_at' for incident at Priority 1. Then report on the 'metric_instance' table. Review the OOB "Incident State Duration" metric. Create a similar one.