diff --git a/relay.pid b/relay.pid index 90fe358..e81fa6f 100644 --- a/relay.pid +++ b/relay.pid @@ -1 +1 @@ -3429782 +3615986 diff --git a/src/api.c b/src/api.c index 332ae52..4aa58dd 100644 --- a/src/api.c +++ b/src/api.c @@ -377,8 +377,8 @@ cJSON* query_subscription_details(void) { return subscriptions_data; } -// Generate and broadcast monitoring event -int generate_monitoring_event(void) { +// Generate event-driven monitoring events (triggered by event storage) +int generate_event_driven_monitoring(void) { // Generate event_kinds monitoring event if (generate_monitoring_event_for_type("event_kinds", query_event_kind_distribution) != 0) { DEBUG_ERROR("Failed to generate event_kinds monitoring event"); @@ -403,22 +403,45 @@ int generate_monitoring_event(void) { 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) if (generate_monitoring_event_for_type("subscription_details", query_subscription_details) != 0) { DEBUG_ERROR("Failed to generate subscription_details monitoring event"); 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) { DEBUG_ERROR("Failed to generate cpu_metrics monitoring event"); return -1; } - DEBUG_INFO("Generated and broadcast all monitoring events"); + DEBUG_INFO("Generated and broadcast subscription-driven monitoring events"); 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 int generate_monitoring_event_for_type(const char* d_tag_value, cJSON* (*query_func)(void)) { // Query the monitoring data @@ -511,20 +534,42 @@ void monitoring_on_event_stored(void) { 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 monitoring events only when someone is listening + + // Generate event-driven monitoring events only when someone is listening 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) diff --git a/src/api.h b/src/api.h index f2e18e1..b08831b 100644 --- a/src/api.h +++ b/src/api.h @@ -61,6 +61,7 @@ int handle_sql_query_unified(cJSON* event, const char* query, char* error_messag // Monitoring system functions void monitoring_on_event_stored(void); +void monitoring_on_subscription_change(void); int get_monitoring_throttle_seconds(void); #endif // API_H \ No newline at end of file diff --git a/src/main.h b/src/main.h index 3c4105f..102daa2 100644 --- a/src/main.h +++ b/src/main.h @@ -10,10 +10,10 @@ #define MAIN_H // Version information (auto-updated by build system) -#define VERSION "v0.7.34" +#define VERSION "v0.7.35" #define VERSION_MAJOR 0 #define VERSION_MINOR 7 -#define VERSION_PATCH 34 +#define VERSION_PATCH 35 // Relay metadata (authoritative source for NIP-11 information) #define RELAY_NAME "C-Relay" diff --git a/src/subscriptions.c b/src/subscriptions.c index 2b56283..511d0d0 100644 --- a/src/subscriptions.c +++ b/src/subscriptions.c @@ -310,6 +310,9 @@ int add_subscription_to_manager(subscription_t* sub) { // Log subscription creation to database (INSERT OR REPLACE handles duplicates) log_subscription_created(sub); + // Trigger monitoring update for subscription changes + monitoring_on_subscription_change(); + 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_subscription_events_sent(sub_id_copy, events_sent_copy); + // Trigger monitoring update for subscription changes + monitoring_on_subscription_change(); + free_subscription(sub); return 0; }