Skip to main content
Example scripts
arrow icon
To homepage
Confluence
Data centre icon
Data Center

Display Spaces View Report

Created 1 year ago, Updated 1 month(s) ago
App in script
ScriptRunner For Confluence
ScriptRunner For Confluence
by Adaptavist
Compatibility
compatibility bullet
Confluence (7.15 - 8.6)
compatibility bullet
ScriptRunner For Confluence (7.10.0)
Language |
groovy
import org.joda.time.DateTime
import groovy.json.JsonSlurper
import groovy.xml.MarkupBuilder
import org.joda.time.DateTimeZone
import groovyx.net.http.URIBuilder
import com.atlassian.sal.api.UrlMode
import com.atlassian.sal.api.net.Request
import com.atlassian.sal.api.ApplicationProperties
import com.atlassian.sal.api.net.TrustedRequestFactory
import com.onresolve.scriptrunner.parameters.annotation.Select
import com.onresolve.scriptrunner.parameters.annotation.meta.Option
import com.onresolve.scriptrunner.runner.customisers.PluginModule

@PluginModule
TrustedRequestFactory trustedRequestFactory

@PluginModule
ApplicationProperties applicationProperties

@Select(
        label = "Space Type",
        description = "Select the <b>'Space Type'</b> you wish to include in your result",
        options = [
                @Option(label = "Global", value = "global"),
                @Option(label = "Personal", value = "personal"),
                @Option(label = "Global & Personal", value = "global,personal"),
        ]
)
String spaceType

@Select(
        label = "Content",
        description = "Select the <b>'Content'</b> you wish to include in your result ",
        options = [
                @Option(label = "Page", value = "page"),
                @Option(label = "Blog", value = "blog"),
                @Option(label = "Page & Blog", value = "page,blog"),
        ]
)
String content

@Select(
        label = "Sort Field",
        description = "This is to set which column you want to sort as <b>'Last Viewed'</b> or <b>'Total Views'</b> in the result.",
        options = [
                @Option(label = "Last Viewed", value = "VIEWED_LAST_DATE"),
                @Option(label = "Total Views", value = "VIEWED_COUNT"),
        ]
)
String sortField

@Select(
        label = "Sort Order",
        description = "This is to set which column you want to sort the <b>'Sort Field'</b> above to either ASC or DESC.",
        options = [
                @Option(label = "Ascending", value = "ASC"),
                @Option(label = "Descending", value = "DESC"),
        ]
)
String sortOrder

// Number of days you want to minus from current date. This is one of parameter (Date Ranges) needed in the rest request.
def days = 100
def toDate = new DateTime( new DateTime() , DateTimeZone.UTC )
def fromDate = toDate.minusDays(days)

// Rest request to get the activityBySpace
def rest = '/rest/confanalytics/1.0/instance/paginated/activityBySpace'
def host = applicationProperties.getBaseUrl(UrlMode.CANONICAL)

def url = new URIBuilder( host )
        .setPath(host + rest)
        .addQueryParam("fromDate", fromDate)
        .addQueryParam("toDate", toDate)
        .addQueryParam("period", "week")
        .addQueryParam("spaceType", spaceType)
        .addQueryParam("content", content)
        .addQueryParam("timezone", "GMT+00:00")
        .addQueryParam("type", "total")
        .addQueryParam("limit", "100")
        .addQueryParam("sortField", sortField)
        .addQueryParam("sortOrder", sortOrder) as String

def request = trustedRequestFactory.createTrustedRequest(Request.MethodType.GET, url)

def access = new URIBuilder(url).host
request.addTrustedTokenAuthentication(access)

try {
    def responseBody = request.execute()

    def result = new JsonSlurper().parseText(responseBody) as Map

    def activityBySpace = result.activityBySpace as List<Map>

    def stringWriter = new StringWriter()
    def build = new MarkupBuilder(stringWriter)

// build the output into a table
    build.table(class: "aui") {
        tbody {
            tr {
                th { p("Space Key") }
                th { p("Space Name") }
                th { p("Last Viewed") }
                th { p("Total Views") }
            }
        }
        activityBySpace.each { space ->
            def spaceLink = "<a href='${space?.link}'>${space?.name}</a>"
            tr {
                td { p(space?.key) }
                td { mkp.yieldUnescaped(spaceLink) }
                td { p(space?.lastViewedAt[0..9]) }
                td { p(space?.views) }
            }
        }
    }
    stringWriter.toString()
} catch ( Exception e ) {
    log.warn "Exception >>> ${e.message}"
    return e.message
}
Having an issue with this script?
Report it here