Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

Extract time in status

Deleted user April 6, 2017

Hi,

I want to create a scripted field in scriptrunner that calculates time in status (like Jira suite utilities) in certain conditions. 

Is the best way to create some custom fields that populate a date and time or can I script this?

 

10 answers

2 accepted

Suggest an answer

Log in or Sign up to answer
2 votes
Answer accepted
JohnsonHoward
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
April 11, 2017

Can you show me your code please?

Deleted user April 11, 2017
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.history.ChangeItemBean

def changeHistoryManager = ComponentAccessor.getChangeHistoryManager()

def inProgressName = "Assess 2nd Line"

List<Long> rt = [0L]
def changeItems = changeHistoryManager.getChangeItemsForField(issue, "status")
changeItems.reverse().each {ChangeItemBean item ->
    item.toString == inProgressName

    def timeDiff = System.currentTimeMillis() - item.created.getTime()
    if (item.fromString == inProgressName) {
        rt << -timeDiff
    }
    if (item.toString == inProgressName){
        rt << timeDiff
    }
}

def total = rt.sum() as Long
return total ?: 0L
JohnsonHoward
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
April 11, 2017

Try adding this return instead of the one you have:

def total = rt as int []
return DateUtils.getDurationString(Math.round(total.sum() / 1000))
JohnsonHoward
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
April 11, 2017

You will have to import: 

import com.atlassian.core.util.DateUtils

Deleted user April 12, 2017

2017-04-12 09_59_07-Script Fields - Objectway Jira Test - Opera.pngNow I'm getting an error

 

JohnsonHoward
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
April 12, 2017

What are you passing into the getDuration() method? 

 

Deleted user April 12, 2017

It's actually a double error.

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.history.ChangeItemBean
import com.atlassian.core.util.DateUtils

def changeHistoryManager = ComponentAccessor.getChangeHistoryManager()

def inProgressName = "Assess 2nd Line"

List<Long> rt = [0L]
def changeItems = changeHistoryManager.getChangeItemsForField(issue, "status")
changeItems.reverse().each {ChangeItemBean item ->
    item.toString == inProgressName

    def timeDiff = System.currentTimeMillis() - item.created.getTime()
    if (item.fromString == inProgressName) {
        rt << -timeDiff
    }
    if (item.toString == inProgressName){
        rt << timeDiff
    }
}

def total = rt as int []
return DateUtils.getDurationString(Math.round(total.sum() / 1000))

2017-04-12 10_31_13-Script Fields - Objectway Jira Test - Opera.png

JohnsonHoward
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
April 12, 2017

This removes the errors I believe:

rt = rt as long []
def total = rt.sum()/ 1000 as long
return DateUtils.getDurationString(total)

You wil need the template for the scripted field to be Text-Field. Otherwise it will fail.

 

Thanks

Deleted user April 12, 2017

Still errors:2017-04-12 16_07_43-Script Fields - Objectway Jira Test - Opera.png2017-04-12 16_07_51-Script Fields - Objectway Jira Test - Opera.png

JohnsonHoward
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
April 12, 2017

Apologies, try this:

def newRT = rt as long []
def total = newRT.sum()/ 1000 as long
return DateUtils.getDurationString(total)

Does that work?

 

Deleted user April 12, 2017

It works like a charm! No, I can build on this.

 

Thanks!!

 

JohnsonHoward
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
April 12, 2017

No problem! 

Bill Tanner November 16, 2017

I tried this scripted field as well, and I always get 0 min as the time in status.  No errors shown in code:

 

time in status script.PNG

danielho95 August 15, 2018

Same problem as you Bill, did you find a fix by any chance?

Bill Tanner August 16, 2018

I have switched to using a new add-on called Issue History Collector:

https://marketplace.atlassian.com/apps/1211499/issue-history-collector?hosting=server&tab=overview

And, it is free.  

Just follow the documentation and it is pretty easy to get up an running.

danielho95 August 16, 2018

Will check it out, thanks for the reply! Really appreciate it

Slava Gefen June 13, 2023

Hey Guys, what's about Cloud?

Is it possible to somehow implement it there?

1 vote
Answer accepted
JohnsonHoward
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
April 11, 2017

Hi Robin,

Take a look at this link: https://scriptrunner.adaptavist.com/latest/jira/scripted-fields.html#_total_time_this_issue_has_been_in_progress

You can edit this code to show the total time for any status.

Is that what you are looking for?

Thanks,

Johnson Howard

 

Deleted user April 11, 2017

It looks like it, but the values are not correct :)

test.png

It was in the status for less than a minute.

Kind regards,

Robin

Like Sam likes this
0 votes
Valeriia_Havrylenko_SaaSJet
Marketplace Partner
Marketplace Partners provide apps and integrations available on the Atlassian Marketplace that extend the power of Atlassian products.
February 15, 2024

Hi @Slava Gefen  and everyone who is looking for solution 😌

