- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-01-2024 08:46 AM
I have a basic metric that calculates the time it takes from when a group is assigned to the first time assigned to it is populated. I cannot use the duration value as hoped in a report. How could I update the script to calculate a numeric value representing hours from the metric script's duration value and populate it in the "value field"?
The script I am currently using:
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-01-2024 10:34 AM - edited ‎10-01-2024 10:37 AM
Hi @Scott29,
the following script logic may help you. Tested in Scripts - Background:
var mi = new GlideRecord('metric_instance');
mi.addEncodedQuery('duration!=NULL');
mi.query();
while (mi.next()) {
gs.info("Table: " + mi.table + ", field: " + mi.field + ", duration = " + mi.duration);
var durDateTime = new GlideDateTime(mi.duration);
gs.info('durDateTime = ' + durDateTime);
var noSeconds = durDateTime.getNumericValue() / 1000;
gs.info('noSeconds = ' + noSeconds);
var rem = noSeconds % 3600; // get remainder (minutes, seconds) we don't care about
var noHours = (noSeconds - rem) / 3600; // divide by no of seconds in a hour
gs.info('noHours = ' + noHours);
}
You can remove the 'gs.info()' lines used for debugging. You could add a custom field to the metric_instance table to hold the hours value, or create a custom table with desired fields to report on.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-01-2024 10:34 AM - edited ‎10-01-2024 10:37 AM
Hi @Scott29,
the following script logic may help you. Tested in Scripts - Background:
var mi = new GlideRecord('metric_instance');
mi.addEncodedQuery('duration!=NULL');
mi.query();
while (mi.next()) {
gs.info("Table: " + mi.table + ", field: " + mi.field + ", duration = " + mi.duration);
var durDateTime = new GlideDateTime(mi.duration);
gs.info('durDateTime = ' + durDateTime);
var noSeconds = durDateTime.getNumericValue() / 1000;
gs.info('noSeconds = ' + noSeconds);
var rem = noSeconds % 3600; // get remainder (minutes, seconds) we don't care about
var noHours = (noSeconds - rem) / 3600; // divide by no of seconds in a hour
gs.info('noHours = ' + noHours);
}
You can remove the 'gs.info()' lines used for debugging. You could add a custom field to the metric_instance table to hold the hours value, or create a custom table with desired fields to report on.