attachments by month

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

This widget could not be displayed.

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

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

 

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

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.

 

 

 

@Jamie Echlin (Adaptavist)

 

Any advice?

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 ?

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
Community showcase
Posted Tuesday in Jira

What modern development practices are at the heart of how your team delivers software?

Hey Community mates! Claire here from the Software Product Marketing team. We all know software development changes rapidly, and it's often tough to keep up. But from our research, we've found the h...

220 views 1 3
Join discussion

Atlassian User Groups

Connect with like-minded Atlassian users at free events near you!

Find a group

Connect with like-minded Atlassian users at free events near you!

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you