Advice: how to best incorporate a script to populate table

project description: dynamically populate a table made in xwiki
question: best way to incorporate the script

Not the best example:

{{velocity}}
#set ($sql = "select doc.fullName, email.value from XWikiDocument as doc, BaseObject as obj, StringProperty as email where doc.fullName = obj.name and obj.className = 'XWiki.XWikiUsers' and obj.id = email.id.id and email.id.name ='email' and doc.creationDate > '2019-02-04' order by doc.fullName")
(% class="table-condensed" style="auto;" %)
|=User|=Email|
#set ($result = $services.query.hql($sql).execute())
#foreach ($item in $result)
|$xwiki.getUserName($item[0], false)|$item[1]|
#end
 | |
{{/velocity}}

image

or look at https://extensions.xwiki.org/xwiki/bin/view/Extension/Livetable%20Macro