NETRAVE/lib/utils/database_manager.rb
VetheonGames e4df29b0c1 Implementing Thread-Safe Alert System with Ring Buffer
This commit introduces a significant enhancement to our Ruby program by implementing a thread-safe alert system using a ring buffer data structure.

New Classes:
1. Alert: This class is responsible for creating and displaying alerts in the Curses Text User Interface (TUI). It takes a message and severity level as arguments and uses these to display color-coded alerts to the user.

2. AlertQueueManager: This class manages the queues for alerts using a ring buffer data structure. It continuously checks the queue and displays alerts as they arrive. It uses a mutex lock to ensure thread safety when accessing the ring buffer.

3. RingBuffer: This class is a custom implementation of a ring buffer, also known as a circular buffer. It's a fixed-size buffer that effectively overwrites old data when it is full. The buffer size has been optimized to 2MB to balance memory usage and performance.

Modifications to Existing Methods:
The DatabaseManager class has been updated to integrate the new alert system. The methods in this class now create Alert instances and enqueue them in the AlertQueueManager instead of directly displaying alerts to the user. This change ensures that alerts are displayed in a thread-safe manner and allows for better control over the timing and order of alert displays.

Thread Safety Measures:
Mutex locks and condition variables have been used to synchronize access to the ring buffer and prevent race conditions. This ensures that only one thread can access the buffer at a time, preventing data corruption and ensuring the correct operation of the alert system.

Testing:
Rigorous testing has been conducted to validate the correct functioning of the new system and to handle edge cases. This includes tests for the correct display of alerts, the correct operation of the ring buffer, and the correct synchronization of threads.

Documentation:
Detailed comments have been added to the code to explain the purpose and operation of the new classes and methods. This documentation will serve as a valuable reference for future development and maintenance of the codebase.

This commit represents a significant improvement in the functionality and robustness of our Ruby program's alert system.
2023-07-05 12:35:20 -06:00

118 lines
4.0 KiB
Ruby

# frozen_string_literal: true
require 'sequel'
require 'mysql2'
require_relative 'system_information_gather'
require_relative '../utils/utilities'
# database manager
class DatabaseManager
include Utilities
def initialize(logger, alert_queue_manager)
@db = nil
@loggman = logger
@alert_queue_manager = alert_queue_manager
end
def test_db_connection(username, password, database) # rubocop:disable Metrics/MethodLength
@loggman.log_info('Attempting to connect to the database...')
# Create the connection string
connection_string = "mysql2://#{username}:#{password}@localhost/#{database}"
@db = Sequel.connect(connection_string)
# Try a simple query to test the connection
@db.run 'SELECT 1'
@loggman.log_info('Successfully connected to the database.')
alert = Alert.new('Successfully connected to the database.', :info)
@alert_queue_manager.enqueue_alert(alert)
true
rescue Sequel::DatabaseConnectionError => e
@loggman.log_error("Failed to connect to the database: #{e.message}")
alert = Alert.new('Failed to connect to the database!', :error)
@alert_queue_manager.enqueue_alert(alert)
false
end
def create_system_info_table # rubocop:disable Metrics/MethodLength, Metrics/AbcSize
if @db.nil?
# Attempt to establish a connection
username = ENV['DB_USERNAME']
password = decrypt_string_chacha20(ENV['DB_PASSWORD'], ENV['DB_SECRET_KEY'])
database = ENV['DB_DATABASE']
if test_db_connection(username, password, database)
@loggman.log_info('Successfully connected to the database.')
alert = Alert.new('Successfully connected to the database.')
@alert_queue_manager.enqueue_alert(alert)
else
# If the connection attempt fails, log an error and return
@loggman.log_error('Failed to connect to the database.')
return
end
end
if @db.nil?
@loggman.log_error('@db is still nil after attempting to connect to the database.')
return
end
@db.create_table? :system_info do
primary_key :id
Integer :uplink_speed
Integer :downlink_speed
Integer :total_bandwidth
end
end
def store_system_info(system_info) # rubocop:disable Metrics/MethodLength
# Check if the system_info already exists in the database
@loggman.log_info('Checking if info exists in the Database...')
existing_system_info = @db[:system_info].where(uplink_speed: system_info[:uplink_speed],
downlink_speed: system_info[:downlink_speed],
total_bandwidth: system_info[:total_bandwidth]).first
if existing_system_info
# If it exists, update it
@loggman.log_info('Info already exists. Updating instead of adding more data to the table...')
@db[:system_info].where(id: existing_system_info[:id]).update(system_info)
else
# If it doesn't exist, insert it
@loggman.log_info('Info does not exist already, inserting it...')
@db[:system_info].insert(system_info)
end
end
def create_services_table
@db.create_table? :services do
primary_key :id
String :service_name
TrueClass :status, default: true
end
end
def store_services(services) # rubocop:disable Metrics/MethodLength
services.each do |service|
# Check if the service already exists in the database
@loggman.log_info('Checking if info exists in the Database...')
existing_service = @db[:services].where(service_name: service).first
if existing_service
# If it exists, update it
@loggman.log_info('Info already exists, updating instead of adding more data to the table...')
@db[:services].where(id: existing_service[:id]).update(service_name: service, status: true)
else
# If it doesn't exist, insert it
@loggman.log_info('Info does not exist already, inserting it...')
@db[:services].insert(service_name: service, status: true)
end
end
end
end