This documentation is for an old version of Dataplane Reports.
View the latest documentation, or the list of documentation for all versions.
Reporting on Calculated Values with ScriptRunner
- Scott Dudley
- David Goldstein
Dataplane supports reporting on (and charting of) calculated fields created with the ScriptRunner add-on.
ScriptRunner allows administrators to create customized and programmable Scripted Fields that can be used almost anywhere that a regular Jira field can be used, and which can perform arbitrary calculations on issue-related data using the Groovy language.
Using ScriptRunner with Dataplane
Dataplane can use your existing Scripted Fields, or you can add new fields to be used in conjunction with Dataplane.
Dataplane supports scripted fields that contain numeric, date, and text data. Scripted Fields can be used in most reports in Dataplane.
Since Scripted Field values are calculated on the fly and changes to a Scripted Field are not stored in the Jira changelog, only the current values of Scripted Fields (and not the historical previously-calculated values) can be used in Dataplane.
To add a Scripted Field for use with Dataplane, follow the steps below. Existing scripted fields can also be used, so long as the Search Template is configured for the appropriate data type (as described below).
Creating a Scripted Field
To create a new Scripted Field:
- Install the ScriptRunner add-on
- Navigate to Toolgear » Issues » Custom Fields
- Click Add Custom Field
- Click on the Advanced tab on the left-hand side of the dialog
- Find the Scripted Field field type and click Next
- Give the new field a name and then click Create
- If you do not want the scripted field to be visible to end users, do not associate the field with any screens. You may, however, want to associate the field with the Default or View screens for initial testing purposes, since you will not otherwise be able to see the rendered results of your field.
- Scroll down the custom field list to find your newly-created field, click the toolgear icon on the right hand side, and select Edit
Configure the field's Search Template option based on the type of data you wish to store in the field:
To use the field in Dataplane as... ...select for Search Template: Numeric data "Number Search" or "Number Range Searcher" Date "Date Time Range Picker" Text "Free Text Searcher" - Finally, click Update to save the changes to the field.
The scripted custom field's Search Template option controls the data type of the field as seen by Dataplane, and it influences the reports in which the custom field is available.
If you do not configure the Search Template option, the new custom field will not report any data when used in Dataplane reports.
Writing a Script for the Scripted Field
To define the functionality of the newly added Scripted Field:
- Navigate to Toolgear » Add-ons and select Script Fields on the left-hand navigation menu
- Scroll down to find your field, click the edit link, and write the script for your field
You must reindex Jira if you have either newly added or modified a custom field script. This is required to make the updated field values visible to Jira and Dataplane.
You must also navigate to Dataplane Reports » Administration and click the "Sync Index" button to refresh the list of custom fields available in Dataplane reports. Performing this synchronization is required only after adding an entirely new field (or changing the Search Template of an existing field). If you have simply updated the script for a field, reindexing Jira itself is sufficient – no additional Dataplane operations need be performed.
The following tips are helpful for script testing, and for speeding up a Jira reindex:
- For testing your script, before doing a full reindex of Jira, you can perform an edit and save on a single Jira issue, or perform a transition on a single issue, and ScriptRunner will update the value of the scripted field for that issue only. You can then see the results in the Issue Navigator columns list (by adding the Scripted Field column) or when viewing the issue (if you added the custom field to the appropriate screen)
- If your version of Jira supports it, you can perform a background reindex so that no downtime is required.
- Performing a reindex of only a single project is also possible.
Script Examples
This section provides examples of a few scripts that access issue data.
The ScriptRunner documentation on Scripted Fields provides many more examples of how to use scripted fields. Most Groovy language concepts can be used within scripted fields, including the ability to access other custom field values, perform arithmetic, and call arbitrary Java or Groovy APIs.
Numeric Scripted Field
The following is an example of a very simple script that obtains the value of an existing custom field named "Product Price", multiplies the value by 50%, and returns the result.
The field for this script must be configured with a Search Template of "Number Search" or "Numeric Range Searcher", as described above, in order to be usable in numeric reports.
def price = getCustomFieldValue("Product Price") if (price) { return price * 0.5; } else { return null }
Date Scripted Field
The following is a simple script that subtracts one week from the value of an existing Date custom field called "Payment Date".
The field for this script must be configured with a Search Template of "Date Time Range Picker", as described above, in order to be selectable as a Date Basis in reports that support dates.
def paymentDate = getCustomFieldValue("Payment Date") if (paymentDate) { def cal = new java.util.GregorianCalendar(); cal.setTimeInMillis(paymentDate.getTime()); cal.add(java.util.Calendar.DAY_OF_MONTH, -7); return new java.sql.Timestamp(cal.getTimeInMillis()); } else { return null }
String Scripted Field
In this example we take the current value of an existing, select list field named "Team" and map that value to a geographic location.
The field for this script should be configured with a Search Template of "Free Text Searcher".
final Map<String,String> LOCATION_MAP = new HashMap<String,String>() {{ put("Team A", "San Francisco") put("Team B", "San Francisco") put("Team C", "Montreal") put("Team D", "Montreal") put("Team E", "Montreal") }}; def cfValue = getCustomFieldValue("Team") if (cfValue != null) { def location = LOCATION_MAP.get(cfValue.toString()) if (location != null) { return location } } return "" // no match found
In the following example we use the same remapping concept to group an issue's possible statuses into a smaller set of values.
This is a common solution for presenting reports to management using a less granular view of the engineering workflow.
import com.atlassian.jira.issue.status final Map<String,String> STATUS_MAP = new HashMap<String,String>() {{ put("Open", "New") put("In Triage", "New") put("Assigned", "In Engineering") put("Work in Progress", "In Engineering") put("In QA", "In Engineering") put("Waiting for Customer", "Waiting") put("Waiting for Approval", "Waiting") put("QA Approved", "Ready") put("Ready for Deployment", "Ready") put("Closed", "Done") put("Reopened", "In Engineering") }}; def origStatus = issue.getStatus().getSimpleStatus().getName() def newStatus = STATUS_MAP.get(origStatus) return (newStatus != null) ? newStatus : origStatus
First Level of a Cascading Select List Field
Dataplane directly supports reporting on Jira Cascading Select List custom fields, and will aggregate report results based on each unique combination of first and second level selections as in "US : San Francisco", "US : New York", "Canada : Toronto" and "Canada : Montreal".
Sometimes it can be useful though to aggregate report results based only on the first level selection in the Cascading Select List field, as in "US" and "Canada" in the above examples. To do this, set up a new ScriptRunner custom field configured with a Search Template of "Free Text Searcher".
Use the following script to return just the first level selection in the Cascading Select List field:
import com.atlassian.jira.component.ComponentAccessor import com.atlassian.jira.issue.CustomFieldManager import com.atlassian.jira.issue.customfields.option.Option import com.atlassian.jira.issue.fields.CustomField // don't change any of these def SELECT_LEVEL_1 = null def SELECT_LEVEL_2 = "1" def EMPTY_STRING = "" // modify these def CASCADING_SELECT_FIELD_NAME = "My Field" // name of your cascading select field def SELECT_LEVEL = SELECT_LEVEL_1 // grab either the 1st or 2nd level only CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager() CustomField cf = customFieldManager.getCustomFieldObjectByName(CASCADING_SELECT_FIELD_NAME) HashMap<String, Option> value = (HashMap<String, Option>)issue.getCustomFieldValue(cf) return (value != null) ? value.get(SELECT_LEVEL) : EMPTY_STRING
Page Contents