sms-logparser 0.12.0 → 0.12.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: be3b90122d60da5a8342511ee4a4aa3879c549a7
4
- data.tar.gz: 9928653741ef481378f0761ba5f5d5f25c83bc74
3
+ metadata.gz: 8392eba52e67bfc44639dc8b2e916ca9497335e2
4
+ data.tar.gz: 3409ba5f83db6a33334d58bbd2ccb1f00e1165ae
5
5
  SHA512:
6
- metadata.gz: bc1cb0028bd7ac7392a7e504aebd6f471b8f636f0983872f7e2f7e23b9dba18eb11359f34714efaf2d49b33aefa849583821a8b003af0bfe82db52961a23830f
7
- data.tar.gz: 139425fa7c82772ab61af0c2aefe88acec215177f559f770da307b8df00c4e848e4c834704b73d5c8ea80b47fbcc8fc1280b6d19f0498f519f80b9715151ea98
6
+ metadata.gz: b3350474625d5cc675cfb18b5e4243ce81e6912ef123df9a86d9d7bef67a59b831926678288684e683b58043b29314ee7ff9a8794566319ece8f2f6045f12148
7
+ data.tar.gz: ac650bc8109cede2eb992035c94e0ae6936633e21cb3d408ece3c1a9a00962204bcf0e0a18a41208c1aa759bff075344a42a3bdf59e5d28b060a904531215dd5
@@ -14,7 +14,6 @@ module SmsLogparser
14
14
  end
15
15
 
16
16
 
17
-
18
17
  def send(data)
19
18
  requests = build_urls(data)
20
19
  return requests if @options[:simulate]
@@ -37,6 +36,45 @@ module SmsLogparser
37
36
  requests
38
37
  end
39
38
 
39
+ def send_from_queue(data_sets)
40
+ queue = Queue.new
41
+ semaphore = Mutex.new
42
+ data_sets.each {|set| queue << set }
43
+ threads = 4.times.map do
44
+ Thread.new do
45
+ while !queue.empty?
46
+ begin
47
+ data = queue.pop
48
+ url = @base_path + [
49
+ data[:customer_id],
50
+ data[:author_id],
51
+ data[:project_id],
52
+ data[:type],
53
+ data[:value]
54
+ ].join('/')
55
+ if @options[:simulate]
56
+ semaphore.synchronize {
57
+ yield url, 0
58
+ }
59
+ break
60
+ end
61
+ response = @connection.post(url)
62
+ rescue => e
63
+ raise RuntimeError, "Can't send request to #{url}. #{e.message}", caller
64
+ end
65
+ unless @accepted_responses.include?(response.status)
66
+ msg = "Received HTTP status #{response.status} from API. Only accepting #{@accepted_responses.join(', ')}."
67
+ raise RuntimeError, msg, caller
68
+ end
69
+ semaphore.synchronize {
70
+ yield url, response.status
71
+ }
72
+ end
73
+ end
74
+ end
75
+ threads.each {|thread| thread.join }
76
+ end
77
+
40
78
  def build_urls(data)
41
79
  requests = []
42
80
  path = @base_path + [data[:customer_id], data[:author_id], data[:project_id]].join('/')
@@ -88,24 +88,74 @@ module SmsLogparser
88
88
  end
89
89
 
90
90
  desc "cached_pase", "Check the database for pcache logs and put them into the cache"
91
+ option :api_base_url, aliases: %w(-a),
92
+ desc: "Base path of the SMS API (Default: http://localhost:8080/creator/rest/)"
93
+ option :api_key, aliases: %w(-k), desc: "SMS API Key"
94
+ option :simulate, type: :boolean, aliases: %w(-s),
95
+ desc: "Dry run without submitting any data"
96
+ option :verbose, type: :boolean, aliases: %w(-v), desc: "Verbose output"
91
97
  option :limit, type: :numeric, aliases: %w(-L), desc: "Limit the number of entries to query"
98
+ option :accepted_api_responses, type: :array, aliases: %w(-r),
99
+ desc: "API HTTP responses which are accepted (Default: only accept 200)."
92
100
  def cached_parse
101
+ start_message = "Parser started"
102
+ start_message += options[:simulate] ? " in simulation mode." : "."
103
+ logger.info(start_message)
104
+ mysql = Mysql.new(options)
105
+ if !options[:simulate] && mysql.parser_running?
106
+ logger.warn("Exit. Another instance of the parser is already running.")
107
+ exit!
108
+ end
109
+ state = {
110
+ last_event_id: mysql.get_last_parse_id,
111
+ match_count: 0,
112
+ status: STATUS[:running],
113
+ started_at: Time.now,
114
+ run_time: 0.0
115
+ }
116
+ state = mysql.start_run(state) unless options[:simulate]
93
117
  cache = DataCache.new
94
118
  mysql = Mysql.new(options)
95
119
  say "Getting entries from database..."
96
- mysql.get_entries(last_id: mysql.get_last_parse_id, limit: options[:limit]) do |entries|
120
+ mysql.get_entries(last_id: state[:last_event_id], limit: options[:limit]) do |entries|
97
121
  entries.each do |entry|
98
122
  Parser.extract_data_from_msg(entry['Message']) do |data|
99
123
  if data
100
124
  cache.add(data)
101
- say "Cached data ", :magenta
102
- say data
125
+ logger.debug {"Cached data: #{data}"}
126
+ state[:match_count] += 1
127
+ state[:last_event_id] = entry['ID']
103
128
  end
104
129
  end
105
130
  end
106
131
  end
107
- puts
108
- puts cache.cache
132
+ api = Api.new(options)
133
+ api.send_from_queue(cache.data_sets) do |url, response|
134
+ say "#{url} (#{response})"
135
+ end
136
+ rescue SystemExit, Interrupt
137
+ logger.error("Received an interrupt. Stopping the parser run.")
138
+ state[:status] = STATUS[:interrupted] if state
139
+ rescue => e
140
+ logger.error "Aborting the parser run."
141
+ logger.error e
142
+ state[:status] = STATUS[:api_error] if state
143
+ else
144
+ state[:status] = STATUS[:ok]
145
+ ensure
146
+ begin
147
+ if mysql && state
148
+ state[:run_time] = (Time.now - state[:started_at]).round(2)
149
+ if state[:id]
150
+ mysql.write_parse_result(state) unless options[:simulate]
151
+ end
152
+ log_parse_results(state)
153
+ SmsLogparser::Loggster.instance.close
154
+ end
155
+ rescue => e
156
+ logger.fatal e
157
+ end
158
+
109
159
  end
110
160
 
111
161
  desc "history", "List the last paser runs"
@@ -20,6 +20,23 @@ module SmsLogparser
20
20
  @cache
21
21
  end
22
22
 
23
+ def data_sets
24
+ sets = []
25
+ types = %w(TRAFFIC_PODCAST TRAFFIC_MOBILE TRAFFIC_WEBCAST VISITORS_PODCAST VISITORS_MOBILE VISITORS_WEBCAST)
26
+ @cache.each do |key, values|
27
+ types.each do |type|
28
+ sets << {
29
+ customer_id: values[:customer_id],
30
+ author_id: values[:author_id],
31
+ project_id: values[:project_id],
32
+ type: type,
33
+ value: values[type]
34
+ } if values[type]
35
+ end
36
+ end
37
+ sets
38
+ end
39
+
23
40
  private
24
41
 
25
42
  def initialize_value(data)
@@ -1,3 +1,3 @@
1
1
  module SmsLogparser
2
- VERSION = "0.12.0"
2
+ VERSION = "0.12.1"
3
3
  end
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.12.0
4
+ version: 0.12.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - niwo