It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

Extract time in status

Hi,

I want to create a scripted field in scriptrunner that calculates time in status (like Jira suite utilities) in certain conditions. 

Is the best way to create some custom fields that populate a date and time or can I script this?

 

6 answers

2 accepted

2 votes
Answer accepted

Can you show me your code please?

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

def changeHistoryManager = ComponentAccessor.getChangeHistoryManager()

def inProgressName = "Assess 2nd Line"

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 == inProgressName) {
        rt << -timeDiff
    }
    if (item.toString == inProgressName){
        rt << timeDiff
    }
}

def total = rt.sum() as Long
return total ?: 0L

Try adding this return instead of the one you have:

def total = rt as int []
return DateUtils.getDurationString(Math.round(total.sum() / 1000))

You will have to import: 

import com.atlassian.core.util.DateUtils

2017-04-12 09_59_07-Script Fields - Objectway Jira Test - Opera.pngNow I'm getting an error

 

What are you passing into the getDuration() method? 

 

It's actually a double error.

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

def changeHistoryManager = ComponentAccessor.getChangeHistoryManager()

def inProgressName = "Assess 2nd Line"

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 == inProgressName) {
        rt << -timeDiff
    }
    if (item.toString == inProgressName){
        rt << timeDiff
    }
}

def total = rt as int []
return DateUtils.getDurationString(Math.round(total.sum() / 1000))

2017-04-12 10_31_13-Script Fields - Objectway Jira Test - Opera.png

This removes the errors I believe:

rt = rt as long []
def total = rt.sum()/ 1000 as long
return DateUtils.getDurationString(total)

You wil need the template for the scripted field to be Text-Field. Otherwise it will fail.

 

Thanks

Apologies, try this:

def newRT = rt as long []
def total = newRT.sum()/ 1000 as long
return DateUtils.getDurationString(total)

Does that work?

 

It works like a charm! No, I can build on this.

 

Thanks!!

 

I tried this scripted field as well, and I always get 0 min as the time in status.  No errors shown in code:

 

time in status script.PNG

Same problem as you Bill, did you find a fix by any chance?

I have switched to using a new add-on called Issue History Collector:

https://marketplace.atlassian.com/apps/1211499/issue-history-collector?hosting=server&tab=overview

And, it is free.  

Just follow the documentation and it is pretty easy to get up an running.

Will check it out, thanks for the reply! Really appreciate it

1 vote
Answer accepted

Hi Robin,

Take a look at this link: https://scriptrunner.adaptavist.com/latest/jira/scripted-fields.html#_total_time_this_issue_has_been_in_progress

You can edit this code to show the total time for any status.

Is that what you are looking for?

Thanks,

Johnson Howard

 

It looks like it, but the values are not correct :)

test.png

It was in the status for less than a minute.

Kind regards,

Robin

Can you please set this answer to accepted?

@Robin De Bruyn @JohnsonHoward 

 

We followed same steps but we are getting below exception , Can you please look into it  and help us to get resolved .

An error occurred whilst rendering this message. Please contact the administrators, and inform them of this bug. Details: ------- org.apache.velocity.exception.MethodInvocationException: Invocation of method 'formatDurationPretty' in class com.atlassian.core.util.DateUtils threw exception java.lang.NumberFormatException: For input string: "0m" at templates/customfield/view-duration.vm[line 3, column 16] at org.apache.velocity.runtime.parser.node.ASTMethod.handleInvocationException(ASTMethod.java:337) at org.apache.velocity.runtime.parser.node.ASTMethod.execute(ASTMethod.java:284)  

Is it possible to set that duration time in relation to already configured service calendars/times? The method above includes also non-working times.

Thanks for inspiration :)

The display of the data works, but querying is inconsistent.

You are in effect using datetime.now() as a basis of your scripts calculation, which in itself is a non-deterministic function. (On ANY platform you shouldnt really ever index a piece of data that is based on a non-deterministic function...but I wanted to reprove this to myself in JIRA too!)

My own objective was to be able to alert when a ticket has been in any given state which exceeds the time-estimate for it (to highlight flow hotspots)

I created a field: "Time in current status" that effectively has a value of now() - date time of last transition (simplified version of script above: just gets the first entry and does the calculation, or if no transition entry then use now() - created date)

Putting my field on the Kanban cards always displayed the right value, but JQL filters were inconsistent. The data in the field was only ever saved if the issue was updated + saved in some way.

My test script was thus:

Create an issue, kanban shows 0 minutes in "Time in current status" field

After 10 minutes refresh the kanban board: issue shows 10 minutes in "Time in current status"

Run JQL: "Time in current status" > 9m : issue doesn't show (bad)

Immediately edit a field (eg Description or Summary) to trigger issue data to be saved

Run JQL: "Time in current status" > 9m : issue shows (good)

I waited 2 minutes:

Run JQL: "Time in current status" > 11m : issue doesn't show (bad)

Then re-index my project (triggers script to run on each issue: NB this is potential danger of using scripted fields)

Run JQL: "Time in current status" > 11m : issue shows (good)

The message is: think carefully what you are doing with any data in your script and whether it can actually support queries on that data - does it rely on a piece of data that can change, without triggering an update to the scripted field on your issue. 

Always ask yourself: when using data external to the issues fields for any calculations: if that piece of data changes does your field need to be recalculated or is your field usage explicitly for a snapshot?

Suggest an answer

Log in or Sign up to answer
Community showcase
Posted in Jira

Watch Slack + Atlassian's Webinar: “Transform cross-functional collaboration with Slack + Atlassian

Hi Atlassian Community! We recently partnered together with Slack on a webinar, “ Transform cross-functional collaboration with Slack + Atlassian ”. Building fast, growing exponentially: all...

65 views 0 5
Join discussion

Community Events

Connect with like-minded Atlassian users at free events near you!

Find an event

Connect with like-minded Atlassian users at free events near you!

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you