Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

How to calculate the time in open status

Johan Jonsson Nilsson
Contributor
September 13, 2017 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

4 votes
Answer accepted
Eryk Leniart
Contributor
September 19, 2017 edited

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
Contributor
September 19, 2017

Yes, this worked wonders! Thanks for the help!

Eryk Leniart
Contributor
September 19, 2017

You're welcome :)

Hardik Parekh
Contributor
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
TAGS
atlassian, atlassian community, loom ai, atlassian loom ai, loom, atlassian ai, record recaps of meetings, meeting recaps, loom recaps, share meeting recaps,

Loom’s guide to great meetings 📹

Join us to learn how your team can stay fully engaged in meetings without worrying about writing everything down. Dive into Loom's newest feature, Loom AI for meetings, which automatically takes notes and tracks action items.

Register today!
AUG Leaders

Atlassian Community Events