Created
May 23, 2015 21:44
-
-
Save lbrenman/1b1d689d6b1e16ae2c68 to your computer and use it in GitHub Desktop.
Appcelerator Arrow Totals Row and KPI Calculation using custom field and getter and a block
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var Arrow = require('arrow'); | |
var PostBlock = Arrow.Block.extend({ | |
name: 'addtotal', | |
description: 'add total row to sales data', | |
action: function(req, resp, next) { | |
// req.log.info("Post Example Block executed"); | |
var body = JSON.parse(resp.body); | |
// console.log("body = "+JSON.stringify(body)); | |
var data = body[body.key]; | |
var dataLen = data.length; | |
var replies = 0; | |
// console.log("data = "+JSON.stringify(data)); | |
// console.log("dataLen = "+dataLen); | |
if(dataLen){ //findAll | |
var actTotal=0, fcstTotal=0; | |
data.forEach(function (_row, _index) { | |
// console.log("region = "+_row.region); | |
// console.log("act = "+_row.act); | |
// console.log("fcst = "+_row.fcst); | |
actTotal += _row.act; | |
fcstTotal += _row.fcst; | |
}); | |
} else { //findOne | |
// console.log("region = "+data.region); | |
// console.log("act = "+data.act); | |
// console.log("fcst = "+data.fcst); | |
actTotal =data.act; | |
fcstTotal = data.fcst; | |
// console.log("data = "+JSON.stringify(data)); | |
} | |
resp.success({ | |
data: body[body.key], | |
totals: {"actTotal": actTotal, "fcstTotal": fcstTotal} | |
}, next); | |
} | |
}); | |
module.exports = PostBlock; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"success": true, | |
"request-id": "853dad2a-7dcf-4b91-b076-775d53f19679", | |
"key": "salesbyregion", | |
"salesbyregion": { | |
"data": [ | |
{ | |
"id": 0, | |
"rid": 0, | |
"region": "NE", | |
"act": 61, | |
"fcst": 101, | |
"percent": 60.396039603960396, | |
"kpi": "yellow" | |
}, | |
{ | |
"id": 1, | |
"rid": 1, | |
"region": "SE", | |
"act": 30, | |
"fcst": 74, | |
"percent": 40.54054054054054, | |
"kpi": "red" | |
}, | |
{ | |
"id": 2, | |
"rid": 2, | |
"region": "West", | |
"act": 32, | |
"fcst": 46, | |
"percent": 69.56521739130434, | |
"kpi": "yellow" | |
}, | |
{ | |
"id": 3, | |
"rid": 3, | |
"region": "Cent", | |
"act": 8, | |
"fcst": 11, | |
"percent": 72.72727272727273, | |
"kpi": "yellow" | |
}, | |
{ | |
"id": 4, | |
"rid": 4, | |
"region": "Can", | |
"act": 20, | |
"fcst": 44, | |
"percent": 45.45454545454545, | |
"kpi": "red" | |
} | |
], | |
"totals": { | |
"actTotal": 151, | |
"fcstTotal": 276 | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"success": true, | |
"request-id": "4ae0b8fc-6c3c-421a-8597-cb4b6d9ff233", | |
"key": "table1s", | |
"table1s": [ | |
{ | |
"id": 0, | |
"rid": 0, | |
"region": "NE", | |
"act": 61, | |
"fcst": 101 | |
}, | |
{ | |
"id": 1, | |
"rid": 1, | |
"region": "SE", | |
"act": 30, | |
"fcst": 74 | |
}, | |
{ | |
"id": 2, | |
"rid": 2, | |
"region": "West", | |
"act": 32, | |
"fcst": 46 | |
}, | |
{ | |
"id": 3, | |
"rid": 3, | |
"region": "Cent", | |
"act": 8, | |
"fcst": 11 | |
}, | |
{ | |
"id": 4, | |
"rid": 4, | |
"region": "Can", | |
"act": 20, | |
"fcst": 44 | |
} | |
] | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var Arrow = require("arrow"); | |
var Model = Arrow.Model.reduce("appc.mysql/table1","SalesByRegion",{ | |
"fields": { | |
"rid": { | |
"type": "number", | |
"required": false, | |
"optional": true, | |
"readonly": false, | |
"writeonly": false | |
}, | |
"region": { | |
"type": "string", | |
"required": false, | |
"optional": true, | |
"readonly": false, | |
"writeonly": false | |
}, | |
"act": { | |
"type": "number", | |
"required": false, | |
"optional": true, | |
"readonly": false, | |
"writeonly": false | |
}, | |
"fcst": { | |
"type": "number", | |
"required": false, | |
"optional": true, | |
"readonly": false, | |
"writeonly": false | |
}, | |
"kpi": { | |
"type": "string", | |
"custom": true, | |
"get": function(val,key,model){ | |
var kpi_val; | |
var p = 100*model.get('act')/model.get('fcst'); | |
if(p < 50) { | |
kpi_val="red"; | |
} else if(p < 70) { | |
kpi_val = "yellow"; | |
} else { | |
kpi_val = "green"; | |
} | |
return kpi_val; | |
} | |
} | |
}, | |
// serialize: function(obj, instance, model) { | |
// console.log(obj); // see what’s in side…its probably something like the body[body.key] array in your block | |
// // You can add your totals here | |
// // obj.xxx = {"test":"aaa"}; | |
// return obj; // you always have to return the data back | |
// }, | |
"after": "addtotal", | |
"actions": [ | |
"create", | |
"read", | |
"update", | |
"delete", | |
"deleteAll" | |
], | |
"singular": "SalesByRegion", | |
"plural": "SalesByRegions" | |
}); | |
module.exports = Model; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment