How to calculate the time in open status Edited

Hi!

I'm trying to calculate the time the issue has been in status Open with a scripted field using Scriptrunner, which is the initial status for the issue.

I've used the example found on this page, which works fine for all other statuses: https://scriptrunner.adaptavist.com/latest/jira/scripted-fields.html#_total_time_this_issue_has_been_in_progress

However, as the status I want to calculate is the first one, the code doesn't work. I've tried to use the code from the answer found here: https://community.atlassian.com/t5/Product-Apps-questions/scripted-field-total-time-a-issue-has-been-in-open-state/qaq-p/374214

This code works fine, but as soon as I switch to the next status, the field goes blank. If I go back to Open, it stays blank. Something needs to be changed in the code below, but I don't know what could fix this. Does anyone have a good solution for this?

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.history.ChangeItemBean

def changeHistoryManager = ComponentAccessor.getChangeHistoryManager()

def inProgressName = "Open"

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

if (!changeItems) {
rt << (System.currentTimeMillis() - issue.getCreated().getTime())
}

def total = rt.sum() as Long
return (total / 1000) as long ?: 0L

 Any help would be appreciated!

1 answer

1 accepted

Accepted Answer
2 votes

Hi Johan,

Try this:

import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.issue.history.ChangeItemBean;
import com.atlassian.jira.issue.Issue;

def changeHistoryManager = ComponentAccessor.getChangeHistoryManager()

def statusName = "Open"
def createdDateDiff = System.currentTimeMillis() - issue.getCreated().getTime()

List<Long> rt = [0L]

rt << createdDateDiff

def changeItems = changeHistoryManager.getChangeItemsForField(issue, "status")
changeItems.reverse().each {ChangeItemBean item -> item.fromString

// Get the time passed since status change
def timeDiff = System.currentTimeMillis() - item.created.getTime()

// If the status change left our status, we want to subtract the time passed since then
if (item.fromString == statusName) {
rt << -timeDiff
}

// If the status change goes to our status, we want to add the time passed since then
if (item.toString == statusName){
rt << timeDiff
}
}

def total = (rt.sum() as Long) / 1000

return Math.round(total) ?: 0

 

Yes, this worked wonders! Thanks for the help!

You're welcome :)

Suggest an answer

Log in or Sign up to answer
Community showcase
Posted Sep 25, 2018 in Jira

Atlassian Research Workshop opportunity on Sep. 28th in Austin, TX

We're looking for participants for a workshop at Atlassian! We need Jira admins who have interesting custom workflows, issue views, or boards. Think you have a story to sha...

449 views 7 5
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