As an alternative, for advanced analysis you can try - Time in Status for Jira (Cloud, Data Center), (developed by SaaSJet) that generates 7 types of status reports including Status Entrance Date report. It shows the date a particular issue has entered each of a status (Resolved or Closed, etc.).

9e9e93ff-9254-4123-8774-817cc45b769d.png

  • set date or date ranges you need
  • get dates when the issue has entered each of the statuses on the grid.

 
On the chart, it shows the number of tasks that have entered a status on a certain date - for example, 1 task out of 5 tasks has entered the Progress status for the first time on the 23-rd of May.

 5f12830b-97ce-442c-ae4c-f964e527c72c.png

Add-on has a 30-day free trial version and free up to 10 users 

Hope it helps 😌

0 votes

This function work for me:

 

def Long getTimeInStatus(String STATUS_ID, issue) {

    def list = ComponentAccessor.changeHistoryManager.getChangeItemsForField(issue, "status").findAll {
        it.to == STATUS_ID || it.from == STATUS_ID
    }.sort { it.created }
    if (list.size() == 0) {
        return new Date().time - issue.created.time
    }
    def timeInStatus = 0;
    def isLastItem = false
    while (!isLastItem) {
        def itemTo = list.find { it.to == STATUS_ID && it.to != it.from }
        if (itemTo) {
            list = list - itemTo
            def itemFrom = list.find { it.from == STATUS_ID && it.to != it.from }
            if (itemFrom) {
                list = list - itemFrom
                timeInStatus += itemFrom.created.time - itemTo.created.time
            } else {
                isLastItem = true
                timeInStatus += new Date().time - itemTo.created.time
                return Long.divideUnsigned(timeInStatus, 1000)
            }
        } else {
            isLastItem = true
            return Long.divideUnsigned(timeInStatus, 1000)
        }
    }
    return Long.divideUnsigned(timeInStatus, 1000);
}
0 votes
Liam Maeder March 18, 2022

So I am using a longer method for getting time in all statuses, see the example below. I am having 2 issues, one I have resolved by using my own calculations:

  1. First issue I had was using the DateUtils.getDurationString(total) method, which kept on returning 0m, I used this:
    • def newRT = rt as long []
      rt.clear()
      def total = newRT.sum()/ 1000 as long

      def seconds = total.intValue()
      def iD = (seconds / 85400).round(0)
      def days = seconds % 86400
      def iH = (days / 3600).round(0)
      def hours = days % 3600
      def iM = (hours /60).round(0)
      def strTimeStr = sName + ": " + iD.toString() + "d " + iH.toString() + "h " + iM.toString() + "m" + " | "
      strResult += strTimeStr

  2. The above solved the output of the time  in strings, however with Backlog I have a weird total, I will iclude the screenshot below:
    •  image.png
    • Expected 12s

Here is the script below (I am going to add logic to the output to only print d m h if there are values for them):

//Import the relevant libraries
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.history.ChangeItemBean

//Define the variables to be used
def strResult = ""
List<Long> rt = [0L]
List<String> statusName = []
def changeHistoryManager = ComponentAccessor.getChangeHistoryManager()
def changeItems = changeHistoryManager.getChangeItemsForField(issue, "status")

//Validate that there is only one of each change item name
for(item in changeItems){
    if(statusName.contains(item.getToString())){
        //Do Nothing
    } else {
        statusName << item.getToString()
    }
}

//Get time difference for each status and concatinate onto strResult
for(sName in statusName){
    changeItems.reverse().each { ChangeItemBean item ->
        if (changeItems.indexOf(sName) == 0){
            rt << issue.create.getTime() - item.create.getTime()
        } else {
            item.fromString == sName
            def timeDiff = System.currentTimeMillis() - item.created.getTime()
         
            if (item.fromString == sName) {
                rt << -timeDiff
            }

            if (item.toString == sName) {
                rt << timeDiff
            }
        }
    }

    //Create the output string
    def newRT = rt as long []
    rt.clear()
    def total = newRT.sum()/ 1000 as long

    def seconds = total.intValue()
    def iD = (seconds / 85400).round(0)
    def days = seconds % 86400
    def iH = (days / 3600).round(0)
    def hours = days % 3600
    def iM = (hours /60).round(0)
    def strTimeStr = sName + ": " + iD.toString() + "d " + iH.toString() + "h " + iM.toString() + "m" + " | "
    strResult += strTimeStr
}

return strResult

If anyone could provide advice for including something to calculate for the first status of the workflow? I know the majority of our projects use Backlog but there may be a few that use another status in the beginning of the workflow

Thanks in advance

0 votes
Chhaya Gadade December 20, 2019

// You can try this  [I am new to script runner please check twice][Added by Sanjay]

 

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.history.ChangeItemBean
import com.atlassian.core.util.DateUtils
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.user.ApplicationUser
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder
import com.atlassian.jira.bc.issue.IssueService.UpdateValidationResult;
import com.atlassian.jira.user.ApplicationUser
import com.atlassian.jira.bc.issue.IssueService;
import com.atlassian.jira.bc.issue.IssueService.IssueResult;
import com.atlassian.jira.issue.IssueInputParameters;
import com.atlassian.jira.bc.issue.IssueService.IssueResult;

