-
Notifications
You must be signed in to change notification settings - Fork 2
feat(opt): rm user-in-project:projectId collections usage #570
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 2 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
edf034d
feat(opt): rm user-in-project:projectId collections usage
slaveeks e9cf8ae
Bump version up to 1.2.14
github-actions[bot] 1075818
add return type desc
slaveeks 7d5148b
Merge branch 'feat/rm-user-in-project' of github.com:codex-team/hawk.…
slaveeks 85e056e
add setup to convertors
slaveeks File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,87 @@ | ||
| require('dotenv').config(); | ||
| require('process'); | ||
| const { MongoClient } = require('mongodb'); | ||
|
|
||
| /** | ||
| * Method that runs convertor script | ||
| */ | ||
| async function run() { | ||
| const fullUri = process.env.MONGO_HAWK_DB_URL; | ||
|
|
||
| // Parse the Mongo URL manually | ||
| const mongoUrl = new URL(fullUri); | ||
| const hawkDatabaseName = 'hawk'; | ||
|
|
||
| // Extract query parameters | ||
| const queryParams = Object.fromEntries(mongoUrl.searchParams.entries()); | ||
|
|
||
| // Compose connection options manually | ||
| const options = { | ||
| useNewUrlParser: true, | ||
| useUnifiedTopology: true, | ||
| authSource: queryParams.authSource || 'admin', | ||
| replicaSet: queryParams.replicaSet || undefined, | ||
| tls: queryParams.tls === 'true', | ||
| tlsInsecure: queryParams.tlsInsecure === 'true', | ||
| // connectTimeoutMS: 3600000, | ||
| // socketTimeoutMS: 3600000, | ||
| }; | ||
|
|
||
| // Remove query string from URI | ||
| mongoUrl.search = ''; | ||
| const cleanUri = mongoUrl.toString(); | ||
|
|
||
| console.log('Connecting to:', cleanUri); | ||
| console.log('With options:', options); | ||
|
|
||
| const client = new MongoClient(cleanUri, options); | ||
|
|
||
| await client.connect(); | ||
| const hawkDb = client.db(hawkDatabaseName); | ||
|
|
||
| console.log(`Connected to database: ${hawkDatabaseName}`); | ||
|
|
||
| const collections = await hawkDb.listCollections({}, { | ||
| authorizedCollections: true, | ||
| nameOnly: true, | ||
| }).toArray(); | ||
|
|
||
| let usersInProjectCollectionsToCheck = collections.filter(col => /^users-in-project:/.test(col.name)).map(col => col.name); | ||
|
|
||
| console.log(`Found ${usersInProjectCollectionsToCheck.length} users in project collections.`); | ||
|
|
||
| const usersDocuments = await hawkDb.collection('users').find({}).toArray(); | ||
|
|
||
| // Convert events | ||
| let i = 1; | ||
| let documentsUpdatedCount = 1; | ||
|
|
||
| for (const collectionName of usersInProjectCollectionsToCheck) { | ||
| console.log(`[${i}/${usersInProjectCollectionsToCheck.length}] Processing ${collectionName}`); | ||
|
|
||
| const usersInProject = await hawkDb.collection(collectionName).find({}).toArray(); | ||
|
|
||
| console.log(`Found ${usersInProject.length} users in project ${collectionName}.`); | ||
|
|
||
| let usersUpdatedCount = 0; | ||
|
|
||
| for (const userInProject of usersInProject) { | ||
| const userDocument = usersDocuments.find(u => u._id.toString() === userInProject.userId.toString()); | ||
| if (userDocument) { | ||
| const projectId = collectionName.split(':')[1]; | ||
| await hawkDb.collection('users').updateOne({ _id: userDocument._id }, { $set: { projectsLastVisit: { [projectId]: userInProject.timestamp } } }); | ||
| usersUpdatedCount++; | ||
| console.log(`Updated ${usersUpdatedCount}/${usersInProject.length} users in project ${collectionName}.`); | ||
| } | ||
| } | ||
|
|
||
| i++; | ||
| } | ||
|
|
||
| await client.close(); | ||
| } | ||
|
|
||
| run().catch(err => { | ||
| console.error('❌ Script failed:', err); | ||
| process.exit(1); | ||
| }); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.