Script Total Time In Status

Deleted user February 9, 2023

Hi,

I'm using the ScriptRunner script for getting the Time in a particular status.  I am able to get the times for all other statuses except the first status after the issue is opened, in this case Open.  Here is the script I'm using:

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

// Status to be counted
final statusName = 'Open '

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

============================================

 

I'm trying to adjust the script so that I can get the time in the first status which at this point keeps returning a zero.  

Any pointers on how to adjust it to get the duration of the first status the issue transitions into? 

Thanks

Note: I'm on JIRA Server version v8.20.11 

 

 

 

1 answer

0 votes
Nic Brough -Adaptavist-
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
February 12, 2023

You've run into something that I think of as a design flaw in the original database.

An issue object is a simple row in a table in the database, holding the current data for it.  That's an intuitive and natural way to hold a representation of an object, the way most humans will think about it.

But it's not the right way to do it for an object that might change.  Instead of "current object" and then a "change history" in a different place, a better design would be to have one row per change and an indicator of "current".  Create issue creates the first row, then edits create new ones.  It's not an efficient use of disk space, but it makes reporting much easier to code for.

Anyway, in the current structure, creating an issue creates the issue object.  There's no history to it, and the history only starts when you first update the issue.

So, you need to catch that.  The first status entry in the change log will contain a "from" status.  You must find that entry, read its date/time, and subtract the issue.created date/time from it (instead of another history log)

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events