Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

Numerical calculation of an Insight attribute field

Marc Drescher June 9, 2021

Hello all,

we are trying to calculate a priority for an Insight object, based on different categories. The whole thing looks like the following example:

Object A:
Attribute Priority Categories: Multiple selection from category 1-5
Attribute Priority: Numeric calculated value based on priority categories

Category 1: Value of 20
Category 2: value of 50
Category 3: value of 10
...
So if an object A has category 1 and 2, the value 70 should be calculated as priority.

Does anyone have ideas what possibilities can be offered for this?

Thanks in advance for your help!

1 answer

0 votes
PD Sheehan
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
June 9, 2021

The actual logic for this is fairly simple ... what's hard is to get the Category values and then update another attribute.

With the utility class I created (see this article) I can achieve your requirement with this script in the insight groovy console:

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.config.util.JiraHome
def jiraHome = ComponentAccessor.getComponent(JiraHome).home
InsightUtils = new GroovyClassLoader(getClass().classLoader).parseClass(new File("$jiraHome/scripts/com/qad/qsm/jira/insight/InsightUtils.groovy"))
def cat = InsightUtils.getAttributeValueFromDotNotation(object, 'Priority Category') as List
def map = ['Category 1':20, 'Category 2':50, 'Category 3':10]
def priority = cat.sum { map[it] }
InsightUtils.setObjectAttribute(object, 'Priority', priority)

 

To do the same from scratch, it would look more like this:

import com.atlassian.jira.component.ComponentAccessor
import com.riadalabs.jira.plugins.insight.channel.external.api.facade.ObjectFacade
import com.riadalabs.jira.plugins.insight.channel.external.api.facade.ObjectTypeFacade
import com.riadalabs.jira.plugins.insight.channel.external.api.facade.ObjectTypeAttributeFacade
def objectFacade = ComponentAccessor.getOSGiComponentInstanceOfType(ObjectFacade)
def objectTypeFacade = ComponentAccessor.getOSGiComponentInstanceOfType(ObjectTypeFacade)
def objectTypeAttributeFacade = ComponentAccessor.getOSGiComponentInstanceOfType(ObjectTypeAttributeFacade)

def objectType = objectTypeFacade.loadObjectType(object.objectTypeId)
def priorityCategoryAttr = objectTypeAttributeFacade.findObjectTypeAttributeBeans(objectType.id).find {it.name == 'Priority Category'}
def priorityAttr = objectTypeAttributeFacade.findObjectTypeAttributeBeans(objectType.id).find {it.name == 'Priority'}

def categories = object.objectAttributeBeans.find{it.objectTypeAttributeId == priorityCategoryAttr.id}?.objectAttributeValueBeans*.value

def categoryPrioritValuesMap = ['Category 1':20, 'Category 2':50, 'Category 3':10]
def priority = categories.sum { categoryPrioritValuesMap[it] }
log.info("Priority will be set to $priority")

def newPriorityBean = object.createObjectAttributeBean(priorityAttr)
def newPriorityValue = newPriorityBean.createObjectAttributeValueBean()

newPriorityValue.setValue(priorityAttr, priority)
newPriorityBean.setObjectAttributeValueBeans([newPriorityValue])

objectFacade.storeObjectAttributeBean(newPriorityBean)

Suggest an answer

Log in or Sign up to answer