Come for the products,
stay for the community

The Atlassian Community can help you and your team get more value out of Atlassian products and practices.

Atlassian Community about banner
4,293,855
Community Members
 
Community Events
165
Community Groups

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

Bloompeak Support Marketplace Partner Feb 12, 2021

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 _Sparxsys_ 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 Software

Upcoming changes to epic fields in company-managed projects

👋 Hi there Jira Community! A few months ago we shared with you plans around renaming epics in your company-managed projects. As part of these changes, we highlighted upcoming changes to epics on...

14,143 views 34 44
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