List 10 last spaces created with a user macro

Patrice Champet February 24, 2020

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

1 accepted

0 votes
Answer accepted
JP _AC Bielefeld Leader_
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.
February 24, 2020
Patrice Champet February 26, 2020

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 ?

Like Nicolas Casel likes this
JP _AC Bielefeld Leader_
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.
February 26, 2020
Patrice Champet February 28, 2020

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
AUG Leaders

Atlassian Community Events