Come for the products,
stay for the community

The Atlassian Community can help you and your team get more value out of Atlassian products and practices.

Atlassian Community about banner
4,294,494
Community Members
 
Community Events
165
Community Groups

Extract time in status

Deleted user Apr 06, 2017

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?

 

9 answers

2 accepted

2 votes
Answer accepted

Can you show me your code please?

Deleted user Apr 11, 2017
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

Deleted user Apr 12, 2017

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? 

 

Deleted user Apr 12, 2017

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

Deleted user Apr 12, 2017

Still errors:2017-04-12 16_07_43-Script Fields - Objectway Jira Test - Opera.png2017-04-12 16_07_51-Script Fields - Objectway Jira Test - Opera.png

Apologies, try this:

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

Does that work?

 

Deleted user Apr 12, 2017

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

 

Deleted user Apr 11, 2017

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

Like Sam likes this

This function work for me:

 

def Long getTimeInStatus(String STATUS_ID, issue) {

    def list = ComponentAccessor.changeHistoryManager.getChangeItemsForField(issue, "status").findAll {
        it.to == STATUS_ID || it.from == STATUS_ID
    }.sort { it.created }
    if (list.size() == 0) {
        return new Date().time - issue.created.time
    }
    def timeInStatus = 0;
    def isLastItem = false
    while (!isLastItem) {
        def itemTo = list.find { it.to == STATUS_ID && it.to != it.from }
        if (itemTo) {
            list = list - itemTo
            def itemFrom = list.find { it.from == STATUS_ID && it.to != it.from }
            if (itemFrom) {
                list = list - itemFrom
                timeInStatus += itemFrom.created.time - itemTo.created.time
            } else {
                isLastItem = true
                timeInStatus += new Date().time - itemTo.created.time
                return Long.divideUnsigned(timeInStatus, 1000)
            }
        } else {
            isLastItem = true
            return Long.divideUnsigned(timeInStatus, 1000)
        }
    }
    return Long.divideUnsigned(timeInStatus, 1000);
}

So I am using a longer method for getting time in all statuses, see the example below. I am having 2 issues, one I have resolved by using my own calculations:

  1. First issue I had was using the DateUtils.getDurationString(total) method, which kept on returning 0m, I used this:
    • def newRT = rt as long []
      rt.clear()
      def total = newRT.sum()/ 1000 as long

      def seconds = total.intValue()
      def iD = (seconds / 85400).round(0)
      def days = seconds % 86400
      def iH = (days / 3600).round(0)
      def hours = days % 3600
      def iM = (hours /60).round(0)
      def strTimeStr = sName + ": " + iD.toString() + "d " + iH.toString() + "h " + iM.toString() + "m" + " | "
      strResult += strTimeStr

  2. The above solved the output of the time  in strings, however with Backlog I have a weird total, I will iclude the screenshot below:
    •  image.png
    • Expected 12s

Here is the script below (I am going to add logic to the output to only print d m h if there are values for them):

//Import the relevant libraries
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.history.ChangeItemBean

//Define the variables to be used
def strResult = ""
List<Long> rt = [0L]
List<String> statusName = []
def changeHistoryManager = ComponentAccessor.getChangeHistoryManager()
def changeItems = changeHistoryManager.getChangeItemsForField(issue, "status")

//Validate that there is only one of each change item name
for(item in changeItems){
    if(statusName.contains(item.getToString())){
        //Do Nothing
    } else {
        statusName << item.getToString()
    }
}

