-
Notifications
You must be signed in to change notification settings - Fork 0
/
journey.js
66 lines (53 loc) · 2.01 KB
/
journey.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
exports.getJourneyData = function (reqUrl, callback) {
var url = require('url');
var azure = require('azure');
var Enumerable = require('linq');
var repo = require('./journey-repository.js');
var maxJourneysInHistory = 10; // see also numberOfPollsToKeep in poll-infoconnect.js
var query = url.parse(reqUrl, true).query;
var ref = query["ref"];
if (!ref || ref == 'undefined') {
callback(new Error("/journey expects ?ref=REF"));
return;
}
var tableQuery = azure.TableQuery
.select()
.from('journey')
.where('PartitionKey eq ?', ref);
repo.get(tableQuery, function (error, entities) {
if (error) {
callback(error);
return;
}
if (entities.length == 0) {
callback(new Error('No data for journey ref ' + ref));
return;
}
// wow - projection - thanks linq.js! Projecting here to strip the Table Storage fluff.
var journeys = Enumerable.from(entities)
.orderByDescending(function (e) { return e.pollDateTime; })
.select(function (e) {
return {
name: e.name,
averageSpeed: e.averageSpeed,
minutes: e.minutes,
pollDateTime: e.pollDateTime
}
})
.take(maxJourneysInHistory)
.toArray();
// last journey = 2nd journey
var lastJourney = (journeys.length > 1) ? journeys[1] : { averageSpeed: null, minutes: null, pollDateTime: null };
var journeyData = {
name: journeys[0].name,
averageSpeed: journeys[0].averageSpeed,
minutes: journeys[0].minutes,
pollDateTime: journeys[0].pollDateTime,
lastAverageSpeed: lastJourney.averageSpeed,
lastMinutes: lastJourney.minutes,
lastPollDateTime: lastJourney.pollDateTime,
journeys: journeys
};
callback(null, journeyData);
});
};