Skip to content

Commit

Permalink
Padronizando timestamp e direcao
Browse files Browse the repository at this point in the history
- O Google Big Query tem seu DateTime no formato YYYY-MM-DD
HH:MM[:SS[.SSSSSS]], compatibilizando usando a lib moment
- Usar NaN na DIRECAO gerou erros de insert na tabela, como solução
passaremos a usar 999 para direção não informada.
  • Loading branch information
marco-jardim committed Nov 30, 2014
1 parent 154e688 commit bbfc1b2
Show file tree
Hide file tree
Showing 3 changed files with 91,937 additions and 14,097 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,4 @@ node_modules

# Users Environment Variables
.lock-wscript
queue.json
12 changes: 9 additions & 3 deletions dataQueuer.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
var http = require('http'); // importing http module. it's a node's default module.
var fs = require('fs'); // importing filesystem module. using fs to read riobus-config.json.
var moment = require('moment'); // easy timestamp formatting

// Access riobus-config.json
var config = JSON.parse(fs.readFileSync(__dirname + "/riobus-config.json")).dataQueuer;
Expand Down Expand Up @@ -41,15 +42,20 @@ function appendDataAsync(data_to_append) {
});
}

// Google's Big Query timestamp format, UTC time
function toTimestamp(datetime) {
return moment(datetime).format('YYYY-MM-DD HH:mm:ss');
}

// Parses the data to insert into bigquery
function data_formatter() {
var formatted_data = "";
var bus_array = data.DATA;
for(var i = 0; i < bus_array.length; i++) {
if(bus_array[i][6] == "") {
bus_array[i][6] = NaN; // avoid leaving DIRECAO as ""
}
formatted_data += '{"DATAHORA": "' + bus_array[i][0] + '", "ORDEM": "' + bus_array[i][1] + '", "LINHA": "' + bus_array[i][2] + '", "LATITUDE": ' + bus_array[i][3] + ', "LONGITUDE": ' + bus_array[i][4] + ', "VELOCIDADE": ' + bus_array[i][5] + ', "DIRECAO":' + bus_array[i][6] + ', "LASTUPDATE": "' + data.LASTUPDATE + '"}\n';
bus_array[i][6] = 999; // avoid leaving DIRECAO as ""
}
formatted_data += '{"DATAHORA": "' + toTimestamp(bus_array[i][0]) + '", "ORDEM": "' + bus_array[i][1] + '", "LINHA": "' + bus_array[i][2] + '", "LATITUDE": ' + bus_array[i][3] + ', "LONGITUDE": ' + bus_array[i][4] + ', "VELOCIDADE": ' + bus_array[i][5] + ', "DIRECAO":' + bus_array[i][6] + ', "LASTUPDATE": "' + toTimestamp(data.LASTUPDATE) + '"}\n';
}

return formatted_data;
Expand Down
Loading

0 comments on commit bbfc1b2

Please sign in to comment.