my_general 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: c4ce678b6da475675e5f6804cae58a7f642367b78568cae33ff63d126b295018
4
- data.tar.gz: 8a2d5d7129071b08376a85c749478b647cf215e13f06bef425e2fd43ec550a99
3
+ metadata.gz: f8f84cc91082b51a74ab1c8b0cc3b895841e53f3b0d25a777f9ce362733fb7c9
4
+ data.tar.gz: '0499ab72306d9933feca4084370219f50e9a4987f889a326303eb3dbf46f2de7'
5
5
  SHA512:
6
- metadata.gz: a9c1a30e12a3f6502b28aa7a72325d003e7520f454d2dc93a4f8bd2e2a9e0abd16a1e22face9cae6c08eaf36ce72ebf94c486ad58cb0506e0ca3cf5a699f1775
7
- data.tar.gz: 10287b2ca71453fc887fa1118bf4feb7fc1df6e66b93e14a0a34d2289ddc739b7a19b246f11c2435ce00110279ee03f0703427aefc90b8b39f9504462e79b39c
6
+ metadata.gz: e61891e2fb0b60aaa3b36b1fd0f05b5f83974ce58504e770cbd86c258a788d846ea77e64ec727fc87ea561a0b421927b8cbc0c0a57af7a0a527404f8296512c4
7
+ data.tar.gz: b45df2362797283c1651d37f81cb5ebd1107d1a9bf2ed4dff97925c598c3f23076af1f09e81ac4aebcce16e6103fc74d1542a0b38d8e0bd3ce32baff205bf4e2
@@ -1,3 +1,4 @@
1
+ require 'date'
1
2
  require 'ruby-progressbar'
2
3
  require 'sequel'
3
4
  require 'yaml'
@@ -2,9 +2,17 @@ class MyGeneral::Instance
2
2
  def initialize(log_file, database_file)
3
3
  @complexity = nil
4
4
  @db = nil
5
+ @time = nil
6
+ @new_command = false
7
+ @command = 'Query'
8
+ @buffer = 'SELECT 1'
9
+
10
+ @insert_count = 0
11
+ @success_count = 0
5
12
 
6
13
  @log_file = log_file
7
14
  @database_file = database_file
15
+ @progressbar = nil
8
16
  end
9
17
 
10
18
  def complexity
@@ -22,33 +30,69 @@ class MyGeneral::Instance
22
30
  puts "[2/4] 🔌 Dailing Database... OK [Connected]"
23
31
  puts '[3/4] ⏳ Importing Data...'
24
32
  run_data
25
- puts "[3/4] ⏳ Importing Data... OK [#{complexity}/#{complexity}]"
33
+ puts "[3/4] ⏳ Importing Data... OK [#{@success_count}/#{@insert_count}]"
26
34
  puts '[4/4] 🚩 Finished!'
27
35
  end
28
36
 
29
37
  def run_data
30
- progressbar = ProgressBar.create
31
- progressbar.total = complexity
38
+ @progressbar = ProgressBar.create(format: '%t: |%B| %c/%C %E')
39
+ @progressbar.total = complexity
32
40
  File.open(@log_file, 'r') do |file|
41
+ 3.times { file.readline } # Skip 3 lines
33
42
  until file.eof?
34
43
  line = file.readline
35
- run_query(line, progressbar)
36
- progressbar.increment
44
+ parse_line(line)
45
+ @progressbar.increment
37
46
  end
38
47
  end
39
48
  end
40
49
 
41
- def run_query(line, progressbar)
42
- data = line.split("\t")
43
- return if data.length < 3 # A connecting message
44
- return if data[-1].upcase.start_with?('CREATE DATABASE') # Ignore database scale query
45
- return unless data[-2].end_with?('Query') # Ignore not query
46
- return if data[-1].upcase.start_with?('SELECT')# Ignore select query
47
- return if data[-1].upcase.start_with?('SHOW') # Ignore show query
48
- @db.run(data[-1])
50
+ def parse_line(line)
51
+ date = line.split("\t")[0]
52
+ if date =~ /^[0-9]{6}/ # Is Date?
53
+ @time = DateTime.parse("#{20}#{date}")
54
+ @command = line.split("\t")[1].split(' ')[-1]
55
+ @new_command = true
56
+ elsif line.start_with?("\t\t")
57
+ @command = line.split("\t")[2].split(' ')[-1]
58
+ @new_command = true
59
+ else
60
+ @new_command = false
61
+ end
62
+
63
+ flush
64
+
65
+ if @command == 'Query'
66
+ @buffer << line.split("\t")[-1]
67
+ end
68
+ rescue => e
69
+ # Ignore
70
+ end
71
+
72
+ def flush
73
+ if @new_command & !@buffer.empty?
74
+ # Do query
75
+ run_query
76
+ # Refresh variables
77
+ @new_command = false
78
+ @buffer = ''
79
+ end
80
+ end
81
+
82
+ VERBS = ['SET', 'INSERT', 'UPDATE', 'DELETE', 'CREATE', 'ALTER', 'DROP'].freeze
83
+
84
+ def run_query
85
+ # return if @time < DateTime.parse("2017-05-13 07:08:00") # Ignore from last backup
86
+ return if @buffer.upcase.start_with?('CREATE DATABASE') # Ignore database scale query
87
+ return unless VERBS.map do |verb|
88
+ @buffer.upcase.start_with?(verb)
89
+ end.reduce(:|) # Ignore unless exact verb
90
+ @insert_count += 1
91
+ @db.run(@buffer)
92
+ @success_count += 1
49
93
  rescue => e
50
- progressbar.log("When executing #{line}")
51
- progressbar.log('We met a problem:')
52
- progressbar.log(e.inspect)
94
+ @progressbar.log("When executing #{@time} #{@buffer}")
95
+ @progressbar.log('We met a problem:')
96
+ @progressbar.log(e.inspect)
53
97
  end
54
98
  end
@@ -1,5 +1,5 @@
1
1
  # MyGenral Module
2
2
  module MyGeneral
3
3
  # Current Version Code
4
- VERSION = '0.0.1'.freeze
4
+ VERSION = '0.0.2'.freeze
5
5
  end
data/test.db ADDED
Binary file
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: my_general
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - HeckPsi Lab
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir:
10
10
  - bin
11
11
  cert_chain: []
12
- date: 2018-01-05 00:00:00.000000000 Z
12
+ date: 2018-01-06 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: ruby-progressbar
@@ -80,6 +80,7 @@ files:
80
80
  - lib/my_general.rb
81
81
  - lib/my_general/instance.rb
82
82
  - lib/my_general/version.rb
83
+ - test.db
83
84
  homepage: https://github.com/dsh0416/my_general
84
85
  licenses:
85
86
  - MIT