Our new Appfire Documentation Space is now live!

Take a look here! If you have any questions please email support@appfire.com

Calculated (Scripted) Duration custom field type

   NEW IN JMCF 2.0.0


A Calculated Duration custom field type represents a duration and is displayed either as a duration string or as a number of seconds. It is a read-only field.

Adding a Calculated Duration custom field type

To add a Calculated Duration custom field type to your instance:

  1. Log in to JIRA as an administrator.
  2. Go to the Administration icon  and click on it.
  3. Click on Issues - > Custom fields.
  4. Click on Add custom field.
  5. Click on Advanced in the left panel.
  6. Locate Calculated Duration Field type and select it.
  7. Click on Next.
  8. Provide a name for the custom field.
  9. You can optionally provide a description for the custom field.
  10. Click on Create.
  11. Associate the custom field to the appropriate screens.
  12. Locate the custom field in the Custom fields administration page and click on the cog wheel.
  13. Click on Configure and create a formula (explained below) for the field.
  14. You can create multiple contexts if you need to associate different formulas with particular projects or issue types.
  15. Perform a re-index as prompted by JIRA.

If you select "CURRENT STATUS", the field returns the Time spent in the current status since the last status change. It will not take into account any time spent in the same status in the past. For example, if an issue spends 1 day in the "To Do" status, then 2 days in the "In Progress" status, then goes back to the "To Do" status, the field will only return the time elapsed since the issue last entered the "To Do" status, disregarding the 1 day it has spent in the same status before.

Search template

When creating a Calculated Duration custom field type, you can configure the Search template as 

  • Duration searcher (24x7 durations) Allows searching for a duration. Durations are considered as calendar durations. Supports Statistics Gadgets.
  • Time Tracking Duration searcher (work time durations) Allows searching for a duration. Durations are considered as work time durations. Supports Statistics Gadgets.
  • Number range searcher - Allows searching for a number that is in a given range.

Using these searchers you will be able to search for issues within a range of values for that custom field. The default search template is Duration searcher (24x7 durations)If you select None, then you won’t see any reference to this field in the Issue Navigator search fields. Note that changing a custom field searcher requires a re-index

Duration searcher (24x7 durations) and Time Tracking Duration searcher are custom statistics compatible searchers provided by JMCF that allow Duration type custom fields of JMCF to be used in the statistics gadgets. See here for more information. 

Configuring the Groovy formula for a Calculated Duration custom field type

To write a formula for the Calculated Duration custom field type;

  1. Locate the custom field on the Custom Fields administration page.
  2. Click on the cog wheel and click on Configure
  3. Click on Edit Groovy Formula
  4. In the editor write a Groovy script that returns a duration String or a number in a Long format representing the number of seconds. Also, you can test your script against any issue using the Groovy script tester.
  5. Click on Save.

As documented on the Expected Value tab of the Groovy editor help, the Groovy formula must return either:

  •  A number in a Long format representing a number of seconds (e.g. 3600)
  • A String representing a duration in Jira duration format (e.g. "1w 3d 5h")

Examples of Groovy formula for a Calculated Duration custom field:

  • A Groovy expression returning a number in a Long format representing a number of seconds:

    144000L
  • A Groovy expression returning a duration String

    "3w 2d"
  • A Groovy expression returning the "Original estimate" of the issue:

    issue.get("originalestimate")
  • A Groovy expression adding a week time to the Time Spent on the issue

    if(issue.timespent){
     return issue.timespent + 144000
    }
    else{
      return null
    }
  • A Groovy expression that returns the duration from issue creation to now. The difference between two date objects returns the number of days and hence you need to convert it into seconds since the calculated field expects a number of seconds

    return (new Date() - issue.created)*24*60*60

Precision

By default, durations are stored with a precision of one second. If you'd like a lower precision, you can round up values to the nearest minute, hour, day or week, which will impact how values are displayed both on individual issues and on the statistics dashboard gadgets. To round up values select the unit from the Precision field. The default is One second. 

Here are a few examples that help you in understanding further.

Example 1: If the calculated duration field evaluates to 174930 seconds, then with One-minute precision, the value is rounded off to 174960.

PrecisionStored valueDuration searcher (24x7 durations)
Default - One second174930

2 days, 35 minutes

Jira will not display the seconds (30 sec in this case)

1 week, 1 day, 35 minutes

Jira will not display the seconds (30 sec in this case)

One minute1749602 days, 36 minutes1 week, 1 day, 36 minutes
One hour1764002 days, 1 hour1 week, 1 day, 1 hour

