Skip to content

Commit a1785fc

Browse files
committed
reducing duplicate code
1 parent c3e5912 commit a1785fc

34 files changed

+1680
-1862
lines changed

.eslintrc

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,29 +6,30 @@
66
"plugin:import/warnings"
77
],
88
"parserOptions": {
9-
"ecmaVersion": 2017,
9+
"ecmaVersion": 2022,
1010
"sourceType": "module"
1111
},
1212
"env": {
1313
"es6": true,
1414
"node": true
1515
},
1616
"rules": {
17-
"quotes": 0,
17+
"comma-dangle": ["error", "never"],
18+
"eol-last": 1,
1819
"indent": ["error", 2, {"SwitchCase": 1}],
19-
"no-console": 0,
20-
"no-debugger": 0,
21-
"semi": [1, "always"],
22-
"no-trailing-spaces": 0,
23-
"eol-last": 0,
24-
"no-underscore-dangle": 0,
25-
"no-alert": 0,
26-
"no-lone-blocks": 0,
27-
"no-unused-vars": [1, {
20+
"no-alert": "warn",
21+
"no-console": "off",
22+
"no-debugger": "warn",
23+
"no-extend-native": "warn",
24+
"no-lone-blocks": "warn",
25+
"no-trailing-spaces": "warn",
26+
"no-underscore-dangle": ["warn", { "allow": ["_id", "_data", "_rolesToScope", "_renameAndClearFields"], "allowAfterThis": true }],
27+
"no-unused-vars": ["warn", {
2828
"vars": "all",
2929
"args": "after-used",
3030
"argsIgnorePattern": "options"
3131
}],
32-
no-extend-native: 0
32+
"quotes": "warn",
33+
"semi": ["warn", "always"],
3334
}
3435
}

lib/server.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,4 @@ process.on('unhandledRejection', (err) => {
2929

3030
console.log(err);
3131
process.exit(1);
32-
});
32+
});

lib/utils.js

Lines changed: 236 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
const Crypto = require('crypto');
2+
const Deepcopy = require('deepcopy');
23
const Fs = require('fs');
34

