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

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 Join to answer
Community showcase
Teodora [Botron]
Published Feb 15, 2018 in Marketplace Apps

Jira Inferno: The Nine Circles of Jira Administration Hell

If you spend enough time as a Jira admin - whether you are managing a single, mid-sized instance, a large enterprise one or juggling multiple instances at once - you will eventually find yourself in ...

1,151 views 6 19
Read article

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
Atlassian Team Tour

Join us on the Team Tour

We're bringing product updates and pro tips on teamwork to ten cities around the world.

Save your spot