-
Notifications
You must be signed in to change notification settings - Fork 9
chore: grouped prompt-total-token , availability, connected-agent-det… #882
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
Open
Anushtha-Rathore
wants to merge
4
commits into
Walkover-Web-Solution:testing
Choose a base branch
from
Anushtha-Rathore:agent-info-updates
base: testing
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
e23c4fb
chore: grouped prompt-total-token , availability, connected-agent-det…
Anushtha-Rathore 8eed292
chore: group prompt_total_tokens, availability, connected_agent_detai…
Natwar589 ea8cc36
Merge branch 'testing' of github.com:Walkover-Web-Solution/AI-middlew…
Natwar589 7ba4391
refactor: convert agent_info migration to migrate-mongo format with r…
Natwar589 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
210 changes: 210 additions & 0 deletions
210
migrations/mongo/20260409224600-agent_info_migration.js
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,210 @@ | ||
| /** | ||
| * @param db {import('mongodb').Db} | ||
| * @param client {import('mongodb').MongoClient} | ||
| * @returns {Promise<void>} | ||
| */ | ||
| export const up = async (db) => { | ||
| console.log("Starting agent_info field migration..."); | ||
|
|
||
| // Process configurations collection (includes availability from page_config) | ||
| await migrateCollection(db, "configurations", true); | ||
|
|
||
| // Process configuration_versions collection (no availability) | ||
| await migrateCollection(db, "configuration_versions", false); | ||
|
|
||
| console.log("Agent_info field migration completed successfully!"); | ||
| }; | ||
|
|
||
| /** | ||
| * @param db {import('mongodb').Db} | ||
| * @param client {import('mongodb').MongoClient} | ||
| * @returns {Promise<void>} | ||
| */ | ||
| export const down = async (db) => { | ||
| console.log("Rolling back agent_info field migration..."); | ||
|
|
||
| // Rollback configurations collection | ||
| await rollbackCollection(db, "configurations", true); | ||
|
|
||
| // Rollback configuration_versions collection | ||
| await rollbackCollection(db, "configuration_versions", false); | ||
|
|
||
| console.log("Agent_info field rollback completed successfully!"); | ||
| }; | ||
|
|
||
| async function migrateCollection(db, collectionName, includeAvailability) { | ||
| console.log(`\nProcessing ${collectionName} collection...`); | ||
|
|
||
| const collection = db.collection(collectionName); | ||
|
|
||
| // Find ALL documents in the collection | ||
| const documents = await collection.find({}).toArray(); | ||
| console.log(`Found ${documents.length} total documents in ${collectionName}`); | ||
|
|
||
| if (documents.length === 0) { | ||
| console.log(`No documents found in ${collectionName}`); | ||
| return; | ||
| } | ||
|
|
||
| // Process in batches | ||
| const batchSize = 100; | ||
| let processedCount = 0; | ||
|
|
||
| for (let i = 0; i < documents.length; i += batchSize) { | ||
| const batch = documents.slice(i, i + batchSize); | ||
| const bulkOps = []; | ||
|
|
||
| for (const doc of batch) { | ||
| // Build the agent_info object with defaults and existing values | ||
| const agent_info = {}; | ||
|
|
||
| // Add prompt_total_tokens with default if missing | ||
| agent_info.prompt_total_tokens = doc.prompt_total_tokens || 0; | ||
|
|
||
| // Add availability from page_config (only for configurations) with default | ||
| if (includeAvailability) { | ||
| agent_info.availability = doc.page_config?.availability || "private"; | ||
| } | ||
|
|
||
| // Add connected_agent_details with default if missing | ||
| agent_info.connected_agent_details = doc.connected_agent_details || {}; | ||
|
|
||
| // Add variables_state with default if missing | ||
| agent_info.variables_state = doc.variables_state || {}; | ||
|
|
||
| // Create update operation | ||
| const updateOp = { | ||
| updateOne: { | ||
| filter: { _id: doc._id }, | ||
| update: { | ||
| $set: { | ||
| agent_info: agent_info | ||
| } | ||
| } | ||
| } | ||
| }; | ||
|
|
||
| // Add $unset for old fields that were moved (only if they existed) | ||
| const unsetFields = {}; | ||
| if (doc.prompt_total_tokens !== undefined) { | ||
| unsetFields.prompt_total_tokens = 1; | ||
| } | ||
| if (doc.connected_agent_details !== undefined) { | ||
| unsetFields.connected_agent_details = 1; | ||
| } | ||
| if (doc.variables_state !== undefined) { | ||
| unsetFields.variables_state = 1; | ||
| } | ||
|
|
||
| // For configurations, also remove availability from page_config if it existed | ||
| if (includeAvailability && doc.page_config?.availability !== undefined) { | ||
| unsetFields["page_config.availability"] = 1; | ||
| } | ||
|
|
||
| if (Object.keys(unsetFields).length > 0) { | ||
| updateOp.updateOne.update.$unset = unsetFields; | ||
| } | ||
|
|
||
| bulkOps.push(updateOp); | ||
| } | ||
|
|
||
| // Execute batch update | ||
| if (bulkOps.length > 0) { | ||
| const result = await collection.bulkWrite(bulkOps); | ||
| processedCount += result.modifiedCount; | ||
| console.log( | ||
| `Processed batch ${Math.floor(i / batchSize) + 1}/${Math.ceil(documents.length / batchSize)}: ${result.modifiedCount} documents updated` | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| console.log(`Completed ${collectionName}: ${processedCount} documents updated`); | ||
|
|
||
| // Verify migration - all documents should now have agent_info field | ||
| const missingAgentInfoDocs = await collection.countDocuments({ agent_info: { $exists: false } }); | ||
| if (missingAgentInfoDocs === 0) { | ||
| console.log(`Verification passed: All documents in ${collectionName} now have agent_info field`); | ||
| } else { | ||
| console.log(`Verification warning: ${missingAgentInfoDocs} documents in ${collectionName} still missing agent_info field`); | ||
| } | ||
|
|
||
| // Check agent_info field count | ||
| const agentInfoDocs = await collection.countDocuments({ agent_info: { $exists: true } }); | ||
| console.log(`${collectionName} now has ${agentInfoDocs} documents with agent_info field`); | ||
| } | ||
|
|
||
| async function rollbackCollection(db, collectionName, includeAvailability) { | ||
| console.log(`\nRolling back ${collectionName} collection...`); | ||
|
|
||
| const collection = db.collection(collectionName); | ||
|
|
||
| // Find all documents with agent_info field | ||
| const documents = await collection.find({ agent_info: { $exists: true } }).toArray(); | ||
| console.log(`Found ${documents.length} documents with agent_info in ${collectionName}`); | ||
|
|
||
| if (documents.length === 0) { | ||
| console.log(`No documents with agent_info found in ${collectionName}`); | ||
| return; | ||
| } | ||
|
|
||
| // Process in batches | ||
| const batchSize = 100; | ||
| let processedCount = 0; | ||
|
|
||
| for (let i = 0; i < documents.length; i += batchSize) { | ||
| const batch = documents.slice(i, i + batchSize); | ||
| const bulkOps = []; | ||
|
|
||
| for (const doc of batch) { | ||
| const agent_info = doc.agent_info || {}; | ||
| const setFields = {}; | ||
| const unsetFields = { agent_info: 1 }; | ||
|
|
||
| // Restore prompt_total_tokens if it exists in agent_info | ||
| if (agent_info.prompt_total_tokens !== undefined) { | ||
| setFields.prompt_total_tokens = agent_info.prompt_total_tokens; | ||
| } | ||
|
|
||
| // Restore connected_agent_details if it exists in agent_info | ||
| if (agent_info.connected_agent_details !== undefined) { | ||
| setFields.connected_agent_details = agent_info.connected_agent_details; | ||
| } | ||
|
|
||
| // Restore variables_state if it exists in agent_info | ||
| if (agent_info.variables_state !== undefined) { | ||
| setFields.variables_state = agent_info.variables_state; | ||
| } | ||
|
|
||
| // Restore availability to page_config for configurations | ||
| if (includeAvailability && agent_info.availability !== undefined) { | ||
| setFields["page_config.availability"] = agent_info.availability; | ||
| } | ||
|
|
||
| const updateOp = { | ||
| updateOne: { | ||
| filter: { _id: doc._id }, | ||
| update: {} | ||
| } | ||
| }; | ||
|
|
||
| if (Object.keys(setFields).length > 0) { | ||
| updateOp.updateOne.update.$set = setFields; | ||
| } | ||
|
|
||
| updateOp.updateOne.update.$unset = unsetFields; | ||
|
|
||
| bulkOps.push(updateOp); | ||
| } | ||
|
|
||
| // Execute batch update | ||
| if (bulkOps.length > 0) { | ||
| const result = await collection.bulkWrite(bulkOps); | ||
| processedCount += result.modifiedCount; | ||
| console.log( | ||
| `Rolled back batch ${Math.floor(i / batchSize) + 1}/${Math.ceil(documents.length / batchSize)}: ${result.modifiedCount} documents updated` | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| console.log(`Completed rollback for ${collectionName}: ${processedCount} documents updated`); | ||
| } |
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 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
Oops, something went wrong.
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.