Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions infra-samples/mongodb/README.md
Original file line number Diff line number Diff line change
@@ -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
```
51 changes: 51 additions & 0 deletions infra-samples/mongodb/reset-gridfs-bucket.js
Original file line number Diff line number Diff line change
@@ -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<void>}
*/
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();
87 changes: 87 additions & 0 deletions infra-samples/mongodb/reset-mongodb-indexes.js
Original file line number Diff line number Diff line change
@@ -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<void>}
*/
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();