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

This widget could not be displayed.

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

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

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.

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

This widget could not be displayed.

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

This widget could not be displayed.

double post

This widget could not be displayed.

Can you please set this answer to accepted?

This widget could not be displayed.

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
Community showcase
Posted Tuesday in Jira

What modern development practices are at the heart of how your team delivers software?

Hey Community mates! Claire here from the Software Product Marketing team. We all know software development changes rapidly, and it's often tough to keep up. But from our research, we've found the h...

197 views 1 3
Join discussion

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