It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

List 10 last spaces created with a user macro

Hi there,

We want to give visibilty to all users on the spaces created, by adding this information on the dashboard.
So I am trying to create a simple user macro to list the 10 last created spaces.

It was quite easy to list all spaces with creation date.
But now, I am looking for some help regarding more complex algo to sort and select only the 10 last spaces.

This is my script right now.

----------------------------------------------------------------------------

## @noparams

#set($containerManagerClass=$content.class.forName("com.atlassian.spring.container.ContainerManager"))
#set($getInstanceMethod=$containerManagerClass.getDeclaredMethod("getInstance",null))
#set($containerManager=$getInstanceMethod.invoke(null,null))
#set($containerContext=$containerManager.containerContext)
#set($spaceLabelManager=$containerContext.getComponent("spaceLabelManager"))

#set ($spaces = $spaceManager.getAllSpaces())

<table class="confluenceTable">
<tr>
<th class="confluenceTh">Espace</th>
<th class="confluenceTh">Date de Création</th>
<th class="confluenceTh">Catégorie(s)</th>
</tr>

#foreach ( $space in $spaces )
#if ($space.isGlobal())
#end
#end

#foreach ( $space in $spaces )
#if ($space.isGlobal())
<tr>
#set($result=$space.getDescription().getDescriptionTitle())
#set($creationdate=$space.getCreationDate())
<td class="confluenceTd"><a href="$req.contextPath$space.getUrlPath()"><img class="space logo" width="32" height="32" src="$spaceManager.getLogoForSpace($space.key).getDownloadPath()" alt="$space.key"/> $space.getName()</a></td>
<td class="confluenceTd">$creationdate</td>
<td class="confluenceTd">
#foreach ($label in $spaceLabelManager.getTeamLabelsOnSpace($space.key))
<ac:structured-macro ac:name="status" ac:schema-version="1" ac:macro-id="73f05bec-6b92-413f-a826-3ff2d59096b3"><ac:parameter ac:name="subtle">true</ac:parameter><ac:parameter ac:name="title">$label.getName()</ac:parameter></ac:structured-macro>
#end
</td>
</tr>
#end
#end
</table>

--------------------------------------------------------------------------------------

Any idea, clue, advice on how to this ?

Thanks !

1 answer

Hi,

Thanks it helps a lot.

I have now the following user macro, which is working as expected.

 

------------------------------------------------

## Macro title:
## Macro has a body: Y or N
## Body processing: Selected body processing option
## Output: Selected output option
##
## Developed by: My Name
## Date created: dd/mm/yyyy
## Installed by: My Name

## @noparams

#set($containerManagerClass=$content.class.forName("com.atlassian.spring.container.ContainerManager"))
#set($getInstanceMethod=$containerManagerClass.getDeclaredMethod("getInstance",null))
#set($containerManager=$getInstanceMethod.invoke(null,null))
#set($containerContext=$containerManager.containerContext)
#set($spaceLabelManager=$containerContext.getComponent("spaceLabelManager"))

#set ( $spaces = $spaceManager.getAllSpaces() )

###### sort list of space by Creation Date
#set($size= ${spaces.size()})

#foreach($junk in $spaces) ##Bubble sort takes n^2 passes

#set($actual=-1) ##Having trouble with math on $velocityCount -- keeping my own count
#foreach($line in $spaces )

#set($actual=$actual+1)
#if($velocityCount<$size) ##Preventing bad array access
##### Compare this and previous
##### If this is smaller, swap

#if ($line.getCreationDate().compareTo($spaces.get($velocityCount).getCreationDate())<0)
#set ($tmp=$spaces.get($velocityCount))
#set ($junk=$spaces.set($velocityCount, $line))
#set ($junk=$spaces.set($actual, $tmp))
#end
#end
#end
#end

#set ($count = 0)
#foreach ( $space in $spaces )
#if($count>4)
#break
#end

#if ($space.isGlobal())
#set ($count = $count +1)

#set($result=$space.getDescription().getDescriptionTitle())
#set($creationdate=$space.getCreationDate())

<a href="$req.contextPath$space.getUrlPath()"><img class="space logo" width="20" height="20" src="$spaceManager.getLogoForSpace($space.key).getDownloadPath()" alt="$space.key"/> $space.getName()</a>
| $creationdate |
#foreach ($label in $spaceLabelManager.getTeamLabelsOnSpace($space.key))
<ac:structured-macro ac:name="status" ac:schema-version="1" ac:macro-id="73f05bec-6b92-413f-a826-3ff2d59096b3"><ac:parameter ac:name="subtle">true</ac:parameter><ac:parameter ac:name="title">$label.getName()</ac:parameter></ac:structured-macro>
#end |
<br>
#end
#end

---------------------------------------------------------------------------------------

 

Only issue is now with the performance, the macro is quite long as it is currently sorting all spaces.


Is there a way to restrict the ( $spaces = $spaceManager.getAllSpaces() ) to get only Global Spaces ?

Thanks for this answer...

Indeed, this is what I am trying to use, but I face the same issue as https://community.atlassian.com/t5/Answers-Developer-Questions/How-to-use-SpacesQuery-from-Velocity-template/qaq-p/493572

I don't know how to use this static class in a user Macro.
Any advice on how to proceed ?

Suggest an answer

Log in or Sign up to answer
TAGS
Community showcase
Posted in Confluence

Lessons and Learnings: Six Months of Working Remote [Discussion]

Hey there, folks! For most of us, the past six months- yes, you read that right- have been a journey. More people than ever before have pivoted to working remotely, and navigating being on-scre...

8,532 views 6 6
Join discussion

Community Events

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

Find an event

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

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you