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() document = xwiki.getDocument(documentName) if (document.getValue('installedCount') != count) { document.set('installedCount', count) document.save('Update install count') } } 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.ExtensionProxyClass) as proxy, 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