From cd040fab03d3c7acfc61da3d13de0591a7d81b66 Mon Sep 17 00:00:00 2001 From: Taukon <83957880+Taukon@users.noreply.github.com> Date: Tue, 8 Jul 2025 00:14:32 +0900 Subject: [PATCH 1/4] feat: add sample scripts to initialize MongoDB --- infra-samples/mongodb/README.md | 17 ++++ infra-samples/mongodb/create-gridfs-bucket.js | 48 +++++++++++ .../mongodb/create-mongodb-indexes.js | 84 +++++++++++++++++++ 3 files changed, 149 insertions(+) create mode 100644 infra-samples/mongodb/README.md create mode 100644 infra-samples/mongodb/create-gridfs-bucket.js create mode 100644 infra-samples/mongodb/create-mongodb-indexes.js diff --git a/infra-samples/mongodb/README.md b/infra-samples/mongodb/README.md new file mode 100644 index 0000000..3d66906 --- /dev/null +++ b/infra-samples/mongodb/README.md @@ -0,0 +1,17 @@ +# Samples for MongoDB + +## GridFS +WebRTC statistics are stored as ZIP files on GridFS bucket. + +### Run +``` +$ node ./infra-samples/aws/create-gridfs-bucket.js +``` + +## MongoDB +MongoDB manages meeting metadata. You can see the schema in [API.md](../../API.md#identity-request) + +### Run +``` +$ node ./infra-samples/aws/create-mongodb-indexes.js +``` \ No newline at end of file diff --git a/infra-samples/mongodb/create-gridfs-bucket.js b/infra-samples/mongodb/create-gridfs-bucket.js new file mode 100644 index 0000000..3338423 --- /dev/null +++ b/infra-samples/mongodb/create-gridfs-bucket.js @@ -0,0 +1,48 @@ +const { MongoClient } = require('mongodb'); + +const { + MONGODB_URI, + DB_NAME, + RTCSTATS_GRIDFS_BUCKET +} = process.env; + + +if (!MONGODB_URI || !DB_NAME || !RTCSTATS_GRIDFS_BUCKET) { + console.error('Error: MONGODB_URI, DB_NAME, and RTCSTATS_GRIDFS_BUCKET environment variables must be set.'); + process.exit(1); +} + +/** + * Sets up the MongoDB GridFS bucket. + * Drops the existing GridFS collections if they exist. + * @returns {Promise} + */ +async function setupGridFSBucket() { + const client = new MongoClient(MONGODB_URI); + + try { + await client.connect(); + console.log('Successfully connected to MongoDB server.'); + + const db = client.db(DB_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.'); + } +} + +setupGridFSBucket(); diff --git a/infra-samples/mongodb/create-mongodb-indexes.js b/infra-samples/mongodb/create-mongodb-indexes.js new file mode 100644 index 0000000..7889d78 --- /dev/null +++ b/infra-samples/mongodb/create-mongodb-indexes.js @@ -0,0 +1,84 @@ +const { MongoClient } = require('mongodb'); + +const { + MONGODB_URI, + DB_NAME, + RTCSTATS_METADATA_TABLE +} = process.env; + + +if (!MONGODB_URI || !DB_NAME || !RTCSTATS_METADATA_TABLE) { + console.error('Error: MONGODB_URI, DB_NAME, and RTCSTATS_METADATA_TABLE environment variables must be set.'); + process.exit(1); +} + +const collectionName = RTCSTATS_METADATA_TABLE; + +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' + } +]; + + +/** + * Sets up the MongoDB collection and applies the required indexes. + * Drops the collection if it already exists and recreates it with the specified indexes. + * @returns {Promise} + */ +async function setupMongoDB() { + const client = new MongoClient(MONGODB_URI); + + try { + await client.connect(); + console.log('Successfully connected to MongoDB server.'); + + const db = client.db(DB_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.'); + } +} + +setupMongoDB(); From 69628c0bf53ba6646bf26c8f8149848e347d1732 Mon Sep 17 00:00:00 2001 From: Taukon <83957880+Taukon@users.noreply.github.com> Date: Sun, 13 Jul 2025 23:34:04 +0900 Subject: [PATCH 2/4] refactor: rename environment variables for clarity --- infra-samples/mongodb/create-gridfs-bucket.js | 11 +++++++---- infra-samples/mongodb/create-mongodb-indexes.js | 15 +++++++++------ 2 files changed, 16 insertions(+), 10 deletions(-) diff --git a/infra-samples/mongodb/create-gridfs-bucket.js b/infra-samples/mongodb/create-gridfs-bucket.js index 3338423..8c97bce 100644 --- a/infra-samples/mongodb/create-gridfs-bucket.js +++ b/infra-samples/mongodb/create-gridfs-bucket.js @@ -2,13 +2,16 @@ const { MongoClient } = require('mongodb'); const { MONGODB_URI, - DB_NAME, + RTCSTATS_MONGODB_NAME, RTCSTATS_GRIDFS_BUCKET } = process.env; -if (!MONGODB_URI || !DB_NAME || !RTCSTATS_GRIDFS_BUCKET) { - console.error('Error: MONGODB_URI, DB_NAME, and RTCSTATS_GRIDFS_BUCKET environment variables must be set.'); +if (!MONGODB_URI || !RTCSTATS_MONGODB_NAME || !RTCSTATS_GRIDFS_BUCKET) { + console.error( + 'Error: MONGODB_URI, RTCSTATS_MONGODB_NAME, and RTCSTATS_GRIDFS_BUCKET ' + + 'environment variables must be set.' + ); process.exit(1); } @@ -24,7 +27,7 @@ async function setupGridFSBucket() { await client.connect(); console.log('Successfully connected to MongoDB server.'); - const db = client.db(DB_NAME); + const db = client.db(RTCSTATS_MONGODB_NAME); const collections = await db.listCollections({ name: `${RTCSTATS_GRIDFS_BUCKET}.files` }).toArray(); diff --git a/infra-samples/mongodb/create-mongodb-indexes.js b/infra-samples/mongodb/create-mongodb-indexes.js index 7889d78..591b8ce 100644 --- a/infra-samples/mongodb/create-mongodb-indexes.js +++ b/infra-samples/mongodb/create-mongodb-indexes.js @@ -2,17 +2,20 @@ const { MongoClient } = require('mongodb'); const { MONGODB_URI, - DB_NAME, - RTCSTATS_METADATA_TABLE + RTCSTATS_MONGODB_NAME, + RTCSTATS_METADATA_COLLECTION } = process.env; -if (!MONGODB_URI || !DB_NAME || !RTCSTATS_METADATA_TABLE) { - console.error('Error: MONGODB_URI, DB_NAME, and RTCSTATS_METADATA_TABLE environment variables must be set.'); +if (!MONGODB_URI || !RTCSTATS_MONGODB_NAME || !RTCSTATS_METADATA_COLLECTION) { + console.error( + 'Error: MONGODB_URI, RTCSTATS_MONGODB_NAME, and RTCSTATS_METADATA_COLLECTION ' + + 'environment variables must be set.' + ); process.exit(1); } -const collectionName = RTCSTATS_METADATA_TABLE; +const collectionName = RTCSTATS_METADATA_COLLECTION; const indexDefinitions = [ { @@ -57,7 +60,7 @@ async function setupMongoDB() { await client.connect(); console.log('Successfully connected to MongoDB server.'); - const db = client.db(DB_NAME); + const db = client.db(RTCSTATS_MONGODB_NAME); const collection = db.collection(collectionName); const collections = await db.listCollections({ name: collectionName }).toArray(); From 7203eeeb45081f06964b23c1754db718744ca88e Mon Sep 17 00:00:00 2001 From: Taukon <83957880+Taukon@users.noreply.github.com> Date: Mon, 14 Jul 2025 00:00:55 +0900 Subject: [PATCH 3/4] refactor: rename setup scripts to clarify reset behavior --- infra-samples/mongodb/README.md | 6 ++++-- .../{create-gridfs-bucket.js => reset-gridfs-bucket.js} | 8 ++++---- ...create-mongodb-indexes.js => reset-mongodb-indexes.js} | 8 ++++---- 3 files changed, 12 insertions(+), 10 deletions(-) rename infra-samples/mongodb/{create-gridfs-bucket.js => reset-gridfs-bucket.js} (88%) rename infra-samples/mongodb/{create-mongodb-indexes.js => reset-mongodb-indexes.js} (91%) diff --git a/infra-samples/mongodb/README.md b/infra-samples/mongodb/README.md index 3d66906..4a3ed78 100644 --- a/infra-samples/mongodb/README.md +++ b/infra-samples/mongodb/README.md @@ -1,11 +1,13 @@ # 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/aws/create-gridfs-bucket.js +$ node ./infra-samples/mongodb/reset-gridfs-bucket.js ``` ## MongoDB @@ -13,5 +15,5 @@ MongoDB manages meeting metadata. You can see the schema in [API.md](../../API.m ### Run ``` -$ node ./infra-samples/aws/create-mongodb-indexes.js +$ node ./infra-samples/mongodb/reset-mongodb-indexes.js ``` \ No newline at end of file diff --git a/infra-samples/mongodb/create-gridfs-bucket.js b/infra-samples/mongodb/reset-gridfs-bucket.js similarity index 88% rename from infra-samples/mongodb/create-gridfs-bucket.js rename to infra-samples/mongodb/reset-gridfs-bucket.js index 8c97bce..062448e 100644 --- a/infra-samples/mongodb/create-gridfs-bucket.js +++ b/infra-samples/mongodb/reset-gridfs-bucket.js @@ -16,11 +16,11 @@ if (!MONGODB_URI || !RTCSTATS_MONGODB_NAME || !RTCSTATS_GRIDFS_BUCKET) { } /** - * Sets up the MongoDB GridFS bucket. - * Drops the existing GridFS collections if they exist. + * Resets the MongoDB GridFS bucket by dropping existing collections. + * This ensures the bucket is clean before use. * @returns {Promise} */ -async function setupGridFSBucket() { +async function resetGridFSBucket() { const client = new MongoClient(MONGODB_URI); try { @@ -48,4 +48,4 @@ async function setupGridFSBucket() { } } -setupGridFSBucket(); +resetGridFSBucket(); diff --git a/infra-samples/mongodb/create-mongodb-indexes.js b/infra-samples/mongodb/reset-mongodb-indexes.js similarity index 91% rename from infra-samples/mongodb/create-mongodb-indexes.js rename to infra-samples/mongodb/reset-mongodb-indexes.js index 591b8ce..8371847 100644 --- a/infra-samples/mongodb/create-mongodb-indexes.js +++ b/infra-samples/mongodb/reset-mongodb-indexes.js @@ -49,11 +49,11 @@ const indexDefinitions = [ /** - * Sets up the MongoDB collection and applies the required indexes. - * Drops the collection if it already exists and recreates it with the specified indexes. + * 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 setupMongoDB() { +async function resetMongoDB() { const client = new MongoClient(MONGODB_URI); try { @@ -84,4 +84,4 @@ async function setupMongoDB() { } } -setupMongoDB(); +resetMongoDB(); From b91faf88b225c932af2c0183d256085b5d0be880 Mon Sep 17 00:00:00 2001 From: Taukon <83957880+Taukon@users.noreply.github.com> Date: Wed, 16 Jul 2025 04:37:41 +0900 Subject: [PATCH 4/4] refactor: rename environment variables --- infra-samples/mongodb/reset-gridfs-bucket.js | 8 ++++---- infra-samples/mongodb/reset-mongodb-indexes.js | 8 ++++---- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/infra-samples/mongodb/reset-gridfs-bucket.js b/infra-samples/mongodb/reset-gridfs-bucket.js index 062448e..d494e13 100644 --- a/infra-samples/mongodb/reset-gridfs-bucket.js +++ b/infra-samples/mongodb/reset-gridfs-bucket.js @@ -1,15 +1,15 @@ const { MongoClient } = require('mongodb'); const { - MONGODB_URI, + RTCSTATS_MONGODB_URI, RTCSTATS_MONGODB_NAME, RTCSTATS_GRIDFS_BUCKET } = process.env; -if (!MONGODB_URI || !RTCSTATS_MONGODB_NAME || !RTCSTATS_GRIDFS_BUCKET) { +if (!RTCSTATS_MONGODB_URI || !RTCSTATS_MONGODB_NAME || !RTCSTATS_GRIDFS_BUCKET) { console.error( - 'Error: MONGODB_URI, RTCSTATS_MONGODB_NAME, and RTCSTATS_GRIDFS_BUCKET ' + 'Error: RTCSTATS_MONGODB_URI, RTCSTATS_MONGODB_NAME, and RTCSTATS_GRIDFS_BUCKET ' + 'environment variables must be set.' ); process.exit(1); @@ -21,7 +21,7 @@ if (!MONGODB_URI || !RTCSTATS_MONGODB_NAME || !RTCSTATS_GRIDFS_BUCKET) { * @returns {Promise} */ async function resetGridFSBucket() { - const client = new MongoClient(MONGODB_URI); + const client = new MongoClient(RTCSTATS_MONGODB_URI); try { await client.connect(); diff --git a/infra-samples/mongodb/reset-mongodb-indexes.js b/infra-samples/mongodb/reset-mongodb-indexes.js index 8371847..bf89906 100644 --- a/infra-samples/mongodb/reset-mongodb-indexes.js +++ b/infra-samples/mongodb/reset-mongodb-indexes.js @@ -1,15 +1,15 @@ const { MongoClient } = require('mongodb'); const { - MONGODB_URI, + RTCSTATS_MONGODB_URI, RTCSTATS_MONGODB_NAME, RTCSTATS_METADATA_COLLECTION } = process.env; -if (!MONGODB_URI || !RTCSTATS_MONGODB_NAME || !RTCSTATS_METADATA_COLLECTION) { +if (!RTCSTATS_MONGODB_URI || !RTCSTATS_MONGODB_NAME || !RTCSTATS_METADATA_COLLECTION) { console.error( - 'Error: MONGODB_URI, RTCSTATS_MONGODB_NAME, and RTCSTATS_METADATA_COLLECTION ' + 'Error: RTCSTATS_MONGODB_URI, RTCSTATS_MONGODB_NAME, and RTCSTATS_METADATA_COLLECTION ' + 'environment variables must be set.' ); process.exit(1); @@ -54,7 +54,7 @@ const indexDefinitions = [ * @returns {Promise} */ async function resetMongoDB() { - const client = new MongoClient(MONGODB_URI); + const client = new MongoClient(RTCSTATS_MONGODB_URI); try { await client.connect();