getaround_utils 0.1.19 → 0.1.20

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
  SHA256:
3
- metadata.gz: f23d197d155aa4f24f2e2d745dda9d8b1c945de036cdf619155abbf226adbeb1
4
- data.tar.gz: 2517396bd5ed9a98e375d8edc390c3c1dcd79c5a33f347344d0f6125be11af57
3
+ metadata.gz: ac89bb57e946209df2cd90a27662dd6eafe09eaf1ad75c70e0120843600eaa73
4
+ data.tar.gz: b5e42696a498a174113437cf8350b1eb742391f74588621a3bae00e12cab7fc4
5
5
  SHA512:
6
- metadata.gz: 43beef4a44ae0a1e99fe96d95ce0a3dff0597e5e71ae522adbb67f4204a01d65ff76dac4b7e07575eca10165fa0f059dca42b0061bb7193f39692a10a9a187d6
7
- data.tar.gz: a05094694bd7d64b80f79425366b3ac6440107e1750a421fdf4449f4e68d34efce4646ba6704a4effb4e928ce4d55441944b8833fc49693cb9a17bfeeeb52bc7
6
+ metadata.gz: 20debe44bc0c094ce9dcc3cde822eb7565c6c399cc4ba5849c2a6c5bbc249c9fa96e8fda50648c9974c03b1177030b3880aab5df65c3eba6ea9632845e796d69
7
+ data.tar.gz: 3017542f9d2b157a8f6ee9637c5659212a3fa3ad8803975ac2fd8b76e255487183e7b671944a6ec8a9b841890917dfb0c3ae7f3973eba0966257d7a9089c0161
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- getaround_utils (0.1.19)
4
+ getaround_utils (0.1.20)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
@@ -2,4 +2,5 @@ module GetaroundUtils::Utils
2
2
  autoload :AsyncQueue, 'getaround_utils/utils/async_queue'
3
3
  autoload :DeepKeyValue, 'getaround_utils/utils/deep_key_value'
4
4
  autoload :HttpReporter, 'getaround_utils/utils/http_reporter'
5
+ autoload :CapturReporter, 'getaround_utils/utils/captur_reporter'
5
6
  end
@@ -2,58 +2,48 @@ module GetaroundUtils; end
2
2
  module GetaroundUtils::Utils; end
3
3
 
4
4
  class GetaroundUtils::Utils::AsyncQueue
5
- class << self
6
- include GetaroundUtils::Mixins::Loggable
5
+ include GetaroundUtils::Mixins::Loggable
7
6
 
8
- MAX_QUEUE_SIZE = 100
9
- MUTEX = Mutex.new
7
+ MAX_QUEUE_SIZE = 1000
8
+ BUFFER_SIZE = 50
10
9
 
11
- def perform
12
- raise NotImplementedError
13
- end
10
+ def initialize
11
+ @queue = []
12
+ @mutex = Mutex.new
13
+ @closed = false
14
+ @worker = Thread.new(&method(:thread_run))
15
+ at_exit { terminate }
16
+ end
14
17
 
15
- def perform_async(*args)
16
- start_once!
18
+ def perform
19
+ raise NotImplementedError
20
+ end
17
21
 
18
- if @queue.size > MAX_QUEUE_SIZE
19
- loggable('warn', 'Queue is overflowing')
20
- return
22
+ def push(payload)
23
+ @mutex.synchronize do
24
+ if @queue.size >= MAX_QUEUE_SIZE
25
+ loggable_log(:error, 'queue overflow')
26
+ else
27
+ @queue.push(payload)
21
28
  end
22
-
23
- @queue.push(args)
24
29
  end
30
+ end
25
31
 
26
- def start_once!
27
- MUTEX.synchronize do
28
- return unless @parent.nil?
29
-
30
- @parent = Process.pid
31
- @queue = Queue.new
32
-
33
- @worker = Thread.new do
34
- while args = @queue.pop
35
- begin
36
- perform(*args)
37
- rescue ClosedQueueError
38
- nil
39
- rescue StandardError => e
40
- loggable('error', e.message, class: e.class.to_s, backtrace: e.backtrace)
41
- end
42
- end
43
- end
44
-
45
- at_exit { terminate }
46
- end
47
- end
32
+ def thread_run
33
+ loop do
34
+ buffer = @mutex.synchronize { @queue.shift(BUFFER_SIZE) }
35
+ loggable_log(:debug, 'thread_run', buffer_size: buffer.size)
36
+ return if @closed && buffer.empty?
48
37
 
49
- def terminate
50
- @queue&.close
51
- @worker&.join
38
+ perform(buffer) unless buffer.empty?
39
+ sleep(1) unless @mutex.synchronize { @queue.any? }
40
+ rescue StandardError => e
41
+ loggable_log(:error, e.message, class: e.class.to_s, backtrace: e.backtrace)
52
42
  end
43
+ end
53
44
 
54
- def reset
55
- terminate
56
- @parent = nil
57
- end
45
+ def terminate
46
+ @mutex.synchronize { @closed = true }
47
+ @worker&.join
58
48
  end
59
49
  end
