Class: MyGeneral::Instance
- Inherits:
-
Object
- Object
- MyGeneral::Instance
- Defined in:
- lib/my_general/instance.rb
Instance Method Summary collapse
- #complexity ⇒ Object
-
#initialize(log_file, database_file) ⇒ Instance
constructor
A new instance of Instance.
- #run ⇒ Object
- #run_data ⇒ Object
- #run_query(line, progressbar) ⇒ Object
Constructor Details
#initialize(log_file, database_file) ⇒ Instance
Returns a new instance of Instance.
2 3 4 5 6 7 8 |
# File 'lib/my_general/instance.rb', line 2 def initialize(log_file, database_file) @complexity = nil @db = nil @log_file = log_file @database_file = database_file end |
Instance Method Details
#complexity ⇒ Object
10 11 12 |
# File 'lib/my_general/instance.rb', line 10 def complexity @complexity.nil? ? (@complexity = `wc -l #{@log_file}`.to_i) : @complexity end |
#run ⇒ Object
14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/my_general/instance.rb', line 14 def run puts "Log File: #{@log_file}" puts "Database YAML: #{@database_file}" puts '[1/4] ⌚ Counting Complexity...' complexity puts "[1/4] ⌚ Counting Complexity... OK [Complexity: #{@complexity}]" puts '[2/4] 🔌 Dailing Database...' @db = Sequel.connect(YAML.load_file(@database_file)) puts "[2/4] 🔌 Dailing Database... OK [Connected]" puts '[3/4] ⏳ Importing Data...' run_data puts "[3/4] ⏳ Importing Data... OK [#{complexity}/#{complexity}]" puts '[4/4] 🚩 Finished!' end |
#run_data ⇒ Object
29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/my_general/instance.rb', line 29 def run_data = ProgressBar.create .total = complexity File.open(@log_file, 'r') do |file| until file.eof? line = file.readline run_query(line, ) .increment end end end |
#run_query(line, progressbar) ⇒ Object
41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/my_general/instance.rb', line 41 def run_query(line, ) data = line.split("\t") return if data.length < 3 # A connecting message return if data[-1].upcase.start_with?('CREATE DATABASE') # Ignore database scale query return unless data[-2].end_with?('Query') # Ignore not query return if data[-1].upcase.start_with?('SELECT')# Ignore select query return if data[-1].upcase.start_with?('SHOW') # Ignore show query @db.run(data[-1]) rescue => e .log("When executing #{line}") .log('We met a problem:') .log(e.inspect) end |