How to calculate the time in open status

Johan Jonsson Nilsson September 13, 2017

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

4 votes
Answer accepted
Eryk Leniart September 19, 2017

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

 

Johan Jonsson Nilsson September 19, 2017

Yes, this worked wonders! Thanks for the help!

Eryk Leniart September 19, 2017

You're welcome :)

Hardik Parekh December 13, 2018

Hi @Eryk Leniart : I am trying to calculate time ticket has been in status "Open"

I am getting -594 for ticket which was in Open status for 2d 9m.

  • If I tried using above script you provided excluding last line (return Math.round(total) ?: 0 //not working for me, giving error)

I am getting 173378.141 for ticket which was in Open status for 2d 9m.

 

How can I get result in days like 2d 9m or if it's not entire day then in hr format ?

Raj Kumar August 21, 2019

Hi @Eryk Leniart ,

Can you please help me with the code where I can also get time spent in Open Status and also In progress. Currently, I am using which gives me in-progress time spent only. Can you please help.

Your help is highly appreciated.

 

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

def componentManager = ComponentManager.getInstance()
def changeHistoryManager = ComponentAccessor.getChangeHistoryManager()

/*Get the current status name */
def currentStatusName = issue?.status?.name

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

}
return (Math.round(((rt.sum() as Long) / 3600000 / 24) as Long) + " Days")

 

 

Thanks,
Raj

gadadechhaya28 December 30, 2019

Hi @Eryk 

Thanks,it works for me

Suggest an answer

Log in or Sign up to answer