proxysql_query_log-parser 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 4966444a16781f8d1bd840ad809dcdd8f7a0252d1178769746ad0b96e7ec97d8
4
+ data.tar.gz: 3e7e50eb10a369739003f91b2e93b172378618707fc8678c5cd91dd898c876f1
5
+ SHA512:
6
+ metadata.gz: a2fe520f18b3a260faeacf6c95a50f5274444383d3535e3a39b244ea6bc8b06e51ebdaa90297b2d170b514dc906796b391ba8933633454e1bc37d53a0ee0e644
7
+ data.tar.gz: 5aa401aa0ab2dbe5e009138b3a249e3da58cb190f0cf89981bced105599ba0185f3b66caaae4b341acc555d109e261e1d1cf8c91f41a03b62f96382f5d1f18a6
data/.gitignore ADDED
@@ -0,0 +1,2 @@
1
+ vendor
2
+ .bundle
data/.rubocop.yml ADDED
@@ -0,0 +1,6 @@
1
+ AllCops:
2
+ TargetRubyVersion: 2.5
3
+ DisplayCopNames: true
4
+ Exclude:
5
+ - 'vendor/**/*'
6
+ - '*.gemspec'
data/.travis.yml ADDED
@@ -0,0 +1,11 @@
1
+ language: ruby
2
+
3
+ rvm:
4
+ - 2.5
5
+
6
+ bundler_args: --jobs=2
7
+
8
+ script:
9
+ - bundle exec rake test
10
+
11
+ cache: bundler
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source "https://rubygems.org"
2
+ gemspec
3
+
data/Gemfile.lock ADDED
@@ -0,0 +1,40 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ proxysql_query_log-parser (0.0.2)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ ast (2.4.0)
10
+ parallel (1.12.1)
11
+ parser (2.5.1.0)
12
+ ast (~> 2.4.0)
13
+ power_assert (1.1.1)
14
+ powerpack (0.1.1)
15
+ rainbow (3.0.0)
16
+ rake (10.5.0)
17
+ rubocop (0.56.0)
18
+ parallel (~> 1.10)
19
+ parser (>= 2.5)
20
+ powerpack (~> 0.1)
21
+ rainbow (>= 2.2.2, < 4.0)
22
+ ruby-progressbar (~> 1.7)
23
+ unicode-display_width (~> 1.0, >= 1.0.1)
24
+ ruby-progressbar (1.9.0)
25
+ test-unit (3.2.8)
26
+ power_assert
27
+ unicode-display_width (1.3.3)
28
+
29
+ PLATFORMS
30
+ ruby
31
+
32
+ DEPENDENCIES
33
+ bundler (~> 1.16)
34
+ proxysql_query_log-parser!
35
+ rake (~> 10.0)
36
+ rubocop
37
+ test-unit (~> 3.0)
38
+
39
+ BUNDLED WITH
40
+ 1.16.2
data/README.md ADDED
@@ -0,0 +1,15 @@
1
+ # ProxySQL Query Log Parser
2
+
3
+ [![Build Status](https://travis-ci.org/takaishi/proxysql_query_log-parser.svg?branch=master)](https://travis-ci.org/takaishi/proxysql_query_log-parser)
4
+
5
+
6
+ ## Usage
7
+
8
+ ```
9
+ require 'proxysql_query_log/parser'
10
+
11
+ parser = ProxysqlQueryLog::Parser.new
12
+ parser.load_file('queries.log.00000011').each do |query|
13
+ query.print
14
+ end
15
+ ```
data/Rakefile ADDED
@@ -0,0 +1,13 @@
1
+ require "bundler"
2
+ Bundler::GemHelper.install_tasks
3
+
4
+ require "rake/testtask"
5
+
6
+ Rake::TestTask.new(:test) do |t|
7
+ t.libs.push("lib", "test")
8
+ t.test_files = FileList["test/**/test_*.rb"]
9
+ t.verbose = true
10
+ t.warning = true
11
+ end
12
+
13
+ task default: [:test]
@@ -0,0 +1,10 @@
1
+ require 'json'
2
+
3
+ require 'proxysql_query_log/parser/parser'
4
+ require 'proxysql_query_log/parser/query'
5
+ require 'proxysql_query_log/parser/version'
6
+
7
+ module ProxysqlQueryLog
8
+
9
+ end
10
+
@@ -0,0 +1,115 @@
1
+ module ProxysqlQueryLog
2
+ class Parser
3
+ def load_file(path)
4
+ io = File.open(path)
5
+ read(io)
6
+ end
7
+
8
+ def read(io)
9
+ queries = []
10
+
11
+ while true
12
+ raw_total_bytes = io.read(1)
13
+ break unless raw_total_bytes
14
+ total_bytes = raw_total_bytes.unpack1('C')
15
+ io.seek(7, IO::SEEK_CUR)
16
+ raw = io.read(total_bytes)
17
+ queries << parse(StringIO.new(raw, 'r+'))
18
+ end
19
+ queries
20
+ end
21
+
22
+ def parse(io)
23
+ q = Query.new
24
+ if io.read(1).unpack1('C') == 0
25
+ q.thread_id = parse_thread_id(io)
26
+ q.username = parse_username(io)
27
+ q.schema_name = parse_schema_name(io)
28
+ q.client = parse_client(io)
29
+ q.hid = parse_hid(io)
30
+ q.server = parse_server(io)
31
+ q.start_time = parse_start_time(io)
32
+ q.end_time = parse_end_time(io)
33
+ q.digest = parse_digest(io)
34
+ q.query = parse_query(io)
35
+ end
36
+ q
37
+ end
38
+
39
+ private
40
+
41
+ def parse_thread_id(io)
42
+ read_encoded_length(io)
43
+ end
44
+
45
+ def parse_username(io)
46
+ read_encoded_string(io,read_encoded_length(io))
47
+ end
48
+
49
+ def parse_schema_name(io)
50
+ read_encoded_string(io, read_encoded_length(io))
51
+ end
52
+
53
+ def parse_client(io)
54
+ read_encoded_string(io, read_encoded_length(io))
55
+ end
56
+
57
+ def parse_hid(io)
58
+ read_encoded_length(io)
59
+ end
60
+
61
+ def parse_server(io)
62
+ read_encoded_string(io,read_encoded_length(io))
63
+ end
64
+
65
+ def parse_start_time(io)
66
+ io.read(1).unpack('C')
67
+ io.read(8).unpack1('Q*')
68
+ end
69
+
70
+ def parse_end_time(io)
71
+ io.read(1).unpack('C')
72
+ io.read(8).unpack1('Q*')
73
+ end
74
+
75
+ def parse_digest(io)
76
+ io.read(1).unpack('C')
77
+ "0x#{io.read(8).unpack('I*').map{|n| sprintf("%X", n)}.join("")}"
78
+ end
79
+
80
+ def parse_query(io)
81
+ query_len = read_encoded_length(io)
82
+ read_encoded_string(io, query_len)
83
+ end
84
+
85
+ def mysql_decode_length(buf)
86
+ case
87
+ when buf <= 0xfb
88
+ 1
89
+ when buf == 0xfc
90
+ 3
91
+ when buf == 0xfd
92
+ 4
93
+ when buf == 0xfe
94
+ 9
95
+ else
96
+ 0
97
+ end
98
+ end
99
+
100
+ def read_encoded_length(io)
101
+ buf = io.read(1).unpack('C')
102
+ len = mysql_decode_length(buf[0])
103
+ unless len == 0
104
+ buf2 = io.read(len-1).unpack('C*')
105
+ buf.concat(buf2)
106
+ return buf == 0 ? 0 : buf[0]
107
+ end
108
+ end
109
+
110
+ def read_encoded_string(io, len)
111
+ io.read(len).unpack1('a*')
112
+ end
113
+ end
114
+ end
115
+
@@ -0,0 +1,44 @@
1
+ module ProxysqlQueryLog
2
+ class Query
3
+ class << self
4
+ def create(thread_id, username, schema_name, client, hid, server, start_time, end_time, digest, query)
5
+ q = Query.new
6
+ q.thread_id = thread_id
7
+ q.username = username
8
+ q.schema_name = schema_name
9
+ q.client = client
10
+ q.hid = hid
11
+ q.server = server
12
+ q.start_time = start_time
13
+ q.end_time = end_time
14
+ q.digest = digest
15
+ q.query = query
16
+
17
+ q
18
+ end
19
+ end
20
+
21
+ attr_accessor :thread_id, :username, :schema_name, :client, :hid, :server, :start_time, :end_time, :digest, :query
22
+
23
+ def print
24
+ puts "ProxySQL LOG QUERY: thread_id=\"#{@thread_id}\" username=\"#{@username}\" schema_name=\"#{@schema_name}\" client=\"#{@client}\" HID=#{@hid} server=\"#{@server}\" starttime=\"#{Time.at(@start_time/1000/1000)}\" endtime=\"#{Time.at(@end_time/1000/1000)}\" duration=#{@end_time - @start_time}us digest=\"#{@digest}\"
25
+ #{@query}"
26
+ end
27
+
28
+ def to_json
29
+ {
30
+ thread_id: thread_id,
31
+ username: username,
32
+ schema_name: schema_name,
33
+ client: client,
34
+ HID: hid,
35
+ server: server,
36
+ start_time: Time.at(start_time/1000/1000),
37
+ end_time: Time.at(end_time/1000/1000),
38
+ duration: end_time - start_time,
39
+ digest: digest
40
+ }.to_json
41
+ end
42
+
43
+ end
44
+ end
@@ -0,0 +1,5 @@
1
+ module ProxysqlQueryLog
2
+ class Parser
3
+ VERSION = "0.0.2"
4
+ end
5
+ end
@@ -0,0 +1,27 @@
1
+
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "proxysql_query_log/parser/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "proxysql_query_log-parser"
8
+ spec.version = ProxysqlQueryLog::Parser::VERSION
9
+ spec.authors = ["r_takaishi"]
10
+ spec.email = ["ryo.takaishi.0@gmail.com"]
11
+
12
+ spec.summary = 'ProxySQL query log parser library'
13
+ spec.description = spec.summary
14
+ spec.homepage = 'https://github.com/takaishi/proxysql_query_log-parser'
15
+
16
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
17
+ f.match(%r{^(test|spec|features)/})
18
+ end
19
+ spec.bindir = "exe"
20
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
21
+ spec.require_paths = ["lib"]
22
+
23
+ spec.add_development_dependency "bundler", "~> 1.16"
24
+ spec.add_development_dependency "rake", "~> 10.0"
25
+ spec.add_development_dependency "test-unit", "~> 3.0"
26
+ spec.add_development_dependency "rubocop"
27
+ end
metadata ADDED
@@ -0,0 +1,111 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: proxysql_query_log-parser
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ platform: ruby
6
+ authors:
7
+ - r_takaishi
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2018-05-29 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.16'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.16'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: test-unit
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rubocop
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ description: ProxySQL query log parser library
70
+ email:
71
+ - ryo.takaishi.0@gmail.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - ".gitignore"
77
+ - ".rubocop.yml"
78
+ - ".travis.yml"
79
+ - Gemfile
80
+ - Gemfile.lock
81
+ - README.md
82
+ - Rakefile
83
+ - lib/proxysql_query_log/parser.rb
84
+ - lib/proxysql_query_log/parser/parser.rb
85
+ - lib/proxysql_query_log/parser/query.rb
86
+ - lib/proxysql_query_log/parser/version.rb
87
+ - proxysql_query_log-parser.gemspec
88
+ homepage: https://github.com/takaishi/proxysql_query_log-parser
89
+ licenses: []
90
+ metadata: {}
91
+ post_install_message:
92
+ rdoc_options: []
93
+ require_paths:
94
+ - lib
95
+ required_ruby_version: !ruby/object:Gem::Requirement
96
+ requirements:
97
+ - - ">="
98
+ - !ruby/object:Gem::Version
99
+ version: '0'
100
+ required_rubygems_version: !ruby/object:Gem::Requirement
101
+ requirements:
102
+ - - ">="
103
+ - !ruby/object:Gem::Version
104
+ version: '0'
105
+ requirements: []
106
+ rubyforge_project:
107
+ rubygems_version: 2.7.6
108
+ signing_key:
109
+ specification_version: 4
110
+ summary: ProxySQL query log parser library
111
+ test_files: []