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

Calculate time in multiple statuses Edited

Hi all,

I'm looking to leverage this following code from Scriptunner Library:

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

// Status to be counted
final statusName = '<YOUR STATUS>'

def changeHistoryManager = ComponentAccessor.changeHistoryManager
def totalStatusTime = [0L]

// Every status change is checked
def changeItems = changeHistoryManager.getChangeItemsForField(issue, "status")
changeItems.reverse().each { ChangeItemBean item ->
def timeDiff = System.currentTimeMillis() - item.created.time
// Subtract time if the "from" status is equal to the status to be checked and from and to statuses are different.
// This allows to count the time the issue is in the state for the first time
if (item.fromString == statusName && item.fromString != item.toString) {
totalStatusTime << -timeDiff
}
// Add time if the "to" status is equal to the status to be checked
if (item.toString == statusName) {
totalStatusTime << timeDiff
}
}

def total = totalStatusTime.sum() as Long
// Every time (added or subtracted) is summed and divided by 1000 to get seconds
(total / 1000) as long ?: 0L

The way that this scripted field code works is that it records the time that an issue has been in a particular status.

Is there a way to change this up so that it can record the time that it has been in more that one status? The reason is because I want to create SLA time fields that records the time that issues are in statuses that are in:

  • To Do category (To Do + Open + Escalated)
  • In Progress category (In Progress + In Review + Testing)
  • Progress is halted (Blocked + Stalled + On Hold)
  • Total time actual ([To Do + Open + Escalated + In Progress + In Review + Testing] - [Blocked + Stalled + On Hold])
  • Total time raw (To Do + Open + Escalated + In Progress + In Review + Testing + Blocked + Stalled + On Hold)

Any tips or pointers in the right direction would be highly appreciated!

-Ian

 

-----

 

UPDATE (2/12/21): I edited the Scriptrunner code above, basically just duplicating the statusName, totalStatusTime, and changeItem blocks, pasting below that and above the summed total, and changed it so that the additional code was reviewing total time for another status. See below:

import com.atlassian.core.util.DateUtils
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.history.ChangeItemBean
def changeHistoryManager = ComponentAccessor.changeHistoryManager


// Status to be counted
def toDo = 'On Hold'
def totalStatusTime = [0L]

// Every status change is checked
def changeItems = changeHistoryManager.getChangeItemsForField(issue, "status")
changeItems.reverse().each { ChangeItemBean item ->
def timeDiff = System.currentTimeMillis() - item.created.time
// Subtract time if the "from" status is equal to the status to be checked and from and to statuses are different.
// This allows to count the time the issue is in the state for the first time
if (item.fromString == toDo && item.fromString != item.toString) {
totalStatusTime << -timeDiff
}
// Add time if the "to" status is equal to the status to be checked
if (item.toString == toDo) {
totalStatusTime << timeDiff
}
}

def intakeReview = "Intake Review"
def totalStatusTime2 = [0L]

changeHistoryManager.getChangeItemsForField (issue, "status")
changeItems.reverse().each {ChangeItemBean item ->
def timeDiff = System.currentTimeMillis() - item.created.time

if (item.fromString == intakeReview && item.fromString != item.toString) {
totalStatusTime << -timeDiff
}
if (item.toString == intakeReview) {
totalStatusTime << timeDiff
}
}

totalStatusTime.addAll(totalStatusTime2)

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

This almost works as intended...but if I use the first status in the workflow (i.e. the default status given once the issue is created...in our case, Open), the code does not add the time in status for each status value, but rather returns null for some reason.

 

I have continued progress on this in another post for further reach.

2 comments

Hi @Ian Balas ,

I am not good at script runner but as an alternative you can try Status Time Free app developed by our team. It displays duration of each status in issue detail page. Hope it helps.

Hi @Bloompeak Support ,

Thank you for the suggestion!

We really want to leverage Scriptrunner for our case, but we shall consider this as well.

Hi @Ian Balas

You can probably achieve this using the history manager. You can refer to this excellent video made by @Ravi Sagar _Adaptavist_ to learn about how to get the history of an issue. Once you have those information you can use the date property to calculate the time that you need. Hope this helps.

Like # people like this

Comment

Log in or Sign up to comment
TAGS
Community showcase
Published in Jira

Announcing the waitlist for Jira Work Management

Hey there Cloud Community members! We’re excited to give you the first glimpse of the new home for business teams on Jira — Jira Work Management. Jira Work Management is the next generation of J...

641 views 10 16
Read article

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