mysql_dump_slow 0.1.0

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
+ SHA1:
3
+ metadata.gz: 7f116ae111cbb94f437455ef3704daef1b40160d
4
+ data.tar.gz: 6389e35046f722c88446e8cc33dcf8bd12721387
5
+ SHA512:
6
+ metadata.gz: 2d45533db841385baedadddb6555e683e9ce66d72204116ddd5c4cebb42c3dd3161cd69d2cae45fb0d6b230ab43e4759968bbe0156eabf5331ba0ce62c856fd0
7
+ data.tar.gz: bfbcfc19eb73b4d52007f2b74381430acfbbc7cdc6c9121a9db52b2c094df92ce385f8e22d9fc311791a51ea1c681e4d69221357e0f7d465f1590dcac287ef42
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.travis.yml ADDED
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.2.1
4
+ before_install: gem install bundler -v 1.10.4
@@ -0,0 +1,13 @@
1
+ # Contributor Code of Conduct
2
+
3
+ As contributors and maintainers of this project, we pledge to respect all people who contribute through reporting issues, posting feature requests, updating documentation, submitting pull requests or patches, and other activities.
4
+
5
+ We are committed to making participation in this project a harassment-free experience for everyone, regardless of level of experience, gender, gender identity and expression, sexual orientation, disability, personal appearance, body size, race, ethnicity, age, or religion.
6
+
7
+ Examples of unacceptable behavior by participants include the use of sexual language or imagery, derogatory comments or personal attacks, trolling, public or private harassment, insults, or other unprofessional conduct.
8
+
9
+ Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct. Project maintainers who do not follow the Code of Conduct may be removed from the project team.
10
+
11
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by opening an issue or contacting one or more of the project maintainers.
12
+
13
+ This Code of Conduct is adapted from the [Contributor Covenant](http://contributor-covenant.org), version 1.0.0, available at [http://contributor-covenant.org/version/1/0/0/](http://contributor-covenant.org/version/1/0/0/)
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in mysql_dump_slow.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 miyakey
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,86 @@
1
+ # MysqlDumpSlow [![Build Status](https://travis-ci.org/monochromegane/mysql_dump_slow.svg?branch=master)](https://travis-ci.org/monochromegane/mysql_dump_slow)
2
+
3
+ A library to summarize MySQL slow\_log records in Ruby.
4
+
5
+ ## Usage
6
+
7
+ ### Summarize and print result.
8
+
9
+ ```ruby
10
+ # Getting slow logs by using ActiveRecord for mysql.slow_log.
11
+ logs = SlowLog.all
12
+
13
+ # Summarize slow logs
14
+ summary = MysqlDumpSlow.summarize(logs)
15
+ summary.each do |conter|
16
+ # counter provides printer that same as mysqldumpslow command.
17
+ counter.to_mysqldumpslow
18
+ # => Count: 2 Time=100s (200s) Lock=200s (400s) Rows=300 (600), 2hosts
19
+ # SELECT * FROM T
20
+ end
21
+ ```
22
+
23
+ ### Use total/average getter
24
+
25
+ ```ruby
26
+ # counter provides every total/avg getter. See also test codes.
27
+ counter.total_query_time # => 100000
28
+ counter.average_query_time # => 100000
29
+ ```
30
+
31
+ - total\_count
32
+ - [ total | average ]\_query\_time
33
+ - [ total | average ]\_lock\_time
34
+ - [ total | average ]\_rows\_set
35
+ - user\_hosts
36
+
37
+ ### ActiveRecord
38
+
39
+ MysqlDumpSlow.summarize require ActiveRecord for mysql.slow\_log table.
40
+ An example is the following.
41
+
42
+ ```ruby
43
+ class SlowLog < ActiveRecord::Base
44
+ self.table_name = 'slow_log'
45
+
46
+ def self.new_connection_info
47
+ connection_info = self.configrations[Rails.env]
48
+ connection_info["database"] = "mysql"
49
+ connection_info
50
+ end
51
+
52
+ establish_connection new_connection_info
53
+ end
54
+ ```
55
+
56
+ ## Installation
57
+
58
+ Add this line to your application's Gemfile:
59
+
60
+ ```ruby
61
+ gem 'mysql_dump_slow'
62
+ ```
63
+
64
+ And then execute:
65
+
66
+ $ bundle
67
+
68
+ Or install it yourself as:
69
+
70
+ $ gem install mysql_dump_slow
71
+
72
+ ## Development
73
+
74
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
75
+
76
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
77
+
78
+ ## Contributing
79
+
80
+ Bug reports and pull requests are welcome on GitHub at https://github.com/monochromegane/mysql\_dump\_slow. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](contributor-covenant.org) code of conduct.
81
+
82
+
83
+ ## License
84
+
85
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
86
+
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ Rake::TestTask.new(:test) do |t|
5
+ t.libs << "test"
6
+ t.libs << "lib"
7
+ t.test_files = FileList['test/**/*_test.rb']
8
+ end
9
+
10
+ task :default => :test
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "mysql_dump_slow"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
data/bin/setup ADDED
@@ -0,0 +1,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,10 @@
1
+ require "mysql_dump_slow/version"
2
+ require "mysql_dump_slow/counter"
3
+ require "mysql_dump_slow/masked_sql_parser"
4
+ require "mysql_dump_slow/summary"
5
+
6
+ module MysqlDumpSlow
7
+ def self.summarize(slow_logs)
8
+ Summary.new(slow_logs)
9
+ end
10
+ end
@@ -0,0 +1,72 @@
1
+ module MysqlDumpSlow
2
+ class Counter
3
+ attr_reader :abstract_query, :total_count, :total_query_time, :total_lock_time, :total_rows_sent, :user_hosts
4
+
5
+ def initialize(abstract_query)
6
+ @abstract_query = abstract_query
7
+ end
8
+
9
+ def count_up(log)
10
+ count_up_query_time(log.query_time)
11
+ count_up_lock_time(log.lock_time)
12
+ count_up_rows_sent(log.rows_sent)
13
+ count_up_user_host(log.user_host)
14
+ count_up_counter
15
+ end
16
+
17
+ def total_user_host
18
+ @user_hosts.size
19
+ end
20
+
21
+ def average_query_time
22
+ total_query_time / total_count
23
+ end
24
+
25
+ def average_lock_time
26
+ total_lock_time / total_count
27
+ end
28
+
29
+ def average_rows_sent
30
+ total_rows_sent / total_count
31
+ end
32
+
33
+ def to_mysqldumpslow
34
+ <<-EOS
35
+ Count: #{total_count} Time=#{average_query_time/1000}s (#{total_query_time/1000}s) Lock=#{average_lock_time/1000}s (#{total_lock_time/1000}s) Rows=#{average_rows_sent} (#{total_rows_sent}), #{ user_hosts.size == 1 ? user_hosts.first : user_hosts.size.to_s + 'hosts'}
36
+ #{abstract_query}
37
+ EOS
38
+ end
39
+
40
+ private
41
+
42
+ def count_up_counter
43
+ @total_count ||= 0
44
+ @total_count += 1
45
+ end
46
+
47
+ def count_up_query_time(query_time)
48
+ @total_query_time ||= 0
49
+ @total_query_time += time_to_ms(query_time)
50
+ end
51
+
52
+ def count_up_lock_time(lock_time)
53
+ @total_lock_time ||= 0
54
+ @total_lock_time += time_to_ms(lock_time)
55
+ end
56
+
57
+ def count_up_rows_sent(rows_sent)
58
+ @total_rows_sent ||= 0
59
+ @total_rows_sent += rows_sent
60
+ end
61
+
62
+ def count_up_user_host(user_host)
63
+ @user_hosts ||= []
64
+ @user_hosts << user_host unless @user_hosts.include?(user_host)
65
+ end
66
+
67
+ def time_to_ms(time)
68
+ time.instance_eval { self.to_i * 1000 + (usec / 1000) }
69
+ end
70
+ end
71
+ end
72
+
@@ -0,0 +1,11 @@
1
+ require "sql-parser"
2
+ module SQLParser
3
+ class SQLVisitor
4
+ def visit_DateTime(o); "'S'"; end
5
+ def visit_Date(o); "'S'"; end
6
+ def visit_String(o); "'S'"; end
7
+ def visit_ApproximateFloat(o); 'N'; end
8
+ def visit_Float(o); 'N'; end
9
+ def visit_Integer(o); 'N'; end
10
+ end
11
+ end
@@ -0,0 +1,37 @@
1
+ module MysqlDumpSlow
2
+ class Summary
3
+ include Enumerable
4
+
5
+ def initialize(logs)
6
+ @logs = logs
7
+ summarize
8
+ end
9
+
10
+ def sort_by(order)
11
+ summary.sort_by do |counter|
12
+ counter.send(order.to_sym) if counter.respond_to?(order.to_sym)
13
+ end.reverse
14
+ end
15
+
16
+ def each
17
+ summary.each {|s| yield s }
18
+ end
19
+
20
+ private
21
+
22
+ def summarize
23
+ parser = SQLParser::Parser.new
24
+ @logs.each do |log|
25
+ sql = parser.scan_str(log.sql_text).to_sql
26
+ counter = summary.find{|s| s.abstract_query == sql }
27
+ counter ||= ( summary << Counter.new(sql) ).last
28
+ counter.count_up(log)
29
+ end
30
+ @summary = sort_by(:average_query_time)
31
+ end
32
+
33
+ def summary
34
+ @summary ||= []
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,3 @@
1
+ module MysqlDumpSlow
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,28 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'mysql_dump_slow/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "mysql_dump_slow"
8
+ spec.version = MysqlDumpSlow::VERSION
9
+ spec.authors = ["monochromegane"]
10
+ spec.email = ["dev.kuro.obi@gmail.com"]
11
+
12
+ spec.summary = %q{A library to summarize MySQL slow_log records in Ruby.}
13
+ spec.description = %q{A library to summarize MySQL slow_log records in Ruby.}
14
+ spec.homepage = "https://github.com/monochromegane/mysql_dump_slow"
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
18
+ spec.bindir = "exe"
19
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
20
+ spec.require_paths = ["lib"]
21
+
22
+ spec.add_dependency "sql-parser", "~> 0.0.2"
23
+
24
+ spec.add_development_dependency "bundler", "~> 1.10"
25
+ spec.add_development_dependency "rake", "~> 10.0"
26
+ spec.add_development_dependency "minitest"
27
+ spec.add_development_dependency "pry"
28
+ end
metadata ADDED
@@ -0,0 +1,129 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mysql_dump_slow
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - monochromegane
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2015-07-10 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: sql-parser
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 0.0.2
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 0.0.2
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.10'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.10'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: minitest
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
+ - !ruby/object:Gem::Dependency
70
+ name: pry
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ description: A library to summarize MySQL slow_log records in Ruby.
84
+ email:
85
+ - dev.kuro.obi@gmail.com
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - ".gitignore"
91
+ - ".travis.yml"
92
+ - CODE_OF_CONDUCT.md
93
+ - Gemfile
94
+ - LICENSE.txt
95
+ - README.md
96
+ - Rakefile
97
+ - bin/console
98
+ - bin/setup
99
+ - lib/mysql_dump_slow.rb
100
+ - lib/mysql_dump_slow/counter.rb
101
+ - lib/mysql_dump_slow/masked_sql_parser.rb
102
+ - lib/mysql_dump_slow/summary.rb
103
+ - lib/mysql_dump_slow/version.rb
104
+ - mysql_dump_slow.gemspec
105
+ homepage: https://github.com/monochromegane/mysql_dump_slow
106
+ licenses:
107
+ - MIT
108
+ metadata: {}
109
+ post_install_message:
110
+ rdoc_options: []
111
+ require_paths:
112
+ - lib
113
+ required_ruby_version: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ required_rubygems_version: !ruby/object:Gem::Requirement
119
+ requirements:
120
+ - - ">="
121
+ - !ruby/object:Gem::Version
122
+ version: '0'
123
+ requirements: []
124
+ rubyforge_project:
125
+ rubygems_version: 2.2.2
126
+ signing_key:
127
+ specification_version: 4
128
+ summary: A library to summarize MySQL slow_log records in Ruby.
129
+ test_files: []