Merge pull request #678 from stvnrlly/max-accuracy

add max_accuracy option in location agent

Andrew Cantino 10 lat temu
rodzic
commit
77d65fd825

+ 12 - 2
app/models/agents/user_location_agent.rb

@@ -10,6 +10,8 @@ module Agents
10 10
 
11 11
 
12 12
         Your POST path will be `https://#{ENV['DOMAIN']}/users/#{user.id}/update_location/:secret` where `:secret` is specified in your options.
13
+
14
+        If you want to only keep more precise locations, set `max_accuracy` to the upper bound, in meters. The default name for this field is `accuracy`, but you can change this by setting a value for `accuracy_field`.
13 15
       MD
14 16
     end
15 17
 
@@ -34,7 +36,10 @@ module Agents
34 36
     end
35 37
 
36 38
     def default_options
37
-      { 'secret' => SecureRandom.hex(7) }
39
+      {
40
+        'secret' => SecureRandom.hex(7),
41
+        'max_accuracy' => ''
42
+      }
38 43
     end
39 44
 
40 45
     def validate_options
@@ -68,7 +73,12 @@ module Agents
68 73
     def handle_payload(payload)
69 74
       location = Location.new(payload)
70 75
 
71
-      if location.present?
76
+      accuracy_field = interpolated[:accuracy_field].presence || 'accuracy'
77
+
78
+      if location.present? && (!interpolated[:max_accuracy].present? || !payload[accuracy_field] || payload[accuracy_field].to_i < interpolated[:max_accuracy].to_i)
79
+        if interpolated[:max_accuracy].present? && !payload[accuracy_field].present?
80
+          log "Accuracy field missing; all locations will be kept"
81
+        end
72 82
         create_event payload: payload, location: location
73 83
       end
74 84
     end

+ 46 - 1
spec/models/agents/user_location_agent_spec.rb

@@ -2,7 +2,10 @@ require 'spec_helper'
2 2
 
3 3
 describe Agents::UserLocationAgent do
4 4
   before do
5
-    @agent = Agent.build_for_type('Agents::UserLocationAgent', users(:bob), :name => 'something', :options => { :secret => 'my_secret' })
5
+    @agent = Agent.build_for_type('Agents::UserLocationAgent', users(:bob),
6
+                                  :name => 'something',
7
+                                  :options => { :secret => 'my_secret',
8
+                                    :max_accuracy => '50' })
6 9
     @agent.save!
7 10
   end
8 11
 
@@ -45,4 +48,46 @@ describe Agents::UserLocationAgent do
45 48
     expect(@agent.events.last.lat).to eq(45)
46 49
     expect(@agent.events.last.lng).to eq(123)
47 50
   end
51
+
52
+  it 'does not create event when too inaccurate' do
53
+    event = Event.new
54
+    event.agent = agents(:bob_weather_agent)
55
+    event.created_at = Time.now
56
+    event.payload = { 'longitude' => 123, 'latitude' => 45, 'accuracy' => '100', 'something' => 'else' }
57
+
58
+    expect {
59
+      @agent.receive([event])
60
+    }.to change { @agent.events.count }.by(0)
61
+  end
62
+
63
+  it 'does create event when accurate enough' do
64
+    event = Event.new
65
+    event.agent = agents(:bob_weather_agent)
66
+    event.created_at = Time.now
67
+    event.payload = { 'longitude' => 123, 'latitude' => 45, 'accuracy' => '20', 'something' => 'else' }
68
+
69
+    expect {
70
+      @agent.receive([event])
71
+    }.to change { @agent.events.count }.by(1)
72
+
73
+    expect(@agent.events.last.payload).to eq({ 'longitude' => 123, 'latitude' => 45, 'accuracy' => '20', 'something' => 'else' })
74
+    expect(@agent.events.last.lat).to eq(45)
75
+    expect(@agent.events.last.lng).to eq(123)
76
+  end
77
+
78
+  it 'allows a custom accuracy field' do
79
+    event = Event.new
80
+    event.agent = agents(:bob_weather_agent)
81
+    event.created_at = Time.now
82
+    @agent.options['accuracy_field'] = 'estimated_to'
83
+    event.payload = { 'longitude' => 123, 'latitude' => 45, 'estimated_to' => '20', 'something' => 'else' }
84
+
85
+    expect {
86
+      @agent.receive([event])
87
+    }.to change { @agent.events.count }.by(1)
88
+
89
+    expect(@agent.events.last.payload).to eq({ 'longitude' => 123, 'latitude' => 45, 'estimated_to' => '20', 'something' => 'else' })
90
+    expect(@agent.events.last.lat).to eq(45)
91
+    expect(@agent.events.last.lng).to eq(123)
92
+  end
48 93
 end