NETRAVE/lib/utils/system_information_gather.rb
VetheonGames 335cd89ebe Detailed Refactoring of Database Interaction
1. **Refactoring of Database Interaction Methods**
   - Refactored the `store_services` method in the `DatabaseManager` class to handle an array of services instead of a hash. This change was made to simplify the interaction with the database and to avoid unnecessary complexity in the data structure.
   - The `store_services` method now iterates over an array of services and inserts each service into the database with a default status of true. This design choice was made to ensure that all services are active by default.

2. **Modification of Database Schema**
   - Modified the `create_services_table` method in the `DatabaseManager` class to create a table with only two columns: `id` and `services`. This change was made to align the database schema with the new data structure used in the `store_services` method.
   - The `status` column was removed from the `services` table because the status of all services is now assumed to be true by default.

3. **Error Handling and Debugging**
   - Encountered a `Sequel::DatabaseError` with the message "Operand should contain 1 column(s)" during the execution of the `store_services` method. This error was caused by an attempt to insert a hash into a single database column.
   - The error was resolved by refactoring the `store_services` method to handle an array of services instead of a hash.

4. **Unorthodox Design Choices**
   - The decision to use an array of services instead of a hash and to assume that the status of all services is true by default may seem unorthodox. However, these design choices were made to simplify the interaction with the database and to avoid unnecessary complexity in the data structure.
   - These design choices also helped to resolve the `Sequel::DatabaseError` that was encountered during the execution of the `store_services` method.

This commit represents a significant refactoring of the database interaction methods in the NETRAVE project. The changes made in this commit have simplified the interaction with the database and have resolved a `Sequel::DatabaseError` that was encountered during the execution of the `store_services` method.
2023-06-09 19:39:54 -06:00

105 lines
3.4 KiB
Ruby

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:
}
# Check if the system_info table exists, if not, create it
@db_manager.create_system_info_table unless @db_manager.table_exists?(:system_info)
# Store the gathered system info in the database
@db_manager.store_system_info(system_info)
# Check if the services table exists, if not, create it
@db_manager.create_services_table unless @db_manager.table_exists?(:services)
# Store the services in the services table
@db_manager.store_services(services)
end
def ask_for_uplink_speed # rubocop:disable Metrics/MethodLength
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)
if valid_speed?(speed)
speed.end_with?('gbps') ? convert_speed_to_mbps(speed) : speed.to_i
else
Curses.setpos(5, 0)
Curses.addstr("Whoops! That didn't appear to be a valid speed. Please try again!")
Curses.refresh
ask_for_uplink_speed
end
end
def ask_for_downlink_speed # rubocop:disable Metrics/MethodLength
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)
if valid_speed?(speed)
speed.end_with?('gbps') ? convert_speed_to_mbps(speed) : speed.to_i
else
Curses.setpos(5, 0)
Curses.addstr("Whoops! That didn't appear to be a valid speed. Please try again!")
Curses.refresh
ask_for_downlink_speed
end
end
def valid_speed?(speed)
speed.to_i.positive?
end
def ask_for_services # rubocop:disable Metrics/MethodLength
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)
if valid_services?(services_arr)
services_arr # return the array of services directly
else
Curses.setpos(7, 0)
Curses.addstr("Whoops! That didn't appear to be a valid list of services. Please try again!")
Curses.refresh
ask_for_services
end
end
def valid_services?(_services)
# TODO: Validate the services
true
end
end