router.get('/sensor_qtr_hour_avg/sensor_id/:name/qty/:qty', function(req, res) { var rows = []; var query = "select * from sensor_qtr_hour_avg where sensor_id = '" + req.name + "' limit " + req.qty client.eachRow(query,function(n, row) { //the callback will be invoked per each row as soon as they are received var qtr_hour_avg = (row.qtr_hour_total / row.qtr_hour_count) / 100; rows.push({"sensor_id": row.sensor_id, "event_date_qtr_hour": row.event_date_qtr_hour, "qtr_hour_avg": qtr_hour_avg}); }, function (err, rowLength) { if (err) console.log(err); res.jsonp(rows); } ); });
router.get('/user_last', function(req, res) { var rows = []; var query = " select user_id, user_ts, latitude, longitude from user_last;" client.eachRow(query, [], consistency, function(n, row) { //the callback will be invoked per each row as soon as they are received var d = new Date(row.value_ts); rows.push({"timestamp": row.user_ts, "user" : row.user_id, "latitude" : row.latitude, "longitude" : row.longitude}); }, function (err, rowLength) { if (err) console.log(err); res.json(rows); } ); });
router.get('/sensor/:sensor_id/day/:day/qty/:qty', function(req, res) { var rows = []; var query = "select * from sensor_date where sensor_id = '" + req.sensor_id + "' and value_date = '" + req.day + "' limit " + req.qty client.eachRow(query,function(n, row) { //the callback will be invoked per each row as soon as they are received // rows["ts:" + row.event_ts] = parseInt(row.event_count); rows.push({"sensor_id": row.sensor_id, "sensor_date": row.value_date, "value_ts": row.value_ts, "value": parseFloat(row.value)}); }, function (err, rowLength) { if (err) console.log(err); res.jsonp(rows); } ); });
router.get('/sensor_last/qty/:qty', function(req, res) { var rows = []; var query = "select * from sensor_last limit " + req.qty; client.eachRow(query, [], consistency, function(n, row) { //the callback will be invoked per each row as soon as they are received var d = new Date(row.value_ts); rows.push({"timestamp": row.value_ts, "sensor" : row.sensor_type, "value" : row.value}); }, function (err, rowLength) { if (err) console.log(err); res.json(rows); } ); });
router.get('/count/type/:name/qty/:qty', function(req, res) { var now = new Date(); var now_utc = new Date(now.getUTCFullYear(), now.getUTCMonth(), now.getUTCDate(), now.getUTCHours(), now.getUTCMinutes(), now.getUTCSeconds()); var rows = []; var query = "select event_ts, event_count from event_count_date where sensor_type = '" + req.name + "' and event_date = '" + dateFormat( now_utc, "yyyymmdd") + "' limit " + req.qty client.eachRow(query,function(n, row) { //the callback will be invoked per each row as soon as they are received // rows["ts:" + row.event_ts] = parseInt(row.event_count); rows.push({"timestamp": row.event_ts, "value": parseInt(row.event_count)}); }, function (err, rowLength) { if (err) console.log(err); res.jsonp(rows); } ); });
router.get('/sensor_hour_avg/sensor_type/:type/qty/:qty', function(req, res) { var cols = []; var rows = []; cols.push({id: 'sensor_id', label: 'Sensor', type: 'string'}); cols.push({id: 'event_date_hour', label: 'Event Date Hour', type: 'string'}); cols.push({id: 'hour_avg', label: 'Hour Average', type: 'number'}); var query = "select * from sensor_type_hour_avg where sensor_type = '" + req.type + "' limit " + req.qty client.eachRow(query,function(n, row) { //the callback will be invoked per each row as soon as they are received var hour_avg = (row.hour_total / row.hour_count) / 100; rows.push({c:[{v: row.sensor_id}, { v: row.event_date_hour}, {v: hour_avg}]}); }, function (err, rowLength) { if (err) console.log(err); res.jsonp({cols: cols, rows: rows}); } ); });
router.get('/count/type/:name', function(req, res) { var now = new Date(); var now_utc = new Date(now.getUTCFullYear(), now.getUTCMonth(), now.getUTCDate(), now.getUTCHours(), now.getUTCMinutes(), now.getUTCSeconds()); var cols = []; var rows = []; var query = "select sensor_type, event_ts, event_count from event_count_date where sensor_type = '" + req.name + "' and event_date = '" + dateFormat( now_utc, "yyyymmdd") + "' limit 1" cols.push({id: 'sensor_type', label: 'Sensor Type', type: 'string'}); cols.push({id: 'count', label: 'Count', type: 'number'}); client.eachRow(query,function(n, row) { //the callback will be invoked per each row as soon as they are received rows.push({c:[{v: row.sensor_type}, {v: row.event_count.low}]}); }, function (err, rowLength) { if (err) console.log(err); res.jsonp({cols: cols, rows: rows}); } ); });