我如何找到每个自定义字段填充的Jira问题的数量?



我正在分析Jira实例上的自定义字段使用情况,我遇到了一个问题。我试图找到问题的数量,其中每个自定义字段填充。我在Scriptrunner中编写了一些Groovy代码,但是性能很糟糕。当我运行代码时,CPU使用率飙升,并且在脚本完成后的一段时间内没有下降。是否有更好的方法来查找信息或更好的方法来构建我的代码?

代码有点蛮力。我得到所有自定义字段的列表(大约500个),然后找到填充该字段的所有问题。大约有一百万个问题,所以我在这个庞大的数据集上搜索了大约500次。我不想打印所有领域的完整问题列表。理想情况下,我会标记仅由少数问题使用的字段,但代码已经如此昂贵,以至于我不愿意添加任何内容。

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.jql.parser.JqlQueryParser
import com.atlassian.jira.web.bean.PagerFilter
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.search.SearchProvider
import org.apache.log4j.Logger
import org.apache.log4j.Level

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.CustomFieldManager
// Set log level to INFO
log.setLevel(Level.INFO)
def html = "" // Quick and dirty way to get csv-formatted output
html += "Field Name,Num issues<br>"
CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager()
List<CustomField> allCustomFields = customFieldManager.getCustomFieldObjects()
log.warn("There are " + allCustomFields.size() + " custom fields.")
// Some components
def user = ComponentAccessor.jiraAuthenticationContext.loggedInUser
def searchService = ComponentAccessor.getComponentOfType(SearchService)
// Here's the loop where things get expensive
for (field in allCustomFields) {
log.info("Checking fieldName (" + field.name + ")")
def jqlSearch = sprintf("'%s' IS NOT EMPTY", field.name) 
log.info("Checking jqlSearch (" + jqlSearch + ")")
// Parse the query
def parseResult = searchService.parseQuery(user, jqlSearch)
if (!parseResult.valid) {
log.error('Invalid query: ' + jqlSearch)
html += "$field.name,Invalid query error<br>"
}
else {
try {
// Perform the query to get the issues
def results = searchService.search(user, parseResult.query, PagerFilter.unlimitedFilter)
def issues = results.results
log.warn(field.name + " size " + issues.size())
html += field.name + "," + issues.size() + "<br>"
issues*.key // I honestly don't know what this does.
} catch (SearchException e) {
log.error ("SearchException from " + field.name)
html += field.name + ",SearchException error<br>"
e.printStackTrace()
null // I honestly don't know what this does.
} catch (NoSuchElementException nsee) {
log.error ("NoSuchElementException from " + field.name)
html += field.name + ",NoSuchElementException error<br>"
nsee.printStackTrace()
null // I honestly don't know what this does.
}
}
}
return html

如果您能够使用scriptrunner,那么我猜您处于本地实例中。新版本的Data Center具有各种分析自定义字段使用情况的功能,这些功能可能会有所帮助。

我之前试着通过DB做分析,也遇到了缩放问题。问题的复杂性不仅在于使用的自定义字段的数量,还在于有多少自定义字段的内容被添加到Lucene索引中。没有搜索器的自定义字段不会真正影响性能。

几年前我采用的一种方法是自动询问项目主管他们真正使用的字段,然后用脚本将其他字段移动到Deprecated字段选项卡。还要重命名这些字段,然后再删除它们。这是很多工作:/

最新更新