Calculating duration of 'In Progress' status in Jira

Need help figuring out time spent in ‘In Progress’ status

I’m working with Jira Data Center and trying to find out how long tasks stay in the ‘In Progress’ status. I don’t care about hours or minutes, just the number of days.

Here’s what I’ve got so far:

import com.atlassian.jira.component.ComponentAccessor

def statusToCount = 'In Progress'
def changeHistoryManager = ComponentAccessor.changeHistoryManager
def statusChanges = changeHistoryManager.getChangeItemsForField(issue, 'status')

statusChanges.findAll { it.toString == statusToCount || it.fromString == statusToCount }.each { change ->
    println change
}

This gives me a list of status changes, but I’m stuck on how to actually calculate the days. Any ideas on how to proceed? Thanks!

I’ve worked on similar Jira reporting tasks before, and here’s a tip that might help: consider edge cases. What if a task is currently in ‘In Progress’? Or if it’s been in that status multiple times?

Here’s a tweak to your script that accounts for these:

def totalDays = 0
def lastInProgressStart = null

statusChanges.reverse().each { change ->
    if (change.toString == statusToCount) {
        lastInProgressStart = change.created
    } else if (change.fromString == statusToCount && lastInProgressStart) {
        totalDays += (change.created.time - lastInProgressStart.time) / (1000 * 60 * 60 * 24)
        lastInProgressStart = null
    }
}

if (lastInProgressStart) {
    totalDays += (new Date().time - lastInProgressStart.time) / (1000 * 60 * 60 * 24)
}

println "Total days in 'In Progress': ${totalDays.intValue()}"

This handles current ‘In Progress’ tasks and multiple status changes. It’s been a lifesaver in my projects. Hope it helps!

I’ve encountered this issue before. Here’s a potential solution:

You’re on the right track with the change history. To calculate days, you’ll need to pair the status changes and use their timestamps. Something like this:

def inProgressPeriods = []
def startTime = null

statusChanges.each { change ->
    if (change.toString == statusToCount) {
        startTime = change.created
    } else if (change.fromString == statusToCount && startTime) {
        inProgressPeriods << [start: startTime, end: change.created]
        startTime = null
    }
}

def totalDays = inProgressPeriods.sum { period ->
    (period.end.time - period.start.time) / (1000 * 60 * 60 * 24)
}

println "Total days in 'In Progress': ${totalDays.intValue()}"

This should give you the total number of days spent in ‘In Progress’. Let me know if you need any clarification.

hey there, I’ve dealt with similar stuff before. have you tried using the Date class to calculate the difference between timestamps? something like:

def duration = endDate - startDate
def days = duration.days

that might help you get the number of days. good luck!