UpdateInstalledExtensionCountScheduler

Last modified by Marius Dumitru Florea on 2021/05/25 15:17

Introducing our new Analytics Application (Pro), a business-ready app that gives you insights about the most accessed pages, searched terms and user behavior - straight into your wiki. Learn more.
Installed extension count update

Update the counter of installed extensions

0 0 0 * * ?
The script is the code that will be executed when the job is triggered by the scheduler. It should be written in the Groovy language. The XWiki API is available through the xwiki and context pre-defined variables.
import groovy.json.JsonOutput; def updateCount(extensionId, documentName) { query = [ 'query': [ 'query_string' : [ 'lowercase_expanded_terms' : false, 'query' : "_timestamp:[now-1d TO *] AND extensions.id:\"$extensionId\"" ] ], 'aggs': [ 'instanceId_count': [ 'cardinality': [ 'field': 'instanceId' ] ] ] ] jsonQuery = JsonOutput.toJson(query) result = services.activeinstalls.searchInstalls('install2', jsonQuery, ['search_type' : 'count']) if (result) { count = result.getAsJsonObject('aggregations').getAsJsonObject('instanceId_count').getAsJsonPrimitive('value').getAsLong() // Get XWikiDocument pxwiki = xwiki.getXWiki() pxcontext = xcontext document = pxwiki.getDocument(documentName, pxcontext) extensionObject = document.getObject('ExtensionCode.ExtensionClass') if (extensionObject.getLongValue('installedCount') != count && extensionObject.getIntValue('showInstalledCount') != 0) { extensionObject.set('installedCount', count, pxcontext) if (document.comment == 'Update install count') { // Don't increment version (otherwise we could end up with a lot of those) document.setMetaDataDirty(false) document.setContentDirty(false) } // Save pxwiki.saveDocument(document, 'Update install count', true, pxcontext) } } else if (services.activeinstalls.lastError) { throw services.activeinstalls.lastError } } def logger = org.slf4j.LoggerFactory.getLogger('ExtensionCode.UpdateInstalledExtensionCountScheduler') logger.info('Starting updating installed extensions count') def extensions = services.query.xwql("select doc.fullName, extension.id from Document doc, doc.object(ExtensionCode.ExtensionClass) as extension").execute() for (extension in extensions) { logger.info('Updating installed extension [{}] count', extension[0]) try { updateCount(extension[1], extension[0]) } catch (Exception e) { logger.error('Failed to update installed count for extension [{}]', extension[0], e) } } logger.info('Installed extensions count update is finished')

Back to the job list

Tags:
    
XWiki SAS Copyright © 2024