-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
132 lines (123 loc) · 4.95 KB
/
main.js
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
var fs = require('fs');
var journeys = [];
var stationCounter = {};
var daysCounter = {};
var visits = [];
var nodes = [];
var weekdays = ['Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday'];
fs.readFile('example.txt', function(err, data) {
if (err) throw err;
dataToObject(data);
console.log(journeys);
var totalTime = secondsToHrs(sumUp(journeys, 'duration'));
var totalDistance = sumUp(journeys, 'distance_km');
var totalCalories = sumUp(journeys, 'calories');
var totalC02saved = sumUp(journeys, 'co2saved_kg');
console.log(totalTime);
console.log(totalDistance, 'km');
console.log(totalC02saved, 'kg');
console.log(journeys.length + ' journeys');
console.log(totalCalories + ' kcal');
stationsCount(journeys);
daysCount(journeys);
for (var i in getSortedKeys(stationCounter)) {
visits.push({
'station': getSortedKeys(stationCounter)[i],
'number': stationCounter[getSortedKeys(stationCounter)[i]]
})
}
console.log(visits);
console.log(daysCounter);
console.log(new Date(journeys[0].date).toDateString(), "to", new Date(journeys[journeys.length - 1].date).toDateString());
// nodesCount(journeys);
});
function nodesCount(data) {
for (var i in data) {
var node = {};
var station1 = data[i].origin;
var station2 = data[i].destination;
node.from = station1;
node.to = station2;
nodes.push(node);
}
console.log(nodes);
}
function stationsCount(data) {
for (var i in data) {
var station1 = data[i].origin;
var station2 = data[i].destination;
if (data[i].origin in stationCounter) {
stationCounter[station1] += 1
} else {
stationCounter[station1] = 1
}
if (data[i].destination in stationCounter) {
stationCounter[station2] += 1
} else {
stationCounter[station2] = 1
}
}
}
function daysCount(data) {
for (var i in data) {
var day = weekdays[new Date(data[i].date).getDay()];
if (day in daysCounter) {
daysCounter[day] += 1
} else {
daysCounter[day] = 1
}
}
}
function getSortedKeys(obj) {
var array = [];
for (var key in obj) array.push(key);
array.sort(function(a, b) {
return obj[b] - obj[a]
});
return array
}
// sums all of elements in an array, rounding the *result* to 2 decimals
function sumUp(array, element) {
var sum = 0;
for (var i = 0; i < array.length; i++) {
sum += array[i][element]
}
return Math.round(sum * 100) / 100;
}
function secondsToHrs(d) {
var h = Math.floor(d / 3600);
var m = Math.floor(d % 3600 / 60);
return ((h > 0 ? h + " hrs " + (m < 10 ? "0" : "") : "") + m + " mins ");
}
function dataToObject(data) { // return journey lines to the journeys object
var array = data.toString().split("\n");
var count = 0;
for (var i in array) {
var line = array[i];
if (line.indexOf("Bike") !== -1 && line.indexOf("Invalid") === -1 && line.indexOf("until") !== -1) { // returns only the lines that relate to actual, valid, journeys
var journey = {}
journey.date = line.match(/\d{4}-\d{2}-\d{2}/)[0];
journey.start_time = line.match(/\d{2}:\d{2}:\d{2}/g)[0];
journey.end_time = line.match(/\d{2}:\d{2}:\d{2}/g)[1];
journey.bike = line.match(/Bike\s(\d{5})/)[1];
var detail = (/\(+([^.]+)\)+/).exec(line)[1]; // regex to search for the journey: between the first and last parentheses
if (detail.indexOf(' - ') !== -1) { // tests if journey has an origin *and* a destination
var bits = detail.split(/(\s-\s)/);
journey.origin = bits[0].replace(/\s.?$/g,""); // remove trailing whitespace that will cause problems
journey.destination = bits[2].replace(/\s.?$/g,"");
} else { // else if a journey has *only* an origin (meaning same origin as destination)
journey.origin = detail;
journey.destination = detail;
}
journey.duration = ( // calculate journey time in seconds
new Date(journey.date + 'T' + journey.end_time) -
new Date(journey.date + 'T' + journey.start_time)
) / 1000;
journey.calories = Math.round(journey.duration * 0.07); // see https://www.cyclestreets.net/journey/help/faq/#calories based on journey duration rather than distance of the route
journey.distance_km = Math.round(journey.duration / 3600 * 10 * 100) / 100 // this is based on a presumed 10km/h speed, average for urban cycling
journey.co2saved_kg = Math.round(journey.distance_km * 0.18641182099 * 100) / 100 // based on distance, which in turn was based on your journey duration. If 100miles saves 30kg C02, then 1km saves 0.18641182099kg
journeys[count] = journey;
count++
}
}
}