Forums

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

Scripted fields calculation : No of Days calculation - issue navigator

Rajesh Asana January 6, 2019

hi,

 

I was trying to calculate cycle time (time since) of issue in the current state using the below code.

It is not giving consistent results.

Don't know why

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.core.util.DateUtils
import com.atlassian.jira.ComponentManager
import com.atlassian.jira.issue.history.ChangeItemBean

def changeHistoryManager = ComponentAccessor.getChangeHistoryManager()
def currentTime = System.currentTimeMillis()
def stateTime = 0

def items = changeHistoryManager.getChangeItemsForField (issue, "status").reverse()

if (items) {
stateTime = items.first().created.time
}
else {
stateTime = issue.getCreated().time
}

def stateCycleTime = (currentTime - stateTime) / (1000*60*60*24)

return (stateCycleTime as Double).round(2) ;

 

 

Can any one help in understanding the problem and a good fix

 

3 answers

1 vote
Sai Ram Kumar Singarapu January 22, 2019

Hi @Rajesh Asana

 

can you please check the below code which works as "Minutes in Particular Status" and change accordingly to days?

 

 

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

def changeHistoryManager = ComponentAccessor.getChangeHistoryManager()

def status_Name= "status_name"

List<Long> rt = [0L]
def changeItems = changeHistoryManager.getChangeItemsForField(issue, "status")
changeItems.reverse().each {ChangeItemBean item ->
item.toString == inProgressName

def timeDiff = System.currentTimeMillis() - item.created.getTime()
if (item.fromString == status_Name) {
rt << -timeDiff
}
if (item.toString == inProgressName){
rt << timeDiff
}
}

def total = rt.sum() as Long
log.warn("Total Time "+total)
total = (total/(1000*60))
return total ?: 0L

rajeshasana
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
January 22, 2019

Thanks @Sai Ram Kumar Singarapu

 

After debugging I found that "System.currentTimeMillis()" is not consistent.

Not sure why

varunSukheja
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
January 23, 2019

hi @Rajesh Asana

you can try getting curretn date in mili sec by `new Date().getTime()`

Rajesh Asana January 28, 2019

hi, I tried with scripted custom field just with this line

return new Date();

 

the result is in-consistent

Do you have any clue?

rajeshasana
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
January 30, 2019

I have the results as shared in the video

 

https://youtu.be/XaECymayhDU

0 votes
Emre Toptancı _OBSS_
Atlassian Partner
April 9, 2019

Hello @Rajesh Asana

I suggest you take a look at the original Timepiece - Time in Status for Jira by OBSS. Our app does exactly what you need.

I know you want to achieve this via custom code rather than an app but believe me, buying our stable app will save you time and money.

  • Our app creates reports on how much time each issue spent on each status, assignee or user group.
  • Number of time each status or transition was used.
  • View durations in days, hours, minutes, etc.
  • Include issue fields in your report.
  • Export data as CSV, XLS or XLSX.
  • Calculate time or count averages, grouped by multi level issue fields that you select.
  • Define your own business calendars.

We now have Server, Data Center and Cloud options. 

0 votes
Mark Kern [SaaSJet]
Contributor
January 15, 2019

Hi @Rajesh Asana
You can get a good fix for this problem by using additional plugins instead of doing it manually with code.

For instance, you can use Time in Status for Jira Cloud by SaaSJet (you can get it from Atlassian Marketplace). This add-on helps you to easily and automatically calculate the time of issue in the current state.

Also, you can try Time Between Statuses for calculation status to status time. This app help you to get the Cycle and Lead Time of your Jira issues

Data from two these add-ons are available for export as XLSX or CSV file.

Hope this helps!

Rajesh Asana January 17, 2019

Thanks for the reply,

My intention was not to add plugin as I want to build my rule of my own.

Could you help me understand the problem behind the code which gives an in- consistent behavior of the return value.

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events