diff --git a/api/controllers/StationsController.js b/api/controllers/StationsController.js index 5f362cd..4264594 100644 --- a/api/controllers/StationsController.js +++ b/api/controllers/StationsController.js @@ -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){ @@ -42,8 +47,8 @@ module.exports = { res.send(stationsCollection); }); - } -, ClassStationsGeo:function(req,res){ + }, + ClassStationsGeo:function(req,res){ var stationsCollection = {}; stationsCollection.type = "FeatureCollection"; @@ -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', @@ -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', @@ -105,15 +108,15 @@ 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){ @@ -121,41 +124,72 @@ module.exports = { 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', @@ -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); }); - }); + //}); }); }, diff --git a/config/routes.js b/config/routes.js index 656b499..5b9d224 100644 --- a/config/routes.js +++ b/config/routes.js @@ -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', @@ -85,7 +91,6 @@ module.exports.routes = { } }, - } /*