Class: MyGeneral::Instance

Inherits:
Object
  • Object
show all
Defined in:
lib/my_general/instance.rb

Instance Method Summary collapse

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

#complexityObject



10
11
12
# File 'lib/my_general/instance.rb', line 10

def complexity
  @complexity.nil? ? (@complexity = `wc -l #{@log_file}`.to_i) : @complexity
end

#runObject



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_dataObject



29
30
31
32
33
34
35
36
37
38
39
# File 'lib/my_general/instance.rb', line 29

def run_data
  progressbar = ProgressBar.create
  progressbar.total = complexity
  File.open(@log_file, 'r') do |file|
    until file.eof?
      line = file.readline
      run_query(line, progressbar)
      progressbar.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, progressbar)
  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
  progressbar.log("When executing #{line}")
  progressbar.log('We met a problem:')
  progressbar.log(e.inspect)
end