-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprocessor.js
More file actions
67 lines (63 loc) · 2.12 KB
/
Copy pathprocessor.js
File metadata and controls
67 lines (63 loc) · 2.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
const fs = require('fs');
const pgp = require('pg-promise')(/*options*/);
const connection = require('./keys').connection;
const db = pgp(connection);
const utils = require('./utils');
const dataFolder = __dirname + '/data/';
function processFiles() {
//get files
fs.readdir(dataFolder, (err, files) => {
files.forEach(async file => {
//read files
var filePath = dataFolder + file;
var content = await utils.readFile(filePath, file);
if (!content) return;
//parse to DB
var type = file.split('_')[0];
switch (type) {
case 'stations':
processStationFile(file, content);
break;
case 'points':
processPointsFile(file, content);
break;
default:
console.log(`Wrong Type. File name is: ${type}`);
return;
}
//remove file
fs.unlinkSync(filePath);
});
});
function processStationFile(file, content) {
var executionTime = new Date(content.executionTime).toISOString();
var downloadTime = new Date(parseInt(file.split('_')[1], 10)).toISOString();
var stationBeanList = content.stationBeanList;
stationBeanList.forEach(function(station) {
station.stationId = station.id;
delete station.id;
station.executionTime = executionTime;
station.downloadTime = downloadTime;
const query = pgp.helpers.insert(station, null, 'citibike_stations');
db.none(query).catch(error => {
console.log('err', error);
console.log('FILE:', file, 'CONTENT:', content);
});
});
}
function processPointsFile(file, content) {
var downloadTime = new Date(parseInt(file.split('_')[1], 10)).toISOString();
var stations = content.stations;
var stationIds = Object.keys(stations);
stationIds.forEach(station => {
const formatted = { stationId: station, points: stations[station], downloadTime };
const query = pgp.helpers.insert(formatted, null, 'citibike_points');
db.none(query).catch(error => {
console.log('err', error);
console.log('FILE:', file, 'CONTENT:', content);
});
});
}
}
// run
processFiles();