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

Jakub Gawryjolek April 27, 2017

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

0 votes
Jakub Gawryjolek April 27, 2017

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