diff --git a/infra-samples/mongodb/README.md b/infra-samples/mongodb/README.md new file mode 100644 index 0000000..4a3ed78 --- /dev/null +++ b/infra-samples/mongodb/README.md @@ -0,0 +1,19 @@ +# Samples for MongoDB + +MongoDB collection and GridFS bucket are created automatically on first use, so it is not necessary to run these scripts before the initial application startup. Their primary purpose is to reset the environment to a clean state. + +## GridFS +WebRTC statistics are stored as ZIP files on GridFS bucket. + +### Run +``` +$ node ./infra-samples/mongodb/reset-gridfs-bucket.js +``` + +## MongoDB +MongoDB manages meeting metadata. You can see the schema in [API.md](../../API.md#identity-request) + +### Run +``` +$ node ./infra-samples/mongodb/reset-mongodb-indexes.js +``` \ No newline at end of file diff --git a/infra-samples/mongodb/reset-gridfs-bucket.js b/infra-samples/mongodb/reset-gridfs-bucket.js new file mode 100644 index 0000000..d494e13 --- /dev/null +++ b/infra-samples/mongodb/reset-gridfs-bucket.js @@ -0,0 +1,51 @@ +const { MongoClient } = require('mongodb'); + +const { + RTCSTATS_MONGODB_URI, + RTCSTATS_MONGODB_NAME, + RTCSTATS_GRIDFS_BUCKET +} = process.env; + + +if (!RTCSTATS_MONGODB_URI || !RTCSTATS_MONGODB_NAME || !RTCSTATS_GRIDFS_BUCKET) { + console.error( + 'Error: RTCSTATS_MONGODB_URI, RTCSTATS_MONGODB_NAME, and RTCSTATS_GRIDFS_BUCKET ' + + 'environment variables must be set.' + ); + process.exit(1); +} + +/** + * Resets the MongoDB GridFS bucket by dropping existing collections. + * This ensures the bucket is clean before use. + * @returns {Promise} + */ +async function resetGridFSBucket() { + const client = new MongoClient(RTCSTATS_MONGODB_URI); + + try { + await client.connect(); + console.log('Successfully connected to MongoDB server.'); + + const db = client.db(RTCSTATS_MONGODB_NAME); + + const collections = await db.listCollections({ name: `${RTCSTATS_GRIDFS_BUCKET}.files` }).toArray(); + + if (collections.length > 0) { + console.log(`Bucket '${RTCSTATS_GRIDFS_BUCKET}' already exists. Dropping and recreating...`); + await db.collection(`${RTCSTATS_GRIDFS_BUCKET}.files`).drop(); + await db.collection(`${RTCSTATS_GRIDFS_BUCKET}.chunks`).drop(); + console.log(`Bucket '${RTCSTATS_GRIDFS_BUCKET}' dropped successfully.`); + } + + console.log(`Bucket '${RTCSTATS_GRIDFS_BUCKET}' is ready for use.`); + + } catch (err) { + console.error('An error occurred during GridFS setup:', err); + } finally { + await client.close(); + console.log('MongoDB connection closed.'); + } +} + +resetGridFSBucket(); diff --git a/infra-samples/mongodb/reset-mongodb-indexes.js b/infra-samples/mongodb/reset-mongodb-indexes.js new file mode 100644 index 0000000..bf89906 --- /dev/null +++ b/infra-samples/mongodb/reset-mongodb-indexes.js @@ -0,0 +1,87 @@ +const { MongoClient } = require('mongodb'); + +const { + RTCSTATS_MONGODB_URI, + RTCSTATS_MONGODB_NAME, + RTCSTATS_METADATA_COLLECTION +} = process.env; + + +if (!RTCSTATS_MONGODB_URI || !RTCSTATS_MONGODB_NAME || !RTCSTATS_METADATA_COLLECTION) { + console.error( + 'Error: RTCSTATS_MONGODB_URI, RTCSTATS_MONGODB_NAME, and RTCSTATS_METADATA_COLLECTION ' + + 'environment variables must be set.' + ); + process.exit(1); +} + +const collectionName = RTCSTATS_METADATA_COLLECTION; + +const indexDefinitions = [ + { + key: { + conferenceId: 1, + dumpId: 1 + }, + name: 'PK_conferenceId_dumpId', + unique: true + }, + { + key: { + conferenceUrl: 1, + startDate: 1 + }, + name: 'GSI_conferenceUrl_startDate' + }, + { + key: { conferenceId: 1, + startDate: 1 + }, + name: 'GSI_conferenceId_startDate' + }, + { + key: { sessionId: 1, + startDate: 1 + }, + name: 'GSI_sessionId_startDate' + } +]; + + +/** + * Resets the MongoDB collection and applies the required indexes. + * It drops the collection if it already exists to ensure a clean state. + * @returns {Promise} + */ +async function resetMongoDB() { + const client = new MongoClient(RTCSTATS_MONGODB_URI); + + try { + await client.connect(); + console.log('Successfully connected to MongoDB server.'); + + const db = client.db(RTCSTATS_MONGODB_NAME); + const collection = db.collection(collectionName); + + const collections = await db.listCollections({ name: collectionName }).toArray(); + + if (collections.length > 0) { + console.log(`Collection '${collectionName}' already exists. Dropping and recreating...`); + await collection.drop(); + console.log(`Collection '${collectionName}' dropped successfully.`); + } + + console.log(`Creating new collection '${collectionName}' and applying indexes...`); + const result = await collection.createIndexes(indexDefinitions); + + console.log('Successfully created indexes:', result); + + } catch (err) { + console.error('An error occurred during MongoDB setup:', err); + } finally { + await client.close(); + console.log('MongoDB connection closed.'); + } +} + +resetMongoDB();