@simonecoelhosfo/optimizely-mcp-server
Version:
Optimizely MCP Server for AI assistants with integrated CLI tools
144 lines • 6.5 kB
JavaScript
/**
* Get System Status Tool - Individual Module
* @description Provides system health check and diagnostics information
* @since 2025-08-04
* @author Tool Modularization Team
*
* Migration Status: COMPLETED
* Original Method: OptimizelyMCPTools.getSystemStatus / healthCheck
* Complexity: LOW
* Dependencies: storage.query, logger, errorMapper, apiClient, cacheManager
*/
/**
* Helper function to get diagnostics data
*/
async function getDiagnosticsData(deps) {
if (!deps.cacheManager) {
throw deps.errorMapper.toMCPError(new Error("Cache manager not initialized"), { operation: 'Get diagnostics data' });
}
// Get database counts
const dbStatus = {
projects: (await deps.storage.query('SELECT COUNT(*) as count FROM projects', []))[0]?.count || 0,
flags: (await deps.storage.query('SELECT COUNT(*) as count FROM flags', []))[0]?.count || 0,
experiments: (await deps.storage.query('SELECT COUNT(*) as count FROM experiments', []))[0]?.count || 0,
environments: (await deps.storage.query('SELECT COUNT(*) as count FROM environments', []))[0]?.count || 0,
};
// Analyze project types
const projects = await deps.storage.query(`
SELECT id, name, platform, is_flags_enabled,
(SELECT COUNT(*) FROM flags WHERE project_id = projects.id) as flag_count,
(SELECT COUNT(*) FROM experiments WHERE project_id = projects.id) as experiment_count
FROM projects
`, []);
const projectTypes = {
web_experimentation: 0,
feature_experimentation: 0,
unknown: 0
};
const syncIssues = [];
const recommendations = [];
// Analyze each project
for (const project of projects) {
// Determine project type
const hasFlags = project.flag_count > 0 || project.is_flags_enabled;
const hasExperiments = project.experiment_count > 0;
if (project.platform === 'custom' || hasFlags) {
projectTypes.feature_experimentation++;
}
else if (project.platform === 'web' || (hasExperiments && !hasFlags)) {
projectTypes.web_experimentation++;
}
else {
projectTypes.unknown++;
}
// Check for sync issues
if (project.is_flags_enabled && project.flag_count === 0) {
syncIssues.push(`Project "${project.name}" (${project.id}) has flags enabled but no flags synced`);
}
}
// Generate recommendations
if (dbStatus.projects === 0) {
recommendations.push('No projects found. Run cache initialization to sync data.');
}
if (syncIssues.length > 0) {
recommendations.push('Some projects may have incomplete data. Consider running a full refresh.');
}
if (projectTypes.unknown > 0) {
recommendations.push(`${projectTypes.unknown} project(s) could not be classified. Check project configuration.`);
}
// Get sync scheduler status
// Note: In the modular version, we don't have direct access to syncScheduler
// This would need to be passed through dependencies if needed
const autoSyncStatus = {
isRunning: false,
note: 'Auto-sync status not available in modular tool'
};
return {
database_status: dbStatus,
project_types: projectTypes,
sync_issues: syncIssues,
recommendations,
auto_sync_status: autoSyncStatus
};
}
/**
* Creates the Get System Status tool with injected dependencies
* @param deps - Injected dependencies (storage, logger, errorMapper, etc.)
* @returns Tool definition with handler
*/
export function createGetSystemStatusTool(deps) {
return {
name: 'get_system_status',
requiresCache: true,
category: 'system',
description: 'Provides system health check and diagnostics information',
handler: async (args) => {
const { include_diagnostics = false, detailed = false } = args || {};
deps.logger.info('OptimizelyMCPTools.getSystemStatus: Checking system status', { include_diagnostics, detailed });
try {
// Always include basic health check
const healthResult = await deps.apiClient.healthCheck();
// Basic response (health_check equivalent)
let response = {
status: healthResult.status,
api_connection: healthResult.status === 'healthy' ? 'Connected' : 'Failed',
timestamp: new Date().toISOString()
};
// Add diagnostics if requested (get_diagnostics equivalent)
if (include_diagnostics || detailed) {
const diagnostics = await getDiagnosticsData(deps);
response = {
...response,
diagnostics: {
database_status: diagnostics.database_status,
project_types: diagnostics.project_types,
sync_issues: diagnostics.sync_issues,
auto_sync_status: diagnostics.auto_sync_status
}
};
// Add even more detail if requested
if (detailed) {
response.diagnostics.recommendations = diagnostics.recommendations;
response.diagnostics.cache_health = {
total_entities: Object.values(diagnostics.database_status)
.filter((val) => typeof val === 'number')
.reduce((a, b) => a + b, 0),
last_sync: diagnostics.auto_sync_status.lastSyncTime,
sync_in_progress: diagnostics.auto_sync_status.syncInProgress
};
}
}
deps.logger.debug({
status: response.status,
includesDiagnostics: include_diagnostics || detailed
}, 'OptimizelyMCPTools.getSystemStatus: System status check completed');
return response;
}
catch (error) {
deps.logger.error({ error: error.message }, 'OptimizelyMCPTools.getSystemStatus: Failed to get system status');
throw deps.errorMapper.toMCPError(error, 'Failed to get system status');
}
}
};
}
//# sourceMappingURL=GetSystemStatus.js.map