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
2 changes: 0 additions & 2 deletions bin/scripts/add_indexes.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,6 @@ pluginManager.dbConnection().then((countlyDb) => {
() => countlyDb.collection('app_users' + app._id).ensureIndex({"tsd": 1}, { background: true }, cb),
() => countlyDb.collection('app_users' + app._id).ensureIndex({"did": 1}, { background: true }, cb),
() => countlyDb.collection('app_users' + app._id).dropIndex("lac_1_ls_1", cb),
() => countlyDb.collection('app_user_merges' + app._id).ensureIndex({cd: 1}, {expireAfterSeconds: 60 * 60 * 3, background: true}, cb),
() => countlyDb.collection('metric_changes' + app._id).ensureIndex({ts: -1}, { background: true }, cb),
() => countlyDb.collection('metric_changes' + app._id).ensureIndex({ts: 1, "cc.o": 1}, { background: true }, cb),
() => countlyDb.collection('metric_changes' + app._id).ensureIndex({uid: 1}, { background: true }, cb)
];
Expand Down
66 changes: 66 additions & 0 deletions bin/scripts/data-cleanup/delete_old_drill_events.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/**
* MongoDB script to delete all collections from a database
* that start with "drill_events" string, but excluding the exact "drill_events" collection
*
*
* Server: MongoDB
* Path: any
* Command: mongosh -u uname -p 'password' --authenticationDatabase admin delete_old_drill_events.js
*/

/* global db, print, quit */

// Set the database name
const dbName = "countly_drill";

// Set to true for dry run, false to actually delete collections
const dryRun = true;

print(`Using database: ${dbName}`);
var cly = db;
// If we need to switch to a different database
if (db.getName() !== dbName) {
cly = db.getSiblingDB(dbName);
}

console.log(`Operating on database: ${dbName}`);

// Get all collection names that start with "drill_events" but are not exactly "drill_events"
const collections = cly.getCollectionInfos().map(info => info.name).filter(collName =>
collName.startsWith("drill_events") && collName !== "drill_events"
);

// Print the collections that will be deleted
print("Collections to be deleted:");
collections.forEach(collName => print(`- ${collName}`));

// Ask for confirmation
if (collections.length === 0) {
print("No matching collections found to delete.");
quit();
}

print(`\nFound ${collections.length} collections to delete.`);


if (dryRun) {
print("Dry run mode is enabled. No collections will be deleted.");
print("To proceed with deletion, set 'dryRun' to false in the script.");
quit();
}

// Delete each collection
let deletedCount = 0;
collections.forEach(collName => {
try {
cly[collName].drop();
print(`Dropped collection: ${collName}`);
deletedCount++;
}
catch (error) {
print(`Error dropping collection ${collName}: ${error}`);
}
});

// Print summary
print(`\nOperation completed. Deleted ${deletedCount} out of ${collections.length} collections.`);
29 changes: 13 additions & 16 deletions bin/scripts/sharding/sharding.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,17 @@
* Sharding Countly collections when DB requires authentication, provide it to authDB.auth command in the code
* Server: mongodb
* Path: any
* Command: mongosh < sharding.js
* Command: mongosh -u uname -p 'password' --authenticationDatabase admin sharding.js
*/

/* global Mongo, print, printjson */
var COUNTLY_DRILL = 'countly_drill',
COUNTLY = 'countly',
COUNT_TO_SHARD = 100000;
/* global db, print, printjson */

// Set countly_drill database name
const COUNTLY_DRILL = 'countly_drill';
// Set countly database name
const COUNTLY = 'countly';
// Set the threshold for sharding collections
const COUNT_TO_SHARD = 100000;

var EXCEPTIONS = [
/^system\./,
Expand All @@ -27,14 +31,8 @@ var COUNTLY_TO_SHARD = [
"feedback",
];

var conn = new Mongo(),
authDB = conn.getDB('admin');

// need to update this info
authDB.auth('<username>', '<password>');

var cly = conn.getDB(COUNTLY),
drill = conn.getDB(COUNTLY_DRILL);
var cly = db.getSiblingDB(COUNTLY),
drill = db.getSiblingDB(COUNTLY_DRILL);

var clyCollections = cly.getCollectionNames(), collections = clyCollections.concat(drill.getCollectionNames()), check = [];

Expand All @@ -61,8 +59,7 @@ check.forEach(function(c) {
var db = clyCollections.indexOf(c) === -1 ? drill : cly,
dbName = clyCollections.indexOf(c) === -1 ? COUNTLY_DRILL : COUNTLY,
count = db[c].count(),
capped = db[c].stats()['capped'],
status = db[c].getShardVersion().ok;
capped = db[c].stats()['capped'];

COUNTLY_TO_SHARD.some((e) => {
if (c.indexOf(e) == 0) {
Expand All @@ -71,7 +68,7 @@ check.forEach(function(c) {
}
});

if (!capped && count > COUNT_TO_SHARD && !status && !exceptional) {
if (!capped && count > COUNT_TO_SHARD && !exceptional) {
print('Creating hashed index & enabling sharding for collection "' + c + '"... ');

db.getCollection(c).createIndex({ _id: 'hashed' });
Expand Down
Loading