Metrics - How to calculate the total duration if status changes from one state to other multiple times

bhargavi7
Tera Contributor

Hi all,

How to calculate total duration ,If we change state from Work in progress to closed.

Scenario is State changed from work in progress to suspended, after that it moved to Work in progress again and moved from work in progress to closed.So,it is in work in progress state 2 times.In metrics,it is creating two records .How to show total duration in duration field through metric scripting without creating new field.

I tried all the script which is in community but not working.

Can anyone share any script 

Thanks in advance.

1 ACCEPTED SOLUTION

T_Prashaanth
Mega Guru

Hi bhargavi ,

This Can be achieved using the following script . Basically from In progress - to - Resolved  and then again if it is moved to In Progress and then to resolved. It will update the same Metric record -

var state = current.state;
if (state == '2' || state == '6') // checking if the state is in Progress or Resolved
timeDurations(state);

function timeDurations(value) {
var mi = new MetricInstance(definition, current);
if (mi.metricExists() && value == '6') { // Triggered when the state changes to resolved and update the duration
var gr = new GlideRecord('metric_instance');
gr.addQuery('id', current.sys_id);
gr.addQuery('definition', '6e87e4d31b03301015cbc991604bcbb1');
gr.orderByDesc('sys_created_on');
gr.query();
if (gr.next()) {
gr.end = current.sys_updated_on;
gr.duration = gs.dateDiff(gr.start.getDisplayValue(), gr.end.getDisplayValue());
gr.calculation_complete = true;
gr.update();
}
return;
}

if (!mi.metricExists() && value == '2') { // Triggered when the state changes in Progress for the first time
gs.log('hii');
var gr1 = mi.getNewRecord();
gr1.start = current.sys_updated_on;
gs.log('hii' + current.sys_updated_on);
gr1.calculation_complete = false;
gr1.insert();
return;
}

if (mi.metricExists() && value == '2') { //Triggered when the state changes back to in progress from any other state then update the start time with a value of (Updated or Current Time ) Minus the (Duration)
var gr2 = new GlideRecord('metric_instance');
gr2.addQuery('id', current.sys_id);
gr2.addQuery('definition', '6e87e4d31b03301015cbc991604bcbb1');
gr2.orderByDesc('sys_created_on');
gr2.query();
if (gr2.next()) {
var gdt = new GlideDateTime(current.sys_updated_on.getDisplayValue());
gs.info('Now Time =' + gdt);
var ms = gr2.duration.dateNumericValue();
gs.info('Now Time Duration =' + gr2.duration);
gdt.add(-ms);
gr2.start = gdt.getValue();
gs.info('Now Time Updated =' + gdt.getValue());
gr2.end = '';
gr2.duration = '';
gr2.calculation_complete = false;
gr2.update();
}
return;
}

}

 

Please , mark my solution as correct , if it is helpful

View solution in original post

7 REPLIES 7

Hi Prashant,

PFB.Here 18 is work in progress and 24 is suspended

var state = current.state;
if (state == '18' || state == '24') // checking if the state is in Progress or Resolved
timeDurations(state);

function timeDurations(value) {
var mi = new MetricInstance(definition, current);
if (mi.metricExists() && value == '24') { // Triggered when the state changes to resolved and update the duration
var gr = new GlideRecord('metric_instance');
gr.addQuery('id', current.sys_id);
gr.addQuery('definition', 'd2d28adfdb8f381460b9d8b6f4961957');
gr.orderByDesc('sys_created_on');
gr.query();
if (gr.next()) {
gr.end = current.sys_updated_on;
gr.duration = gs.dateDiff(gr.start.getDisplayValue(), gr.end.getDisplayValue());
gr.calculation_complete = true;
gr.update();
}
return;
}

if (!mi.metricExists() && value == '18') { // Triggered when the state changes in Progress for the first time
gs.log('hii');
var gr1 = mi.getNewRecord();
gr1.start = current.sys_updated_on;
gs.log('hii' + current.sys_updated_on);
gr1.calculation_complete = false;
gr1.insert();
return;
}

if (mi.metricExists() && value == '18') { //Triggered when the state changes back to in progress from any other state then update the start time with a value of (Updated or Current Time ) Minus the (Duration)
var gr2 = new GlideRecord('metric_instance');
gr2.addQuery('id', current.sys_id);
gr2.addQuery('definition', 'd2d28adfdb8f381460b9d8b6f4961957');
gr2.orderByDesc('sys_created_on');
gr2.query();
if (gr2.next()) {
var gdt = new GlideDateTime(current.sys_updated_on.getDisplayValue());
gs.info('Now Time =' + gdt);
var ms = gr2.duration.dateNumericValue();
gs.info('Now Time Duration =' + gr2.duration);
gdt.add(-ms);
gr2.start = gdt.getValue();
gs.info('Now Time Updated =' + gdt.getValue());
gr2.end = '';
gr2.duration = '';
gr2.calculation_complete = false;
gr2.update();
}
return;
}

}

Hi Prashanth,

Thank you so much for all your time and resolution.It is working even we change multiple times.

I am marking it as helpful ,so that it will help others who is facing the similar issue

 

Thanks,

Bhargavi.

 

 

Thanks Bhargavi .Glad that it worked for you