How-to Setup Metric Definition for Catalog Task

Austin20
Tera Contributor

Hey Community. I'm trying to set up a metric definition to calculate create to resolve duration. I copied the metric definition for incident and made a modification to it. I created a test ticket and I ended up getting time data in the duration field. When I try to report off that I get zero seconds. I see data is in the metric definition that I created. From looking at the incident metric definition it only has one row. Where I have two rows in my new metric definition. Below is the code that I'm using for the sc_tasks. Also attached a picture on what I see in the metric definition.  

 

 

// variables available
// current: GlideRecord -  target sc_tasks
// definition: GlideRecord -  (this row)
var s = current.state;
if (s >= 6)
  createMetric();

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 = true;
  gr.insert();
}
1 ACCEPTED SOLUTION

Steven Slocum
Tera Guru

Since you likely won't be resolving a catalog task, change the metric definition to instead calculate on the time spent as "active"

Name: Open_SCTASK

Table: sc_task

Field: Active

Type: Field Value Duration

Description: Special case that closes out (ends) other metrics when an the active flag in the sc_task turns false

 

 

Script:

// script can set answer to false to terminate processing of the metric
// mi - MonitorInstance
// answer
if (!current.active) {
  answer = false;
  mi.endDuration();
  closeDurations(mi.current);
}

function closeDurations(current) {
    var gr = new GlideRecord('metric_instance');
    gr.addQuery('id', current.sys_id);
    gr.addQuery('calculation_complete', false);
    gr.addQuery('definition.type', 'field_value_duration');
    gr.query();
    while (gr.next()) {
       var definition = new GlideRecord('metric_definition');
       definition.get(gr.definition);
       var mi = new MetricInstance(definition, current);
       mi.endDuration();
    }
}

 

View solution in original post

6 REPLIES 6

Steven Slocum
Tera Guru

Since you likely won't be resolving a catalog task, change the metric definition to instead calculate on the time spent as "active"

Name: Open_SCTASK

Table: sc_task

Field: Active

Type: Field Value Duration

Description: Special case that closes out (ends) other metrics when an the active flag in the sc_task turns false

 

 

Script:

// script can set answer to false to terminate processing of the metric
// mi - MonitorInstance
// answer
if (!current.active) {
  answer = false;
  mi.endDuration();
  closeDurations(mi.current);
}

function closeDurations(current) {
    var gr = new GlideRecord('metric_instance');
    gr.addQuery('id', current.sys_id);
    gr.addQuery('calculation_complete', false);
    gr.addQuery('definition.type', 'field_value_duration');
    gr.query();
    while (gr.next()) {
       var definition = new GlideRecord('metric_definition');
       definition.get(gr.definition);
       var mi = new MetricInstance(definition, current);
       mi.endDuration();
    }
}

 

Thank you! That corrected the metric to only have one row with the duration. 

So I put this into our production last night, and it's bringing in task that have a calculation complete of false. When I tested this in Dev it only brought back calculation complete true. Do I need to change the false in this line to true: 

gr.addQuery('calculation_complete', false);

Hey Steven. I have a question about the metric script. The duration is getting populated in the metric table, but it's not getting populated in the sc_task or task tabl. How do I get it to show up in either one of those tables? 

Thank you!

Austin.