sms-logparser 0.12.4 → 0.13.0

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: ad817588ffc290e40e11563db5d856da73a1c058
4
- data.tar.gz: 6a68fbf74e90aed8127c356cb37d2a6e48762172
3
+ metadata.gz: e157cd8f8e09ea804bd41f02d9a6023338bd1f4a
4
+ data.tar.gz: dd7afb96c6374b04b3e8bacb1d2051f8ab37e9ba
5
5
  SHA512:
6
- metadata.gz: af70780b477df0155f6fe10d06ea02327f7879c4cca38f413778345f3318f531a60a84de91b27124c874ee6911f402520953b9dded8c96de31fd775a1e195d3d
7
- data.tar.gz: 099385b75fb60fa67bc62d002c84d35007870a727af9b4a534cd3332a5eab6a3b30a95e515c6dc42b3c31b633fe3d87e2b4557e63b967879e34b4d93a3af058e
6
+ metadata.gz: 1ea303b8e1abf537c7df2b3ebbbaa508bd50ee3e4ea1d5ff2286184067f40f002bbdcb64154247fac046a9580ffc7cbdd4ba1c2e704d022c563077a802c5c77f
7
+ data.tar.gz: 46b47d8a357cc26ea9362f06b320cbba237b2ed0decc110a37fbf06ac3c5f0c7747a95ac7484b836a2b4bed7f4bc63a010ababd0d98bc3fb6fc16c5214a86d5a
@@ -134,6 +134,28 @@ module SmsLogparser
134
134
  say(e.backtrace.join("\n"), :yellow) if options[:debug]
135
135
  end
136
136
 
137
+ desc "clean-up [mode]", "Clean up the database - delete unused records"
138
+ def clean_up(mode = "all")
139
+ mysql = Mysql.new(options)
140
+ case mode
141
+ when "all"
142
+ mysql.clean_up_parser_table
143
+ mysql.clean_up_events_table
144
+ logger.info("All tables have been cleaned up.")
145
+ when "events"
146
+ mysql.clean_up_events_table
147
+ logger.info("Events table has been cleaned up.")
148
+ when "parser"
149
+ mysql.clean_up_events_table
150
+ logger.info("Parser table has been cleaned up.")
151
+ else
152
+ logger.warn("Mode '#{mode}' not allowed. Allowed modes are 'events', 'parser' or 'all'.")
153
+ end
154
+ SmsLogparser::Loggster.instance.close
155
+ rescue => e
156
+ logger.fatal e
157
+ end
158
+
137
159
  desc "setup", "Create the parser table to track the last logs parsed"
138
160
  option :force, type: :boolean, default: false, aliases: %w(-f),
139
161
  desc: "Drop an existing table if it exists"
@@ -3,9 +3,10 @@ module SmsLogparser
3
3
 
4
4
  def initialize(options)
5
5
  @options = options
6
- @query_limit = options[:query_limit] || 1000
6
+ @query_limit = options[:query_limit] || 5000
7
7
  @client = client
8
8
  @logger = SmsLogparser::Loggster.instance
9
+ @max_retries = 5
9
10
  end
10
11
 
11
12
  def client
@@ -67,14 +68,29 @@ module SmsLogparser
67
68
  end
68
69
 
69
70
  def write_parse_result(options)
70
- client.query(
71
- "UPDATE sms_logparser_runs SET\
72
- last_event_id = #{options[:last_event_id]},\
73
- match_count = #{options[:match_count]},\
74
- status = #{options[:status]},\
75
- run_time = #{options[:run_time]}\
76
- WHERE id = #{options[:id]}"
77
- )
71
+ query = %Q{
72
+ UPDATE sms_logparser_runs
73
+ SET last_event_id = #{options[:last_event_id]},
74
+ match_count = #{options[:match_count]},
75
+ status = #{options[:status]},
76
+ run_time = #{options[:run_time]}
77
+ WHERE id = #{options[:id]};
78
+ }.gsub(/\s+/, " ").strip
79
+ retries = 0
80
+ begin
81
+ client.query(query)
82
+ rescue Mysql2::Error => exception
83
+ @logger.error exception
84
+ if (retries += 1) > @max_retries
85
+ @logger.fatal "Can't write parser result to database after #{@max_retries} retries. (#{@query})"
86
+ raise
87
+ end
88
+ sleeptime = @max_retries * retries
89
+ @logger.info "Retry writing parser results in #{sleeptime} seconds."
90
+ sleep sleeptime
91
+ retry
92
+ end
93
+ true
78
94
  end
79
95
 
80
96
  def get_entries(options={})
@@ -90,11 +106,27 @@ module SmsLogparser
90
106
 
91
107
  def get_last_parse_id
92
108
  last_parse = client.query(
93
- "SELECT last_event_id FROM sms_logparser_runs ORDER BY id DESC LIMIT 1"
109
+ "SELECT last_event_id FROM sms_logparser_runs ORDER BY id DESC LIMIT 1;"
94
110
  )
95
111
  last_parse.first ? last_parse.first['last_event_id'] : 0
96
112
  end
97
113
 
