sms-logparser 0.6.0 → 0.6.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/sms-logparser/cli.rb +65 -72
- data/lib/sms-logparser/mysql.rb +1 -1
- data/lib/sms-logparser/version.rb +1 -1
- data/sms-logparser.gemspec +1 -0
- data/spec/api_spec.rb +1 -1
- data/spec/cli_spec.rb +11 -3
- data/spec/spec_helper.rb +3 -0
- metadata +15 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 763fefca222c68deeee291c3afefc4b86a9b5291
|
4
|
+
data.tar.gz: 6b369ebcb0beda9c45045118c3891d69f4930a71
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 06b1e2e3b4e55770625e8af0053243d1b48eda441432b3e8eb5d397d9b39dab759f53a9d66d32248be16b817da5d97d8550e46126fd0c3bc73570c964ad2f0d1
|
7
|
+
data.tar.gz: 659e7def844b3b476acaa607469c76e3f3d4c7ca2157b05c44e68c847088e7affb945aef3e9737bb538100b7e16ff24a90ad552035c350e26d6f9ba6d37858fb
|
data/lib/sms-logparser/cli.rb
CHANGED
@@ -2,10 +2,7 @@ module SmsLogparser
|
|
2
2
|
class Cli < Thor
|
3
3
|
require 'yaml'
|
4
4
|
|
5
|
-
STATUS = {
|
6
|
-
:ok => 0,
|
7
|
-
:api_error => 1
|
8
|
-
}
|
5
|
+
STATUS = {ok: 0, api_error: 1}
|
9
6
|
|
10
7
|
class_option :config,
|
11
8
|
default: File.join(Dir.home, '.sms-logparser.yml'),
|
@@ -56,51 +53,37 @@ module SmsLogparser
|
|
56
53
|
default: false
|
57
54
|
def parse
|
58
55
|
say "Starting the parser...", :green
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
end
|
76
|
-
end
|
77
|
-
end
|
56
|
+
mysql = Mysql.new(options)
|
57
|
+
state = {
|
58
|
+
last_event_id: mysql.get_last_parse_id,
|
59
|
+
match_count: 0,
|
60
|
+
status: STATUS[:ok],
|
61
|
+
run_at: Time.now,
|
62
|
+
run_time: 0.0
|
63
|
+
}
|
64
|
+
api = Api.new(options)
|
65
|
+
mysql.get_entries(last_id: state[:last_event_id], limit: options[:limit]) do |entries|
|
66
|
+
entries.each do |entry|
|
67
|
+
if data = Parser.extract_data_from_msg(entry['Message'])
|
68
|
+
uris = api.send(data)
|
69
|
+
state[:last_event_id] = entry['ID']
|
70
|
+
state[:match_count] += 1
|
71
|
+
verbose_parser_output(data, uris, entry) if options[:verbose]
|
78
72
|
end
|
79
|
-
rescue => e
|
80
|
-
say "Error: #{e.message}", :red
|
81
|
-
say "Aborting parser run...", :red
|
82
|
-
status = STATUS[:api_error]
|
83
|
-
ensure
|
84
|
-
mysql.write_parse_result(
|
85
|
-
last_event_id: last_id,
|
86
|
-
match_count: count,
|
87
|
-
status: status,
|
88
|
-
run_at: start_time,
|
89
|
-
run_time: (Time.now - start_time).round(2)
|
90
|
-
) unless options[:simulate]
|
91
73
|
end
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
74
|
+
end
|
75
|
+
rescue => e
|
76
|
+
say "Error: #{e.message}", :red
|
77
|
+
say "Aborting parser run...", :red
|
78
|
+
state[:status] = STATUS[:api_error]
|
79
|
+
ensure
|
80
|
+
begin
|
81
|
+
state[:run_time] = (Time.now - state[:run_at]).round(2)
|
82
|
+
mysql.write_parse_result(state) unless options[:simulate]
|
83
|
+
print_parse_results(state)
|
101
84
|
rescue => e
|
102
85
|
say "Error: #{e.message}", :red
|
103
|
-
say
|
86
|
+
say(e.backtrace.join("\n"), :yellow) if options[:debug]
|
104
87
|
end
|
105
88
|
end
|
106
89
|
|
@@ -111,26 +94,25 @@ module SmsLogparser
|
|
111
94
|
aliases: %w(-n),
|
112
95
|
desc: "Number of results to display"
|
113
96
|
def history
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
]
|
126
|
-
end
|
127
|
-
print_table table
|
128
|
-
else
|
129
|
-
say "No parser runs found in the database."
|
97
|
+
runs = Mysql.new(options).last_runs(options[:results])
|
98
|
+
if runs && runs.size > 0
|
99
|
+
table = [%w(run_at count last_id status run_time)]
|
100
|
+
runs.to_a.reverse.each do |run|
|
101
|
+
table << [
|
102
|
+
run['run_at'].strftime('%d.%d.%Y %T'),
|
103
|
+
run['match_count'],
|
104
|
+
run['last_event_id'],
|
105
|
+
STATUS.key(run['status']).upcase,
|
106
|
+
"#{run['run_time']}s"
|
107
|
+
]
|
130
108
|
end
|
131
|
-
|
132
|
-
|
109
|
+
print_table table
|
110
|
+
else
|
111
|
+
say "No parser runs found in the database."
|
133
112
|
end
|
113
|
+
rescue => e
|
114
|
+
say "Error: #{e.message}", :red
|
115
|
+
exit 1
|
134
116
|
end
|
135
117
|
|
136
118
|
desc "setup", "Create the parser table to track the last logs parsed"
|
@@ -140,16 +122,15 @@ module SmsLogparser
|
|
140
122
|
aliases: %w(-f),
|
141
123
|
desc: "Drop an existing table if it exists"
|
142
124
|
def setup
|
143
|
-
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
|
148
|
-
say "Table already exists.", :yellow
|
149
|
-
end
|
150
|
-
rescue => e
|
151
|
-
say "Error: #{e.message}", :red
|
125
|
+
case Mysql.new(options).create_parser_table(options[:force])
|
126
|
+
when 0
|
127
|
+
say "OK, table created.", :green
|
128
|
+
when 1
|
129
|
+
say "Table already exists.", :yellow
|
152
130
|
end
|
131
|
+
rescue => e
|
132
|
+
say "Error: #{e.message}", :red
|
133
|
+
exit 1
|
153
134
|
end
|
154
135
|
|
155
136
|
no_commands do
|
@@ -165,6 +146,18 @@ module SmsLogparser
|
|
165
146
|
puts
|
166
147
|
end
|
167
148
|
|
149
|
+
def print_parse_results(res)
|
150
|
+
say "Started:\t", :cyan
|
151
|
+
say res[:run_at].strftime('%d.%d.%Y %T')
|
152
|
+
say "Runtime:\t", :cyan
|
153
|
+
say "#{res[:run_time]}s"
|
154
|
+
say "Status:\t\t", :cyan
|
155
|
+
say STATUS.key(res[:status]).upcase
|
156
|
+
action = options[:simulate] ? "found" : "sent"
|
157
|
+
say("Events #{action}:\t", :cyan)
|
158
|
+
say res[:match_count]
|
159
|
+
end
|
160
|
+
|
168
161
|
def options
|
169
162
|
original_options = super
|
170
163
|
filename = original_options[:config] || File.join(Dir.home, '.sms-logparser.yml')
|
data/lib/sms-logparser/mysql.rb
CHANGED
data/sms-logparser.gemspec
CHANGED
data/spec/api_spec.rb
CHANGED
data/spec/cli_spec.rb
CHANGED
@@ -4,6 +4,9 @@ describe SmsLogparser::Cli do
|
|
4
4
|
before do
|
5
5
|
TestHelper.create_test_db
|
6
6
|
TestHelper.create_sylog_db_table
|
7
|
+
stub_request(:post, /.*locahost.*/).
|
8
|
+
with(headers: {'Accept'=>'*/*', 'User-Agent'=>'Ruby'}).
|
9
|
+
to_return(status: 200, body: "stubbed response", headers: {})
|
7
10
|
end
|
8
11
|
|
9
12
|
after do
|
@@ -11,8 +14,10 @@ describe SmsLogparser::Cli do
|
|
11
14
|
end
|
12
15
|
|
13
16
|
it "can create the parser_runs database table" do
|
17
|
+
parser = TestHelper.sms_logparser
|
18
|
+
parser.options[:force] = true
|
14
19
|
out, err = capture_io do
|
15
|
-
|
20
|
+
parser.setup
|
16
21
|
end
|
17
22
|
out.must_match /OK.*/
|
18
23
|
end
|
@@ -21,8 +26,9 @@ describe SmsLogparser::Cli do
|
|
21
26
|
TestHelper.seed_db(10)
|
22
27
|
parser = TestHelper.sms_logparser
|
23
28
|
parser.options[:simulate] = true
|
29
|
+
#parser.options[:api_base_url] = "http://localhost/creator/rest/"
|
24
30
|
out, err = capture_io do
|
25
|
-
|
31
|
+
parser.setup
|
26
32
|
parser.parse
|
27
33
|
end
|
28
34
|
out.must_match /\s+10$/
|
@@ -44,8 +50,10 @@ describe SmsLogparser::Cli do
|
|
44
50
|
TestHelper.seed_db(1)
|
45
51
|
parser = TestHelper.sms_logparser
|
46
52
|
parser.options[:simulate] = true
|
53
|
+
parser.options[:force] = true
|
54
|
+
|
47
55
|
out, err = capture_io do
|
48
|
-
|
56
|
+
parser.setup
|
49
57
|
parser.parse
|
50
58
|
parser.history
|
51
59
|
end
|
data/spec/spec_helper.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: sms-logparser
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.6.
|
4
|
+
version: 0.6.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- niwo
|
@@ -38,6 +38,20 @@ dependencies:
|
|
38
38
|
- - '>='
|
39
39
|
- !ruby/object:Gem::Version
|
40
40
|
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: webmock
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - '>='
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - '>='
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
41
55
|
- !ruby/object:Gem::Dependency
|
42
56
|
name: thor
|
43
57
|
requirement: !ruby/object:Gem::Requirement
|