Resolution Time - Code Optimization Problem Fix
Code Optimization Method 1 :
Least Optimized Method :
If you look closely I have added three nested for loops in here, of course this is not going to be a very optimal solution (Hence the execution time is higher)
var resolutionTimeList = [];
var incidentSysIDList = [];
var N = 84;
var incidentList = new GlideRecord('incident');
incidentList.addEncodedQuery('stateIN6,7'); //closed, resolved
incidentList.addNotNullQuery('resolved_at');
incidentList.addNotNullQuery('opened_at');
incidentList.orderByDesc('resolved_at');
incidentList.setLimit(100);
incidentList.query();
while(incidentList.next()){
var openedAt = new GlideDateTime(incidentList.opened_at);
var resolvedAt = new GlideDateTime(incidentList.resolved_at);
var diff = gs.dateDiff(openedAt, resolvedAt, true);
var resolutionTimeInDays = Math.floor(diff/(60*60*24));
resolutionTimeList.push(resolutionTimeInDays);
incidentSysIDList.push(incidentList.sys_id);
}
var i, j, k;
var count = 0;
for(i = 0; i < resolutionTimeList.length; i++) {
for(j = i; j < resolutionTimeList.length; j++) {
var sum = 0;
for(k = i; k <= j; k++) {
sum += resolutionTimeList[k];
}
if(sum == N) {
count++;
}
}
}
gs.print('Count ' +count);
Let’s move towards the better approach
Better Approach :
Here I am going to use two for loops and will try to achieve the same result. The execution time is faster but again not the most optimized solution.
var resolutionTimeList = [];
var incidentSysIDList = [];
var N = 84;
var incidentList = new GlideRecord('incident');
incidentList.addEncodedQuery('stateIN6,7'); //closed, resolved
incidentList.addNotNullQuery('resolved_at');
incidentList.addNotNullQuery('opened_at');
incidentList.orderByDesc('resolved_at');
incidentList.setLimit(100);
incidentList.query();
while(incidentList.next()){
var openedAt = new GlideDateTime(incidentList.opened_at);
var resolvedAt = new GlideDateTime(incidentList.resolved_at);
var diff = gs.dateDiff(openedAt, resolvedAt, true);
var resolutionTimeInDays = Math.floor(diff/(60*60*24));
resolutionTimeList.push(resolutionTimeInDays);
incidentSysIDList.push(incidentList.sys_id);
}
var i, j;
var count = 0;
for(i = 0; i < resolutionTimeList.length; i++){
var sum = 0;
for(j = i; j < resolutionTimeList.length; j++){
sum += resolutionTimeList[j];
if(sum == N){
count++;
}
}
}
gs.print('Count ' +count);
Best Approach:
In this scenario, I have used one of the best approach, which is I am storing the sum at a specific place before printing any solution.
Therefore with the least execution time.
var resolutionTimeList = [];
var incidentSysIDList = [];
var N = 84;
var incidentList = new GlideRecord('incident');
incidentList.addEncodedQuery('stateIN6,7'); //closed, resolved
incidentList.addNotNullQuery('resolved_at');
incidentList.addNotNullQuery('opened_at');
incidentList.orderByDesc('resolved_at');
incidentList.setLimit(100);
incidentList.query();
while(incidentList.next()){
var openedAt = new GlideDateTime(incidentList.opened_at);
var resolvedAt = new GlideDateTime(incidentList.resolved_at);
var diff = gs.dateDiff(openedAt, resolvedAt, true);
var resolutionTimeInDays = Math.floor(diff/(60*60*24));
resolutionTimeList.push(resolutionTimeInDays);
incidentSysIDList.push(incidentList.sys_id);
}
var i, tempSum = 0;
var count = 0;
var tempSumCount = {};
tempSumCount[0] = 1;
for(i = 0; i < resolutionTimeList.length; i++){
tempSum += resolutionTimeList[i];
if((tempSum - N) in tempSumCount){
count += tempSumCount[tempSum -N];
}
if(tempSum in tempSumCount){
tempSumCount[tempSum]++;
}
else{
tempSumCount[tempSum] = 1;
}
}
gs.print('Count ' +count);
Conclusion
This is how it has to be addressed. The better the execution time, the better it performs.
Subscribe to my newsletter
Read articles from Syed Taha Kamal Ahmad directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
Syed Taha Kamal Ahmad
Syed Taha Kamal Ahmad
Being a JavaScript, HTML, CSS and Servicenow Enthusiast with an experience of 7+ years, I have worked on multiple end-to-end projects which include multiple tech stacks like JS, ServiceNow (modules like ITSM, CSM, SecOps, etc.). I LOVE TO CODE! I always knew that I wanted to write and guide people but I wasn't ever sure how to go about it. I started in a very professional way. I started writing Knowledge Articles for the organization. Knowing what the world needs to delivering them what they need is what I'm passionate about. My specialties include ServiceNow, Linkedin Profile reviewing, Writing, Mentorship, Interview guidance for both freshers and experienced, Management. If you catch me outside of work, I like meeting new people, traveling - and an avid lover of cricket.