How to create a field value duration metric?

e_wilber
Tera Guru

Hi everyone.

I'm a little confused. I'm trying to create a new Field Value Duration metric for the cmdb_ci_outage table. I created a string String field for this table and need to determine how long the record stays in each 'state'. 'state' meaning for I need to know how long the text in this field is in this field.

I looked at the sample OOB field value duration types and they just work. There's no script in them. However, my current set up below never created the metric. Can anyone give me some pointers?

Table: Outage [cmdb_ci_outage]

Field: Incident Priority [u_incident_priority]

Type: Field value duration

Active: true

Script: empty

1 ACCEPTED SOLUTION

Kalaiarasan Pus
Giga Sage

if it is not working by default , do the below ...



Create a business rule on the table with the condition as


current.field.changes()



Sample script :



var metricSysID = 'a106eba143557500eb7948f4b14afc9d'; //replace the sys id of the metric definition



var instanceRecord= new GlideRecord('metric_instance');


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


instanceRecord.addQuery('definition',metricSysID);


instanceRecord.addQuery('calculation_complete','false');


instanceRecord.query();




if(!instanceRecord.next()){


      insertMetrics();


}


else


      {


      instanceRecord.end = gs.nowDateTime();


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


      instanceRecord.calculation_complete = true;


      instanceRecord.update();



      if(current.active == true)


              {


              insertMetrics();


      }


}





function insertMetrics()


{



      var instanceRecord= new GlideRecord('metric_instance');


      var metricSysID = 'a106eba143557500eb7948f4b14afc9d'; //replace the sys id of the metric definition


      instanceRecord.initialize();


      instanceRecord.definition = metricSysID;


      instanceRecord.start = gs.nowDateTime();


      instanceRecord.id = current.sys_id;


      instanceRecord.value = current.field; //replace the fieldname here


      instanceRecord.calculation_complete = false;


      instanceRecord.insert();



}



Note : Replace the fieldnames and sys id of the metrics definition...


View solution in original post

4 REPLIES 4

Kalaiarasan Pus
Giga Sage

if it is not working by default , do the below ...



Create a business rule on the table with the condition as


current.field.changes()



Sample script :



var metricSysID = 'a106eba143557500eb7948f4b14afc9d'; //replace the sys id of the metric definition



var instanceRecord= new GlideRecord('metric_instance');


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


instanceRecord.addQuery('definition',metricSysID);


instanceRecord.addQuery('calculation_complete','false');


instanceRecord.query();




if(!instanceRecord.next()){


      insertMetrics();


}


else


      {


      instanceRecord.end = gs.nowDateTime();


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


      instanceRecord.calculation_complete = true;


      instanceRecord.update();



      if(current.active == true)


              {


              insertMetrics();


      }


}





function insertMetrics()


{



      var instanceRecord= new GlideRecord('metric_instance');


      var metricSysID = 'a106eba143557500eb7948f4b14afc9d'; //replace the sys id of the metric definition


      instanceRecord.initialize();


      instanceRecord.definition = metricSysID;


      instanceRecord.start = gs.nowDateTime();


      instanceRecord.id = current.sys_id;


      instanceRecord.value = current.field; //replace the fieldname here


      instanceRecord.calculation_complete = false;


      instanceRecord.insert();



}



Note : Replace the fieldnames and sys id of the metrics definition...


Hey!



Thank you very much for that script. It worked! I knew I was able to create my own script to make this work but I'm curious as to why it's not working out of box. Do you have any idea why the script would be required in this situation and not have it simply work on its own?


There is some business rule that triggers that metrics event update .. This is responsible for updating/inserting the metrics ... But I am afraid I don't remember the business rule at the moment...



Also can you please click on the Correct Answer and/or Helpful Answer buttons on the appropriate reply/replies.. This helps other users to see that a valid response was received.


Hello Kalaiarasan,



Thanks for the above piece of code. It was a nice help for me.



Kind Regards,


Rajshekhar Paul