nexaas-async-collector 1.2.2 → 2.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 8d6262f078636b043ae567b9f2e0e9318b86b80c
4
- data.tar.gz: 85abf3a76680a3194b366fc6de0459cd86d65142
3
+ metadata.gz: '095b630a7a47dcb39b779d55d8a9adec7cec81c3'
4
+ data.tar.gz: 3aeadb92991d1884a3bda6b36ac4a49a3a67071d
5
5
  SHA512:
6
- metadata.gz: 4579d88011523cb251fa330a11cd07db1d357430148daed76cd61c49b3c9d2131844e463191cf28e6c044974d74b1e6dcdc255e6804f61dca6e17bfc04106efb
7
- data.tar.gz: 907207719835d0f6589ecdbafbe248d98e24e9f03b1c6dc6629c885cd3a0b6db32a30d0ebb4432e6ce1c0b52f864b37f3a678a09d8e6a5d9d5fbb88c7c245af3
6
+ metadata.gz: 003da333f56ca3122b2221fd4ca14e54cdd6dcefee8ffc1c5b15189d56104df61b572cff3d9624afd5ece531c9cb9719fdfea804aecdb134b4c64c84c7b0562e
7
+ data.tar.gz: 7831048e2329a696533c56b731c2e95730b8c1a17f590c063265492c27bc2319f0cff83e803d0afa998af4aaf8a8fbc94ab29a71a3ebcf7e951e8219edc2899e
data/README.md CHANGED
@@ -69,7 +69,13 @@ end
69
69
  3) Use the view helper to do all the process:
70
70
 
71
71
  ```ruby
72
- <%= nexaas_async_collect(user.id, ModelService, :model_method, [arg1, arg2]) %>
72
+ <%= nexaas_async_collect({
73
+ scoped_id: scope.id, # (required) the ID of the scope. It ensures only the scope who requested the data will be able to fetch it
74
+ class_name: ModelService, # (required) name of the class
75
+ class_method: :model_method, # (required) name of the class method responsible to generate data
76
+ args: [arg1, arg2], # (optional) arguments to be passed to class method
77
+ instrumentation_context: 'my.custom.instrumentation' # (optional) context of the instrumentation name. It will generate two instrumentation: 'my.custom.instrumentation.start' and 'my.custom.instrumentation.finish'
78
+ }) %>
73
79
  ```
74
80
 
75
81
  ## Contributing
@@ -4,7 +4,7 @@ module Nexaas
4
4
  module ApplicationHelper
5
5
  #
6
6
  # Helper to enqueue AsyncResourceJob and include the JavaScript code to request the result
7
- # - user_id: an ID that is unique for each logged user (maybe user.id, account.id, organization.id, etc)
7
+ # - scoped_id: an ID that is unique (maybe user.id, account.id, organization.id, etc)
8
8
  # - klass_name: The name of the class responsible for generate the content to be stored in the memory
9
9
  # - klass_method: The name of the class method to be called
10
10
  # - args: The arguments to be passed in the call of the class method
@@ -12,8 +12,10 @@ module Nexaas
12
12
  # Example:
13
13
  # <%= nexaas_async_collect(current_user.id, ReportGenerator, :generate, []) %>
14
14
  #
15
- def nexaas_async_collect(user_id, klass_name, klass_method, args=[])
16
- AsyncResourceJob.perform_async(collector_user_id, user_id, klass_name, klass_method, args)
15
+ def nexaas_async_collect(opts={})
16
+ validate_options(opts)
17
+ opts.merge!({ collect_id: collect_id })
18
+ AsyncResourceJob.perform_async(opts)
17
19
  render(partial: 'nexaas/async/collector/async_resource/show')
18
20
  end
19
21
 
@@ -25,8 +27,19 @@ module Nexaas
25
27
 
26
28
  private
27
29
 
28
- def collector_user_id
29
- @collector_user_id ||= SecureRandom.hex(15)
30
+ def validate_options(opts={})
31
+ missing_keys = required_collect_opts - opts.keys.map(&:to_s)
32
+ if missing_keys.any?
33
+ raise "Nexaas::Async::Collector: Required parameter missing: #{missing_keys.join(', ')}"
34
+ end
35
+ end
36
+
37
+ def collect_id
38
+ @collect_id ||= SecureRandom.hex(15)
39
+ end
40
+
41
+ def required_collect_opts
42
+ ['scope_id', 'class_name', 'class_method']
30
43
  end
31
44
 
32
45
  end
@@ -1,7 +1,7 @@
1
1
  <script type="text/javascript" id="js-nexaas-async-collector">
2
2
  var requestContent = function() {
3
3
  $.ajax({
4
- url: '<%= nexaas_async_collector.async_resource_path(@collector_user_id) %>',
4
+ url: '<%= nexaas_async_collector.async_resource_path(@collect_id) %>',
5
5
  dataType: 'script'
6
6
  })
7
7
  }
@@ -6,20 +6,51 @@ module Nexaas
6
6
  include Sidekiq::Worker
