Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

Scriptrunner Cloud - Trying to calculate the time between In Progress and Resolved (Cycle Time)

Sphinx13 August 2, 2023

Hello guys!

I'm trying to come up with a script to calculate the time between two transitions. In this case, the cycle time, so how long it took an issue from the In Progress status until a resolution was applied. I'm terrible with Java dates, but I've managed to create a script that returns this difference in a pretty format, the code is below

import java.time.ZonedDateTime
import java.text.SimpleDateFormat
import java.util.Date
import java.util.concurrent.TimeUnit //for pretty format

 

def issueKey = "TP-5"
// Jira date timestamp field format
def timePattern = "yyyy-MM-dd'T'HH:mm:ss.SSSZ"
//get issuedata thrugh API
def issuedata = get("/rest/api/3/issue/${issueKey}?expand=changelog")
.asObject(Map).body

//filter changelogs when enterd 'in progress'
def enterProgress = issuedata.changelog.histories.findAll{it.items.toString.contains("In Progress")}.created

//getting the date of changelog for resolution
def resChanges = issuedata.changelog.histories.findAll{it.items.field.contains("resolution")}.created


def enterProgressDate = new SimpleDateFormat(timePattern).parse(enterProgress)
def resolvedDate = new SimpleDateFormat(timePattern).parse(resChanges)
def diff = resolvedDate.getTime() - enterProgressDate.getTime()

//difference in minuts
long differenceMin = (diff / 1000)/60;

//Difference in pretty format
long DD = TimeUnit.MILLISECONDS.toDays(diff)
long HH = TimeUnit.MILLISECONDS.toHours(diff) %24
long MM = TimeUnit.MILLISECONDS.toMinutes(diff) % 60
long SS = TimeUnit.MILLISECONDS.toSeconds(diff) % 60
String prettyDuration = String.format("%02dd %02dh %02dm %02ds",DD, HH, MM, SS)

return prettyDuration

Now, here's where it gets a little too complicated for me. Is there a way that I can show the time between those status as working days - where 1 day = 8 hours, and also skipping weekends?

Any insight will be of great help! Thanks in advance!

1 answer

1 accepted

1 vote
Answer accepted
Ram Kumar Aravindakshan _Adaptavist_
Community Champion
August 9, 2023

Hi @Sphinx13 

For your requirement, you could try something like:-

def issueKey = 'MOCK-24'
def changeItems

def result = get("/rest/api/2/issue/${issueKey}/changelog")
.header('Content-Type', 'application/json')
.asObject(Map)

if (result.status == 200){
changeItems = result.body.values
def ids = changeItems['id'] as List
def index = ids.findIndexOf { it == '212354' } // status field id
def creationDates = changeItems['created'] as List


def createdDate = Date.parse("yyyy-MM-d'T'H:mm:ss.yyyy+yyyy", creationDates.get(index).toString())

def currentDate = new Date(System.currentTimeMillis())

def difference = (currentDate.minus(createdDate) / (24 * 3600)) as Double

Math.abs(difference) as Long

} else {
return "Failed to find issue: Status: ${result.status} ${result.body}"
}

Please note that the sample code above is not 100% exact to your environment. Hence, you will need to make the required modifications.

I hope this helps to solve your question. :-)

Thank you and Kind regards,

Ram 

Sphinx13 September 28, 2023

Hey Ram, thanks for the answer above, this is exactly what I needed =D

Thank you!!

Suggest an answer

Log in or Sign up to answer
DEPLOYMENT TYPE
CLOUD
PRODUCT PLAN
FREE
PERMISSIONS LEVEL
Product Admin
TAGS
AUG Leaders

Atlassian Community Events