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?

 

4 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?

Suggest an answer

Log in or Join to answer
Community showcase
Teodora [Botron]
Published Thursday in Marketplace Apps

Jira Inferno: The Nine Circles of Jira Administration Hell

If you spend enough time as a Jira admin - whether you are managing a single, mid-sized instance, a large enterprise one or juggling multiple instances at once - you will eventually find yourself in ...

655 views 4 15
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
Atlassian Team Tour

Join us on the Team Tour

We're bringing product updates and pro tips on teamwork to ten cities around the world.

Save your spot