attachments by month

Lacey McDonnell May 25, 2016

Can't figure out how to get attachment sizing trends out of JIRA.

 

Example:

 

 

Unzipped

 

Zipped

 

Total Attachments by Month

Month

Files

Bytes

Files

Bytes

Files

Bytes

2014-01

2,264

2,117,700,771

575

1,608,367,762

2,839

3,726,068,533

2014-02

2,053

1,713,796,369

372

1,388,456,805

2,425

3,102,253,174

1 answer

2 votes
Boris Georgiev _Appfire_
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.
May 25, 2016

Here's a Groovy Script Runner sample code that will get you a nice HTML table. You can put this script into a scripted field or anywhere else that you can execute groovy code.

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.AttachmentManager
import com.atlassian.jira.issue.attachment.Attachment;
import com.atlassian.jira.issue.attachment.AttachmentConstants
import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.Multimap;
import java.text.SimpleDateFormat;
import org.joda.time.DateTime
List<Attachment> attachments = ComponentAccessor.getOfBizDelegator().findAll(AttachmentConstants.ATTACHMENT_ENTITY_NAME).collect({
	ComponentAccessor.getAttachmentManager().getAttachment(it.getLong("id"))
})
SimpleDateFormat monthFormat =  new SimpleDateFormat("Y-MM");
Multimap<String, Attachment> multimap = ArrayListMultimap.create();
attachments.each({
	multimap.put(monthFormat.format(it.created),  it)
})
def zipmimes = ["application/zip"]
def lines = multimap.keySet().collect({ key ->
	def atts = multimap.get(key)
	def tfiles = atts.size()
	def tbytes = String.format("%,8d%n",atts.sum{it.filesize }?:0)
	//
	def zips = atts.findAll({it.zip || zipmimes.contains(it.mimetype)})
	def zfiles = zips.size()
	def zbytes = String.format("%,8d%n",zips.sum{it.filesize}?:0)
	//
	def uzips = atts.findAll({!it.zip && !zipmimes.contains(it.mimetype)})
	def uzfiles = uzips.size()
	def uzbytes = String.format("%,8d%n", uzips.sum{it.filesize}?:0)
	
	"<tr><td>$key</td><td>$uzfiles</td><td>$uzbytes</td><td>$zfiles</td><td>$zbytes</td><td>$tfiles</td><td>$tbytes</td>"
})
$/
<table class="aui">
<thead><tr><th id="month">Month</th><th id="name" colspan="2">Unzipped</th><th id="type" colspan="2">Zipped</th>
    <th id="order" colspan="2">Total Attachments by Month</th></tr></thead>
<tbody>
<tr><td>Month</td><td>Files</td><td>Bytes</td><td>Files</td><td>Bytes</td><td>Files</td><td>Bytes</td>${lines.join()}
</tbody>
</table>
/$

Here's a sample table produced by this script:

MonthUnzippedZippedTotal Attachments by Month
MonthFilesBytesFilesBytesFilesBytes
2015-11119,315,89400119,315,894
2016-01276,905,98600276,905,986
2015-106646,9832286,2908933,273
2014-03222,667,61831,270,255253,937,873
2016-055521,215,4961576,7595621,792,255
2016-049724,387,8052233,0849924,620,889
Lacey McDonnell May 26, 2016

Well, figured out the first part of my idiocy. which was expecting it to run NOT in admin>script fields. I do have a follow-up, though. We have a couple of exception errors on it.

Mostly property errors in static type checking

  1. no such property; filesize for class: java.lang.object @line 21, column 50
  2. no such property; zip for class: java.lang.object @line 23, column 30
  3. no such property; mimetype for class: java.lang.object @line 23, column 58
  4. no such property; filesize for class: java.lang.object @line 25, column 50
  5. no such property; zip for class: java.lang.object @line 27, column 32
  6. no such property; mimetype for class: java.lang.object @line 27, column 61
  7. no such property; filesize for class: java.lang.object @line 29, column 53
  8. Cannot find matching method java.util.list#join(). Please check if declared type is right and if the method exists. Possible solutions: join(java.lang.String), min(), min(groovy.lang.Closure), min(java.util.Comparator), wait(), find(), @line 38, column 104

 

image2016-5-26 10:25:26.png

 

Boris Georgiev _Appfire_
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.
May 26, 2016

You can ignore static type checks or modify the code and add types to the variables. Is it working now ?

Lacey McDonnell May 26, 2016

No; selecting Preview only swirls into oblivion with timeout error. If I bypass preview and simply add the script to the field, then the executions fail.

 

 

 

Lacey McDonnell May 26, 2016

@Jamie Echlin (Adaptavist)

 

Any advice?

Boris Georgiev _Appfire_
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.
May 26, 2016

This is a pretty heavy script if your instance is quite big and has a lot of attachments and this might be the reason for the timeout. My intention was to give you an idea of how the script should look like, but you might need to optimise it.

Would you provide some more context about the use case you're trying to implement ?

Lacey McDonnell May 26, 2016

Well, I know that our attachments currently account for around 150GB of space on the server. I need to trend the data flows, though, and this was a handy way to do it. I know our old server admin did it and ended up with the same chart, which is how I knew it was possible. 

I don't need it for every case, or on every issue, unlike some scripted fields. This would be a one-shot run for the field, maybe run every quarter for trending data. 

Does that help?

Suggest an answer

Log in or Sign up to answer