v0.7.35 - Implement event-driven monitoring system with dual triggers for events and subscriptions

This commit is contained in:
Your Name
2025-10-22 10:48:57 -04:00
parent 9179d57cc9
commit 182e12817d
5 changed files with 64 additions and 12 deletions

View File

@@ -1 +1 @@
3429782 3615986

View File

@@ -377,8 +377,8 @@ cJSON* query_subscription_details(void) {
return subscriptions_data; return subscriptions_data;
} }
// Generate and broadcast monitoring event // Generate event-driven monitoring events (triggered by event storage)
int generate_monitoring_event(void) { int generate_event_driven_monitoring(void) {
// Generate event_kinds monitoring event // Generate event_kinds monitoring event
if (generate_monitoring_event_for_type("event_kinds", query_event_kind_distribution) != 0) { if (generate_monitoring_event_for_type("event_kinds", query_event_kind_distribution) != 0) {
DEBUG_ERROR("Failed to generate event_kinds monitoring event"); DEBUG_ERROR("Failed to generate event_kinds monitoring event");
@@ -403,22 +403,45 @@ int generate_monitoring_event(void) {
return -1; return -1;
} }
// Generate CPU metrics monitoring event (also triggered by event storage)
if (generate_monitoring_event_for_type("cpu_metrics", query_cpu_metrics) != 0) {
DEBUG_ERROR("Failed to generate cpu_metrics monitoring event");
return -1;
}
DEBUG_INFO("Generated and broadcast event-driven monitoring events");
return 0;
}
// Generate subscription-driven monitoring events (triggered by subscription changes)
int generate_subscription_driven_monitoring(void) {
// Generate active_subscriptions monitoring event (subscription changes affect this)
if (generate_monitoring_event_for_type("active_subscriptions", query_active_subscriptions) != 0) {
DEBUG_ERROR("Failed to generate active_subscriptions monitoring event");
return -1;
}
// Generate subscription_details monitoring event (admin-only) // Generate subscription_details monitoring event (admin-only)
if (generate_monitoring_event_for_type("subscription_details", query_subscription_details) != 0) { if (generate_monitoring_event_for_type("subscription_details", query_subscription_details) != 0) {
DEBUG_ERROR("Failed to generate subscription_details monitoring event"); DEBUG_ERROR("Failed to generate subscription_details monitoring event");
return -1; return -1;
} }
// Generate CPU metrics monitoring event // Generate CPU metrics monitoring event (also triggered by subscription changes)
if (generate_monitoring_event_for_type("cpu_metrics", query_cpu_metrics) != 0) { if (generate_monitoring_event_for_type("cpu_metrics", query_cpu_metrics) != 0) {
DEBUG_ERROR("Failed to generate cpu_metrics monitoring event"); DEBUG_ERROR("Failed to generate cpu_metrics monitoring event");
return -1; return -1;
} }
DEBUG_INFO("Generated and broadcast all monitoring events"); DEBUG_INFO("Generated and broadcast subscription-driven monitoring events");
return 0; return 0;
} }
// Generate and broadcast monitoring event (legacy function - now calls event-driven version)
int generate_monitoring_event(void) {
return generate_event_driven_monitoring();
}
// Helper function to generate monitoring event for a specific type // Helper function to generate monitoring event for a specific type
int generate_monitoring_event_for_type(const char* d_tag_value, cJSON* (*query_func)(void)) { int generate_monitoring_event_for_type(const char* d_tag_value, cJSON* (*query_func)(void)) {
// Query the monitoring data // Query the monitoring data
@@ -511,20 +534,42 @@ void monitoring_on_event_stored(void) {
static time_t last_monitoring_time = 0; static time_t last_monitoring_time = 0;
time_t current_time = time(NULL); time_t current_time = time(NULL);
int throttle_seconds = get_monitoring_throttle_seconds(); int throttle_seconds = get_monitoring_throttle_seconds();
if (current_time - last_monitoring_time < throttle_seconds) { if (current_time - last_monitoring_time < throttle_seconds) {
return; return;
} }
// Check if anyone is subscribed to monitoring events (kind 24567) // Check if anyone is subscribed to monitoring events (kind 24567)
// This is the ONLY activation check needed - if someone subscribes, they want monitoring // This is the ONLY activation check needed - if someone subscribes, they want monitoring
if (!has_subscriptions_for_kind(24567)) { if (!has_subscriptions_for_kind(24567)) {
return; // No subscribers = no expensive operations return; // No subscribers = no expensive operations
} }
// Generate monitoring events only when someone is listening // Generate event-driven monitoring events only when someone is listening
last_monitoring_time = current_time; last_monitoring_time = current_time;
generate_monitoring_event(); generate_event_driven_monitoring();
}
// Monitoring hook called when subscriptions change (create/close)
void monitoring_on_subscription_change(void) {
// Check throttling first (cheapest check)
static time_t last_monitoring_time = 0;
time_t current_time = time(NULL);
int throttle_seconds = get_monitoring_throttle_seconds();
if (current_time - last_monitoring_time < throttle_seconds) {
return;
}
// Check if anyone is subscribed to monitoring events (kind 24567)
// This is the ONLY activation check needed - if someone subscribes, they want monitoring
if (!has_subscriptions_for_kind(24567)) {
return; // No subscribers = no expensive operations
}
// Generate subscription-driven monitoring events only when someone is listening
last_monitoring_time = current_time;
generate_subscription_driven_monitoring();
} }
// Forward declaration for known_configs (defined in config.c) // Forward declaration for known_configs (defined in config.c)

View File

@@ -61,6 +61,7 @@ int handle_sql_query_unified(cJSON* event, const char* query, char* error_messag
// Monitoring system functions // Monitoring system functions
void monitoring_on_event_stored(void); void monitoring_on_event_stored(void);
void monitoring_on_subscription_change(void);
int get_monitoring_throttle_seconds(void); int get_monitoring_throttle_seconds(void);
#endif // API_H #endif // API_H

View File

@@ -10,10 +10,10 @@
#define MAIN_H #define MAIN_H
// Version information (auto-updated by build system) // Version information (auto-updated by build system)
#define VERSION "v0.7.34" #define VERSION "v0.7.35"
#define VERSION_MAJOR 0 #define VERSION_MAJOR 0
#define VERSION_MINOR 7 #define VERSION_MINOR 7
#define VERSION_PATCH 34 #define VERSION_PATCH 35
// Relay metadata (authoritative source for NIP-11 information) // Relay metadata (authoritative source for NIP-11 information)
#define RELAY_NAME "C-Relay" #define RELAY_NAME "C-Relay"

View File

@@ -310,6 +310,9 @@ int add_subscription_to_manager(subscription_t* sub) {
// Log subscription creation to database (INSERT OR REPLACE handles duplicates) // Log subscription creation to database (INSERT OR REPLACE handles duplicates)
log_subscription_created(sub); log_subscription_created(sub);
// Trigger monitoring update for subscription changes
monitoring_on_subscription_change();
return 0; return 0;
} }
@@ -357,6 +360,9 @@ int remove_subscription_from_manager(const char* sub_id, struct lws* wsi) {
// Update events sent counter before freeing // Update events sent counter before freeing
update_subscription_events_sent(sub_id_copy, events_sent_copy); update_subscription_events_sent(sub_id_copy, events_sent_copy);
// Trigger monitoring update for subscription changes
monitoring_on_subscription_change();
free_subscription(sub); free_subscription(sub);
return 0; return 0;
} }