-
Notifications
You must be signed in to change notification settings - Fork 1
/
mysql_trip.js
192 lines (176 loc) · 5.99 KB
/
mysql_trip.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
var mysql = require('./mysql.js');
var Trip = require('./trip.js');
var TripTrace = require('./traces/trip_trace.js');
var AccelerometerTrace = require('./traces/accelerometer_trace.js');
var GyroscopeTrace = require('./traces/gyroscope_trace.js');
var Promise = require("bluebird");
function traceTypeAndColumn(stringname) {
if(stringname == "Trip") {
return [TripTrace, "trip_trace"];
} else if(stringname == "Accel") {
return [AccelerometerTrace, "accelerometer_trace"];
} else if(stringname == "Gyro") {
return [GyroscopeTrace, "gyroscope_trace"];
} else if(stringname == "Rotation") {
return null;
} else {
return null;
}
}
// NOTE: Trip GUIDs are ONLY GUARANTEED TO BE UNIQUE PER USER
// You cannot only select by guid without a userid or you may get more than one / the wrong trip
var mysqltrip = function() {
}
//trace format:
// { "type": "Trip", "value": { "alt": 0, "lat": 43, "lon": -89.004, "speed": 0,"time": 1479247246164, etc.... }}
mysqltrip.prototype.addTracesToTrip = function(trace_messages, trip, callback) {
mysql.getConnection(function(err, conn) {
if(!trip.tripid) {
callback("No trip id for traces");
return;
}
if(err) {
callback(err);
return;
}
function callandrelease(err) {
callback(err);
conn.release();
}
var traces_to_insert = {};
for (var i = trace_messages.length - 1; i >= 0; i--) {
tracemessage = trace_messages[i];
var trace_type_name = tracemessage.type;
var value = tracemessage.value;
var typeColumn = traceTypeAndColumn(trace_type_name);
if(typeColumn) {
var table_name = typeColumn[1];
var trace_class = typeColumn[0];
if(!traces_to_insert[table_name])
{
traces_to_insert[table_name] = {'trace_class':trace_class, 'values':[]}
}
trace= new trace_class();
trace.fromObjectSafe(value);
trace.tripid = trip.tripid;
traces_to_insert[table_name].values.push(trace)
} else {
//console.log("No SQL column type for trace " + tracemessage.type);
}
}
var query_promises = [];
var promise_query = Promise.promisify(conn.query, {context: conn});
for(var table_name in traces_to_insert) {
var trace_class = traces_to_insert[table_name].trace_class;
var column_names = trace_class.user_facing.concat(trace_class.private);
// console.log(traces_to_insert[table_name].values);
var values = [];
for (var i = 0; i < traces_to_insert[table_name].values.length; i++) {
var single_trace = traces_to_insert[table_name].values[i];
values.push(column_names.map(function(col) {return single_trace[col]}));
}
console.log("Got trace type: "+table_name);
//console.log(column_names);
// console.log(values);
var sql = "INSERT IGNORE INTO ?? (??) VALUES ?"
query_promises.push(promise_query(sql, [table_name,column_names, values]));
}
Promise.all(query_promises).then(function() {
console.log("All the traces were added");
callandrelease();
}, function(err) {
callandrelease(err);
});
});
}
mysqltrip.prototype.updateOrCreateTrip = function (trip, user, callback) {
if(trip.guid){
trip.userid = user.userid;
mysql.getConnection(function(err, conn) {
if(err) {
callback(err, null);
return;
}
function callandrelease(err, trip) {
conn.release();
callback(err, trip);
}
console.log("Insert or update for "+trip.guid+" for user "+trip.userid);
var sql="INSERT INTO `trip` SET ? ON DUPLICATE KEY UPDATE ?; SELECT * FROM `trip` WHERE `userid` = ? and `guid` LIKE ?;"
conn.query(sql, [trip, trip, trip.userid, trip.guid], function(err, rows){
if(err) {
console.log(err);
callandrelease(err, null);
} else {
//trip was inserted
trip = new Trip();
trip.fromObject(rows[1][0]);
callandrelease(null, trip);
}
});
});
}
}
mysqltrip.prototype.getTripTraces = function (userid, tripguid, tracetypename, start, callback) {
var typeColumn = traceTypeAndColumn(tracetypename)
if(!typeColumn) {
callback("No valid type specified", null);
return;
}
Promise.using(mysql.getConnectionDisposer(), function(conn) {
var sql = "SELECT * FROM `trip` LEFT JOIN ?? as T ON T.tripid = trip.tripid WHERE trip.guid = ? AND T.time >= ? AND trip.userid = ?";
return conn.query(sql, [typeColumn[1], tripguid, start, userid]).then(function(rows) {
var traces = rows.map(function(elem){
var trace = new typeColumn[0]();
trace.fromObjectSafe(elem);
return trace;
});
callback(null, traces);
}).catch(function(error) {
console.log(error);
callback(err, null);
});
});
}
mysqltrip.prototype.getTripsByUserID = function (userid, callback) {
mysql.getConnection(function(err, conn) {
if(err) {
callback(err, null);
return;
}
var sql = "select ?? from derived_trip where userid = ?;";
conn.query(sql, [Trip.user_facing, userid], function(err, rows, field){
if(err) {
callback(err, null);
} else {
callback(null, rows);
}
conn.release();
});
});
}
mysqltrip.prototype.searchTrips = function (userid, start, end, callback) {
mysql.getConnection(function(err, conn) {
if(err) {
callback(err, null);
return;
}
var sql = "SELECT * FROM derived_trip WHERE data_starttime >= " + start +" and data_endtime <= " + end + " and userid = " + userid + " and status >= 1 ORDER BY data_endtime DESC";
conn.query(sql, function(err, rows) {
if (err) {
callback(err, null);
} else if (rows) {
var trips = rows.map(function(elem){
var trip = new Trip();
trip.fromObjectSafe(elem);
return trip;
});
callback(null, trips);
} else {
callback(null, null);
}
conn.release();
});
});
}
module.exports = new mysqltrip();