-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathdbFunctions.js
264 lines (205 loc) · 10 KB
/
dbFunctions.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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
/****************************************************************************
* dbFunctions.js
* March 2021
*****************************************************************************/
const crypto = require('crypto');
/**
* Generate a random, alphanumeric ID of length 10
*/
function generateID() {
return crypto.randomBytes(5).toString('hex');
}
/**
* Add a row to the Uploads table
* @param {object} dbClient Postgres client
* @param {string} deviceID Unique, 16 character internal EFM32 serial number
* @param {string} email User email address
* @param {string} subscription User push notification subscription
* @param {float} maxVelocity Maximum receiver velocity
* @param {string} nickname Uplaod nickname
* @param {function} callback Function called on completion
*/
exports.addUpload = (dbClient, deviceID, email, subscription, maxVelocity, nickname, callback) => {
const now = new Date();
const year = now.getUTCFullYear();
const month = now.getUTCMonth() + 1;
const day = now.getUTCDate();
const hours = now.getUTCHours();
const minutes = now.getUTCMinutes();
const seconds = now.getUTCSeconds();
const msString = now.getUTCMilliseconds().toString().padStart(3, '0');
let dtString = year.toString();
dtString += '-';
dtString += month.toString();
dtString += '-';
dtString += day.toString();
dtString += ' ';
dtString += hours.toString();
dtString += ':';
dtString += minutes.toString();
dtString += ':';
dtString += seconds.toString();
dtString += '.';
dtString += msString;
maxVelocity = (!isNaN(parseFloat(maxVelocity)) && isFinite(maxVelocity)) ? maxVelocity : null;
// Earliest processing date defaults to the UNIX epoch, then gets updated once all the snapshots have been looked at and the earliest date calculated
dbClient.query('INSERT INTO uploads(upload_id, device_id, status, earliest_processing_date, datetime, email, subscription, max_velocity, nickname) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9) RETURNING upload_id', [generateID(), deviceID, 'uploading', '1970-01-01 00:00:00', dtString, email, subscription, maxVelocity, nickname], callback);
};
/**
* Create a new reference point associated with a given upload ID
* @param {object} dbClient Postgres client
* @param {string} uploadID Unique upload ID
* @param {float} lat Latitiude
* @param {float} lng Longitude
* @param {string} timeString UTC string of the date/time when the reference point was collected
* @param {function} callback Function called on completion
*/
exports.addReferencePoint = (dbClient, uploadID, lat, lng, timeString, callback) => {
const values = [lat, lng, timeString, uploadID];
dbClient.query('INSERT INTO reference_points(reference_id, lat, lng, datetime, upload_id) VALUES (DEFAULT, $1, $2, $3, $4)', values, callback);
};
/**
* Get all reference points associated with an upload
* @param {object} dbClient Postgres client
* @param {string} uploadID Unique upload ID
* @param {function} callback Function called on completion
*/
exports.getReferencePoints = (dbClient, uploadID, callback) => {
dbClient.query('SELECT lat, lng, datetime FROM reference_points WHERE upload_id = $1', [uploadID], callback);
};
/**
* Get the contents of the uploads table
* @param {object} dbClient Postgres client
* @param {function} callback Function called on completion
*/
exports.listUploads = (dbClient, callback) => {
const selectQuery = 'SELECT * FROM uploads';
dbClient.query(selectQuery, callback);
};
/**
* Add a row to the Snapshots table
* @param {object} dbClient Postgres client
* @param {string} uploadID Unique upload ID
* @param {string} deviceID Unique, 16 character internal EFM32 serial number
* @param {object} datetime Datetime object representing estimated time snapshot was taken
* @param {number} battery Floating point number representing battery level at time of snapshot
* @param {number} hxfoCount HXFO count
* @param {number} lxfoCount LXFO count
* @param {number} temperature Floating point number representing temperature at time of snapshot
* @param {object} buff Buffer containing snapshot data
* @param {function} callback Function called on completion
*/
exports.addSnapshot = (dbClient, uploadID, datetime, battery, hxfoCount, lxfoCount, temperature, buff, callback) => {
const year = datetime.getUTCFullYear();
const month = datetime.getUTCMonth() + 1;
const day = datetime.getUTCDate();
const hours = datetime.getUTCHours();
const minutes = datetime.getUTCMinutes();
const seconds = datetime.getUTCSeconds();
const msString = datetime.getUTCMilliseconds().toString().padStart(3, '0');
let dtString = year.toString();
dtString += '-';
dtString += month.toString();
dtString += '-';
dtString += day.toString();
dtString += ' ';
dtString += hours.toString();
dtString += ':';
dtString += minutes.toString();
dtString += ':';
dtString += seconds.toString();
dtString += '.';
dtString += msString;
/* Append a double-escaped x to announce to postgres that bytea data is in the hex format, rather than escaped */
const header = Buffer.from('\\x');
const hexBuffer = Buffer.concat([header, buff]);
const values = [uploadID, dtString, battery, hxfoCount, lxfoCount, temperature, hexBuffer];
dbClient.query('INSERT INTO snapshots(snapshot_id, upload_id, datetime, battery, hxfo_count, lxfo_count, temperature, data) VALUES (DEFAULT, $1, $2, $3, $4, $5, $6, $7)', values, callback);
};
/**
* For an existing upload, change the status to one of the predefined enum values
* @param {object} dbClient Postgres client
* @param {string} uploadID Unique upload ID
* @param {string} status New status using the following enums: 'uploading', 'waiting', 'processing', 'complete'
* @param {function} callback Function called on completion
*/
exports.updateUploadStatus = (dbClient, uploadID, status, callback) => {
dbClient.query('UPDATE uploads SET status = $1 WHERE upload_id = $2 RETURNING *', [status, uploadID], callback);
};
/**
* Update the date which an upload should be processed
* @param {object} dbClient Postgres client
* @param {string} uploadID Unique upload ID
* @param {string} earliestProcessingDateString UTC string of the date/time of the earliest point an upload can be processed
* @param {function} callback Function called upon completion
*/
exports.updateUploadProcessingDate = (dbClient, uploadID, earliestProcessingDateString, callback) => {
dbClient.query('UPDATE uploads SET earliest_processing_date = $1 WHERE upload_id = $2 RETURNING *', [earliestProcessingDateString, uploadID], callback);
};
/**
* Delete an existing upload and all associated snapshots (table is set to CASCADE DELETE)
* @param {object} dbClient Postgres client
* @param {string} uploadID Unique upload ID
* @param {function} callback Fucntion called on completion
*/
exports.deleteUpload = (dbClient, uploadID, callback) => {
dbClient.query('DELETE FROM uploads WHERE upload_id = $1', [uploadID], callback);
};
/**
* Request the information about an upload needed to process a snapshot
* @param {object} dbClient Postgres client
* @param {string} uploadID Unique upload ID
* @param {function} callback Function called on completion
*/
exports.getUploadInformation = (dbClient, uploadID, callback) => {
dbClient.query('SELECT datetime, device_id, max_velocity, nickname FROM uploads WHERE upload_id = $1', [uploadID], callback);
};
/**
* Get the number of snapshots associated with a given upload
* @param {object} dbClient Postgres client
* @param {string} uploadID Unique upload ID
* @param {function} callback Function called on completion
*/
exports.getSnapshotCount = (dbClient, uploadID, callback) => {
dbClient.query('SELECT COUNT(snapshot_id) FROM snapshots WHERE upload_id = $1', [uploadID], callback);
};
/**
* Get timestamp of first snapshot associated with a given upload
* @param {object} dbClient Postgres client
* @param {string} uploadID Unique upload ID
* @param {function} callback Function called on completion
*/
exports.getFirstSnapshotTimestamp = (dbClient, uploadID, callback) => {
dbClient.query('SELECT datetime FROM snapshots WHERE upload_id = $1 ORDER BY datetime ASC LIMIT 1', [uploadID], callback);
};
/**
* Get timestamp of last snapshot associated with a given upload
* @param {object} dbClient Postgres client
* @param {string} uploadID Unique upload ID
* @param {function} callback Function called on completion
*/
exports.getLastSnapshotTimestamp = (dbClient, uploadID, callback) => {
dbClient.query('SELECT datetime FROM snapshots WHERE upload_id = $1 ORDER BY datetime DESC LIMIT 1', [uploadID], callback);
};
/**
* Given an upload ID, return an array of position objects including the GPS position and a corrected timestamp
* @param {object} dbClient Postgres client
* @param {string} uploadID Unique upload ID
* @param {function} callback Function called on completion
*/
exports.getPositions = async (dbClient, uploadID, callback) => {
const positions = [];
// Get all snapshots associated with the given upload ID
const response = await dbClient.query('SELECT * FROM (SELECT DISTINCT ON (temp.snapshot_id) datetime, temperature, battery, estimated_lat, estimated_lng, estimated_time_correction, estimated_horizontal_error FROM (SELECT snapshot_id, datetime, temperature, battery FROM snapshots WHERE upload_id = $1) AS temp INNER JOIN positions ON temp.snapshot_id=positions.snapshot_id ORDER BY temp.snapshot_id, position_id DESC) as temp2 ORDER BY datetime ASC', [uploadID]);
for (let i = 0; i < response.rows.length; i++) {
positions.push({
estimated_lat: response.rows[i].estimated_lat,
estimated_lng: response.rows[i].estimated_lng,
timestamp: (response.rows[i].datetime.getTime() / 1000) + response.rows[i].estimated_time_correction,
temperature: response.rows[i].temperature,
battery: response.rows[i].battery,
estimated_horizontal_error: response.rows[i].estimated_horizontal_error
});
}
callback(false, positions);
};