Extract time in status

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?

 

5 answers

1 accepted

Can you show me your code please?

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

Try adding this return instead of the one you have:

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

You will have to import: 

import com.atlassian.core.util.DateUtils

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

 

What are you passing into the getDuration() method? 

 

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

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

Apologies, try this:

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

Does that work?

 

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

 

Thanks!!

 

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

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

 

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

Can you please set this answer to accepted?

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 :)

Suggest an answer

Log in or Sign up to answer
How to earn badges on the Atlassian Community

How to earn badges on the Atlassian Community

Badges are a great way to show off community activity, whether you’re a newbie or a Champion.

Learn more
Community showcase
Published Thursday in Marketplace Apps

Tips on how to choose the best estimation method for your planning

Planning and grooming sessions all come with their own sets of rules. Team members meet to estimate stories or other work items, all according to an agreed-upon process. And with every session comes ...

71 views 0 11
Read article

Atlassian User Groups

Connect with like-minded Atlassian users at free events near you!

Find a group

Connect with like-minded Atlassian users at free events near you!

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you