Calculate time in multiple statuses

Ian Balas February 11, 2021

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.

3 answers

3 votes
Amadou Diallo February 12, 2021

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.

0 votes
Ian Balas February 12, 2021

Hi @Bloompeak Support ,

Thank you for the suggestion!

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

0 votes
Bloompeak Support
Marketplace Partner
Marketplace Partners provide apps and integrations available on the Atlassian Marketplace that extend the power of Atlassian products.
February 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.

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events