IssueManager im = ComponentAccessor.getIssueManager()
def customFieldManager =ComponentAccessor.getCustomFieldManager();
def Time_in_Current_Status = customFieldManager.getCustomFieldObject("customfield_10803")
MutableIssue issue = im.getIssueObject("DP-2") //STATIC VALUE [need to change]
def changeHistoryManager = ComponentAccessor.getChangeHistoryManager()
def name = issue.getStatus().name;
log.warn(name); //Troubleshoot

List<Long> rt = [0L]
def changeItems = changeHistoryManager.getChangeItemsForField(issue, "status")
changeItems.reverse().each {ChangeItemBean item ->
item.toString == name
def timeDiff = System.currentTimeMillis() - item.created.getTime()
if (item.fromString == name) {
rt << -timeDiff
}
if (item.toString == name){
rt << timeDiff
}
}

def newRT = rt as long []
def total = newRT.sum()/ 1000 as long
def mytotal =DateUtils.getDurationString(total)
IssueService issueService = ComponentAccessor.getIssueService();
IssueInputParameters issueInputParameters = ComponentAccessor.issueService.newIssueInputParameters();
log.warn(issueInputParameters.addCustomFieldValue(10803,mytotal)) //10803 is a custom field id
def autoUser = ComponentAccessor.getUserManager().getUserByName("admin") as ApplicationUser
UpdateValidationResult updateValidationResult = issueService.validateUpdate(autoUser, issue.id, issueInputParameters);
if (updateValidationResult.isValid())
{
IssueResult updateResult = issueService.update(autoUser, updateValidationResult);
if (updateResult.isValid())
{
log.warn("Approvals Added successfully")
}else{
updateResult.getErrorCollection().getErrors().each{ error->
log.warn("Approvals not added bcz:"+error)
}
}
}else{
updateValidationResult.getErrorCollection().getErrors().each{ error->
log.warn("Approvals not added bcz:"+error)
}
}
return "Time duration of issue in status "+name+" is "+mytotal

0 votes
david_parry February 26, 2019

The display of the data works, but querying is inconsistent.

You are in effect using datetime.now() as a basis of your scripts calculation, which in itself is a non-deterministic function. (On ANY platform you shouldnt really ever index a piece of data that is based on a non-deterministic function...but I wanted to reprove this to myself in JIRA too!)

My own objective was to be able to alert when a ticket has been in any given state which exceeds the time-estimate for it (to highlight flow hotspots)

I created a field: "Time in current status" that effectively has a value of now() - date time of last transition (simplified version of script above: just gets the first entry and does the calculation, or if no transition entry then use now() - created date)

Putting my field on the Kanban cards always displayed the right value, but JQL filters were inconsistent. The data in the field was only ever saved if the issue was updated + saved in some way.

My test script was thus:

Create an issue, kanban shows 0 minutes in "Time in current status" field

After 10 minutes refresh the kanban board: issue shows 10 minutes in "Time in current status"

Run JQL: "Time in current status" > 9m : issue doesn't show (bad)

Immediately edit a field (eg Description or Summary) to trigger issue data to be saved

Run JQL: "Time in current status" > 9m : issue shows (good)

I waited 2 minutes:

Run JQL: "Time in current status" > 11m : issue doesn't show (bad)

Then re-index my project (triggers script to run on each issue: NB this is potential danger of using scripted fields)

Run JQL: "Time in current status" > 11m : issue shows (good)

The message is: think carefully what you are doing with any data in your script and whether it can actually support queries on that data - does it rely on a piece of data that can change, without triggering an update to the scripted field on your issue. 

Always ask yourself: when using data external to the issues fields for any calculations: if that piece of data changes does your field need to be recalculated or is your field usage explicitly for a snapshot?

0 votes
elisa May 25, 2018

Is it possible to set that duration time in relation to already configured service calendars/times? The method above includes also non-working times.

Thanks for inspiration :)

0 votes
JohnsonHoward
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
April 12, 2017

Can you please set this answer to accepted?

Deleted user April 12, 2017

Done :)

 

Mahesh Kallepalli October 30, 2019

@[deleted] @JohnsonHoward 

 

We followed same steps but we are getting below exception , Can you please look into it  and help us to get resolved .

An error occurred whilst rendering this message. Please contact the administrators, and inform them of this bug. Details: ------- org.apache.velocity.exception.MethodInvocationException: Invocation of method 'formatDurationPretty' in class com.atlassian.core.util.DateUtils threw exception java.lang.NumberFormatException: For input string: "0m" at templates/customfield/view-duration.vm[line 3, column 16] at org.apache.velocity.runtime.parser.node.ASTMethod.handleInvocationException(ASTMethod.java:337) at org.apache.velocity.runtime.parser.node.ASTMethod.execute(ASTMethod.java:284)  

0 votes
Deleted user April 11, 2017

double post

TAGS
AUG Leaders

Atlassian Community Events