Skip to content

Commit 85e056e

Browse files
committed
add setup to convertors
1 parent 7d5148b commit 85e056e

File tree

2 files changed

+73
-63
lines changed

2 files changed

+73
-63
lines changed
Lines changed: 29 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -1,87 +1,53 @@
11
require('dotenv').config();
22
require('process');
3-
const { MongoClient } = require('mongodb');
3+
const { setup } = require('./setup');
44

55
/**
66
* Method that runs convertor script
77
*/
88
async function run() {
9-
const fullUri = process.env.MONGO_HAWK_DB_URL;
9+
const { client, hawkDb } = await setup();
1010

11-
// Parse the Mongo URL manually
12-
const mongoUrl = new URL(fullUri);
13-
const hawkDatabaseName = 'hawk';
11+
const collections = await hawkDb.listCollections({}, {
12+
authorizedCollections: true,
13+
nameOnly: true,
14+
}).toArray();
1415

15-
// Extract query parameters
16-
const queryParams = Object.fromEntries(mongoUrl.searchParams.entries());
16+
let usersInProjectCollectionsToCheck = collections.filter(col => /^users-in-project:/.test(col.name)).map(col => col.name);
1717

18-
// Compose connection options manually
19-
const options = {
20-
useNewUrlParser: true,
21-
useUnifiedTopology: true,
22-
authSource: queryParams.authSource || 'admin',
23-
replicaSet: queryParams.replicaSet || undefined,
24-
tls: queryParams.tls === 'true',
25-
tlsInsecure: queryParams.tlsInsecure === 'true',
26-
// connectTimeoutMS: 3600000,
27-
// socketTimeoutMS: 3600000,
28-
};
18+
console.log(`Found ${usersInProjectCollectionsToCheck.length} users in project collections.`);
2919

30-
// Remove query string from URI
31-
mongoUrl.search = '';
32-
const cleanUri = mongoUrl.toString();
20+
const usersDocuments = await hawkDb.collection('users').find({}).toArray();
3321

34-
console.log('Connecting to:', cleanUri);
35-
console.log('With options:', options);
22+
// Convert events
23+
let i = 1;
3624

37-
const client = new MongoClient(cleanUri, options);
25+
for (const collectionName of usersInProjectCollectionsToCheck) {
26+
console.log(`[${i}/${usersInProjectCollectionsToCheck.length}] Processing ${collectionName}`);
3827

39-
await client.connect();
40-
const hawkDb = client.db(hawkDatabaseName);
28+
const usersInProject = await hawkDb.collection(collectionName).find({}).toArray();
4129

42-
console.log(`Connected to database: ${hawkDatabaseName}`);
30+
console.log(`Found ${usersInProject.length} users in project ${collectionName}.`);
4331

44-
const collections = await hawkDb.listCollections({}, {
45-
authorizedCollections: true,
46-
nameOnly: true,
47-
}).toArray();
32+
let usersUpdatedCount = 0;
4833

49-
let usersInProjectCollectionsToCheck = collections.filter(col => /^users-in-project:/.test(col.name)).map(col => col.name);
34+
for (const userInProject of usersInProject) {
35+
const userDocument = usersDocuments.find(u => u._id.toString() === userInProject.userId.toString());
36+
if (userDocument) {
37+
const projectId = collectionName.split(':')[1];
38+
await hawkDb.collection('users').updateOne({ _id: userDocument._id }, { $set: { projectsLastVisit: { [projectId]: userInProject.timestamp } } });
39+
usersUpdatedCount++;
40+
console.log(`Updated ${usersUpdatedCount}/${usersInProject.length} users in project ${collectionName}.`);
41+
}
42+
}
5043

51-
console.log(`Found ${usersInProjectCollectionsToCheck.length} users in project collections.`);
52-
53-
const usersDocuments = await hawkDb.collection('users').find({}).toArray();
54-
55-
// Convert events
56-
let i = 1;
57-
let documentsUpdatedCount = 1;
58-
59-
for (const collectionName of usersInProjectCollectionsToCheck) {
60-
console.log(`[${i}/${usersInProjectCollectionsToCheck.length}] Processing ${collectionName}`);
61-
62-
const usersInProject = await hawkDb.collection(collectionName).find({}).toArray();
63-
64-
console.log(`Found ${usersInProject.length} users in project ${collectionName}.`);
65-
66-
let usersUpdatedCount = 0;
67-
68-
for (const userInProject of usersInProject) {
69-
const userDocument = usersDocuments.find(u => u._id.toString() === userInProject.userId.toString());
70-
if (userDocument) {
71-
const projectId = collectionName.split(':')[1];
72-
await hawkDb.collection('users').updateOne({ _id: userDocument._id }, { $set: { projectsLastVisit: { [projectId]: userInProject.timestamp } } });
73-
usersUpdatedCount++;
74-
console.log(`Updated ${usersUpdatedCount}/${usersInProject.length} users in project ${collectionName}.`);
75-
}
44+
i++;
7645
}
7746

78-
i++;
79-
}
80-
81-
await client.close();
47+
await client.close();
8248
}
8349

8450
run().catch(err => {
85-
console.error('❌ Script failed:', err);
86-
process.exit(1);
51+
console.error('❌ Script failed:', err);
52+
process.exit(1);
8753
});

convertors/setup.js

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
const { MongoClient } = require('mongodb');
2+
3+
async function setup() {
4+
const fullUri = process.env.MONGO_HAWK_DB_URL;
5+
6+
// Parse the Mongo URL manually
7+
const mongoUrl = new URL(fullUri);
8+
const hawkDatabaseName = 'hawk';
9+
10+
// Extract query parameters
11+
const queryParams = Object.fromEntries(mongoUrl.searchParams.entries());
12+
13+
// Compose connection options manually
14+
const options = {
15+
useNewUrlParser: true,
16+
useUnifiedTopology: true,
17+
authSource: queryParams.authSource || 'admin',
18+
replicaSet: queryParams.replicaSet || undefined,
19+
tls: queryParams.tls === 'true',
20+
tlsInsecure: queryParams.tlsInsecure === 'true',
21+
// connectTimeoutMS: 3600000,
22+
// socketTimeoutMS: 3600000,
23+
};
24+
25+
// Remove query string from URI
26+
mongoUrl.search = '';
27+
const cleanUri = mongoUrl.toString();
28+
29+
console.log('Connecting to:', cleanUri);
30+
console.log('With options:', options);
31+
32+
const client = new MongoClient(cleanUri, options);
33+
34+
await client.connect();
35+
const hawkDb = client.db(hawkDatabaseName);
36+
37+
console.log(`Connected to database: ${hawkDatabaseName}`);
38+
39+
return { client, hawkDb };
40+
}
41+
42+
module.exports = { setup };
43+
44+

0 commit comments

Comments
 (0)