NETRAVE/lib/utils/database_manager.rb
VetheonGames 13fa1e53e6 Major Refactoring and Feature Addition for Improved User Experience and Code Quality
This commit includes several significant changes aimed at improving the functionality, user experience, and code quality of our Ruby project.

1. Decryption Fix: We identified and resolved an issue where the database password was being decrypted multiple times, leading to connection problems. The code was refactored to ensure that decryption only occurs once, thereby enhancing the efficiency and reliability of our database connections.

2. Infinite Loop Resolution: We addressed a critical issue where the program would enter an infinite loop if the .env file was missing or contained incorrect information. The code was updated to handle these situations appropriately, providing meaningful feedback to the user and preventing unnecessary resource consumption.

3. .env File Handling Improvement: We improved the handling of the .env file, ensuring that the program can function correctly even in the absence of this file or if it contains incorrect data. This change enhances the robustness of our application.

4. Curses Alerts Integration: We integrated a new feature to display alerts in the terminal using the Curses library. These alerts can have different severity levels (info, warning, error), which are displayed in different colors (blue, yellow, red). This feature improves the user experience by providing clear and immediate feedback on the program's status.

5. Automatic Alert Dismissal: We implemented a feature where alerts automatically disappear after 5 seconds. This was achieved using Ruby threads, ensuring that the rest of the program is not blocked while the alert is displayed. This change enhances the user experience by preventing the screen from being cluttered with old alerts.

6. Debugging Libraries Exploration: We explored the possibility of using the Tracer and Debug libraries to trace the execution of the program and assist with debugging. While these libraries were not integrated in this commit, they remain a potential resource for future debugging efforts.

This commit represents a significant step forward in the development of our Ruby project, improving both the user experience and the quality of our codebase.
2023-06-12 15:31:34 -06:00

84 lines
2.4 KiB
Ruby

# frozen_string_literal: true
require 'sequel'
require 'mysql2'
require_relative 'system_information_gather'
require_relative '../utils/utilities'
require_relative 'logg_man'
# database manager
class DatabaseManager
include Utilities
def initialize
@db = nil
@loggman = LoggMan.new
end
def test_db_connection(username, password, database) # rubocop:disable Metrics/MethodLength
loggman = LoggMan.new
loggman.log_info('Attempting to connect to the database...')
display_alert('Attempting to connect to the database...', :info)
# 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.')
display_alert('Successfully connected to the database.', :info)
true
rescue Sequel::DatabaseConnectionError => e
loggman.log_error("Failed to connect to the database: #{e.message}")
display_alert('Failed to connect to the database!', :error)
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.')
display_alert('Successfully connected to the Database1', :info)
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)
@db[:system_info].insert(system_info)
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)
services.each do |service|
@db[:services].insert(service_name: service, status: true)
end
end
end