Excerpt |
---|
A Calculated Date/Time custom field type returning a |
Adding a Calculated Date/Time custom field type
To add a Calculated Date/Time custom field type to your instance:
- Log in to JIRA as an administrator.
- Go to the Administration icon and click on it.
- Click on
Issues - > Custom fields.
- Click on
Add custom field
. - Click on
Advanced
in the left panel. - Locate
Calculated Date/Time Field type
and select it. - Click on
Next
. - Provide a name for the custom field
- You can optionally provide a description for the custom field.
- Click on
Create
- Associate the custom field to the appropriate screens.
- Locate the custom field in the Custom fields administration page and click on the cog wheel.
- Click on
Configure
and create a formula (explained below) for the field. - You can create multiple contexts if you need to associate different formulas with particular projects or issue types.
- Perform a re-index as prompted by JIRA.
Panel | ||
---|---|---|
| ||
|
Panel | ||
---|---|---|
| ||
Note |
---|
If you have upgraded from JMCF |
Search template
When creating a Calculated Date/Time custom field type, you can configure the Search template as Date range picker
to be able to search issues within a date/time range of values for that custom field. If you select “None” for the searcher, 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.
Configuring the Groovy formula for a Calculated Date/Time custom field type
Note |
---|
Starting from 2.0.0 BeanShell has been replaced by Groovy in the JMCF add-on. Scripts written in BeanShell will work unchanged in Groovy except for some specific cases. |
To write a formula for the Calculated Date/Time custom field type;
- Locate the custom field on the Custom Fields administration page.
- Click on the cog wheel and click on
Configure
- Click on
Edit Groovy Formula
- In the editor write a Groovy script that returns a
java.util.Date
. You can test you written script against any issue using the Groovy script tester. - Click on
Save.
Some examples of a Groovy formula:
A Groovy expression returning a java.util.Date.
For example returning the current date/time
Code Block | ||||
---|---|---|---|---|
| ||||
new Date() |
A Groovy expression that references any field of the issue that returns a java.util.Date.
For example, storing the issue updated date/time.
Code Block | ||||
---|---|---|---|---|
| ||||
issue.get("updated") |
A Groovy expression that includes arithmetic operators as well as any other Groovy operator, and Groovy method calls. For example, adding a specific number of days to the issue creation date.
Code Block | ||||
---|---|---|---|---|
| ||||
issue.get("created") + 5 |
Note |
---|
Make sure that the formula returns either a |
Customizing the display of the Calculated Date/Time custom field type value
By default, the Calculated Date/Time custom field type value is displayed using Jira's default date format (if you leave the Date Format
to Default
). However, you can customize it. To select a format for a Calculated Date/Time custom field type value;
- Locate the custom field on the Custom Fields administration page.
- Click on the cog wheel and click on
Configure
Click on
Edit Date Format.
- Select a format from the
Date format
drop-down. - Click on
Save.
Customizing the display of the Calculated Date/Time custom field type value in the List views
By default, the Calculated Date/Time custom field type value is displayed using Jira's default date format (if you leave the Date Column Format
to Default
) in the tabular views. However, you can customize it. To select a format for a Calculated Date/Time custom field type value in the List views;
- Locate the custom field on the Custom Fields administration page.
- Click on the cog wheel and click on
Configure
Click on
Edit Date Column Format.
- Select a format from the
Date format
drop-down. - Click on
Save.
COMPLETE
option in the Edit Date Format/Edit Date Column Format
, and the value returned by the Groovy formula is Thu Jan 25 15:20:34 IST 2018
, then the value will be formatted to 25/Jan/18 3:20 PM
.
Status | ||||
---|---|---|---|---|
|
Excerpt |
---|
Adding a Calculated Duration custom field type
To add a Calculated Duration custom field type to your instance:
- Log in to JIRA as an administrator.
- Go to the Administration icon and click on it.
- Click on
Issues - > Custom fields.
- Click on
Add custom field
. - Click on
Advanced
in the left panel. - Locate
Calculated Duration Field type
and select it. - Click on
Next
. - Provide a name for the custom field.
- You can optionally provide a description for the custom field.
- Click on
Create.
- Associate the custom field to the appropriate screens.
- Locate the custom field in the Custom fields administration page and click on the cog wheel.
- Click on
Configure
and create a formula (explained below) for the field. - You can create multiple contexts if you need to associate different formulas with particular projects or issue types.
- Perform a re-index as prompted by JIRA.
Panel | ||
---|---|---|
| ||
|
Panel | ||
---|---|---|
| ||
Note |
---|
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;
- Locate the custom field on the Custom Fields administration page.
- Click on the cog wheel and click on
Configure
- Click on
Edit Groovy Formula
- 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.
- 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:
Code Block language groovy linenumbers true 144000L
A Groovy expression returning a duration String
Code Block language groovy linenumbers true "3w 2d"
A Groovy expression returning the "Original estimate" of the issue:
Code Block language groovy linenumbers true issue.get("originalestimate")
A Groovy expression adding a week time to the Time Spent on the issue
Code Block language groovy linenumbers true 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
Code Block language groovy linenumbers true 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.
Precision | Stored value | Duration searcher (24x7 durations) | |||||
---|---|---|---|---|---|---|---|
Default - One second | 174930 | 2 days, 35 minutes
| 1 week, 1 day, 35 minutes
| ||||
One minute | 174960 | 2 days, 36 minutes | 1 week, 1 day, 36 minutes | ||||
One hour | 176400 | 2 days, 1 hour | 1 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
Precision | Stored value | Duration searcher (24x7 durations) | |
---|---|---|---|
Default - One second | 1494175 | 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 minute | 1494180 | 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 hour | 1494000 | 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 day | 1468800 | 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 week | 1209600 | 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 as1day, 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.
- 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,
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;
- Locate the custom field on the Custom Fields administration page.
- Click on the cog wheel and click on
Configure
- Click on
Edit Velocity Template
. - Input the template.
- Click on
Save.
- Click on the cog wheel and click on
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:
Code Block |
---|
<div style="border: dashed">
$formattedValue
</div> |
displays the formatted value of the field with a dashed border.
Code Block |
---|
#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;
- Locate the custom field on the Custom Fields administration page.
- Click on the cogwheel and click on
Configure
- Click on
Edit List View Velocity Template
. - Input the template.
- Click on
Save.
- Click on the cogwheel and click on
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:
Code Block |
---|
<div style="border: dashed">
$formattedValue
</div> |
displays the formatted value of the field with a dashed border.
Code Block |
---|
#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
get("Your custom field name") or get("customfield_xxxxx")
that returns a number in a Double format.getAsString("Your custom field name")
or
getAsString("customfield_xxxxx")
that returns aString
representing the duration in number of seconds