If the calculated duration field evaluates to 1494175 seconds which accounts to 2 weeks, 3 days, 7 hours, 2 minutes, 55 seconds or 10 weeks, 1 day, 7 hours, 2 minutes, 55 seconds (depends on the searcher) with the following precisions

PrecisionStored valueDuration searcher (24x7 durations)
Default - One second1494175

2 weeks, 3 days, 7 hours, 2 minutes

Rounding: Jira doesn't display the seconds (55 sec in this case)

10 weeks, 1 day, 7 hours, 2 minutes

Rounding: Jira doesn't display the seconds (55 sec in this case)

One minute1494180

2 weeks, 3 days, 7 hours, 3 minutes

Rounding: The 55 seconds is rounded off to one minute

10 weeks, 1 day, 7 hours, 3 minutes

Rounding: The 55 seconds is rounded off to one minute

One hour1494000

2 weeks, 3 days, 7 hours

Rounding: The 3 minutes are ignored and the value is rounded off.

10 weeks, 1 day, 7 hours

Rounding: The 3 minutes are ignored and the value is rounded off.

One day1468800

2 weeks, 3 days

Rounding: The 7 hours are rounded off to 8 hours.

10 weeks, 2 days

Rounding: The 7 hours are ignored and the value is rounded off.

One week1209600

2 weeks

Rounding: The 3 days are ignored and the value is rounded off.

10 weeks

Rounding: The 3 days are ignored and the value is rounded off.

Customizing the formatting of the Calculated Duration custom field value

For a Calculated Duration field, the Search Template also determines how durations are displayed, and how duration strings returned by the formula are parsed. By default, the search template is Duration searcher (24x7 durations). However, you can customize it by changing the Search template. When you select the template,

  • Duration searcher (24x7 durations), the duration is displayed in general duration format, where a day has 24 hours. For example, 144000 seconds is displayed as 1day, 16hours.
    • Duration (Time Tracking Format), the duration is displayed as configured in Jira settings, where a day has 8 hours by default and a week has 5 days. For example, 144000 seconds is displayed as 1w.
    • Number of seconds, the duration is displayed as a number in a Long format representing the number of seconds.
    • None, the duration is displayed in general duration format, where a day has 24 hours.

Customizing the display of the Calculated Duration custom field value on the issue view

You can also define a velocity template that will return the Html representation of the calculated custom field on the issue detail view. Leave it empty to use the default template. 

To write a velocity template for a Calculated Duration custom field type value;

  1. Locate the custom field on the Custom Fields administration page.
    1. Click on the cog wheel and click on Configure
    2. Click on Edit Velocity Template.
    3. Input the template.
    4. Click on Save.

You can use the following Velocity variables in the template:

  • $value: the raw field value, a Long value representing a number of seconds
    • $formattedValue: the field value formatted according to the field configuration above
    • and other variables described on this page

For example:

<div style="border: dashed">
  $formattedValue
</div>

displays the formatted value of the field with a dashed border.

#if( $value > 2 )
  <font color= "red">
    <b> Delayed by $formattedValue </b>
    </font>
#else
  <font color= "blue">
      Delayed by $formattedValue
</font>
#end

Customizing the display of the Calculated Duration custom field value on the list view

You can also define a velocity template that will return the Html representation of the calculated custom field on the search result list views. Leave it empty to use the default template. 

To write a velocity template for a Calculated Duration custom field type value;

  1. Locate the custom field on the Custom Fields administration page.
    1. Click on the cogwheel and click on Configure
    2. Click on Edit List View Velocity Template.
    3. Input the template.
    4. Click on Save.

You can use the following Velocity variables in the template:

  • $value: the raw field value, a Long value representing a number of seconds
    • $formattedValue: the field value formatted according to the field configuration above
    • and other variables described on this page

For example:

<div style="border: dashed">
  $formattedValue
</div>

displays the formatted value of the field with a dashed border.

#if( $value > 2 )
  <font color= "red">
    <b> Delayed by $formattedValue </b>
    </font>
#else
  <font color= "blue">
      Delayed by $formattedValue
</font>
#end

Displaying the Calculated Duration custom field on Transition and Edit screens

The Never show on Transition and Edit screens option controls whether the calculated Duration custom field can appear on transition and edit screens. If you select this option, the field will not be visible on any Edit or Transition screen, even if it is added to the screen.

Accessing the Calculated Duration custom field type from other Groovy scripts

You can access this field using any of the following getters of the Issue interface