Metric - script calculation - update after insert?

dave_shuman
Kilo Explorer

My intention is to create a metric when an incident is created, then update it when a field is populated.

Something like this:

if(!current.opened_at.nil())
createMetric();

if(!current.u_first_contact.nil())
updateMetric();

I've been able to get each function to write to the log, what I have failed at is the proper manipulation of the Metric object. There doesn't seem to be many details on the wiki as to what methods we have on that object. I'd like to create the Metric, then at a later change in the record, Update the metric.

This code doesn't work, but it shows some of my intentions:

function createMetric()
{
var mi = new MetricInstance(definition, current);
if (mi.metricExists())
return;

var gr = mi.getNewRecord();
gr.start = current.sys_created_on;
gr.calculation_complete = false;
gr.insert();
}

function updateMetric()
{
var mi = new MetricInstance(definition, current);
if (!mi.metricExists())
return;

var gr = mi;
gr.end = current.u_first_contact;
gr.duration = gs.dateDiff(gr.start.getDisplayValue(), gr.end.getDisplayValue());
gr.calculation_complete = true;
gr.update();
}

Does anyone have an advanced Metric they would be willing to share? Or any thoughts as to what I might try differently?

Particularly around what can be done with MetricInstance.

7 REPLIES 7

We got something similar working by using the following code. We wanted to monitor changes from the default value of the Affected CI field on an incident to another value when the ticket was created through Self Service or via email and assigned to the Service Desk.



// variables available
// current: GlideRecord - target task
// definition: GlideRecord - (this row)
if (current.contact_type == 'Self Service' || current.contact_type == 'email'){
if(current.assignment_group == '6f8855204a36232f000e600e31f936f3' && current.sys_updated_by != current.opened_by.getDisplayValue())
createMetric(current.cmdb_ci.name);

}

function createMetric(value) {
var mi = new MetricInstance(definition, current);
if (mi.metricExists()){
var gr2 = new GlideRecord('metric_instance');
gr2.addQuery('id', current.sys_id);
gr2.addQuery('definition', 'eb0b7b0a0a0a3c660050e5025571eb14')
gr2.query()
while(gr2.next()){
//gs.log('Found Existing Record and Updating');
gr2.value = value;
gr2.end = current.sys_updated_on;
gr2.duration = gs.dateDiff(gr2.start.getDisplayValue(), gr2.end.getDisplayValue());
gr2.u_task_updated_by.setDisplayValue(current.sys_updated_by);
gr2.calculation_complete = true;
gr2.update();
}
return;
}
if (value == 'Other'){
var gr = mi.getNewRecord();
gr.start = current.sys_created_on;
gr.u_task_number.setDisplayValue(current.number);
gr.u_task_contact_type.setDisplayValue(current.contact_type);
gr.calculation_complete = false;
gr.field_value = value;
gr.field = null;
gr.insert();
}
}


Not applicable

Hi All, Im looking at metrics to try and determine how long a suport group (assignment_group) have worked on an incident during its lifecycle. Im wanting to use the incident state field as the trigger. So when the incident state is set to "work in progess" the clock starts and when the incident state is changed to "resolved" it stops.

Has anyone setup a metric script to do this?

Any help greatly appreciated


T_Prashaanth
Mega Guru

HI you can try something like this for duration between work in progress and closed -

var state = current.incident_state;
if (state == 2 || state==7)

timeDurations(state);

function timeDurations(value){
var mi = new MetricInstance(definition, current);
if (mi.metricExists() && value == '7') {
var gr = new GlideRecord('metric_instance');
gr.addQuery('id', current.sys_id);
gr.addQuery('definition', 'Sys ID of the Metrics Definition');
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') {
var gr1 = mi.getNewRecord();
gr1.start = current.sys_updated_on;
gr1.calculation_complete = false;
gr1.insert();
}

}

 

Mark it right if this is helpful