5+
const {
6+
loweringsTable,
7+
cruisesTable
8+
} = require('../config/db_constants');
9+
410
const randomString = (length, chars) => {
511

612
if (!chars) {
@@ -34,7 +40,7 @@ const randomAsciiString = (length) => {
3440
const rmDir = (dirPath) => {
3541

3642
try {
37-
const files = Fs.readdirSync(dirPath);
43+
const files = Fs.readdirSync(dirPath);
3844

3945
if (files.length > 0) {
4046
for (let i = 0; i < files.length; ++i) {
@@ -50,7 +56,7 @@ const rmDir = (dirPath) => {
5056
}
5157
catch (err) {
5258
console.log(err);
53-
throw err;
59+
throw err;
5460
}
5561

5662
try {
@@ -65,7 +71,7 @@ const rmDir = (dirPath) => {
6571
const mvFilesToDir = (sourceDirPath, destDirPath) => {
6672

6773
try {
68-
const files = Fs.readdirSync(sourceDirPath);
74+
const files = Fs.readdirSync(sourceDirPath);
6975
if (files.length > 0) {
7076
for (let i = 0; i < files.length; ++i) {
7177
const sourceFilePath = sourceDirPath + '/' + files[i];
@@ -101,7 +107,7 @@ const mvFilesToDir = (sourceDirPath, destDirPath) => {
101107
}
102108
};
103109

104-
const array_move = (arr, old_index, new_index) => {
110+
const arrayMove = (arr, old_index, new_index) => {
105111

106112
if (new_index >= arr.length) {
107113
let k = new_index - arr.length + 1;
@@ -114,11 +120,235 @@ const array_move = (arr, old_index, new_index) => {
114120
return arr; // for testing
115121
};
116122

123+
const flattenEventObjs = (event_objs) => {
124+
125+
const flat_events = event_objs.map((event) => {
126+
127+
const copied_event = Deepcopy(event);
128+
129+
let enumerator = 0;
130+
if (copied_event.aux_data) {
131+
132+
copied_event.aux_data.map((data) => {
133+
134+
data.data_array.map((data2) => {
135+
136+
const elementName = `${data.data_source}.${data2.data_name}`;
137+
const elementUOM = `${data.data_source}.${data2.data_name}`;
138+
139+
if (!(elementName + '_value' in copied_event)) {
140+
copied_event[elementName + '_value'] = data2.data_value;
141+
copied_event[elementUOM + '_uom'] = data2.data_uom;
142+
}
143+
else {
144+
enumerator = 2;
145+
while (enumerator > 1) {
146+
if (!(elementName + '_' + enumerator + '_value' in copied_event)) {
147+
copied_event[elementName + '_' + enumerator + '_value'] = data2.data_value;
148+
copied_event[elementUOM + '_' + enumerator + '_uom'] = data2.data_uom;
149+
enumerator = 1;
150+
}
151+
else {
152+
enumerator++;
153+
}
154+
}
155+
}
156+
});
157+
});
158+
delete copied_event.aux_data;
159+
}
160+
161+
enumerator = 0;
162+
copied_event.event_options.map((data) => {
163+
164+
const elementName = `event_option.${data.event_option_name}`;
165+
if (!(elementName in copied_event)) {
166+
copied_event[elementName] = data.event_option_value;
167+
}
168+
else {
169+
enumerator = 2;
170+
while (enumerator > 1) {
171+
if (!(elementName + '_' + enumerator in copied_event)) {
172+
copied_event[elementName + '_' + enumerator] = data.event_option_value;
173+
enumerator = 1;
174+
}
175+
else {
176+
enumerator++;
177+
}
178+
}
179+
}
180+
});
181+
182+
delete copied_event.event_options;
183+
184+
copied_event.ts = copied_event.ts.toISOString();
185+
copied_event.id = copied_event.id.id.toString('hex');
186+
copied_event.event_free_text = copied_event.event_free_text.replace(/"/g, '"');
187+
return copied_event;
188+
});
189+
190+
return flat_events;
191+
};
192+
193+
const buildEventCSVHeaders = (flat_events) => {
194+
195+
let csv_headers = flat_events.reduce((headers, event) => {
196+
197+
const key_names = Object.keys(event);
198+
199+
return headers.concat(key_names).filter((value, index, self) => {
200+
201+
return self.indexOf(value) === index;
202+
});
203+
}, ['id','ts','event_value','event_author','event_free_text']);
204+
205+
csv_headers = csv_headers.slice(0, 5).concat(csv_headers.slice(5).filter((header) => header.startsWith('event_option')).sort(), csv_headers.slice(5).filter((header) => !header.startsWith('event_option')).sort());
206+
207+
const cruise_index = csv_headers.findIndex((header) => header === 'cruise_id');
208+
if (cruise_index > -1) {
209+
csv_headers = arrayMove(csv_headers, cruise_index, 1);
210+
}
211+
212+
const lowering_index = csv_headers.findIndex((header) => header === 'lowering_id');
213+
if (lowering_index > -1) {
214+
csv_headers = arrayMove(csv_headers, lowering_index, 2);
215+
}
216+
217+
return csv_headers;
218+
219+
};
220+
221+
const buildEventsQuery = (request, start_ts = new Date('1970-01-01T00:00:00.000Z'), stop_ts = new Date() ) => {
222+
223+
const query = {};
224+
if (request.query.author) {
225+
if (Array.isArray(request.query.author)) {
226+
const regex_query = request.query.author.map((author) => {
227+
228+
const return_regex = new RegExp(author, 'i');
229+
return return_regex;
230+
});
231+
232+
query.event_author = { $in: regex_query };
233+
}
234+
else {
235+
query.event_author = new RegExp(request.query.author, 'i');
236+
}
237+
}
238+
239+
if (request.query.value) {
240+
if (Array.isArray(request.query.value)) {
241+
242+
const inList = [];
243+
const ninList = [];
244+
245+
for ( const value of request.query.value ) {
246+
if (value.startsWith('!')) {
247+
ninList.push( new RegExp(value.substr(1), 'i'));
248+
}
249+
else {
250+
inList.push(new RegExp(value, 'i'));
251+
}
252+
}
253+
254+
if ( inList.length > 0 && ninList.length > 0) {
255+
query.event_value = { $in: inList, $nin: ninList };
256+
}
257+
else if (inList.length > 0) {
258+
query.event_value = { $in: inList };
259+
}
260+
else {
261+
query.event_value = { $nin: ninList };
262+
}
263+
264+
}
265+
else {
266+
if (request.query.value.startsWith('!')) {
267+
query.event_value = new RegExp('^(?!.*' + request.query.value.substr(1) + ')', 'i');
268+
}
269+
else {
270+
query.event_value = new RegExp(request.query.value, 'i');
271+
}
272+
}
273+
}
274+
275+
if (request.query.freetext) {
276+
query.event_free_text = new RegExp(request.query.freetext, 'i');
277+
}
278+
279+
//Time filtering
280+
if (request.query.startTS) {
281+
const tempStartTS = new Date(request.query.startTS);
282+
const startTS = (tempStartTS >= start_ts && tempStartTS <= stop_ts) ? tempStartTS : start_ts;
283+
query.ts = { $gte: startTS };
284+
}
285+
else {
286+
query.ts = { $gte: start_ts };
287+
}
288+
289+
if (request.query.stopTS) {
290+
const tempStopTS = new Date(request.query.stopTS);
291+
const stopTS = (tempStopTS >= start_ts && tempStopTS <= stop_ts) ? tempStopTS : stop_ts;
292+
query.ts.$lte = stopTS;
293+
}
294+
else {
295+
query.ts.$lte = stop_ts;
296+
}
297+
298+
// console.log("query:", query);
299+
return query;
300+
};
301+
302+
const addEventRecordIDs = async (request, records) => {
303+
304+
const db = request.mongo.db;
305+
306+
const new_results = await records.map(async (doc) => {
307+
308+
const cruise_lowering_query = {};
309+
310+
// time bounds based on event start/stop times
311+
cruise_lowering_query.$and = [{ start_ts: { $lte: doc.ts } }, { stop_ts: { $gte: doc.ts } }];
312+
313+
try {
314+
const event_cruise = await db.collection(cruisesTable).findOne(cruise_lowering_query);
315+
316+
if (event_cruise) {
317+
doc.cruise_id = event_cruise.cruise_id;
318+
}
319+
}
320+
catch (err) {
321+
console.error('ERROR:', err);
322+
}
323+
324+
try {
325+
const event_lowering = await db.collection(loweringsTable).findOne(cruise_lowering_query);
326+
327+
if (event_lowering) {
328+
doc.lowering_id = event_lowering.lowering_id;
329+
}
330+
}
331+
catch (err) {
332+
console.error('ERROR:', err);
333+
}
334+
335+
return doc;
336+
});
337+
338+
await Promise.all(new_results);
339+
340+
return records;
341+
342+
};
343+
344+
117345
module.exports = {
118346
randomString,
119347
randomAsciiString,
120348
rmDir,
121349
mvFilesToDir,
122-
array_move
350+
flattenEventObjs,
351+
buildEventCSVHeaders,
352+
buildEventsQuery,
353+
addEventRecordIDs
123354
};
124-

0 commit comments

Comments
 (0)