Skip to content

Commit

Permalink
finished stations route
Browse files Browse the repository at this point in the history
  • Loading branch information
econ24 committed May 29, 2014
1 parent a58035b commit 2004285
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 35 deletions.
102 changes: 68 additions & 34 deletions api/controllers/StationsController.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,15 @@
* @docs :: http://sailsjs.org/#!documentation/controllers
*/
var googleapis = require('googleapis');
var jwt = new googleapis.auth.JWT('424930963222-s59k4k5usekp20guokt0e605i06psh0d@developer.gserviceaccount.com', 'availwim.pem', '3d161a58ac3237c1a1f24fbdf6323385213f6afc', ['https://www.googleapis.com/auth/bigquery']);
var jwt = new googleapis.auth.JWT(
'424930963222-s59k4k5usekp20guokt0e605i06psh0d@developer.gserviceaccount.com',
'availwim.pem',
'3d161a58ac3237c1a1f24fbdf6323385213f6afc',
['https://www.googleapis.com/auth/bigquery']
);
jwt.authorize();

module.exports = {

module.exports = {

stationsGeo:function(req,res){

Expand All @@ -42,8 +47,8 @@ module.exports = {

res.send(stationsCollection);
});
}
, ClassStationsGeo:function(req,res){
},
ClassStationsGeo:function(req,res){

var stationsCollection = {};
stationsCollection.type = "FeatureCollection";
Expand All @@ -68,9 +73,8 @@ module.exports = {
},
getAllStations:function(req,res){
googleapis.discover('bigquery', 'v2').execute(function(err, client) {
jwt.authorize(function(err, result) {
//jwt.authorize(function(err, result) {
if (err) console.log(err);
console.log()
var request = client.bigquery.jobs.query({
kind: "bigquery#queryRequest",
projectId: 'avail-wim',
Expand All @@ -79,24 +83,23 @@ module.exports = {
request.body = {};
request.body.query = 'select state_fips,station_id,count(1) as num_trucks FROM [tmasWIM12.wim2012] where state_fips is not null group by state_fips,station_id order by state_fips,num_trucks desc;';
request.body.projectId = 'avail-wim';
console.log(request);
//console.log(request);
request
.withAuthClient(jwt)
.execute(function(err, response) {
if (err) console.log(err);
console.log(response);
//console.log(response);
res.json(response);
});
});
//});
});
},

getStateStations:function(req,res){
var state_fips = req.param('stateFips');
googleapis.discover('bigquery', 'v2').execute(function(err, client) {
jwt.authorize(function(err, result) {
//jwt.authorize(function(err, result) {
if (err) console.log(err);
console.log()
var request = client.bigquery.jobs.query({
kind: "bigquery#queryRequest",
projectId: 'avail-wim',
Expand All @@ -105,57 +108,88 @@ module.exports = {
request.body = {};
request.body.query = 'select station_id, year,count( distinct num_months) as numMon,count(distinct num_days) as numDay, count(distinct num_hours)/8760 as percent, sum(total)/count(distinct num_days) as AADT from (select station_id,year,concat(string(year),string(month)) as num_months,concat(string(year),string(month),string(day)) as num_days ,concat(string(year),string(month),string(day),string(hour)) as num_hours, count(station_id) as total FROM [tmasWIM12.wim2012] where state_fips="'+state_fips+'" and state_fips is not null group by station_id,year,num_hours,num_months,num_days) group by station_id,year order by station_id,year';
request.body.projectId = 'avail-wim';
console.log(request);
//console.log(request);
request
.withAuthClient(jwt)
.execute(function(err, response) {
if (err) console.log(err);
console.log(response);
//console.log(response);
res.json(response);
});
});
//});
});
},
getStationData:function(req,res){
if(typeof req.param('station_id') == 'undefined'){
res.send('{status:"error",message:"station_id required"}',500);
return console.log(err);
}
var station_id = req.param('station_id');
var sql = "select "
+ "concat('20',string(year),'-',string(month)) as date, class, count(1)"
+ "from [tmasWIM12.wim2012] where station_id = '"+station_id+"'"
+ "group by station_id,date,class"
+ "order by station_id,date,class ";
var station_id = req.param('station_id'),
depth = req.param('depth');

//SELECT year, month, class, count(*) as amount FROM [tmasWIM12.wim2012]
//where station_id = '000014' group by year, month, class order by month, class;
var select = {
1: 'year',
2: 'month',
3: 'day',
4: 'hour'
};

var SQL = generateSQL();

//console.log(SQL);

googleapis.discover('bigquery', 'v2').execute(function(err, client) {
jwt.authorize(function(err, result) {
if (err) console.log(err);
console.log()
//jwt.authorize(function(err, result) {
if (err) {
console.log(err);
return;
}

var request = client.bigquery.jobs.query({
kind: "bigquery#queryRequest",
projectId: 'avail-wim',
timeoutMs: '30000'
});

request.body = {};
request.body.query = sql;
request.body.query = SQL;
request.body.projectId = 'avail-wim';
console.log(request);
//console.log(request);
request.withAuthClient(jwt)
.execute(function(err, response) {
if (err) console.log(err);
console.log(response);
if (err) {
console.log(err);
return;
}
//console.log(response);
res.json(response);
});
});
//});
});
function generateSQL() {
var sql = "SELECT " + select[depth.length] + ", class, total_weight as weight, count(*) as amount "
+ "FROM [tmasWIM12.wim2012] "
+ "WHERE station_id = '"+station_id+"' "
+ addPredicates()
+ "GROUP BY " + select[depth.length] + ", class, weight "
+ "ORDER BY " + select[depth.length] + ";";
return sql;
}
function addPredicates() {
var preds = '';
for (var i = 1; i < depth.length; i++) {
preds += 'AND ' + select[i] + ' = ' + depth[i] + ' ';
}
return preds;
}
},
getTrucks:function(req,res){
var station_id = req.param('stationId');
googleapis.discover('bigquery', 'v2').execute(function(err, client) {
jwt.authorize(function(err, result) {
//jwt.authorize(function(err, result) {
if (err) console.log(err);
console.log()
var request = client.bigquery.jobs.query({
kind: "bigquery#queryRequest",
projectId: 'avail-wim',
Expand All @@ -164,14 +198,14 @@ module.exports = {
request.body = {};
request.body.query = 'select num_days,count(num_days) as numDay,month,day,class from(select station_id,class,concat(string(year),string(month),string(day)) as num_days, month,day FROM [tmasWIM12.wim2012] where station_id="'+station_id+'" and station_id is not null) group by num_days,month,day,class';
request.body.projectId = 'avail-wim';
console.log(request);
//console.log(request);
request.withAuthClient(jwt)
.execute(function(err, response) {
if (err) console.log(err);
console.log(response);
//console.log(response);
res.json(response);
});
});
//});
});
},

Expand Down
7 changes: 6 additions & 1 deletion config/routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,12 @@ module.exports.routes = {
'/stations/graphData/:station_id':{
controller : 'StationsController',
action : 'getStationData',
cors: {
origin: '*',
methods: 'GET, POST, PUT, DELETE, OPTIONS, HEAD',
headers: 'content-type,X-Requested-With'
}
},
'/stations/byStation/:stationId':{
controller : 'StationsController',
action : 'getTrucks',
Expand All @@ -85,7 +91,6 @@ module.exports.routes = {
}
},

}


/*
Expand Down

0 comments on commit 2004285

Please sign in to comment.