//Get time difference for each status and concatinate onto strResult
for(sName in statusName){
    changeItems.reverse().each { ChangeItemBean item ->
        if (changeItems.indexOf(sName) == 0){
            rt << issue.create.getTime() - item.create.getTime()
        } else {
            item.fromString == sName
            def timeDiff = System.currentTimeMillis() - item.created.getTime()
         
            if (item.fromString == sName) {
                rt << -timeDiff
            }

            if (item.toString == sName) {
                rt << timeDiff
            }
        }
    }

    //Create the output string
    def newRT = rt as long []
    rt.clear()
    def total = newRT.sum()/ 1000 as long

    def seconds = total.intValue()
    def iD = (seconds / 85400).round(0)
    def days = seconds % 86400
    def iH = (days / 3600).round(0)
    def hours = days % 3600
    def iM = (hours /60).round(0)
    def strTimeStr = sName + ": " + iD.toString() + "d " + iH.toString() + "h " + iM.toString() + "m" + " | "
    strResult += strTimeStr
}

return strResult

If anyone could provide advice for including something to calculate for the first status of the workflow? I know the majority of our projects use Backlog but there may be a few that use another status in the beginning of the workflow

Thanks in advance

// You can try this  [I am new to script runner please check twice][Added by Sanjay]

 

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.history.ChangeItemBean
import com.atlassian.core.util.DateUtils
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.user.ApplicationUser
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder
import com.atlassian.jira.bc.issue.IssueService.UpdateValidationResult;
import com.atlassian.jira.user.ApplicationUser
import com.atlassian.jira.bc.issue.IssueService;
import com.atlassian.jira.bc.issue.IssueService.IssueResult;
import com.atlassian.jira.issue.IssueInputParameters;
import com.atlassian.jira.bc.issue.IssueService.IssueResult;

IssueManager im = ComponentAccessor.getIssueManager()
def customFieldManager =ComponentAccessor.getCustomFieldManager();
def Time_in_Current_Status = customFieldManager.getCustomFieldObject("customfield_10803")
MutableIssue issue = im.getIssueObject("DP-2") //STATIC VALUE [need to change]
def changeHistoryManager = ComponentAccessor.getChangeHistoryManager()
def name = issue.getStatus().name;
log.warn(name); //Troubleshoot

List<Long> rt = [0L]
def changeItems = changeHistoryManager.getChangeItemsForField(issue, "status")
changeItems.reverse().each {ChangeItemBean item ->
item.toString == name
def timeDiff = System.currentTimeMillis() - item.created.getTime()
if (item.fromString == name) {
rt << -timeDiff
}
if (item.toString == name){
rt << timeDiff
}
}

def newRT = rt as long []
def total = newRT.sum()/ 1000 as long
def mytotal =DateUtils.getDurationString(total)
IssueService issueService = ComponentAccessor.getIssueService();
IssueInputParameters issueInputParameters = ComponentAccessor.issueService.newIssueInputParameters();
log.warn(issueInputParameters.addCustomFieldValue(10803,mytotal)) //10803 is a custom field id
def autoUser = ComponentAccessor.getUserManager().getUserByName("admin") as ApplicationUser
UpdateValidationResult updateValidationResult = issueService.validateUpdate(autoUser, issue.id, issueInputParameters);
if (updateValidationResult.isValid())
{
IssueResult updateResult = issueService.update(autoUser, updateValidationResult);
if (updateResult.isValid())
{
log.warn("Approvals Added successfully")
}else{
updateResult.getErrorCollection().getErrors().each{ error->
log.warn("Approvals not added bcz:"+error)
}
}
}else{
updateValidationResult.getErrorCollection().getErrors().each{ error->
log.warn("Approvals not added bcz:"+error)
}
}
return "Time duration of issue in status "+name+" is "+mytotal

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?

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 :)

Can you please set this answer to accepted?

Deleted user Apr 12, 2017

Done :)

 

@[deleted] @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)  

0 votes

double post

Suggest an answer

Log in or Sign up to answer
TAGS
Community showcase
Posted in Jira Service Management

Jira Service Management Documentation Opportunities

Hello everyone, Hope everyone is safe! A few months ago we posted an article sharing all the new articles and documentation that we, the AMER Jira Service Management team created. As mentioned ...

261 views 0 6
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