Random values in Search and ticket details for time-related field Edited

Hello,

I've been trying to calculate the Cycle Time for our tickets and display the value as a custom field in Filters Search, Ticket Details, etc. When inspecting the value in the Preview in Script Runner, the value is always correct. However, in Filters Search, for example, the value randomly changes. This is related to the issues discussed here:

https://community.atlassian.com/t5/JIRA-Core-questions/How-to-calculate-ticket-age-with-script-runner/qaq-p/121842/comment-id/7867#M7867

But I couldn't get it to work.

Here's the code:

import org.joda.time.DateTime
import org.joda.time.Days
import org.joda.time.LocalDate
import org.joda.time.Seconds
import com.atlassian.core.util.DateUtils
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.history.ChangeItemBean

def changeHistoryManager = ComponentAccessor.getChangeHistoryManager()
def cycleStatuses = ["In Progress", "In Code Review", "Local UX/UI", "Local QA"]
def rt = [0]

boolean isInCycleStatus = false
DateTime nowTime = new DateTime(new Date().getTime())
 
changeHistoryManager.getChangeItemsForField (issue, "status").each {ChangeItemBean item ->
    long changeItemCreatedTime = item.created.getTime()
    int seconds = Seconds.secondsBetween(new DateTime(changeItemCreatedTime), nowTime).getSeconds()

    if (isInCycleStatus && (item.fromString in cycleStatuses) && !(item.toString in cycleStatuses)) {
        isInCycleStatus = false
        rt << -seconds
    }    

    if (!(item.fromString in cycleStatuses) && (item.toString in cycleStatuses)) {
        isInCycleStatus = true
        rt << seconds
    }    
 }

DateUtils.getDurationString((long)rt.sum())

Our Cycle Time is defined as time spent in any of the four statuses: "In Progress", "In Code Review", "Local UX/UI", "Local QA"

Is there a good way to query calculated fields on temporal data?

1 answer

This widget could not be displayed.

Found a good explanation of the behaviour on Jamie's page:

https://jamieechlin.atlassian.net/wiki/pages/viewpage.action?pageId=57999377#ScriptedFields-MoreAdvancedNotesonCaching

Tried to disable the cache on this field and it seems to be working fine.

Suggest an answer

Log in or Sign up to answer
Atlassian Summit 2018

Meet the community IRL

Atlassian Summit is an excellent opportunity for in-person support, training, and networking.

Learn more
Community showcase
Posted Wednesday in New to Jira

Are you planning to trial, or are currently trialling Jira Software? - We want to talk to you!

Hello! I'm Rayen, a product manager at Atlassian. My team and I are working hard to improve the trial experience for Jira Software Cloud. We are interested in   talking to 20 people planning t...

163 views 2 0
Join discussion

Atlassian User Groups

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

Find a group

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

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you