v0.7.33 - Refactor monitoring system to use subscription-based activation with ephemeral events - fixes recursive crash bug

This commit is contained in:
Your Name
2025-10-19 10:26:09 -04:00
parent 57a0089664
commit 9cb9b746d8
11 changed files with 112 additions and 260 deletions

View File

@@ -54,9 +54,8 @@
<div class="section flex-section" id="databaseStatisticsSection" style="display: none;">
<div class="section-header">
<h2>DATABASE STATISTICS</h2>
<!-- Monitoring toggle button will be inserted here by JavaScript -->
<!-- Temporarily disable auto-refresh button for real-time monitoring -->
<!-- <button type="button" id="refresh-stats-btn" class="countdown-btn"></button> -->
<!-- Monitoring is now subscription-based - no toggle button needed -->
<!-- Subscribe to kind 24567 events to receive real-time monitoring data -->
</div>
<!-- Event Rate Graph Container -->

View File

@@ -850,7 +850,7 @@ async function subscribeToConfiguration() {
console.log(`Generated subscription ID: ${subscriptionId}`);
console.log(`User pubkey ${userPubkey}`)
// Subscribe to kind 23457 events (admin response events), kind 4 (NIP-04 DMs), kind 1059 (NIP-17 GiftWrap), and kind 34567 (monitoring events)
// Subscribe to kind 23457 events (admin response events), kind 4 (NIP-04 DMs), kind 1059 (NIP-17 GiftWrap), and kind 24567 (ephemeral monitoring events)
const subscription = relayPool.subscribeMany([url], [{
since: Math.floor(Date.now() / 1000) - 5, // Look back 5 seconds to avoid race condition
kinds: [23457],
@@ -870,7 +870,7 @@ async function subscribeToConfiguration() {
limit: 50
}, {
since: Math.floor(Date.now() / 1000), // Start from current time
kinds: [34567], // Real-time monitoring events
kinds: [24567], // Real-time ephemeral monitoring events
authors: [getRelayPubkey()], // Only listen to monitoring events from the relay
"#d": isLoggedIn ? ["event_kinds", "time_stats", "top_pubkeys", "active_subscriptions", "subscription_details"] : ["event_kinds", "time_stats", "top_pubkeys", "active_subscriptions"], // Include subscription_details only when authenticated
limit: 50
@@ -956,8 +956,8 @@ async function subscribeToConfiguration() {
processAdminResponse(event);
}
// Handle monitoring events (kind 34567)
if (event.kind === 34567) {
// Handle monitoring events (kind 24567 - ephemeral)
if (event.kind === 24567) {
console.log('=== MONITORING EVENT RECEIVED ===');
console.log('Monitoring event:', event);
@@ -1145,14 +1145,14 @@ function createChartStubElements() {
console.log('Chart stub elements created');
}
// Handle monitoring events (kind 34567)
// Handle monitoring events (kind 24567 - ephemeral)
async function processMonitoringEvent(event) {
try {
console.log('=== PROCESSING MONITORING EVENT ===');
console.log('Monitoring event:', event);
// Verify this is a kind 34567 monitoring event
if (event.kind !== 34567) {
// Verify this is a kind 24567 ephemeral monitoring event
if (event.kind !== 24567) {
console.log('Ignoring non-monitoring event, kind:', event.kind);
return;
}
@@ -2433,29 +2433,11 @@ async function saveAuthRule(event) {
}
}
// Auto-enable monitoring when admin logs in
// Monitoring is now subscription-based - no auto-enable needed
// Monitoring automatically activates when someone subscribes to kind 24567 events
async function autoEnableMonitoring() {
if (!isLoggedIn || !relayPool) {
log('Cannot auto-enable monitoring: not logged in or no relay connection', 'WARNING');
return;
}
try {
log('Auto-enabling monitoring for admin session...', 'INFO');
// Send enable_monitoring command
const commandArray = ["enable_monitoring"];
const requestEvent = await sendAdminCommand(commandArray);
if (requestEvent) {
log('Monitoring auto-enabled for admin session', 'INFO');
} else {
log('Failed to auto-enable monitoring', 'ERROR');
}
} catch (error) {
log(`Failed to auto-enable monitoring: ${error.message}`, 'ERROR');
}
log('Monitoring system is subscription-based - no manual enable needed', 'INFO');
log('Subscribe to kind 24567 events to receive real-time monitoring data', 'INFO');
}
// Update existing logout and showMainInterface functions to handle auth rules and NIP-17 DMs
@@ -4900,106 +4882,33 @@ function getConfigToggleButton(configKey) {
return configToggleButtons.get(configKey);
}
// Initialize toggle button for monitoring config
// Monitoring is now subscription-based - no toggle button needed
// Monitoring automatically activates when someone subscribes to kind 24567 events
function initializeMonitoringToggleButton() {
console.log('=== INITIALIZING MONITORING TOGGLE BUTTON ===');
// Check if button already exists to prevent duplicates
const existingButton = getConfigToggleButton('kind_34567_reporting_enabled');
if (existingButton) {
console.log('Monitoring toggle button already exists, skipping creation');
return existingButton;
}
// Find the DATABASE STATISTICS section header
const sectionHeader = document.querySelector('#databaseStatisticsSection .section-header h2');
console.log('Section header found:', sectionHeader);
if (!sectionHeader) {
log('Could not find DATABASE STATISTICS section header for toggle button', 'WARNING');
return;
}
// Create the toggle button
const button = new ConfigToggleButton('kind_34567_reporting_enabled', sectionHeader.parentElement, {
dataType: 'boolean',
category: 'monitoring'
});
console.log('Monitoring toggle button created:', button);
console.log('Button element:', button.button);
console.log('Button in DOM:', document.contains(button.button));
log('Monitoring toggle button initialized', 'INFO');
return button;
console.log('=== MONITORING IS NOW SUBSCRIPTION-BASED ===');
console.log('No toggle button needed - monitoring activates automatically when subscribing to kind 24567');
log('Monitoring system is subscription-based - no manual toggle required', 'INFO');
return null;
}
// Enhanced config update response handler to update toggle buttons
// Monitoring is subscription-based - no toggle button response handling needed
const originalHandleConfigUpdateResponse = handleConfigUpdateResponse;
handleConfigUpdateResponse = function(responseData) {
console.log('=== ENHANCED CONFIG UPDATE RESPONSE HANDLER ===');
console.log('=== CONFIG UPDATE RESPONSE HANDLER ===');
console.log('Response data:', responseData);
// Call original handler
originalHandleConfigUpdateResponse(responseData);
// Update toggle buttons if this was a config update response
if (responseData.query_type === 'config_update' && responseData.status === 'success' && responseData.processed_configs) {
console.log('Processing config update response for toggle buttons');
responseData.processed_configs.forEach(config => {
console.log('Processing config:', config);
const button = getConfigToggleButton(config.key);
console.log('Button found:', button);
if (button) {
const success = config.status === 'updated';
const value = String(config.value).toLowerCase();
console.log('Calling handleResponse with:', success, value);
button.handleResponse(success, value);
}
});
} else {
console.log('Not a config update response or no processed_configs');
}
// Also handle config query responses to initialize toggle buttons
if ((responseData.query_type === 'config_query' || responseData.query_type === 'config_all') && responseData.status === 'success' && responseData.data) {
console.log('Config query response - initializing toggle buttons');
initializeToggleButtonsFromConfig(responseData);
}
// Monitoring is now subscription-based - no toggle buttons to update
console.log('Monitoring system is subscription-based - no toggle buttons to handle');
};
// Initialize toggle button when config is loaded
// Monitoring is now subscription-based - no toggle buttons needed
function initializeToggleButtonsFromConfig(configData) {
console.log('=== INITIALIZING TOGGLE BUTTONS FROM CONFIG ===');
console.log('Config data:', configData);
if (!configData || !configData.data) {
console.log('No config data available');
return;
}
// Find monitoring enabled config
const monitoringConfig = configData.data.find(c => c.key === 'kind_34567_reporting_enabled');
console.log('Monitoring config found:', monitoringConfig);
if (monitoringConfig) {
const button = getConfigToggleButton('kind_34567_reporting_enabled');
console.log('Button instance:', button);
if (button) {
// Convert config value to string for state setting
const configValue = String(monitoringConfig.value).toLowerCase();
console.log('Setting button state to:', configValue);
// Set initial state from config
button.setState(configValue);
log(`Monitoring toggle button set to: ${configValue}`, 'INFO');
} else {
console.log('Button instance not found in registry - button should have been created on DOM ready');
}
} else {
console.log('Monitoring config not found in config data');
}
console.log('=== MONITORING IS SUBSCRIPTION-BASED ===');
console.log('No toggle buttons needed - monitoring activates automatically when subscribing to kind 24567');
log('Monitoring system initialized - subscription-based activation ready', 'INFO');
}
// Initialize toggle button after DOM is ready