Improve the way DataOutputAgent digs past events

In the first run, it now checks `2 * events_to_show` events for each
source.

This also fixes the problem where older events are selected when
`events_order` is not specified by sorting events by the `id`.

Akinori MUSHA 8 ans auparavant
Parent
Commettre
9b96a6fe63
1 fichiers modifiés avec 17 ajouts et 9 suppressions
  1. 17 9
      app/models/agents/data_output_agent.rb

+ 17 - 9
app/models/agents/data_output_agent.rb

@@ -188,18 +188,26 @@ module Agents
188 188
         end
189 189
 
190 190
       if reload
191
+        memory[:events_order] = events_order
192
+        memory[:events_to_show] = events_to_show
193
+
191 194
         new_events =
192 195
           if last_event_id = memory[:last_event_id]
193
-            received_events.order(id: :desc).where(Event.arel_table[:id].gt(last_event_id))
196
+            received_events.where(Event.arel_table[:id].gt(last_event_id)).
197
+              order(id: :asc).to_a
194 198
           else
195
-            # dig at least twice as many events as the number of
196
-            # `events_to_show`
197
-            received_events.order(id: :desc).limit([source_ids.count, 2].max * events_to_show)
198
-          end.to_a
199
-        events = new_events.concat(events)
200
-        memory[:events_order] = events_order
201
-        memory[:events_to_show] = events_to_show
202
-        memory[:last_event_id] = events.first.try!(:id)
199
+            source_ids.flat_map { |source_id|
200
+              # dig twice as many events as the number of
201
+              # `events_to_show`
202
+              received_events.where(agent_id: source_id).
203
+                last(2 * events_to_show)
204
+            }.sort_by(&:id)
205
+          end
206
+
207
+        unless new_events.empty?
208
+          memory[:last_event_id] = new_events.last.id
209
+          events.concat(new_events)
210
+        end
203 211
       end
204 212
 
205 213
       events = sort_events(events).last(events_to_show)