mycmd 0.0.3 → 0.0.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/bin/mycmd +1 -0
- data/lib/mycmd/cli.rb +2 -0
- data/lib/mycmd/clis/settings_commands.rb +1 -1
- data/lib/mycmd/clis/status_commands.rb +45 -0
- data/lib/mycmd/configuration.rb +1 -1
- data/lib/mycmd/version.rb +1 -1
- data/spec/mycmd/clis/settings_commands_spec.rb +31 -18
- data/spec/mycmd/clis/status_commands_spec.rb +113 -0
- metadata +6 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 4032fa8d14c8bef5767ba0e34e6a43ca9102a5f2
|
4
|
+
data.tar.gz: 0bdfd6067cadebe7fdb73ce724566bd264d8256e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 0203fe3e2003fdeb3f210e59b8a0c37887f2322beeb507aa801e8724029279db7af1fe396a80f04372df02c43d869b33ef622b2fab3801d81f5d155d847668ee
|
7
|
+
data.tar.gz: 9057067f77ee44feb682d5198a022dbf38cc850a4f17729cf78294878c990efbe7ef2c58068fb1aa014d1f33d54f7ae92558bbef0eff0c5a8c825d53ba385f8c
|
data/bin/mycmd
CHANGED
data/lib/mycmd/cli.rb
CHANGED
@@ -2,12 +2,14 @@
|
|
2
2
|
|
3
3
|
require "mycmd/clis/config_commands"
|
4
4
|
require "mycmd/clis/settings_commands"
|
5
|
+
require "mycmd/clis/status_commands"
|
5
6
|
|
6
7
|
module Mycmd
|
7
8
|
class CLI < Thor
|
8
9
|
default_command :console
|
9
10
|
register(ConfigCommands, "config", "config [COMMAND]", "commands for config")
|
10
11
|
register(SettingsCommands, "settings", "settings [COMMAND]", "commands for settings")
|
12
|
+
register(StatusCommands, "status", "status [COMMAND]", "commands for status")
|
11
13
|
|
12
14
|
desc "console", "console will start sql shell."
|
13
15
|
def console
|
@@ -27,7 +27,7 @@ module Mycmd
|
|
27
27
|
desc "search innodb_buffer_pool_size", "search will print settings"
|
28
28
|
def search(keyword)
|
29
29
|
client = Configuration.connect
|
30
|
-
printer = Printer.new(client.query("SHOW GLOBAL VARIABLES LIKE
|
30
|
+
printer = Printer.new(client.query("SHOW GLOBAL VARIABLES LIKE '%#{keyword}%'"))
|
31
31
|
printer.print
|
32
32
|
end
|
33
33
|
|
@@ -0,0 +1,45 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
|
3
|
+
module Mycmd
|
4
|
+
class StatusCommands < Thor
|
5
|
+
namespace :status
|
6
|
+
|
7
|
+
desc "size", "size will print database/table size"
|
8
|
+
option :database, aliases: "-d", desc: "target database."
|
9
|
+
def size
|
10
|
+
if options["database"].nil?
|
11
|
+
sql = "SELECT T.TABLE_SCHEMA, CAST((SUM(T.DATA_LENGTH+T.INDEX_LENGTH)/1024/1024) AS CHAR) AS SIZE_MB FROM INFORMATION_SCHEMA.TABLES AS T GROUP BY T.TABLE_SCHEMA UNION SELECT 'all_databases', CAST((SUM(T.DATA_LENGTH+T.INDEX_LENGTH)/1024/1024) AS CHAR) FROM INFORMATION_SCHEMA.TABLES AS T"
|
12
|
+
else
|
13
|
+
sql = "SELECT T.TABLE_NAME, CAST(((T.DATA_LENGTH+T.INDEX_LENGTH)/1024/1024) AS CHAR) AS SIZE_MB FROM INFORMATION_SCHEMA.TABLES AS T WHERE T.TABLE_SCHEMA = '#{options['database']}' UNION SELECT 'all_tables', CAST((SUM(T.DATA_LENGTH+T.INDEX_LENGTH)/1024/1024) AS CHAR) FROM INFORMATION_SCHEMA.TABLES AS T WHERE T.TABLE_SCHEMA = '#{options['database']}'"
|
14
|
+
end
|
15
|
+
client = Configuration.connect
|
16
|
+
printer = Printer.new(client.query(sql), true)
|
17
|
+
printer.print
|
18
|
+
end
|
19
|
+
|
20
|
+
desc "qcache_hit_rate", "qcache_hit_rate will print query cache hit rate"
|
21
|
+
def qcache_hit_rate
|
22
|
+
client = Configuration.connect
|
23
|
+
rate = client.query("SELECT (SELECT (SELECT G.VARIABLE_VALUE FROM INFORMATION_SCHEMA.GLOBAL_STATUS AS G WHERE G.VARIABLE_NAME = 'QCACHE_HITS')/(SELECT SUM(G.VARIABLE_VALUE) FROM INFORMATION_SCHEMA.GLOBAL_STATUS AS G WHERE G.VARIABLE_NAME IN ('QCACHE_HITS','QCACHE_INSERTS','QCACHE_NOT_CACHED')) * 100) AS rate")
|
24
|
+
print_rate(rate, 20)
|
25
|
+
end
|
26
|
+
|
27
|
+
desc "innodb_buffer_hit_rate", "innodb_buffer_hit_rate will print buffer hit rate"
|
28
|
+
def innodb_buffer_hit_rate
|
29
|
+
client = Configuration.connect
|
30
|
+
rate = client.query("SELECT (1 - ((SELECT G.VARIABLE_VALUE FROM INFORMATION_SCHEMA.GLOBAL_STATUS AS G WHERE G.VARIABLE_NAME = 'INNODB_BUFFER_POOL_READS')/(SELECT G.VARIABLE_VALUE FROM INFORMATION_SCHEMA.GLOBAL_STATUS AS G WHERE G.VARIABLE_NAME = 'INNODB_BUFFER_POOL_READ_REQUESTS'))) * 100 AS rate")
|
31
|
+
print_rate(rate, 90)
|
32
|
+
end
|
33
|
+
|
34
|
+
private
|
35
|
+
def print_rate(rate, threshold)
|
36
|
+
if rate.nil?
|
37
|
+
rate = "\e[31munknown\e[m"
|
38
|
+
else
|
39
|
+
rate = rate.first["rate"]
|
40
|
+
rate = rate >= threshold ? "\e[32m#{rate} %\e[m" : "\e[31m#{rate} %\e[m"
|
41
|
+
end
|
42
|
+
puts rate
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
data/lib/mycmd/configuration.rb
CHANGED
data/lib/mycmd/version.rb
CHANGED
@@ -3,20 +3,15 @@ require "spec_helper"
|
|
3
3
|
describe Mycmd::SettingsCommands do
|
4
4
|
describe "#search" do
|
5
5
|
let(:args) {["search", "param_name"]}
|
6
|
-
let(:conn_mock)
|
7
|
-
mock = double("connection mock")
|
8
|
-
mock.stub(:query).and_return(create_result)
|
9
|
-
mock
|
10
|
-
end
|
6
|
+
let(:conn_mock) {double("connection mock")}
|
11
7
|
|
12
|
-
before
|
8
|
+
before do
|
9
|
+
conn_mock.stub(:query).and_return(create_result)
|
10
|
+
Mycmd::Configuration.stub(:connect).and_return(conn_mock)
|
11
|
+
end
|
13
12
|
|
14
13
|
after do
|
15
|
-
expect(
|
16
|
-
capture(:stdout) {
|
17
|
-
Mycmd::SettingsCommands.start(args)
|
18
|
-
}
|
19
|
-
).not_to be_nil
|
14
|
+
expect(capture(:stdout){Mycmd::SettingsCommands.start(args)}).not_to be_nil
|
20
15
|
end
|
21
16
|
|
22
17
|
it "should call Configuration.connect" do
|
@@ -34,14 +29,32 @@ describe Mycmd::SettingsCommands do
|
|
34
29
|
|
35
30
|
describe "#memories" do
|
36
31
|
let(:args){["memories"]}
|
32
|
+
before {Mycmd::Configuration.stub(:get_variables).and_return(create_variables)}
|
33
|
+
|
34
|
+
after do
|
35
|
+
expect(capture(:stdout){Mycmd::SettingsCommands.start(args)}).not_to be_nil
|
36
|
+
end
|
37
|
+
|
38
|
+
it "should call Configuration.#get_variables" do
|
39
|
+
Mycmd::Configuration.should_receive(:get_variables).and_return(create_variables)
|
40
|
+
end
|
37
41
|
|
38
|
-
it "
|
39
|
-
Mycmd::
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
).
|
42
|
+
it "should call SettingsCommands#create_result_variables" do
|
43
|
+
Mycmd::Printer.stub(:new).and_return(double.as_null_object)
|
44
|
+
Mycmd::SettingsCommands.any_instance.should_receive(:create_result_variables).exactly(2).and_return([["key", "varue"], 310378496])
|
45
|
+
end
|
46
|
+
|
47
|
+
it "should call SettingsCommands#expected_memory_used" do
|
48
|
+
Mycmd::Printer.stub(:new).and_return(double.as_null_object)
|
49
|
+
Mycmd::SettingsCommands.any_instance.should_receive(:expected_memory_used).and_return([["key", "varue"], ["key", "value"]])
|
50
|
+
end
|
51
|
+
|
52
|
+
it "should call Printer.#print_title" do
|
53
|
+
Mycmd::Printer.should_receive(:print_title).exactly(3)
|
54
|
+
end
|
55
|
+
|
56
|
+
it "should create Printer object" do
|
57
|
+
Mycmd::Printer.should_receive(:new).exactly(3).and_return(double.as_null_object)
|
45
58
|
end
|
46
59
|
end
|
47
60
|
end
|
@@ -0,0 +1,113 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
|
3
|
+
describe Mycmd::StatusCommands do
|
4
|
+
let(:conn_mock) {double("connection mock")}
|
5
|
+
|
6
|
+
describe "#size" do
|
7
|
+
let(:ptr_mock) {double("printer mock").as_null_object}
|
8
|
+
|
9
|
+
before do
|
10
|
+
conn_mock.stub(:query).and_return(create_result)
|
11
|
+
Mycmd::Configuration.stub(:connect).and_return(conn_mock)
|
12
|
+
Mycmd::Printer.stub(:new).and_return(ptr_mock)
|
13
|
+
end
|
14
|
+
|
15
|
+
context "with not arguments" do
|
16
|
+
let(:args) {["size"]}
|
17
|
+
after {Mycmd::StatusCommands.start(args)}
|
18
|
+
|
19
|
+
it "should call Configuration.#connect" do
|
20
|
+
Mycmd::Configuration.should_receive(:connect).and_return(conn_mock)
|
21
|
+
end
|
22
|
+
|
23
|
+
it "should create Printer object" do
|
24
|
+
Mycmd::Printer.should_receive(:new).and_return(ptr_mock)
|
25
|
+
end
|
26
|
+
|
27
|
+
it "should call Printer#print" do
|
28
|
+
ptr_mock.should_receive(:print)
|
29
|
+
end
|
30
|
+
|
31
|
+
it "should output the size of all databases" do
|
32
|
+
conn_mock.should_receive(:query).with("SELECT T.TABLE_SCHEMA, CAST((SUM(T.DATA_LENGTH+T.INDEX_LENGTH)/1024/1024) AS CHAR) AS SIZE_MB FROM INFORMATION_SCHEMA.TABLES AS T GROUP BY T.TABLE_SCHEMA UNION SELECT 'all_databases', CAST((SUM(T.DATA_LENGTH+T.INDEX_LENGTH)/1024/1024) AS CHAR) FROM INFORMATION_SCHEMA.TABLES AS T")
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
it "should output the size of all tables" do
|
37
|
+
conn_mock.should_receive(:query).with("SELECT T.TABLE_NAME, CAST(((T.DATA_LENGTH+T.INDEX_LENGTH)/1024/1024) AS CHAR) AS SIZE_MB FROM INFORMATION_SCHEMA.TABLES AS T WHERE T.TABLE_SCHEMA = 'some_db' UNION SELECT 'all_tables', CAST((SUM(T.DATA_LENGTH+T.INDEX_LENGTH)/1024/1024) AS CHAR) FROM INFORMATION_SCHEMA.TABLES AS T WHERE T.TABLE_SCHEMA = 'some_db'")
|
38
|
+
Mycmd::StatusCommands.start(["size", "-d", "some_db"])
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
describe "#qcache_hit_rate" do
|
43
|
+
let(:args) {["qcache_hit_rate"]}
|
44
|
+
|
45
|
+
before do
|
46
|
+
conn_mock.stub(:query).and_return(double.as_null_object)
|
47
|
+
Mycmd::Configuration.stub(:connect).and_return(conn_mock)
|
48
|
+
end
|
49
|
+
|
50
|
+
after do
|
51
|
+
expect(capture(:stdout){Mycmd::StatusCommands.start(args)}).not_to be_nil
|
52
|
+
end
|
53
|
+
|
54
|
+
it "should call Configuration.#connect" do
|
55
|
+
Mycmd::Configuration.should_receive(:connect).and_return(conn_mock)
|
56
|
+
end
|
57
|
+
|
58
|
+
it "should output the query cache hit rate" do
|
59
|
+
conn_mock.should_receive(:query).with("SELECT (SELECT (SELECT G.VARIABLE_VALUE FROM INFORMATION_SCHEMA.GLOBAL_STATUS AS G WHERE G.VARIABLE_NAME = 'QCACHE_HITS')/(SELECT SUM(G.VARIABLE_VALUE) FROM INFORMATION_SCHEMA.GLOBAL_STATUS AS G WHERE G.VARIABLE_NAME IN ('QCACHE_HITS','QCACHE_INSERTS','QCACHE_NOT_CACHED')) * 100) AS rate").and_return(double.as_null_object)
|
60
|
+
end
|
61
|
+
|
62
|
+
it "should output red color of char if rate is unknown" do
|
63
|
+
conn_mock.should_receive(:query).and_return(nil)
|
64
|
+
expect(capture(:stdout){Mycmd::StatusCommands.start(args)}).to eq("\e[31munknown\e[m\n")
|
65
|
+
end
|
66
|
+
|
67
|
+
it "should output red color of char if rate < 20" do
|
68
|
+
conn_mock.should_receive(:query).and_return([{'rate' => 10}])
|
69
|
+
expect(capture(:stdout){Mycmd::StatusCommands.start(args)}).to eq("\e[31m10 %\e[m\n")
|
70
|
+
end
|
71
|
+
|
72
|
+
it "should output green color of char if rate >= 20" do
|
73
|
+
conn_mock.should_receive(:query).and_return([{'rate' => 20}])
|
74
|
+
expect(capture(:stdout){Mycmd::StatusCommands.start(args)}).to eq("\e[32m20 %\e[m\n")
|
75
|
+
end
|
76
|
+
end
|
77
|
+
|
78
|
+
describe "#innodb_buffer_hit_rate" do
|
79
|
+
let(:args) {["innodb_buffer_hit_rate"]}
|
80
|
+
|
81
|
+
before do
|
82
|
+
conn_mock.stub(:query).and_return(double.as_null_object)
|
83
|
+
Mycmd::Configuration.stub(:connect).and_return(conn_mock)
|
84
|
+
end
|
85
|
+
|
86
|
+
after do
|
87
|
+
expect(capture(:stdout){Mycmd::StatusCommands.start(args)}).not_to be_nil
|
88
|
+
end
|
89
|
+
|
90
|
+
it "should call Configuration.#connect" do
|
91
|
+
Mycmd::Configuration.should_receive(:connect).and_return(conn_mock)
|
92
|
+
end
|
93
|
+
|
94
|
+
it "should output the innodb buffer hit rate" do
|
95
|
+
conn_mock.should_receive(:query).with("SELECT (1 - ((SELECT G.VARIABLE_VALUE FROM INFORMATION_SCHEMA.GLOBAL_STATUS AS G WHERE G.VARIABLE_NAME = 'INNODB_BUFFER_POOL_READS')/(SELECT G.VARIABLE_VALUE FROM INFORMATION_SCHEMA.GLOBAL_STATUS AS G WHERE G.VARIABLE_NAME = 'INNODB_BUFFER_POOL_READ_REQUESTS'))) * 100 AS rate").and_return(double.as_null_object)
|
96
|
+
end
|
97
|
+
|
98
|
+
it "should output red color of char if rate is unknown" do
|
99
|
+
conn_mock.should_receive(:query).and_return(nil)
|
100
|
+
expect(capture(:stdout){Mycmd::StatusCommands.start(args)}).to eq("\e[31munknown\e[m\n")
|
101
|
+
end
|
102
|
+
|
103
|
+
it "should output red color of char if rate < 90" do
|
104
|
+
conn_mock.should_receive(:query).and_return([{'rate' => 80}])
|
105
|
+
expect(capture(:stdout){Mycmd::StatusCommands.start(args)}).to eq("\e[31m80 %\e[m\n")
|
106
|
+
end
|
107
|
+
|
108
|
+
it "should output green color of char if rate >= 90" do
|
109
|
+
conn_mock.should_receive(:query).and_return([{'rate' => 90}])
|
110
|
+
expect(capture(:stdout){Mycmd::StatusCommands.start(args)}).to eq("\e[32m90 %\e[m\n")
|
111
|
+
end
|
112
|
+
end
|
113
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: mycmd
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- i2bskn
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2013-
|
11
|
+
date: 2013-09-12 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: thor
|
@@ -100,6 +100,7 @@ files:
|
|
100
100
|
- lib/mycmd/cli.rb
|
101
101
|
- lib/mycmd/clis/config_commands.rb
|
102
102
|
- lib/mycmd/clis/settings_commands.rb
|
103
|
+
- lib/mycmd/clis/status_commands.rb
|
103
104
|
- lib/mycmd/configuration.rb
|
104
105
|
- lib/mycmd/printer.rb
|
105
106
|
- lib/mycmd/version.rb
|
@@ -107,6 +108,7 @@ files:
|
|
107
108
|
- spec/mycmd/cli_spec.rb
|
108
109
|
- spec/mycmd/clis/config_commands_spec.rb
|
109
110
|
- spec/mycmd/clis/settings_commands_spec.rb
|
111
|
+
- spec/mycmd/clis/status_commands_spec.rb
|
110
112
|
- spec/mycmd/configuration_spec.rb
|
111
113
|
- spec/mycmd/printer_spec.rb
|
112
114
|
- spec/spec_helper.rb
|
@@ -131,7 +133,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
131
133
|
version: '0'
|
132
134
|
requirements: []
|
133
135
|
rubyforge_project:
|
134
|
-
rubygems_version: 2.0.
|
136
|
+
rubygems_version: 2.0.6
|
135
137
|
signing_key:
|
136
138
|
specification_version: 4
|
137
139
|
summary: MySQL command line tool
|
@@ -139,6 +141,7 @@ test_files:
|
|
139
141
|
- spec/mycmd/cli_spec.rb
|
140
142
|
- spec/mycmd/clis/config_commands_spec.rb
|
141
143
|
- spec/mycmd/clis/settings_commands_spec.rb
|
144
|
+
- spec/mycmd/clis/status_commands_spec.rb
|
142
145
|
- spec/mycmd/configuration_spec.rb
|
143
146
|
- spec/mycmd/printer_spec.rb
|
144
147
|
- spec/spec_helper.rb
|