Sum of original estimate by selected day

Robi Anton February 23, 2016

Hello, 

I want to get the sum of all the tickets for a selected date.

It has to calculate instantly ( like on a calculated field, ou scripted field)

Is it possible to do by groovy script on script runner? 

 

Thank you !!

 

2 answers

0 votes
Robi Anton April 17, 2016

@Kristian Walker (Adaptavist)

I have found out a groovy script who could be ran by a scheduler. 

This script calculates the sum of time spent for a period, then update a custom field of an issue.

I wanted to share this with you...

import org.apache.log4j.Category
import com.atlassian.jira.bc.issue.search.SearchService
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.search.SearchException
import com.atlassian.jira.web.bean.PagerFilter
import com.atlassian.jira.issue.util.IssueChangeHolder
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder;
import com.atlassian.jira.issue.ModifiedValue
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.ComponentManager;
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.issue.customfields.manager.OptionsManager
import com.atlassian.jira.issue.customfields.option.Option
import com.atlassian.jira.issue.*;
import java.sql.Timestamp
Calendar localCalendar = Calendar.getInstance(TimeZone.getDefault());
int currentDay = localCalendar.get(Calendar.DATE);
println currentDay

if ( currentDay == 18 ){
println "WE ARE IN CURRENT DAY _script Somme Precedent"
def jqlSearch = "project = 'AISC - Suivi des demandes' and status was in (Validation) and ('Date de livraison' >= startOfMonth(-1) and 'Date de livraison' <=endOfMonth(-1))"
def userManager = ComponentAccessor.getUserManager()
def issueManager = ComponentAccessor.getIssueManager()
def searchService = ComponentAccessor.getComponentOfType(SearchService)
def user = userManager.getUserByName("yk3520").directoryUser
SearchService.ParseResult parseResult = searchService.parseQuery(user, jqlSearch)

def value = 0
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def myCustomField = customFieldManager.getCustomFieldObjectByName("Temps d'intervention")

if (parseResult.isValid()) {
try {
def results = searchService.search(user, parseResult.getQuery(), PagerFilter.getUnlimitedFilter())
def issues = results.getIssues()
issues.each {
if (it.getCustomFieldValue(myCustomField))
value += it.getCustomFieldValue(myCustomField).toInteger()
}
} catch (SearchException e) {
e.printStackTrace()
}
} else {
log.warn("Invalid query")
return null
}

// Save the value at custom field with name A number custom field of issue with key TEST-1
def targetIssue = issueManager.getIssueByCurrentKey("SL-8")
def customField = customFieldManager.getCustomFieldObjects(targetIssue).find {it.name == "Total intervention M-1"}
if (customField) {
    def changeHolder = new DefaultIssueChangeHolder()
    customField.updateValue(null, targetIssue,
            new ModifiedValue(targetIssue.getCustomFieldValue(customField), value.toString()),changeHolder)
    log.debug "Custom field $customField.name} updated with value ${value}"
    return
}
log.warn "Custom field ${customField?.name} was not updated"

}
else
{
println "WE ARE NOT IN CURRENT DAY"
}
0 votes
Kristian Walker _Adaptavist_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
February 23, 2016

Hi Robi,

You can use aggregate JQL function as described here in order to sum the value of original estimate for all issues which have an estimate updated on a certain day such as the current day by using the JQL below.

createdDate >= startOfDay() AND updatedDate >= startOfDay() AND originalEstimate is not EMPTY AND issueFunction in aggregateExpression("Total Estimate for all Issues", "originalEstimate.sum()")

The JQL query would calculate instantly whenever you run it.

You could possibly look to run the JQL query through a script field if you wanted to store the results in a script field.

I hope this helps.

Thanks

Kristian

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events