Help with metric script

veronica_malmo
Kilo Expert

Hi,

I have a metric script that should calculate the duration from a change is created and to it's in the state: proposed, sub_state: awaiting_customer_proposal, but something is not working with it. I'm not the one that has created this metric in the first place, I just hoped I could use it.. can anyone see any obvious mistakes?

// variables available

// current: GlideRecord -   target Change

// definition: GlideRecord -   (this row)

var s = current.state;

var ss = current.u_sub_state;

// We attach a metric when Change is created

if (current.sys_updated_on == current.sys_created_on)

createMetric();

// If Change state reaches Proposed and Sub-State Awaiting Customer approval we close the metric.

if(s == -7 && ss == 'awaiting_customer_approval')

closeMetric();

function createMetric() {

var mi = new MetricInstance(definition, current);

if (mi.metricExists())

return;

var gr = mi.getNewRecord();

gr.start = current.sys_created_on;

// gr.end = current.sys_updated_on;

// gr.duration = gs.dateDiff(gr.start.getDisplayValue(), gr.end.getDisplayValue());

gr.calculation_complete = false;

gr.insert();

}

function closeMetric() {

var gr = new GlideRecord('metric_instance');

gr.addQuery('id', current.sys_id);

gr.addQuery('calculation_complete', false);

gr.addQuery('definition.type','calculation');

gr.addQuery('definition',definition.sys_id);

gr.query();

while(gr.next()){

// gs.log("==>>DEBUG. Found existing Problem metric. Updating");

// gs.log("==>>DEBUG. closing: "+ gr.definition.name+" for: "+ current.number);

gr.calculation_complete = true;

gr.end = current.sys_updated_on;

gs.log("==>>DEBUG. CHANGE KPI Start: " + gr.start.getDisplayValue());

gs.log("==>>DEBUG. CHANGE KPI End: " + gr.end.getDisplayValue());

gr.duration = gs.dateDiff(gr.start.getDisplayValue(), gr.end.getDisplayValue());

gs.log("==>>DEBUG. " + gr.duration);

gs.log("==>>DEBUG. Updating Metric: " + gr.id);

gs.log("==>>DEBUG. current.sys_id: " + current.sys_id);

// var definition =new GlideRecord('metric_definition');

// definition.get(gr.definition);

// var mi =new MetricInstance(definition, current);

// mi.endDuration();

gr.update();

}

}

8 REPLIES 8

Is the metric created but not closed?



if(s == -7 && ss == 'awaiting_customer_approval')


closeMetric();



If so, is your condition awaiting_customer_approval set in the same update as when it is set to state -7?



The script is only called when substate changes, so it needs to be in the same update.


Otherwise, consider moving the close logic to a BR.


No, the metric is not even created..


Other metrics are working? Then I would add some logging, like   gs.log("Metric fired","metricdebug")


Perhaps I'm trying to do something impossible in metrics? When I try to do a metric with calculation on only State from Created to State proposed everything works, it is when I try to calculate the duration in several states PLUSS a substate in the last state that everything stops working..


So perhaps I have to do one metric to calculate the States duration and one for the substate, and then add them up..