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

63 lines
1.7 KiB
Ruby

# frozen_string_literal: true
require 'securerandom'
require 'digest'
require 'base64'
require 'openssl'
# Utiltiies Module
module Utilities
# Converts speed from Gbps to Mbps if necessary
def convert_speed_to_mbps(speed)
if speed.end_with?('gbps')
speed.to_i * 1000
else
speed.to_i
end
end
# Converts an array of services into a hash
def services_to_hash(services)
services_hash = {}
services.each { |service| services_hash[service] = true }
services_hash
end
# Calculates total bandwidth from uplink and downlink speeds
def calculate_total_bandwidth(uplink_speed, downlink_speed)
uplink_speed + downlink_speed
end
def generate_key
Base64.encode64(SecureRandom.bytes(32)).chomp
end
def encrypt_string_chacha20(string, key)
cipher = OpenSSL::Cipher.new('chacha20')
cipher.encrypt
cipher.key = Base64.decode64(key)
encrypted = cipher.update(string) + cipher.final
Base64.encode64(encrypted).chomp
end
def decrypt_string_chacha20(encrypted_string, key)
decipher = OpenSSL::Cipher.new('chacha20')
decipher.decrypt
decipher.key = Base64.decode64(key)
decrypted = Base64.decode64(encrypted_string)
decipher.update(decrypted) + decipher.final
end
# Encrypts a given data object using Blowfish and returns the encrypted string
def encrypt_data_blowfish(data, key)
plain_text = YAML.dump(data)
encrypt_string_blowfish(plain_text, key)
end
# Decrypts a given encrypted string using Blowfish and returns the original data object
def decrypt_data_blowfish(encrypted_text, key)
plain_text = decrypt_string_blowfish(encrypted_text, key)
YAML.load(plain_text)
end
end