Groovy is available in multiple contexts in 1Gateway: it can run as a poller plugin, a mapping, filters and as sender plugins. Much of the functionality can be placed in any of these contexts and choosing where to place a certain data manipulation function can seem arbitrary.

 

Conceptually, plugins are meant to handle communications with endpoints only. Mappers are intended to handle all data transformations. Some exceptions to this rule can be necessary though, especially when dealing with large volumes of data. If a poller plugin retrieves a very large dataset that contains redundant data, it can parse that dataset and publish a much smaller message with the redundant data removed.

 

 

Consider the following example:

 

ret=option.http.get("https://opendata.ecdc.europa.eu/covid19/nationalcasedeath/json")
map=parser.asMap(ret)
calendar=Calendar.getInstance()
weekfilter=calendar.get(Calendar.YEAR)+"-"+(calendar.get(Calendar.WEEK_OF_YEAR)-2)
weekdata=map.findAll { it.year_week==weekfilter}
api.publish(weekdata,"CovidStats")
return null;

 

This ECDC webservice returns weekly statistics for each country since the start of the pandemic. We query this webservice periodically to check the site for updated data and publish the result here: https://1gateway.com/product/covid-19-dashboard/

The Groovy script filters out all old data and publishes only the data from the last week. 

Iterating over lists of objects can be done much easier in a mapper than in a plugin by using regular expressions in the from field. These techniques can be combined, using the power of mappers to iterate over datastructures while using the plugin to remove any redundant data.

 

 

SortingLink to Sorting

 

mydb=option.dbstore("MyDBStore")
lastTS=mydb.get("LastTSS")
if (lastTS==null) lastTS=0
else lastTS=Integer.parseInt(lastTS)

myArray=[[id: 10],[id: 12],[id: 6]]
myArray.sort{ it.id }
myArray.each() { myTS ->
println "current TS: "+myTS.id
   if (myTS.id>lastTS) {
       mydb.put("LastTSS",myTS.id)
       lastTS=myTS.id
   }
}
println " Highest: "+lasts
def sorted = incident_jsonslurp.result.sort { it.sys_updated_on.value }.sys_updated_on.value
FirstTS = sorted.first()
LastTS = sorted.last()
println("LastTS: " + LastTS + " FirstTS: " + FirstTS)