NETRAVE/lib/utils/system_information_gather.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

99 lines
3.2 KiB
Ruby

# frozen_string_literal: true
require 'curses'
require 'yaml'
require_relative 'utilities'
require_relative 'database_manager'
require 'dynamic_curses_input'
# gather system info
class SystemInformationGather
include Utilities
def initialize(db_manager)
@db_manager = db_manager
end
def gather_system_info # rubocop:disable Metrics/MethodLength
uplink_speed = ask_for_uplink_speed
downlink_speed = ask_for_downlink_speed
services = ask_for_services
total_bandwidth = uplink_speed + downlink_speed
system_info = {
uplink_speed:,
downlink_speed:,
total_bandwidth:
}
@db_manager.create_system_info_table unless @db_manager.table_exists?(:system_info)
@db_manager.store_system_info(system_info)
@db_manager.create_services_table unless @db_manager.table_exists?(:services)
@db_manager.store_services(services)
end
def ask_for_uplink_speed # rubocop:disable Metrics/MethodLength
loop do
Curses.clear
Curses.addstr("Please enter your uplink speed (upload speed, e.g., 1000Mbps or 1Gbps).\n" \
"This is typically the maximum upload speed provided by your ISP.\n" \
"You can check your ISP bill, use an online speed test, or contact your ISP if you're unsure.\n\n")
Curses.refresh
Curses.addstr('Uplink Speed: ')
speed = DCI.catch_input(true)
return speed.end_with?('gbps') ? convert_speed_to_mbps(speed) : speed.to_i if valid_speed?(speed)
Curses.setpos(5, 0)
Curses.addstr("Whoops! That didn't appear to be a valid speed. Please try again!")
Curses.refresh
end
end
def ask_for_downlink_speed # rubocop:disable Metrics/MethodLength
loop do
Curses.clear
Curses.addstr("Please enter your downlink speed (download speed, e.g., 1000Mbps or 1Gbps).\n" \
"This is typically the maximum download speed provided by your ISP.\n"\
"You can check your ISP bill, use an online speed test, or contact your ISP if you're unsure.\n\n")
Curses.refresh
Curses.addstr('Downlink Speed: ')
speed = DCI.catch_input(true)
return speed.end_with?('gbps') ? convert_speed_to_mbps(speed) : speed.to_i if valid_speed?(speed)
Curses.setpos(5, 0)
Curses.addstr("Whoops! That didn't appear to be a valid speed. Please try again!")
Curses.refresh
end
end
def valid_speed?(speed)
speed.to_i.positive?
end
def ask_for_services # rubocop:disable Metrics/MethodLength
loop do
Curses.clear
Curses.addstr("Please enter the services the system should be aware of (e.g., webserver or database).\n" \
"Enter the services as a comma-separated list (e.g., webserver,database).\n\n")
Curses.refresh
Curses.addstr('Services: ')
services = DCI.catch_input(true)
services_arr = services.strip.downcase.split(',').map(&:strip)
return services_arr if valid_services?(services_arr)
Curses.setpos(7, 0)
Curses.addstr("Whoops! Thatdidn't appear to be a valid list of services. Please try again!")
Curses.refresh
end
end
def valid_services?(services)
# TODO: Validate the services
# For now, just check if the array is not empty
!services.empty?
end
end