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 Sign up to answer
How to earn badges on the Atlassian Community

How to earn badges on the Atlassian Community

Badges are a great way to show off community activity, whether you’re a newbie or a Champion.

Learn more
Community showcase
Published Thursday in Confluence

Three common content challenges + how to manage them

An efficient enterprise content management system, or ECM, is a must-have for companies that create work online (cough   cough, all companies). If content calendars, marketing plans, and bu...

73 views 0 4
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