Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

Next challenges

Recent achievements

  • Global
  • Personal

Recognition

  • Give kudos
  • Received
  • Given

Leaderboard

  • Global

Trophy case

Kudos (beta program)

Kudos logo

You've been invited into the Kudos (beta program) private group. Chat with others in the program, or give feedback to Atlassian.

View group

It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

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

3 votes
Answer accepted

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 :)

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 ?

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

Hi @Eryk 

Thanks,it works for me

Suggest an answer

Log in or Sign up to answer
TAGS

Community Events

Connect with like-minded Atlassian users at free events near you!

Find an event

Connect with like-minded Atlassian users at free events near you!

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you