...
DataUtil.groovy
Code Block |
---|
|
void LoadData(){
def ctx = api.getDatamartContext()
def dm = ctx.getDatamart("Transactions")
def q1 = ctx.newQuery(dm, true)
q1.select("sku")
q1.select("SUM(NetSales)", "SumNetSales ")
q1.select("SUM(Cost)", "SumCost")
q1.where(Filter.equal("sku", api.product("sku")))
api.global.queryData = ctx.executeQuery(q1).getData()
} |
ProcessData.groovy
Code Block |
---|
|
DataUtil.LoadData() // fills api.global.queryData from query
for(row in api.global.queryData){
row.ProfitMargin = (row.SumNetSales - row.SumCost) / row.SumNetSales
} |
ResultMatrix.groovy
Code Block |
---|
|
def resultMatrix = api.newMatrix("SKU", "ProfitMargin")
api.global.queryData.each{ row ->
resultMatrix.addRow(row.sku, row.ProfitMargin)
}
return resultMatrix |
...
DataUtil.groovy
Code Block |
---|
|
List<Map> GetData(){
def ctx = api.getDatamartContext()
def dm = ctx.getDatamart("Transactions")
def q1 = ctx.newQuery(dm, true)
q1.select("sku")
q1.select("SUM(NetSales)", "SumNetSales ")
q1.select("SUM(Cost)", "SumCost")
q1.where(Filter.equal("sku", api.product("sku")))
return ctx.executeQuery(q1).getData()
} |
ProcessData.groovy
Code Block |
---|
|
List<Map> data = DataUtil.GetData() // fills local variable from query
for(row in data ){
row.ProfitMargin = (row.SumNetSales - row.SumCost) / row.SumNetSales
}
return data |
ResultMatrix.groovy
Code Block |
---|
|
def resultMatrix = api.newMatrix("SKU", "ProfitMargin")
out.ProcessData.each{ row ->
resultMatrix.addRow(row.sku, row.ProfitMargin)
}
return resultMatrix |
...
DataUtil.groovy
Code Block |
---|
|
List<Map> GetData(){
def ctx = api.getDatamartContext()
def dm = ctx.getDatamart("Transactions")
def q1 = ctx.newQuery(dm, true)
q1.select("sku")
q1.select("SUM(NetSales)", "SumNetSales ")
q1.select("SUM(Cost)", "SumCost")
q1.where(Filter.equal("sku", api.product("sku")))
return ctx.executeQuery(q1).getData()
}
api.global.queryData = GetData() |
ProcessData.groovy
Code Block |
---|
|
api.global.queryData.each{ it ->
// do something
} |
...
DataUtil.groovy
Code Block |
---|
|
List<Map> GetData(){
return libs.SharedLib.CacheUtils.getOrSet("MyDataCacheKey"){
def ctx = api.getDatamartContext()
def dm = ctx.getDatamart("Transactions")
def q1 = ctx.newQuery(dm, true)
q1.select("sku")
q1.select("SUM(NetSales)", "SumNetSales ")
q1.select("SUM(Cost)", "SumCost")
q1.where(Filter.equal("sku", api.product("sku")))
return ctx.executeQuery(q1).getData()
}
} |
ProcessData.groovy
Code Block |
---|
|
List<Map> data = DataUtil.GetData()
data.each{ it ->
// do something
} |
...
DataLibrary.groovy (library logic)
Code Block |
---|
|
List<Map> GetTransactionData(){
def ctx = api.getDatamartContext()
def dm = ctx.getDatamart("Transactions")
def q1 = ctx.newQuery(dm, true)
q1.select("sku")
q1.select("SUM(NetSales)", "SumNetSales ")
q1.select("SUM(Cost)", "SumCost")
q1.where(Filter.equal("sku", api.product("sku")))
q1.where(Filter.equal("category", api.getElement("Category")))
q1.where(Filter.greaterThan("netSales", out.NetSalesMinimum))
q1.where(Filter.notIn("sku", api.global.exclusionSkus))
return ctx.executeQuery(q1).getData()
} |
...
Instead, this lib method should use method parameters, so it can be easily reused by any other piece of code like so:
DataLibrary.groovy (library logic)
Code Block |
---|
|
List<Map> GetTransactionData(String sku, String category, Number netSalesMinimum, List<String> exclusionSkus){
def ctx = api.getDatamartContext()
def dm = ctx.getDatamart("Transactions")
def q1 = ctx.newQuery(dm, true)
q1.select("sku")
q1.select("SUM(NetSales)", "SumNetSales ")
q1.select("SUM(Cost)", "SumCost")
q1.where(Filter.equal("sku", sku))
q1.where(Filter.equal("category", category))
q1.where(Filter.greaterThan("netSales", netSalesMinimum))
q1.where(Filter.notIn("sku", exclusionSkus))
return ctx.executeQuery(q1).getData()
} |
...