7
7
  sidekiq_options queue: Nexaas::Async::Collector.queue_name
8
8
 
9
- def perform(collector_id, user_id, klass_name, klass_method, args=[])
10
- content = call_for_method(klass_name, klass_method, args)
11
- Persist.save(user_id, collector_id, content)
9
+ def perform(opts={})
10
+ initialize_options(opts)
11
+ start_time = Time.current.to_i
12
+ instrument_start(start_time)
13
+ Persist.save(@scoped_id, @collect_id, generate_content)
14
+ instrument_finish(start_time)
12
15
  end
13
16
 
14
17
  private
15
18
 
16
- def call_for_method(klass_name, klass_method, args=[])
17
- if args.any?
18
- klass_name.to_s.constantize.send(klass_method, *args)
19
+ def initialize_options(opts)
20
+ opts.each do |k, v|
21
+ instance_variable_set("@#{k}", v)
22
+ end
23
+ end
24
+
25
+ def generate_content
26
+ if @args && @args.any?
27
+ @class_name.to_s.constantize.send(@class_method, *@args)
19
28
  else
20
- klass_name.to_s.constantize.send(klass_method)
29
+ @class_name.to_s.constantize.send(@class_method)
21
30
  end
22
31
  end
32
+
33
+ def instrument_start(start_time)
34
+ ActiveSupport::Notifications.instrument("#{instrumentation_context}.start", {
35
+ collect_id: @collect_id, scoped_id: @scoped_id,
36
+ class_name: @class_name, class_method: @class_method,
37
+ start: start_time
38
+ })
39
+ end
40
+
41
+ def instrument_finish(start_time)
42
+ finish = Time.current.to_i
43
+ duration = (finish - start_time)
44
+ ActiveSupport::Notifications.instrument("#{instrumentation_context}.finish", {
45
+ collect_id: @collect_id, scoped_id: @scoped_id,
46
+ class_name: @class_name, class_method: @class_method,
47
+ finish: finish, duration: duration
48
+ })
49
+ end
50
+
51
+ def instrumentation_context
52
+ @instrumentation_context || 'nexaas-async-collector'
53
+ end
23
54
  end
24
55
  end
25
56
  end
@@ -3,13 +3,13 @@ module Nexaas
3
3
  module Collector
4
4
  class Persist
5
5
  class << self
6
- def save(user_id, key, value)
7
- storage.set(key, content(user_id, value))
6
+ def save(scope_id, key, value)
7
+ storage.set(key, content(scope_id, value))
8
8
  end
9
9
 
10
- def content(user_id, value)
10
+ def content(scope_id, value)
11
11
  {
12
- 'user_id' => user_id,
12
+ 'scope_id' => scope_id,
13
13
  'content' => value
14
14
  }.to_json
15
15
  end
@@ -1,21 +1,20 @@
1
1
  # Content is stored in the database as the following JSON:
2
2
  # {
3
- # 'user_id' => 'user_id',
3
+ # 'scope_id' => 'scope_id',
4
4
  # 'content' => 'generated content'
5
5
  # }
6
- # The content is scoped by user, so any other user who discover the key (id) of the content, will not be able
7
- # to access only. Only the user who created it.
8
- # Maybe leave this more generic?
6
+ # The content is scoped by scope, so any other scope who discover the key (id) of the content, will not be able
7
+ # to access only. Only the scope who created it.
9
8
 
10
9
  module Nexaas
11
10
  module Async
12
11
  module Collector
13
12
  class Result
14
13
 
15
- attr_reader :user_id, :id, :content
14
+ attr_reader :scope_id, :id, :content
16
15
 
17
- def initialize(user_id, id)
18
- @user_id = user_id
16
+ def initialize(scope_id, id)
17
+ @scope_id = scope_id
19
18
  @id = id
20
19
  end
21
20
 
@@ -24,7 +23,7 @@ module Nexaas
24
23
  end
25
24
 
26
25
  def content_is_ready?
27
- _content && _content['user_id'] == user_id && _content['content']
26
+ _content && _content['scope_id'] == scope_id && _content['content']
28
27
  end
29
28
 
30
29
  private
@@ -1,7 +1,7 @@
1
1
  module Nexaas
2
2
  module Async
3
3
  module Collector
4
- VERSION = '1.2.2'
4
+ VERSION = '2.0.0'
5
5
  end
6
6
  end
7
7
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: nexaas-async-collector
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.2.2
4
+ version: 2.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Eduardo Hertz
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-07-13 00:00:00.000000000 Z
11
+ date: 2017-07-14 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -108,6 +108,20 @@ dependencies:
108
108
  - - '='
109
109
  - !ruby/object:Gem::Version
110
110
  version: 9.0.6
111
+ - !ruby/object:Gem::Dependency
112
+ name: timecop
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
111
125
  description: Agnostic collector and generator of async content for Rails apps.
112
126
  email:
113
127
  - eduardohertz@gmail.com