@@ -0,0 +1,111 @@ |
||
| 1 |
+module Agents |
|
| 2 |
+ class AttributeDifferenceAgent < Agent |
|
| 3 |
+ cannot_be_scheduled! |
|
| 4 |
+ |
|
| 5 |
+ description <<-MD |
|
| 6 |
+ The Attribute Difference Agent receives events and emits a new event with |
|
| 7 |
+ the difference or change of a specific attribute in comparison to the previous |
|
| 8 |
+ event received. |
|
| 9 |
+ |
|
| 10 |
+ `path` specifies the JSON path of the attribute to be used from the event. |
|
| 11 |
+ |
|
| 12 |
+ `output` specifies the new attribute name that will be created on the original payload |
|
| 13 |
+ and it will contain the difference or change. |
|
| 14 |
+ |
|
| 15 |
+ `method` specifies if it should be... |
|
| 16 |
+ |
|
| 17 |
+ * `percentage_change` eg. Previous value was `160`, new value is `116`. Percentage change is `-27.5` |
|
| 18 |
+ * `decimal_difference` eg. Previous value was `5.5`, new value is `15.2`. Difference is `9.7` |
|
| 19 |
+ * `integer_difference` eg. Previous value was `50`, new value is `40`. Difference is `-10` |
|
| 20 |
+ |
|
| 21 |
+ `decimal_precision` defaults to `3`, but you can override this if you want. |
|
| 22 |
+ |
|
| 23 |
+ `expected_update_period_in_days` is used to determine if the Agent is working. |
|
| 24 |
+ |
|
| 25 |
+ The resulting event will be a copy of the received event with the difference |
|
| 26 |
+ or change added as an extra attribute. If you use the `percentage_change` the |
|
| 27 |
+ attribute will be formatted as such `{{attribute}}_change`, otherwise it will
|
|
| 28 |
+ be `{{attribute}}_diff`.
|
|
| 29 |
+ |
|
| 30 |
+ All configuration options will be liquid interpolated based on the incoming event. |
|
| 31 |
+ MD |
|
| 32 |
+ |
|
| 33 |
+ event_description <<-MD |
|
| 34 |
+ This will change based on the source event. |
|
| 35 |
+ MD |
|
| 36 |
+ |
|
| 37 |
+ def default_options |
|
| 38 |
+ {
|
|
| 39 |
+ 'path' => '.data.rate', |
|
| 40 |
+ 'output' => 'rate_diff', |
|
| 41 |
+ 'method' => 'integer_difference', |
|
| 42 |
+ 'expected_update_period_in_days' => 1 |
|
| 43 |
+ } |
|
| 44 |
+ end |
|
| 45 |
+ |
|
| 46 |
+ def validate_options |
|
| 47 |
+ unless options['path'].present? && options['method'].present? && options['output'].present? && options['expected_update_period_in_days'].present? |
|
| 48 |
+ errors.add(:base, 'The attribute, method and expected_update_period_in_days fields are all required.') |
|
| 49 |
+ end |
|
| 50 |
+ end |
|
| 51 |
+ |
|
| 52 |
+ def working? |
|
| 53 |
+ event_created_within?(interpolated['expected_update_period_in_days']) && !recent_error_logs? |
|
| 54 |
+ end |
|
| 55 |
+ |
|
| 56 |
+ def receive(incoming_events) |
|
| 57 |
+ incoming_events.each do |event| |
|
| 58 |
+ handle(interpolated(event), event) |
|
| 59 |
+ end |
|
| 60 |
+ end |
|
| 61 |
+ |
|
| 62 |
+ private |
|
| 63 |
+ |
|
| 64 |
+ def handle(opts, event) |
|
| 65 |
+ opts['decimal_precision'] ||= 3 |
|
| 66 |
+ attribute_value = Utils.value_at(event.payload, opts['path']) |
|
| 67 |
+ attribute_value = attribute_value.nil? ? 0 : attribute_value |
|
| 68 |
+ payload = event.payload.deep_dup |
|
| 69 |
+ |
|
| 70 |
+ if opts['method'] == 'percentage_change' |
|
| 71 |
+ change = calculate_percentage_change(attribute_value, opts['decimal_precision']) |
|
| 72 |
+ payload[opts['output']] = change |
|
| 73 |
+ |
|
| 74 |
+ elsif opts['method'] == 'decimal_difference' |
|
| 75 |
+ difference = calculate_decimal_difference(attribute_value, opts['decimal_precision']) |
|
| 76 |
+ payload[opts['output']] = difference |
|
| 77 |
+ |
|
| 78 |
+ elsif opts['method'] == 'integer_difference' |
|
| 79 |
+ difference = calculate_integer_difference(attribute_value) |
|
| 80 |
+ payload[opts['output']] = difference |
|
| 81 |
+ end |
|
| 82 |
+ |
|
| 83 |
+ created_event = create_event(payload: payload) |
|
| 84 |
+ log('Propagating new event', outbound_event: created_event, inbound_event: event)
|
|
| 85 |
+ update_memory(attribute_value) |
|
| 86 |
+ end |
|
| 87 |
+ |
|
| 88 |
+ def calculate_integer_difference(new_value) |
|
| 89 |
+ return 0 if last_value.nil? |
|
| 90 |
+ (new_value.to_i - last_value.to_i) |
|
| 91 |
+ end |
|
| 92 |
+ |
|
| 93 |
+ def calculate_decimal_difference(new_value, dec_pre) |
|
| 94 |
+ return 0.0 if last_value.nil? |
|
| 95 |
+ (new_value.to_f - last_value.to_f).round(dec_pre.to_i) |
|
| 96 |
+ end |
|
| 97 |
+ |
|
| 98 |
+ def calculate_percentage_change(new_value, dec_pre) |
|
| 99 |
+ return 0.0 if last_value.nil? |
|
| 100 |
+ (((new_value.to_f / last_value.to_f) * 100) - 100).round(dec_pre.to_i) |
|
| 101 |
+ end |
|
| 102 |
+ |
|
| 103 |
+ def last_value |
|
| 104 |
+ memory['last_value'] |
|
| 105 |
+ end |
|
| 106 |
+ |
|
| 107 |
+ def update_memory(new_value) |
|
| 108 |
+ memory['last_value'] = new_value |
|
| 109 |
+ end |
|
| 110 |
+ end |
|
| 111 |
+end |
@@ -0,0 +1,131 @@ |
||
| 1 |
+require 'rails_helper' |
|
| 2 |
+ |
|
| 3 |
+describe Agents::AttributeDifferenceAgent do |
|
| 4 |
+ def create_event(value=nil) |
|
| 5 |
+ event = Event.new |
|
| 6 |
+ event.agent = agents(:jane_weather_agent) |
|
| 7 |
+ event.payload = {
|
|
| 8 |
+ rate: value |
|
| 9 |
+ } |
|
| 10 |
+ event.save! |
|
| 11 |
+ |
|
| 12 |
+ event |
|
| 13 |
+ end |
|
| 14 |
+ |
|
| 15 |
+ before do |
|
| 16 |
+ @valid_params = {
|
|
| 17 |
+ path: 'rate', |
|
| 18 |
+ output: 'rate_diff', |
|
| 19 |
+ method: 'integer_difference', |
|
| 20 |
+ expected_update_period_in_days: '1' |
|
| 21 |
+ } |
|
| 22 |
+ |
|
| 23 |
+ @checker = Agents::AttributeDifferenceAgent.new(name: 'somename', options: @valid_params) |
|
| 24 |
+ @checker.user = users(:jane) |
|
| 25 |
+ @checker.save! |
|
| 26 |
+ end |
|
| 27 |
+ |
|
| 28 |
+ describe 'validation' do |
|
| 29 |
+ before do |
|
| 30 |
+ expect(@checker).to be_valid |
|
| 31 |
+ end |
|
| 32 |
+ |
|
| 33 |
+ it 'should validate presence of output' do |
|
| 34 |
+ @checker.options[:output] = nil |
|
| 35 |
+ expect(@checker).not_to be_valid |
|
| 36 |
+ end |
|
| 37 |
+ |
|
| 38 |
+ it 'should validate presence of path' do |
|
| 39 |
+ @checker.options[:path] = nil |
|
| 40 |
+ expect(@checker).not_to be_valid |
|
| 41 |
+ end |
|
| 42 |
+ |
|
| 43 |
+ it 'should validate presence of method' do |
|
| 44 |
+ @checker.options[:method] = nil |
|
| 45 |
+ expect(@checker).not_to be_valid |
|
| 46 |
+ end |
|
| 47 |
+ |
|
| 48 |
+ it 'should validate presence of expected_update_period_in_days' do |
|
| 49 |
+ @checker.options[:expected_update_period_in_days] = nil |
|
| 50 |
+ expect(@checker).not_to be_valid |
|
| 51 |
+ end |
|
| 52 |
+ end |
|
| 53 |
+ |
|
| 54 |
+ describe '#working?' do |
|
| 55 |
+ before :each do |
|
| 56 |
+ # Need to create an event otherwise event_created_within? returns nil |
|
| 57 |
+ event = create_event |
|
| 58 |
+ @checker.receive([event]) |
|
| 59 |
+ end |
|
| 60 |
+ |
|
| 61 |
+ it 'is when event created within :expected_update_period_in_days' do |
|
| 62 |
+ @checker.options[:expected_update_period_in_days] = 2 |
|
| 63 |
+ expect(@checker).to be_working |
|
| 64 |
+ end |
|
| 65 |
+ |
|
| 66 |
+ it 'isnt when event created outside :expected_update_period_in_days' do |
|
| 67 |
+ @checker.options[:expected_update_period_in_days] = 2 |
|
| 68 |
+ |
|
| 69 |
+ time_travel_to 2.days.from_now do |
|
| 70 |
+ expect(@checker).not_to be_working |
|
| 71 |
+ end |
|
| 72 |
+ end |
|
| 73 |
+ end |
|
| 74 |
+ |
|
| 75 |
+ describe '#receive' do |
|
| 76 |
+ before :each do |
|
| 77 |
+ @event = create_event('5.5')
|
|
| 78 |
+ end |
|
| 79 |
+ |
|
| 80 |
+ it 'creates events when memory is empty' do |
|
| 81 |
+ expect {
|
|
| 82 |
+ @checker.receive([@event]) |
|
| 83 |
+ }.to change(Event, :count).by(1) |
|
| 84 |
+ expect(Event.last.payload[:rate_diff]).to eq(0) |
|
| 85 |
+ end |
|
| 86 |
+ |
|
| 87 |
+ it 'creates event with extra attribute for integer_difference' do |
|
| 88 |
+ @checker.receive([@event]) |
|
| 89 |
+ event = create_event('6.5')
|
|
| 90 |
+ |
|
| 91 |
+ expect {
|
|
| 92 |
+ @checker.receive([event]) |
|
| 93 |
+ }.to change(Event, :count).by(1) |
|
| 94 |
+ expect(Event.last.payload[:rate_diff]).to eq(1) |
|
| 95 |
+ end |
|
| 96 |
+ |
|
| 97 |
+ it 'creates event with extra attribute for decimal_difference' do |
|
| 98 |
+ @checker.options[:method] = 'decimal_difference' |
|
| 99 |
+ @checker.receive([@event]) |
|
| 100 |
+ event = create_event('6.4')
|
|
| 101 |
+ |
|
| 102 |
+ expect {
|
|
| 103 |
+ @checker.receive([event]) |
|
| 104 |
+ }.to change(Event, :count).by(1) |
|
| 105 |
+ expect(Event.last.payload[:rate_diff]).to eq(0.9) |
|
| 106 |
+ end |
|
| 107 |
+ |
|
| 108 |
+ it 'creates event with extra attribute for percentage_change' do |
|
| 109 |
+ @checker.options[:method] = 'percentage_change' |
|
| 110 |
+ @checker.receive([@event]) |
|
| 111 |
+ event = create_event('9')
|
|
| 112 |
+ |
|
| 113 |
+ expect {
|
|
| 114 |
+ @checker.receive([event]) |
|
| 115 |
+ }.to change(Event, :count).by(1) |
|
| 116 |
+ expect(Event.last.payload[:rate_diff]).to eq(63.636) |
|
| 117 |
+ end |
|
| 118 |
+ |
|
| 119 |
+ it 'creates event with extra attribute for percentage_change with the correct rounding' do |
|
| 120 |
+ @checker.options[:method] = 'percentage_change' |
|
| 121 |
+ @checker.options[:decimal_precision] = 5 |
|
| 122 |
+ @checker.receive([@event]) |
|
| 123 |
+ event = create_event('9')
|
|
| 124 |
+ |
|
| 125 |
+ expect {
|
|
| 126 |
+ @checker.receive([event]) |
|
| 127 |
+ }.to change(Event, :count).by(1) |
|
| 128 |
+ expect(Event.last.payload[:rate_diff]).to eq(63.63636) |
|
| 129 |
+ end |
|
| 130 |
+ end |
|
| 131 |
+end |