114
+ def clean_up_events_table
115
+ last_parse = client.query(
116
+ "SELECT last_event_id FROM sms_logparser_runs WHERE status = 0 ORDER BY id DESC LIMIT 1;"
117
+ )
118
+ if last_parse.first
119
+ id = last_parse.first['last_event_id']
120
+ client.query("DELETE FROM sms_logparser_runs WHERE id < #{id};")
121
+ end
122
+ true
123
+ end
124
+
125
+ def clean_up_parser_table(keep_days = 7)
126
+ time = (Time.now - 3600 * 24 * keep_days.to_i).strftime("%Y-%m-%d %H:%M:%S")
127
+ client.query("DELETE FROM sms_logparser_runs WHERE run_at < '#{time}';")
128
+ end
129
+
98
130
  private
99
131
 
100
132
  def get_max_id(last_id, user_limit = nil)
@@ -118,20 +150,20 @@ module SmsLogparser
118
150
 
119
151
  def get_last_event_id
120
152
  last_event = client.query(
121
- "SELECT MAX(ID) as max_id FROM SystemEvents"
153
+ "SELECT MAX(ID) as max_id FROM SystemEvents;"
122
154
  )
123
155
  last_event.first ? last_event.first['max_id'] : 0
124
156
  end
125
157
 
126
158
  def parser_table_exists?
127
159
  client.query(
128
- "SHOW TABLES LIKE 'sms_logparser_runs'"
160
+ "SHOW TABLES LIKE 'sms_logparser_runs';"
129
161
  ).size > 0
130
162
  end
131
163
 
132
164
  def drop_parser_table
133
165
  return false unless parser_table_exists?
134
- client.query("DROP TABLE sms_logparser_runs")
166
+ client.query("DROP TABLE sms_logparser_runs;")
135
167
  true
136
168
  end
137
169
 
@@ -40,7 +40,7 @@ module SmsLogparser
40
40
  # for mobile browser detection
41
41
  def self.get_traffic_type(user_agent)
42
42
  case user_agent
43
- when /.*(iTunes).*/
43
+ when /.*(iTunes).*/i
44
44
  'TRAFFIC_PODCAST'
45
45
  when /.*(Mobi|IEMobile|Mobile Safari|iPhone|iPod|iPad|Android|BlackBerry|Opera Mini).*/
46
46
  'TRAFFIC_MOBILE'
@@ -1,3 +1,3 @@
1
1
  module SmsLogparser
2
- VERSION = "0.12.4"
2
+ VERSION = "0.13.0"
3
3
  end
@@ -96,5 +96,5 @@ INSERT INTO Syslog.SystemEvents (FromHost, Message) VALUES ("pcache", '- - [25/F
96
96
  INSERT INTO Syslog.SystemEvents (FromHost, Message) VALUES ("pcache", '- - [25/Feb/2014:17:28:57 +0100] \"GET /content/2/719/54986/simvid_1.f4v HTTP/1.1\" 200 6741309 \"-\" \"Mozilla/5.0 (Macintosh; Intel Mac OS X 10.9; rv:27.0) Gecko/20100101 Firefox/27.0\"');
97
97
  INSERT INTO Syslog.SystemEvents (FromHost, Message) VALUES ("pcache", '- - [25/Feb/2014:17:28:57 +0100] \"GET /content/2/719/54986/simvid_1.f4v HTTP/1.1\" 200 6741309 \"-\" \"Mozilla/5.0 (Macintosh; Intel Mac OS X 10.9; rv:27.0) Gecko/20100101 Firefox/27.0\"');
98
98
  INSERT INTO Syslog.SystemEvents (FromHost, Message) VALUES ("pcache", '- - [25/Feb/2014:17:28:57 +0100] \"GET /content/2/719/54986/simvid_1.f4v HTTP/1.1\" 200 6741309 \"-\" \"Mozilla/5.0 (Macintosh; Intel Mac OS X 10.9; rv:27.0) Gecko/20100101 Firefox/27.0\"');
99
- INSERT INTO Syslog.SystemEvents (FromHost, Message) VALUES ("pcache", '- - [25/Feb/2014:17:28:57 +0100] \"GET /content/2/719/54986/simvid_1.f4v HTTP/1.1\" 200 6741309 \"-\" \"Mozilla/5.0 (Macintosh; Intel Mac OS X 10.9; rv:27.0) Gecko/20100101 Firefox/27.0\"');
100
- INSERT INTO Syslog.SystemEvents (FromHost, Message) VALUES ("pcache", '- - [25/Feb/2014:17:28:57 +0100] \"GET /content/2/719/54986/simvid_1.f4v HTTP/1.1\" 200 6741309 \"-\" \"Mozilla/5.0 (Macintosh; Intel Mac OS X 10.9; rv:27.0) Gecko/20100101 Firefox/27.0\"');
99
+ INSERT INTO Syslog.SystemEvents (FromHost, Message) VALUES ("pcache", '- - [25/Feb/2014:17:28:57 +0100] \"GET /content/2/719/54986/dadad.mp3 HTTP/1.1\" 200 6741309 \"-\" \"iTunes/9.0.3 (Macintosh; U; Intel Mac OS X 10_6_2; en-ca)\"');
100
+ INSERT INTO Syslog.SystemEvents (FromHost, Message) VALUES ("pcache", '- - [25/Feb/2014:17:28:57 +0100] \"GET /content/2/719/54986/bla.mp4 HTTP/1.1\" 200 6741309 \"-\" \"itunes/9.0.2 (Macintosh; Intel Mac OS X 10.4.11) AppleWebKit/531.21.8\"');
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sms-logparser
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.12.4
4
+ version: 0.13.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - niwo
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-04-16 00:00:00.000000000 Z
11
+ date: 2014-04-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler