forked from mwakao/iota-starter-server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinsights.js
126 lines (116 loc) · 3.8 KB
/
insights.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
/**
* Copyright 2016 IBM Corp. All Rights Reserved.
*
* Licensed under the IBM License, a copy of which may be obtained at:
*
* http://www14.software.ibm.com/cgi-bin/weblap/lap.pl?li_formnum=L-DDIN-AEGGZJ&popup=y&title=IBM%20IoT%20for%20Automotive%20Sample%20Starter%20Apps%20%28Android-Mobile%20and%20Server-all%29
*
* You may not use this file except in compliance with the license.
*/
/*
* REST APIs using Driver Behavior service as backend
*/
var Q = require('q');
var router = module.exports = require('express').Router();
var authenticate = require('./auth.js').authenticate;
var driverInsightsProbe = require('../../driverInsights/probe');
var driverInsightsAnalyze = require('../../driverInsights/analyze');
var driverInsightsTripRoutes = require('../../driverInsights/tripRoutes.js');
var dbClient = require('../../cloudantHelper.js');
router.get('/probeData', authenticate, function(req, res) {
driverInsightsProbe.getCarProbeDataListAsDate().then(function(msg){
res.send(msg);
})["catch"](function(error){
res.send(error);
});
});
router.get('/driverInsights', authenticate, function(req, res) {
getUserTrips(req).then(function(tripIdList){
driverInsightsAnalyze.getList(tripIdList).then(function(msg){
res.send(msg);
});
})["catch"](function(error){
res.send(error);
});
});
router.get('/driverInsights/statistics', authenticate, function(req, res) {
getUserTrips(req).then(function(tripIdList){
driverInsightsAnalyze.getStatistics(tripIdList).then(function(msg){
res.send(msg);
});
})["catch"](function(error){
res.send(error);
});
});
router.get('/driverInsights/behaviors', authenticate, function(req, res) {
getUserTrips(req).then(function(tripIdList){
driverInsightsAnalyze.getTripList(tripIdList, req.query.all).then(function(msg){
res.send(msg);
});
})["catch"](function(error){
res.send(error);
});
});
router.get('/driverInsights/:trip_uuid', authenticate, function(req, res) {
driverInsightsAnalyze.getDetail(req.params.trip_uuid).then(function(msg){
res.send(msg);
})["catch"](function(error){
res.send(error);
});
});
router.get('/driverInsights/behaviors/latest', authenticate, function(req, res) {
driverInsightsAnalyze.getLatestBehavior().then(function(msg){
res.send(msg);
})["catch"](function(error){
res.send(error);
});
});
router.get('/driverInsights/behaviors/:trip_uuid', authenticate, function(req, res) {
driverInsightsAnalyze.getBehavior(req.params.trip_uuid).then(function(msg){
res.send(msg);
})["catch"](function(error){
res.send(error);
});
});
router.get("/driverInsights/triproutes/:trip_uuid", function(req, res){
driverInsightsTripRoutes.getTripRoute(req.params.trip_uuid).then(function(msg){
res.send(msg);
})["catch"](function(error){
res.send(error);
})
});
router.get("/driverInsights/tripanalysisstatus/:trip_id", function(req, res){
driverInsightsAnalyze.getTripAnalysisStatus(req.params.trip_id).then(function(msg){
res.send(msg);
})["catch"](function(error){
res.send(error);
});
});
router.get("/triproutes/:trip_id", function(req, res){
driverInsightsTripRoutes.getTripRouteById(req.params.trip_id, req.query).then(function(msg){
res.send(msg);
})["catch"](function(error){
res.send(error);
})
});
function getUserTrips(req){
var userid = req.user && req.user.id;
if(!userid){
return Q([]);
}
var deferred = Q.defer();
dbClient.searchView("closedReservations", {key: userid}).then(
function(result){
var trip_ids = result.rows.map(function(item) {
var reservation = item.value;
return reservation.trip_id;
}).filter(function(trip_id){
return trip_id;
});
deferred.resolve(trip_ids);
}
)["catch"](function(error){
deferred.reject(error);
});
return deferred.promise;
}