@@ -0,0 +1,32 @@
1
+ require 'json'
2
+ require 'faraday'
3
+ require 'getaround_utils/utils/async_queue'
4
+
5
+ class GetaroundUtils::Utils::CapturReporter < GetaroundUtils::Utils::AsyncQueue
6
+ CAPTUR_URL = ENV['CAPTUR_URL']
7
+
8
+ def perform(events)
9
+ return unless CAPTUR_URL&.match('^https?://')
10
+
11
+ Faraday.post(CAPTUR_URL) do |req|
12
+ req.options[:open_timeout] = 1
13
+ req.options[:timeout] = 1
14
+ req.headers = { 'Content-Type': 'application/json' }
15
+ req.body = JSON.generate(events: events, metas: metas)
16
+ end
17
+ end
18
+
19
+ def metas
20
+ {}
21
+ end
22
+
23
+ def push(uuid:, type:, anonymous_id:, timestamp: nil, attributes: {})
24
+ super(
25
+ uuid: uuid,
26
+ type: type,
27
+ timestamp: timestamp || Time.now.iso8601,
28
+ anonymous_id: anonymous_id,
29
+ attributes: attributes,
30
+ )
31
+ end
32
+ end
@@ -2,47 +2,48 @@ module GetaroundUtils; end
2
2
  module GetaroundUtils::Utils; end
3
3
 
4
4
  module GetaroundUtils::Utils::DeepKeyValue
5
+ def self.escape(value, max_length = 512)
6
+ value = value[1...-1] if value =~ /^".*"$/
7
+ value = "#{value[0...max_length]} ..." if value.length >= max_length
8
+ value.inspect
9
+ end
10
+
5
11
  def self.serialize(data)
6
12
  case data
7
13
  when Array
8
- flattify(data).map { |key, value| "#{key}=#{value}" }.join(' ')
14
+ flattify(data).map { |key, value| "#{key}=#{serialize(value)}" }.join(' ')
9
15
  when Hash
10
- flattify(data).map { |key, value| "#{key}=#{value}" }.join(' ')
16
+ flattify(data).map { |key, value| "#{key}=#{serialize(value)}" }.join(' ')
11
17
  when Numeric
12
18
  data.to_s
13
19
  when String
14
- data =~ /^".*"$/ ? data : data.inspect
20
+ escape(data)
15
21
  else
16
- data.to_s.inspect
22
+ escape(data.to_s)
17
23
  end
18
24
  end
19
25
 
20
26
  # https://stackoverflow.com/questions/48836464/how-to-flatten-a-hash-making-each-key-a-unique-value
21
- def self.flattify(value, result = {}, path = [], max_length = 512, max_depth = 5)
27
+ def self.flattify(value, result = {}, path = [], max_depth = 5)
22
28
  if path.length > max_depth
23
- result[path.join(".")] = '"..."'
29
+ result[path.join(".")] = '...'
24
30
  return result
25
31
  end
26
32
  case value
27
33
  when Array
28
34
  value.each.with_index(0) do |v, i|
29
- flattify(v, result, path + [i], max_length, max_depth)
35
+ flattify(v, result, path + [i], max_depth)
30
36
  end
31
37
  when Hash
32
38
  value.each do |k, v|
33
- flattify(v, result, path + [k], max_length, max_depth)
39
+ flattify(v, result, path + [k], max_depth)
34
40
  end
35
41
  when Numeric
36
- result[path.join(".")] = value.to_s
42
+ result[path.join(".")] = value
37
43
  when String
38
- value = if value =~ /^".*"$/
39
- value.length >= max_length ? %{#{value[0...max_length]} ..."} : value
40
- else
41
- value.length >= max_length ? %{#{value[0...max_length]} ...}.inspect : value.inspect
42
- end
43
44
  result[path.join(".")] = value
44
45
  else
45
- flattify(value.to_s, result, path, max_length, max_depth)
46
+ flattify(value.to_s, result, path, max_depth)
46
47
  end
47
48
  result
48
49
  end
@@ -1,29 +1,11 @@
1
- require 'json'
2
- require 'faraday'
3
- require 'getaround_utils/utils/async_queue'
4
-
5
1
  class GetaroundUtils::Utils::HttpReporter
6
- class AsyncQueue < GetaroundUtils::Utils::AsyncQueue
7
- def self.perform(url:, params: {}, headers: {}, body: nil)
8
- Faraday.post(url) do |req|
9
- req.options[:open_timeout] = 1
10
- req.options[:timeout] = 1
11
- req.params = params
12
- req.headers = headers
13
- req.body = body
14
- end
15
- end
16
- end
2
+ include GetaroundUtils::Mixins::Loggable
17
3
 
18
- def initialize(url:)
19
- @url = url
4
+ def initialize(_)
5
+ loggable_log(:warn, 'use of deprecated class')
20
6
  end
21
7
 
22
- def report(event)
23
- AsyncQueue.perform_async(
24
- url: @url,
25
- headers: { 'Content-Type' => 'application/json' },
26
- body: JSON.generate(event)
27
- )
8
+ def report(_)
9
+ loggable_log(:warn, 'use of deprecated class')
28
10
  end
29
11
  end
@@ -1,3 +1,3 @@
1
1
  module GetaroundUtils
2
- VERSION = '0.1.19'.freeze
2
+ VERSION = '0.1.20'.freeze
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: getaround_utils
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.19
4
+ version: 0.1.20
5
5
  platform: ruby
6
6
  authors:
7
7
  - Drivy
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2020-02-04 00:00:00.000000000 Z
12
+ date: 2020-02-15 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: bundler
@@ -213,6 +213,7 @@ files:
213
213
  - lib/getaround_utils/railties/lograge.rb
214
214
  - lib/getaround_utils/utils.rb
215
215
  - lib/getaround_utils/utils/async_queue.rb
216
+ - lib/getaround_utils/utils/captur_reporter.rb
216
217
  - lib/getaround_utils/utils/deep_key_value.rb
217
218
  - lib/getaround_utils/utils/http_reporter.rb
218
219
  - lib/getaround_utils/version.rb