UNPKG

@simonecoelhosfo/optimizely-mcp-server

Version:

Optimizely MCP Server for AI assistants with integrated CLI tools

150 lines (126 loc) 6.02 kB
import { config } from 'dotenv'; import Database from 'better-sqlite3'; import { OptimizelyAPIHelper } from './dist/api/OptimizelyAPIHelper.js'; import { ChangeHistoryTracker } from './dist/sync/ChangeHistoryTracker.js'; import { SQLiteEngine } from './dist/storage/SQLiteEngine.js'; import { IncrementalSyncManager } from './dist/sync/IncrementalSyncManager.js'; import { CacheManager } from './dist/cache/CacheManager.js'; config(); const apiToken = process.env.OPTIMIZELY_API_TOKEN; if (!apiToken) { console.error('OPTIMIZELY_API_TOKEN environment variable is required'); process.exit(1); } async function debugSync() { console.log('=== Starting Sync Debug ===\n'); // Initialize storage const storage = new SQLiteEngine(); await storage.initialize(); // Initialize API helper const api = new OptimizelyAPIHelper(apiToken); // Initialize change tracker const changeTracker = new ChangeHistoryTracker(storage, api); // Initialize cache manager const cache = new CacheManager(api, storage); // Initialize incremental sync manager const incrementalSync = new IncrementalSyncManager(changeTracker, cache, storage, api); try { // 1. Check database state console.log('1. Checking database state...'); const projects = await storage.query('SELECT id, name, is_flags_enabled FROM projects'); console.log(`Found ${projects.length} projects:`); projects.forEach(p => console.log(` - ${p.name} (ID: ${p.id}, Feature Flags: ${p.is_flags_enabled ? 'Yes' : 'No'})`)); // 2. Check sync state console.log('\n2. Checking sync state...'); const syncStates = await storage.query('SELECT * FROM sync_state'); if (syncStates.length === 0) { console.log('No sync state found - initializing...'); // Initialize sync state for each project for (const project of projects) { await changeTracker.updateSyncState(project.id, { last_sync_time: new Date(Date.now() - 24 * 60 * 60 * 1000).toISOString(), // 24 hours ago sync_in_progress: false }); } } else { syncStates.forEach(s => { console.log(` Project ${s.project_id}:`); console.log(` Last sync: ${s.last_sync_time}`); console.log(` In progress: ${s.sync_in_progress}`); console.log(` Errors: ${s.error_count}`); }); } // 3. Check change history table console.log('\n3. Checking change history...'); const changeCount = await storage.get('SELECT COUNT(*) as count FROM change_history'); console.log(`Total changes in database: ${changeCount.count}`); // 4. Test change history API directly if (projects.length > 0) { const testProject = projects[0]; console.log(`\n4. Testing change history API for project: ${testProject.name}`); const since = new Date(Date.now() - 7 * 24 * 60 * 60 * 1000); // 7 days ago console.log(` Fetching changes since: ${since.toISOString()}`); try { const changes = await changeTracker.getChangesSince(testProject.id, since); console.log(` Found ${changes.length} changes from API`); if (changes.length > 0) { console.log(' First 3 changes:'); changes.slice(0, 3).forEach((change, i) => { console.log(` ${i + 1}. ${change.entity_type} ${change.entity_id} - ${change.action} at ${change.timestamp}`); }); // Try to record these changes console.log('\n Recording changes to database...'); await changeTracker.recordChanges(changes); // Verify they were saved const newCount = await storage.get('SELECT COUNT(*) as count FROM change_history WHERE project_id = ?', [testProject.id]); console.log(` Changes in database for this project: ${newCount.count}`); } } catch (error) { console.error(` Error fetching changes: ${error.message}`); } } // 5. Run incremental sync for one project if (projects.length > 0) { const testProject = projects[0]; console.log(`\n5. Running incremental sync for project: ${testProject.name}`); try { const result = await incrementalSync.syncProject(testProject.id); console.log(' Sync result:'); console.log(` Success: ${result.success}`); console.log(` Changes processed: ${result.changesProcessed}`); console.log(` Created: ${result.recordsCreated}`); console.log(` Updated: ${result.recordsUpdated}`); console.log(` Deleted: ${result.recordsDeleted}`); console.log(` Errors: ${result.errors.length}`); if (result.errors.length > 0) { console.log(' Errors:'); result.errors.forEach((err, i) => { console.log(` ${i + 1}. ${err.message}`); }); } } catch (error) { console.error(` Sync error: ${error.message}`); console.error(` Stack: ${error.stack}`); } } // 6. Final change history check console.log('\n6. Final change history check...'); const finalCount = await storage.get('SELECT COUNT(*) as count FROM change_history'); console.log(`Total changes in database: ${finalCount.count}`); const recentChanges = await storage.query( 'SELECT * FROM change_history ORDER BY timestamp DESC LIMIT 5' ); if (recentChanges.length > 0) { console.log('Recent changes:'); recentChanges.forEach(change => { console.log(` ${change.entity_type} ${change.entity_id} - ${change.action} at ${change.timestamp}`); }); } } catch (error) { console.error('Error during debug:', error); } finally { await storage.close(); } } // Run the debug debugSync().catch(console.error);