forked from usdot-jpo-ode/jpo-cvmanager
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathb_create_indexes.js
622 lines (580 loc) · 17.2 KB
/
b_create_indexes.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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
// Create indexes on all collections
/*
This is the second script responsible for configuring mongoDB automatically on startup.
This script is responsible for creating users, creating collections, adding indexes, and configuring TTLs
For more information see the header in a_init_replicas.js
*/
console.log('')
console.log('Running create_indexes.js')
// Setup Username and Password Definitions
const CM_MONGO_ROOT_USERNAME = process.env.CM_MONGO_ROOT_USERNAME
const CM_MONGO_ROOT_PASSWORD = process.env.CM_MONGO_ROOT_PASSWORD
const CM_MONGO_CONNECTOR_USERNAME = process.env.CM_MONGO_CONNECTOR_USERNAME
const CM_MONGO_CONNECTOR_PASSWORD = process.env.CM_MONGO_CONNECTOR_PASSWORD
const CM_MONGO_API_USERNAME = process.env.CM_MONGO_API_USERNAME
const CM_MONGO_API_PASSWORD = process.env.CM_MONGO_API_PASSWORD
const CM_MONGO_USER_USERNAME = process.env.CM_MONGO_USER_USERNAME
const CM_MONGO_USER_PASSWORD = process.env.CM_MONGO_USER_PASSWORD
const CM_DATABASE_NAME = process.env.CM_DATABASE_NAME || 'ConflictMonitor'
const expireSeconds = 5184000 // 2 months
const retryMilliseconds = 10000
const users = [
// {username: CM_MONGO_ROOT_USERNAME, password: CM_MONGO_ROOT_PASSWORD, roles: "root", database: "admin" },
{
username: CM_MONGO_CONNECTOR_USERNAME,
password: CM_MONGO_CONNECTOR_PASSWORD,
roles: 'readWrite',
database: CM_DATABASE_NAME,
},
{ username: CM_MONGO_API_USERNAME, password: CM_MONGO_API_PASSWORD, roles: 'readWrite', database: CM_DATABASE_NAME },
{ username: CM_MONGO_USER_USERNAME, password: CM_MONGO_USER_PASSWORD, roles: 'read', database: CM_DATABASE_NAME },
]
// name -> collection name
// ttlField -> field to perform ttl on
// timeField -> field to index for time queries
// intersectionField -> field containing intersection id for id queries
const collections = [
// ODE Json data
{
name: 'OdeDriverAlertJson',
ttlField: 'recordGeneratedAt',
timeField: 'metadata.odeReceivedAt',
intersectionField: null,
rsuIP: 'metadata.originIp',
},
{
name: 'OdeBsmJson',
ttlField: 'recordGeneratedAt',
timeField: 'metadata.odeReceivedAt',
intersectionField: null,
rsuIP: 'metadata.originIp',
},
{
name: 'OdeMapJson',
ttlField: 'recordGeneratedAt',
timeField: 'metadata.odeReceivedAt',
intersectionField: null,
rsuIP: 'metadata.originIp',
},
{
name: 'OdeSpatJson',
ttlField: 'recordGeneratedAt',
timeField: 'metadata.odeReceivedAt',
intersectionField: null,
rsuIP: 'metadata.originIp',
},
{
name: 'OdeSpatRxJson',
ttlField: 'recordGeneratedAt',
timeField: 'metadata.odeReceivedAt',
intersectionField: null,
rsuIP: 'metadata.originIp',
},
{
name: 'OdeSrmJson',
ttlField: 'recordGeneratedAt',
timeField: 'metadata.odeReceivedAt',
intersectionField: null,
rsuIP: 'metadata.originIp',
},
{
name: 'OdeSsmJson',
ttlField: 'recordGeneratedAt',
timeField: 'metadata.odeReceivedAt',
intersectionField: null,
rsuIP: 'metadata.originIp',
},
{
name: 'OdeTimJson',
ttlField: 'recordGeneratedAt',
timeField: 'metadata.odeReceivedAt',
intersectionField: null,
rsuIP: 'metadata.originIp',
},
{
name: 'OdeTimBroadcastJson',
ttlField: 'recordGeneratedAt',
timeField: 'metadata.odeReceivedAt',
intersectionField: null,
rsuIP: 'metadata.originIp',
},
{
name: 'OdeTIMCertExpirationTimeJson',
ttlField: 'recordGeneratedAt',
timeField: 'metadata.odeReceivedAt',
intersectionField: null,
rsuIP: 'metadata.originIp',
},
// Ode Raw ASN
{
name: 'OdeRawEncodedBSMJson',
ttlField: 'recordGeneratedAt',
timeField: 'BsmMessageContent.metadata.utctimestamp',
intersectionField: null,
rsuIP: 'BsmMessageContent.metadata.originRsu',
},
{
name: 'OdeRawEncodedMAPJson',
ttlField: 'recordGeneratedAt',
timeField: 'MapMessageContent.metadata.utctimestamp',
intersectionField: null,
rsuIP: 'MapMessageContent.metadata.originRsu',
},
{
name: 'OdeRawEncodedSPATJson',
ttlField: 'recordGeneratedAt',
timeField: 'SpatMessageContent.metadata.utctimestamp',
intersectionField: null,
rsuIP: 'SpatMessageContent.metadata.originRsu',
},
{
name: 'OdeRawEncodedSRMJson',
ttlField: 'recordGeneratedAt',
timeField: 'SrmMessageContent.metadata.utctimestamp',
intersectionField: null,
rsuIP: 'SpatMessageContent.metadata.originRsu',
},
{
name: 'OdeRawEncodedSSMJson',
ttlField: 'recordGeneratedAt',
timeField: 'SsmMessageContent.metadata.utctimestamp',
intersectionField: null,
rsuIP: 'SpatMessageContent.metadata.originRsu',
},
{
name: 'OdeRawEncodedTIMJson',
ttlField: 'recordGeneratedAt',
timeField: 'TimMessageContent.metadata.utctimestamp',
intersectionField: null,
rsuIP: 'SpatMessageContent.metadata.originRsu',
},
// GeoJson Converter Data
{
name: 'ProcessedMap',
ttlField: 'recordGeneratedAt',
timeField: 'properties.timeStamp',
intersectionField: 'properties.intersectionId',
},
{
name: 'ProcessedSpat',
ttlField: 'recordGeneratedAt',
timeField: 'utcTimeStamp',
intersectionField: 'intersectionId',
},
// Conflict Monitor Events
{
name: 'CmStopLineStopEvent',
ttlField: 'eventGeneratedAt',
timeField: 'eventGeneratedAt',
intersectionField: 'intersectionID',
},
{
name: 'CmStopLinePassageEvent',
ttlField: 'eventGeneratedAt',
timeField: 'eventGeneratedAt',
intersectionField: 'intersectionID',
},
{
name: 'CmIntersectionReferenceAlignmentEvents',
ttlField: 'eventGeneratedAt',
timeField: 'eventGeneratedAt',
intersectionField: 'intersectionID',
},
{
name: 'CmSignalGroupAlignmentEvents',
ttlField: 'eventGeneratedAt',
timeField: 'eventGeneratedAt',
intersectionField: 'intersectionID',
},
{
name: 'CmConnectionOfTravelEvent',
ttlField: 'eventGeneratedAt',
timeField: 'eventGeneratedAt',
intersectionField: 'intersectionID',
},
{
name: 'CmSignalStateConflictEvents',
ttlField: 'eventGeneratedAt',
timeField: 'eventGeneratedAt',
intersectionField: 'intersectionID',
},
{
name: 'CmLaneDirectionOfTravelEvent',
ttlField: 'eventGeneratedAt',
timeField: 'eventGeneratedAt',
intersectionField: 'intersectionID',
},
{
name: 'CmSpatTimeChangeDetailsEvent',
ttlField: 'eventGeneratedAt',
timeField: 'eventGeneratedAt',
intersectionField: 'intersectionID',
},
{
name: 'CmSpatMinimumDataEvents',
ttlField: 'eventGeneratedAt',
timeField: 'eventGeneratedAt',
intersectionField: 'intersectionID',
},
{
name: 'CmMapBroadcastRateEvents',
ttlField: 'eventGeneratedAt',
timeField: 'eventGeneratedAt',
intersectionField: 'intersectionID',
},
{
name: 'CmMapMinimumDataEvents',
ttlField: 'eventGeneratedAt',
timeField: 'eventGeneratedAt',
intersectionField: 'intersectionID',
},
{
name: 'CmSpatBroadcastRateEvents',
ttlField: 'eventGeneratedAt',
timeField: 'eventGeneratedAt',
intersectionField: 'intersectionID',
},
{
name: 'CmBsmEvents',
ttlField: 'recordGeneratedAt',
timeField: 'recordGeneratedAt',
intersectionField: 'intersectionID',
},
// Conflict Monitor Assessments
{
name: 'CmLaneDirectionOfTravelAssessment',
ttlField: 'assessmentGeneratedAt',
timeField: 'assessmentGeneratedAt',
intersectionField: 'intersectionID',
},
{
name: 'CmConnectionOfTravelAssessment',
ttlField: 'assessmentGeneratedAt',
timeField: 'assessmentGeneratedAt',
intersectionField: 'intersectionID',
},
{
name: 'CmSignalStateEventAssessment',
ttlField: 'assessmentGeneratedAt',
timeField: 'assessmentGeneratedAt',
intersectionField: 'intersectionID',
},
{
name: 'CmStopLineStopAssessment',
ttlField: 'assessmentGeneratedAt',
timeField: 'assessmentGeneratedAt',
intersectionField: 'intersectionID',
},
// Conflict Monitor Notifications
{
name: 'CmSpatTimeChangeDetailsNotification',
ttlField: 'notificationGeneratedAt',
timeField: 'notificationGeneratedAt',
intersectionField: 'intersectionID',
},
{
name: 'CmLaneDirectionOfTravelNotification',
ttlField: 'notificationGeneratedAt',
timeField: 'notificationGeneratedAt',
intersectionField: 'intersectionID',
},
{
name: 'CmConnectionOfTravelNotification',
ttlField: 'notificationGeneratedAt',
timeField: 'notificationGeneratedAt',
intersectionField: 'intersectionID',
},
{
name: 'CmAppHealthNotifications',
ttlField: 'notificationGeneratedAt',
timeField: 'notificationGeneratedAt',
intersectionField: 'intersectionID',
},
{
name: 'CmSignalStateConflictNotification',
ttlField: 'notificationGeneratedAt',
timeField: 'notificationGeneratedAt',
intersectionField: 'intersectionID',
},
{
name: 'CmSignalGroupAlignmentNotification',
ttlField: 'notificationGeneratedAt',
timeField: 'notificationGeneratedAt',
intersectionField: 'intersectionID',
},
{
name: 'CmStopLinePassageNotification',
ttlField: 'notificationGeneratedAt',
timeField: 'notificationGeneratedAt',
intersectionField: 'intersectionID',
},
{
name: 'CmStopLineStopNotification',
ttlField: 'notificationGeneratedAt',
timeField: 'notificationGeneratedAt',
intersectionField: 'intersectionID',
},
{
name: 'CmNotification',
ttlField: 'notificationGeneratedAt',
timeField: 'notificationGeneratedAt',
intersectionField: 'intersectionID',
},
// Mongo Management Collection
{ name: 'MongoStorage', ttlField: null, timeField: 'recordGeneratedAt', intersectionField: null },
]
try {
db.getMongo().setReadPref('primaryPreferred')
db = db.getSiblingDB('ConflictMonitor')
db.getMongo().setReadPref('primaryPreferred')
var isMaster = db.isMaster()
if (isMaster.primary) {
console.log('Connected to the primary replica set member.')
} else {
console.log('Not connected to the primary replica set member. Current node: ' + isMaster.host)
}
} catch (err) {
console.log('Could not switch DB to Sibling DB')
console.log(err)
}
// Create Users in Database
for (user of users) {
createUser(user)
}
// Wait for the collections to exist in mongo before trying to create indexes on them
let missing_collection_count
do {
try {
missing_collection_count = 0
const collectionNames = db.getCollectionNames()
for (collection of collections) {
// Create Collection if it doesn't exist
let created = false
if (!collectionNames.includes(collection.name)) {
created = createCollection(collection)
// created = true;
} else {
created = true
}
if (created) {
createTTLIndex(collection)
createTimeIntersectionIndex(collection)
createTimeRsuIpIndex(collection)
createTimeIndex(collection)
} else {
missing_collection_count++
console.log('Collection ' + collection.name + ' does not exist yet')
}
}
if (missing_collection_count > 0) {
console.log(
'Waiting on ' +
missing_collection_count +
' collections to be created...will try again in ' +
retryMilliseconds +
' ms'
)
sleep(retryMilliseconds)
}
} catch (err) {
console.log('Error while setting up TTL indexes in collections')
console.log(rs.status())
console.error(err)
sleep(retryMilliseconds)
}
} while (missing_collection_count > 0)
console.log('Finished Creating All TTL indexes')
function createUser(user) {
try {
console.log('Creating User: ' + user.username + ' with Permissions: ' + user.roles)
db.createUser({
user: user.username,
pwd: user.password,
roles: [{ role: user.roles, db: user.database }],
})
} catch (err) {
console.log(err)
console.log('Unable to Create User. Perhaps the User already exists.')
}
}
function createCollection(collection) {
try {
db.createCollection(collection.name)
return true
} catch (err) {
console.log('Unable to Create Collection: ' + collection.name)
console.log(err)
return false
}
}
// Create TTL Indexes
function createTTLIndex(collection) {
try {
if (collection.hasOwnProperty('ttlField') && collection.ttlField != null) {
const ttlField = collection.ttlField
const collectionName = collection.name
let indexJson = {}
indexJson[ttlField] = 1
if (ttlIndexExists(collection)) {
db.runCommand({
collMod: collectionName,
index: {
keyPattern: indexJson,
expireAfterSeconds: expireSeconds,
},
})
console.log('Updated TTL index for ' + collectionName + ' using the field: ' + ttlField + ' as the timestamp')
} else {
db[collectionName].createIndex(indexJson, { expireAfterSeconds: expireSeconds })
console.log('Created TTL index for ' + collectionName + ' using the field: ' + ttlField + ' as the timestamp')
}
}
} catch (err) {
console.log(
'Failed to Create or Update index for ' + collectionName + 'using the field: ' + ttlField + ' as the timestamp'
)
}
}
function createTimeIndex(collection) {
if (timeIndexExists(collection)) {
// Skip if Index already Exists
return
}
if (collection.hasOwnProperty('timeField') && collection.timeField != null) {
const collectionName = collection.name
const timeField = collection.timeField
console.log('Creating Time Index for ' + collectionName)
var indexJson = {}
indexJson[timeField] = -1
try {
db[collectionName].createIndex(indexJson)
console.log(
'Created Time Intersection index for ' + collectionName + ' using the field: ' + timeField + ' as the timestamp'
)
} catch (err) {
db.runCommand({
collMod: collectionName,
index: {
keyPattern: indexJson,
},
})
console.log('Updated Time index for ' + collectionName + ' using the field: ' + timeField + ' as the timestamp')
}
}
}
function createTimeRsuIpIndex() {
if (timeRsuIpIndexExists(collection)) {
// Skip if Index already Exists
return
}
if (
collection.hasOwnProperty('timeField') &&
collection.timeField != null &&
collection.hasOwnProperty('rsuIP') &&
collection.rsuIP != null
) {
const collectionName = collection.name
const timeField = collection.timeField
const rsuIP = collection.rsuIP
console.log('Creating Time rsuIP Index for ' + collectionName)
var indexJson = {}
indexJson[rsuIP] = -1
indexJson[timeField] = -1
try {
db[collectionName].createIndex(indexJson)
console.log(
'Created Time rsuIP Intersection index for ' +
collectionName +
' using the field: ' +
timeField +
' as the timestamp and : ' +
rsuIP +
' as the rsuIP'
)
} catch (err) {
db.runCommand({
collMod: collectionName,
index: {
keyPattern: indexJson,
},
})
console.log(
'Updated Time rsuIP index for ' +
collectionName +
' using the field: ' +
timeField +
' as the timestamp and : ' +
rsuIP +
' as the rsuIP'
)
}
}
}
function createTimeIntersectionIndex(collection) {
if (timeIntersectionIndexExists(collection)) {
// Skip if Index already Exists
return
}
if (
collection.hasOwnProperty('timeField') &&
collection.timeField != null &&
collection.hasOwnProperty('intersectionField') &&
collection.intersectionField != null
) {
const collectionName = collection.name
const timeField = collection.timeField
const intersectionField = collection.intersectionField
console.log('Creating time intersection index for ' + collectionName)
var indexJson = {}
indexJson[intersectionField] = -1
indexJson[timeField] = -1
try {
db[collectionName].createIndex(indexJson)
console.log(
'Created time intersection index for ' +
collectionName +
' using the field: ' +
timeField +
' as the timestamp and : ' +
intersectionField +
' as the rsuIP'
)
} catch (err) {
db.runCommand({
collMod: collectionName,
index: {
keyPattern: indexJson,
},
})
console.log(
'Updated time intersection index for ' +
collectionName +
' using the field: ' +
timeField +
' as the timestamp and : ' +
intersectionField +
' as the rsuIP'
)
}
}
}
function ttlIndexExists(collection) {
return db[collection.name].getIndexes().find((idx) => idx.hasOwnProperty('expireAfterSeconds')) !== undefined
}
function timeIntersectionIndexExists(collection) {
return (
db[collection.name]
.getIndexes()
.find((idx) => idx.name == collection.intersectionField + '_-1_' + collection.timeField + '_-1') !== undefined
)
}
function timeRsuIpIndexExists(collection) {
return (
db[collection.name]
.getIndexes()
.find((idx) => idx.name == collection.rsuIP + '_-1_' + collection.timeField + '_-1') !== undefined
)
}
function timeIndexExists(collection) {
return db[collection.name].getIndexes().find((idx) => idx.name == collection.timeField + '_-1') !== undefined
}